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
- #add_foreign_key_constraint(foreign_key_constraint, code_comment = nil) ⇒ Object
- #recreate_foreign_key_constraint(original_foreign_key_constraint, updated_foreign_key_constraint) ⇒ Object
- #remove_foreign_key_constraint(foreign_key_constraint, code_comment = nil) ⇒ Object
-
#remove_foreign_key_constraint_comment(foreign_key_constraint, code_comment = nil) ⇒ Object
remove the comment from a foreign_key_constraint.
-
#set_foreign_key_constraint_comment(foreign_key_constraint, code_comment = nil) ⇒ Object
add a comment to a foreign_key_constraint.
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(", :")}]" = {} [:name] = ":#{foreign_key_constraint.name}" # only provide values if they are different from the defaults unless foreign_key_constraint.initially_deferred == false [:initially_deferred] = foreign_key_constraint.initially_deferred end unless foreign_key_constraint.deferrable == false [:deferrable] = foreign_key_constraint.deferrable end unless foreign_key_constraint.on_delete == :no_action [:on_delete] = ":#{foreign_key_constraint.on_delete}" end unless foreign_key_constraint.on_update == :no_action [:on_update] = ":#{foreign_key_constraint.on_update}" end unless foreign_key_constraint.table.schema.name == foreign_key_constraint.foreign_schema_name [:foreign_schema] = ":#{foreign_key_constraint.foreign_schema_name}" end unless foreign_key_constraint.description.nil? [:comment] = " <<~COMMENT\n \#{indent foreign_key_constraint.description}\n COMMENT\n RUBY\n end\n\n options_syntax = options.map { |k, v| \"\#{k}: \#{v}\" }.join(\", \")\n\n add_fragment schema: foreign_key_constraint.table.schema,\n table: foreign_key_constraint.table,\n migration_method: :add_foreign_key,\n object: foreign_key_constraint,\n code_comment: code_comment,\n dependent_table: foreign_key_constraint.foreign_table,\n migration: <<~RUBY\n add_foreign_key :\#{foreign_key_constraint.table.name}, \#{column_names}, :\#{foreign_key_constraint.foreign_table.name}, \#{foreign_column_names}, \#{options_syntax}\n RUBY\nend\n" |
#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, " Removing original foreign key constraint because it has changed (it is recreated below)\n Changes:\n \#{indent original_foreign_key_constraint.differences_descriptions(updated_foreign_key_constraint).join(\"\\n\")}\n CODE_COMMENT\n\n # recrete the foreign_key_constraint with the new options\n recreation_fragment = add_foreign_key_constraint updated_foreign_key_constraint, <<~CODE_COMMENT\n Recreating this foreign key constraint\n CODE_COMMENT\n\n # return the new fragments (the main reason to return them here is for the specs)\n [removal_fragment, recreation_fragment]\nend\n" |
#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: " remove_foreign_key :\#{foreign_key_constraint.table.name}, :\#{foreign_key_constraint.name}\n RUBY\nend\n" |
#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: " remove_foreign_key_comment :\#{foreign_key_constraint.table.name}, :\#{foreign_key_constraint.name}\n RUBY\nend\n" |
#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: " set_foreign_key_comment :\#{foreign_key_constraint.table.name}, :\#{foreign_key_constraint.name}, <<~COMMENT\n \#{indent description}\n COMMENT\n RUBY\nend\n" |