class Mkxms::Mssql::AdoptionScriptWriter::CheckConstraintAdoptionChecks

Attributes

cnstr[R]
cnstr_id[R]
schema_name[R]
table_name[R]

Public Class Methods

new(cnstr, error_sql_proc) click to toggle source
Calls superclass method
# File lib/mkxms/mssql/adoption_script_writer.rb, line 1231
def initialize(cnstr, error_sql_proc)
  super()
  
  @cnstr = cnstr
  @cnstr_id = "check constraint%s on #{cnstr.qualified_table}" % [
    cnstr.name ? cnstr.name + " " : ""
  ]
  @error_sql_proc = error_sql_proc
  
  @schema_name = unquoted_identifier cnstr.schema
  @table_name = unquoted_identifier cnstr.table
  @cnstr_name = unquoted_identifier(cnstr.name) if cnstr.name
  
  if cnstr.name
    add_named_constraint_tests
  else
    add_unnamed_constraint_tests
  end
end

Public Instance Methods

add_named_constraint_tests() click to toggle source
# File lib/mkxms/mssql/adoption_script_writer.rb, line 1257
def add_named_constraint_tests
  dsl {
    puts "IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT * FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.name = #{strlit cnstr_name}
      }
    end
    puts "BEGIN"
    indented {
      puts error_sql "#{cnstr_id.capitalize} does not exist."
    }
    puts "END ELSE IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT * FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.parent_object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.name = #{strlit cnstr_name}
        AND cc.definition = #{strlit cnstr.definition}
      }
    end
    puts "BEGIN"
    indented {
      puts error_sql "#{cnstr_id.capitalize} does not have expected definition."
    }
    puts "END"
  }
end
add_unnamed_constraint_tests() click to toggle source
# File lib/mkxms/mssql/adoption_script_writer.rb, line 1292
def add_unnamed_constraint_tests
  dsl {
    puts "IF NOT EXISTS (%s)" do
      puts dedent %Q{
        SELECT cc.object_id
        FROM sys.check_constraints cc
        INNER JOIN sys.tables t ON cc.parent_object_id = t.object_id
        INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
        WHERE s.name = #{strlit schema_name}
        AND t.name = #{strlit table_name}
        AND cc.definition = #{strlit cnstr.definition}
      }
    end
    puts "BEGIN".."END" do
      puts error_sql "Expected #{cnstr_id} does not exist."
    end
  }
end
error_sql(s) click to toggle source
# File lib/mkxms/mssql/adoption_script_writer.rb, line 1253
def error_sql(s)
  @error_sql_proc.call(s)
end