Module: DynamicMigrations::ActiveRecord::Migrators::ForeignKeyConstraint

Included in:
DynamicMigrations::ActiveRecord::Migrators
Defined in:
lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb

Defined Under Namespace

Classes: ForeignKeyOnDeleteOptionsError, UnexpectedReferentialActionError

Instance Method Summary collapse

Instance Method Details

#add_foreign_key(table_name, column_names, foreign_table_name, foreign_column_names, name:, foreign_schema: nil, initially_deferred: false, deferrable: false, on_delete: :no_action, on_update: :no_action, comment: nil) ⇒ Object

because rails migrations don’t support composite (multiple column) foreign keys column_names and foreign_column_names can be a single column name or an array of column names



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb', line 14

def add_foreign_key table_name, column_names, foreign_table_name, foreign_column_names, name:, foreign_schema: nil, initially_deferred: false, deferrable: false, on_delete: :no_action, on_update: :no_action, comment: nil
  if initially_deferred == true && deferrable == false
    raise DeferrableOptionsError, "A constraint can only be initially deferred if it is also deferrable"
  end

  # convert single column names into arrays, this simplifies the logic below
  column_names = column_names.is_a?(Array) ? column_names : [column_names]
  foreign_column_names = foreign_column_names.is_a?(Array) ? foreign_column_names : [foreign_column_names]
  # default to the current schema
  foreign_schema_name = foreign_schema.nil? ? schema_name : foreign_schema

  # allow it to be deferred, and defer it by default
  deferrable_sql = if initially_deferred
    "DEFERRABLE INITIALLY DEFERRED"

  # allow it to be deferred, but do not deferr by default
  elsif deferrable
    "DEFERRABLE INITIALLY IMMEDIATE"

  # it can not be deferred (this is the default)
  else
    "NOT DEFERRABLE"
  end

  execute <<~SQL
    ALTER TABLE #{table_name}
      ADD CONSTRAINT #{name}
        FOREIGN KEY (#{column_names.join(", ")})
          REFERENCES  #{foreign_schema_name}.#{foreign_table_name} (#{foreign_column_names.join(", ")})
      ON DELETE #{referential_action_to_sql on_delete}
      ON UPDATE #{referential_action_to_sql on_update}
      #{deferrable_sql};
  SQL

  if comment.is_a? String
    set_foreign_key_comment table_name, name, comment
  end
end

#remove_foreign_key(table_name, name) ⇒ Object



53
54
55
56
57
58
# File 'lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb', line 53

def remove_foreign_key table_name, name
  execute <<~SQL
    ALTER TABLE #{table_name}
      DROP CONSTRAINT #{name};
  SQL
end

#remove_foreign_key_comment(table_name, foreign_key_name) ⇒ Object

remove a foreign_key comment



68
69
70
71
72
# File 'lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb', line 68

def remove_foreign_key_comment table_name, foreign_key_name
  execute <<~SQL
    COMMENT ON CONSTRAINT #{foreign_key_name} ON #{schema_name}.#{table_name} IS NULL;
  SQL
end

#set_foreign_key_comment(table_name, foreign_key_name, comment) ⇒ Object

add a comment to the foreign_key



61
62
63
64
65
# File 'lib/dynamic_migrations/active_record/migrators/foreign_key_constraint.rb', line 61

def set_foreign_key_comment table_name, foreign_key_name, comment
  execute <<~SQL
    COMMENT ON CONSTRAINT #{foreign_key_name} ON #{schema_name}.#{table_name} IS #{quote comment};
  SQL
end