Module: DynamicMigrations::Postgres::Generator::PrimaryKey
- Included in:
- DynamicMigrations::Postgres::Generator
- Defined in:
- lib/dynamic_migrations/postgres/generator/primary_key.rb
Instance Method Summary collapse
- #add_primary_key(primary_key, code_comment = nil) ⇒ Object
- #recreate_primary_key(original_primary_key, updated_primary_key) ⇒ Object
- #remove_primary_key(primary_key, code_comment = nil) ⇒ Object
-
#remove_primary_key_comment(primary_key, code_comment = nil) ⇒ Object
remove the comment from a primary_key.
-
#set_primary_key_comment(primary_key, code_comment = nil) ⇒ Object
add a comment to a primary_key.
Instance Method Details
#add_primary_key(primary_key, 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 |
# File 'lib/dynamic_migrations/postgres/generator/primary_key.rb', line 5 def add_primary_key primary_key, 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 column_names = (primary_key.column_names.count == 1) ? ":#{primary_key.column_names.first}" : "[:#{primary_key.column_names.join(", :")}]" = { name: ":#{primary_key.name}" } unless primary_key.description.nil? [:comment] = " <<~COMMENT\n \#{indent primary_key.description}\n COMMENT\n RUBY\n end\n\n options_syntax = options.map { |k, v| \"\#{k}: \#{v}\" }.join(\", \")\n\n add_fragment schema: primary_key.table.schema,\n table: primary_key.table,\n migration_method: :add_primary_key,\n object: primary_key,\n code_comment: code_comment,\n migration: <<~RUBY\n add_primary_key :\#{primary_key.table.name}, \#{column_names}, \#{options_syntax}\n RUBY\nend\n" |
#recreate_primary_key(original_primary_key, updated_primary_key) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dynamic_migrations/postgres/generator/primary_key.rb', line 46 def recreate_primary_key original_primary_key, updated_primary_key # remove the original primary_key removal_fragment = remove_primary_key original_primary_key, " Removing original primary key because it has changed (it is recreated below)\n Changes:\n \#{indent original_primary_key.differences_descriptions(updated_primary_key).join(\"\\n\")}\n CODE_COMMENT\n\n # recrete the primary_key with the new options\n recreation_fragment = add_primary_key updated_primary_key, <<~CODE_COMMENT\n Recreating this primary key\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_primary_key(primary_key, code_comment = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dynamic_migrations/postgres/generator/primary_key.rb', line 35 def remove_primary_key primary_key, code_comment = nil add_fragment schema: primary_key.table.schema, table: primary_key.table, migration_method: :remove_primary_key, object: primary_key, code_comment: code_comment, migration: " remove_primary_key :\#{primary_key.table.name}, :\#{primary_key.name}\n RUBY\nend\n" |
#remove_primary_key_comment(primary_key, code_comment = nil) ⇒ Object
remove the comment from a primary_key
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dynamic_migrations/postgres/generator/primary_key.rb', line 84 def remove_primary_key_comment primary_key, code_comment = nil add_fragment schema: primary_key.table.schema, table: primary_key.table, migration_method: :remove_primary_key_comment, object: primary_key, code_comment: code_comment, migration: " remove_primary_key_comment :\#{primary_key.table.name}, :\#{primary_key.name}\n RUBY\nend\n" |
#set_primary_key_comment(primary_key, code_comment = nil) ⇒ Object
add a comment to a primary_key
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dynamic_migrations/postgres/generator/primary_key.rb', line 64 def set_primary_key_comment primary_key, code_comment = nil description = primary_key.description if description.nil? raise MissingDescriptionError, "Missing required description for primary_key `#{primary_key.name}` in table `#{primary_key.table.schema.name}.#{primary_key.table.name}`" end add_fragment schema: primary_key.table.schema, table: primary_key.table, migration_method: :set_primary_key_comment, object: primary_key, code_comment: code_comment, migration: " set_primary_key_comment :\#{primary_key.table.name}, :\#{primary_key.name}, <<~COMMENT\n \#{indent description}\n COMMENT\n RUBY\nend\n" |