Module: DynamicMigrations::Postgres::Generator::ForeignKeyConstraint

Included in:
DynamicMigrations::Postgres::Generator
Defined in:
lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb

Instance Method Summary collapse

Instance Method Details

#add_foreign_key_constraint(foreign_key_constraint, code_comment = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
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
52
53
54
# File 'lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb', line 5

def add_foreign_key_constraint foreign_key_constraint, code_comment = nil
  # the migration accepts either a single column name or an array of column names
  # we use the appropriate syntax just to make the migration prettier and easier
  # to understand
  p_c_names = foreign_key_constraint.column_names
  column_names = (p_c_names.count == 1) ? ":#{p_c_names.first}" : "[:#{p_c_names.join(", :")}]"

  f_c_names = foreign_key_constraint.foreign_column_names
  foreign_column_names = (f_c_names.count == 1) ? ":#{f_c_names.first}" : "[:#{f_c_names.join(", :")}]"

  options = {}
  options[:name] = ":#{foreign_key_constraint.name}"

  # only provide values if they are different from the defaults
  unless foreign_key_constraint.initially_deferred == false
    options[:initially_deferred] = foreign_key_constraint.initially_deferred
  end
  unless foreign_key_constraint.deferrable == false
    options[:deferrable] = foreign_key_constraint.deferrable
  end
  unless foreign_key_constraint.on_delete == :no_action
    options[:on_delete] = ":#{foreign_key_constraint.on_delete}"
  end
  unless foreign_key_constraint.on_update == :no_action
    options[:on_update] = ":#{foreign_key_constraint.on_update}"
  end
  unless foreign_key_constraint.table.schema.name == foreign_key_constraint.foreign_schema_name
    options[:foreign_schema] = ":#{foreign_key_constraint.foreign_schema_name}"
  end

  unless foreign_key_constraint.description.nil?
    options[:comment] = <<~RUBY
      <<~COMMENT
        #{indent foreign_key_constraint.description}
      COMMENT
    RUBY
  end

  options_syntax = options.map { |k, v| "#{k}: #{v}" }.join(", ")

  add_fragment schema: foreign_key_constraint.table.schema,
    table: foreign_key_constraint.table,
    migration_method: :add_foreign_key,
    object: foreign_key_constraint,
    code_comment: code_comment,
    dependent_table: foreign_key_constraint.foreign_table,
    migration: <<~RUBY
      add_foreign_key :#{foreign_key_constraint.table.name}, #{column_names}, :#{foreign_key_constraint.foreign_table.name}, #{foreign_column_names}, #{options_syntax}
    RUBY
end

#recreate_foreign_key_constraint(original_foreign_key_constraint, updated_foreign_key_constraint) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb', line 67

def recreate_foreign_key_constraint original_foreign_key_constraint, updated_foreign_key_constraint
  # remove the original foreign_key_constraint
  removal_fragment = remove_foreign_key_constraint original_foreign_key_constraint, <<~CODE_COMMENT
    Removing original foreign key constraint because it has changed (it is recreated below)
    Changes:
      #{indent original_foreign_key_constraint.differences_descriptions(updated_foreign_key_constraint).join("\n")}
  CODE_COMMENT

  # recrete the foreign_key_constraint with the new options
  recreation_fragment = add_foreign_key_constraint updated_foreign_key_constraint, <<~CODE_COMMENT
    Recreating this foreign key constraint
  CODE_COMMENT

  # return the new fragments (the main reason to return them here is for the specs)
  [removal_fragment, recreation_fragment]
end

#remove_foreign_key_constraint(foreign_key_constraint, code_comment = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb', line 56

def remove_foreign_key_constraint foreign_key_constraint, code_comment = nil
  add_fragment schema: foreign_key_constraint.table.schema,
    table: foreign_key_constraint.table,
    migration_method: :remove_foreign_key,
    object: foreign_key_constraint,
    code_comment: code_comment,
    migration: <<~RUBY
      remove_foreign_key :#{foreign_key_constraint.table.name}, :#{foreign_key_constraint.name}
    RUBY
end

#remove_foreign_key_constraint_comment(foreign_key_constraint, code_comment = nil) ⇒ Object

remove the comment from a foreign_key_constraint



105
106
107
108
109
110
111
112
113
114
# File 'lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb', line 105

def remove_foreign_key_constraint_comment foreign_key_constraint, code_comment = nil
  add_fragment schema: foreign_key_constraint.table.schema,
    table: foreign_key_constraint.table,
    migration_method: :remove_foreign_key_constraint_comment,
    object: foreign_key_constraint,
    code_comment: code_comment,
    migration: <<~RUBY
      remove_foreign_key_comment :#{foreign_key_constraint.table.name}, :#{foreign_key_constraint.name}
    RUBY
end

#set_foreign_key_constraint_comment(foreign_key_constraint, code_comment = nil) ⇒ Object

add a comment to a foreign_key_constraint



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dynamic_migrations/postgres/generator/foreign_key_constraint.rb', line 85

def set_foreign_key_constraint_comment foreign_key_constraint, code_comment = nil
  description = foreign_key_constraint.description

  if description.nil?
    raise MissingDescriptionError, "Missing required description for foreign_key_constraint `#{foreign_key_constraint.name}` in table `#{foreign_key_constraint.table.schema.name}.#{foreign_key_constraint.table.name}`"
  end

  add_fragment schema: foreign_key_constraint.table.schema,
    table: foreign_key_constraint.table,
    migration_method: :set_foreign_key_constraint_comment,
    object: foreign_key_constraint,
    code_comment: code_comment,
    migration: <<~RUBY
      set_foreign_key_comment :#{foreign_key_constraint.table.name}, :#{foreign_key_constraint.name}, <<~COMMENT
        #{indent description}
      COMMENT
    RUBY
end