Class: Mkxms::Mssql::AdoptionScriptWriter::CheckConstraintAdoptionChecks

Inherits:
IndentedStringBuilder show all
Includes:
SqlStringManipulators
Defined in:
lib/mkxms/mssql/adoption_script_writer.rb

Constant Summary

Constants included from SqlStringManipulators

SqlStringManipulators::MSSQL

Constants inherited from IndentedStringBuilder

IndentedStringBuilder::NAMED_SUBSTITUTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SqlStringManipulators

#bit_test, #boolean_desc, #dedent, #stresc, #strlit, #unquoted_identifier

Methods inherited from IndentedStringBuilder

dsl, #dsl, #each, #indented, #puts, #to_s

Constructor Details

#initialize(cnstr, error_sql_proc) ⇒ CheckConstraintAdoptionChecks

Returns a new instance of CheckConstraintAdoptionChecks.



1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# 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

Instance Attribute Details

#cnstrObject (readonly)

Returns the value of attribute cnstr.



1251
1252
1253
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1251

def cnstr
  @cnstr
end

#cnstr_idObject (readonly)

Returns the value of attribute cnstr_id.



1251
1252
1253
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1251

def cnstr_id
  @cnstr_id
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



1251
1252
1253
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1251

def schema_name
  @schema_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



1251
1252
1253
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1251

def table_name
  @table_name
end

Instance Method Details

#add_named_constraint_testsObject



1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
# 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_testsObject



1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
# 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) ⇒ Object



1253
1254
1255
# File 'lib/mkxms/mssql/adoption_script_writer.rb', line 1253

def error_sql(s)
  @error_sql_proc.call(s)
end