class Mkxms::Mssql::ForeignKey

Attributes

delete_reconciliation[RW]
enabled[RW]
name[RW]
references[RW]
schema[RW]
table[RW]
update_reconciliation[RW]

Public Class Methods

generated_name() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 9
def self.generated_name
  "XMigra_unnamed_foreign_key_constraint_#{@anon_counter = (@anon_counter || 0) + 1}"
end
new(schema, table, name, on_delete: 'NO ACTION', on_update: 'NO ACTION', enabled: true) click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 13
def initialize(schema, table, name, on_delete: 'NO ACTION', on_update: 'NO ACTION', enabled: true)
  @schema = schema
  @table = table
  @delete_reconciliation = on_delete
  @update_reconciliation = on_update
  @enabled = enabled
  @name = name || self.class.generated_name
  @is_unnamed = !name
  @links = []
end

Public Instance Methods

property_subject_identifiers() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 48
def property_subject_identifiers
  ['SCHEMA', schema, 'TABLE', table, 'CONSTRAINT', name].map {|n| Utils.unquoted_name(n)}
end
qualified_name() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 44
def qualified_name
  "#@schema.#@name" if @name
end
qualified_table() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 40
def qualified_table
  "#@schema.#@table"
end
to_sql() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 29
def to_sql
  "ALTER TABLE #{qualified_table} ADD CONSTRAINT #@name FOREIGN KEY (%s) REFERENCES #{@references[0]}.#{@references[1]} (%s)%s%s;" % [
    @links.map {|e| e[0]}.join(', '),
    @links.map {|e| e[1]}.join(', '),
    (" ON DELETE #@delete_reconciliation" if @delete_reconciliation),
    (" ON UPDATE #@update_reconciliation" if @update_reconciliation),
  ] + (
    @enabled ? '' : "\nALTER TABLE #{qualified_table} NOCHECK CONSTRAINT #@name;"
  ) + extended_properties_sql.joined_on_new_lines
end
unnamed?() click to toggle source
# File lib/mkxms/mssql/foreign_key_handler.rb, line 52
def unnamed?
  @is_unnamed
end