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] = <<~RUBY <<~COMMENT #{indent primary_key.description} COMMENT RUBY end = .map { |k, v| "#{k}: #{v}" }.join(", ") add_fragment schema: primary_key.table.schema, table: primary_key.table, migration_method: :add_primary_key, object: primary_key, code_comment: code_comment, migration: <<~RUBY add_primary_key :#{primary_key.table.name}, #{column_names}, #{} RUBY end |
#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, <<~CODE_COMMENT Removing original primary key because it has changed (it is recreated below) Changes: #{indent original_primary_key.differences_descriptions(updated_primary_key).join("\n")} CODE_COMMENT # recrete the primary_key with the new options recreation_fragment = add_primary_key updated_primary_key, <<~CODE_COMMENT Recreating this primary key CODE_COMMENT # return the new fragments (the main reason to return them here is for the specs) [removal_fragment, recreation_fragment] end |
#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: <<~RUBY remove_primary_key :#{primary_key.table.name}, :#{primary_key.name} RUBY end |
#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: <<~RUBY remove_primary_key_comment :#{primary_key.table.name}, :#{primary_key.name} RUBY end |
#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: <<~RUBY set_primary_key_comment :#{primary_key.table.name}, :#{primary_key.name}, <<~COMMENT #{indent description} COMMENT RUBY end |