Module: DynamicMigrations::Postgres::Generator::Column
- Included in:
- DynamicMigrations::Postgres::Generator
- Defined in:
- lib/dynamic_migrations/postgres/generator/column.rb
Defined Under Namespace
Classes: NoColumnCommentError
Instance Method Summary collapse
- #add_column(column, code_comment = nil) ⇒ Object
- #change_column(column, code_comment = nil) ⇒ Object
- #remove_column(column, code_comment = nil) ⇒ Object
-
#remove_column_comment(column, code_comment = nil) ⇒ Object
remove the comment from a column.
-
#set_column_comment(column, code_comment = nil) ⇒ Object
add a comment to a column.
Instance Method Details
#add_column(column, code_comment = nil) ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dynamic_migrations/postgres/generator/column.rb', line 8 def add_column column, code_comment = nil if column.description.nil? raise NoColumnCommentError, "Refusing to generate add_column migration, no description was provided for `#{column.table.schema.name}`.`#{column.table.name}` column `#{column.name}`" end = {} [:null] = column.null unless column.default.nil? [:default] = "\"#{column.default}\"" end if column.array? [:array] = true end # comment has to be last if column.description [:comment] = <<~RUBY <<~COMMENT #{indent column.description} COMMENT RUBY end = .map { |k, v| "#{k}: #{v}" }.join(", ") data_type = column.data_type.to_s # if it's an array, then we need to remove the [] from the end if column.array? data_type = data_type.sub(/\[\]\z/, "") end # if its a custom type (has special characters) then we need to quote it # otherwise, present it as a symbol data_type = if data_type.match?(/\A\w+\z/) ":#{data_type}" else "\"#{data_type}\"" end # we only provide a dependent enum if the enum has more than one column # or is in a different schema, otherwise it is added to the same migration as this # column and we don't need to worry about dependencies dependent_enum = nil # column.enum will be nil if the column is not an enum if column.enum && (column.enum.columns.count > 1 || column.table.schema != column.enum.schema) dependent_enum = column.enum end add_fragment schema: column.table.schema, table: column.table, migration_method: :add_column, dependent_enum: dependent_enum, object: column, code_comment: code_comment, migration: <<~RUBY add_column :#{column.table.name}, :#{column.name}, #{data_type}, #{} RUBY end |
#change_column(column, code_comment = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/dynamic_migrations/postgres/generator/column.rb', line 68 def change_column column, code_comment = nil = {} [:null] = column.null unless column.default.nil? [:default] = "\"#{column.default}\"" end if column.array? [:array] = true end = .map { |k, v| "#{k}: #{v}" }.join(", ") data_type = column.data_type if column.array? data_type = ":\"#{data_type}\"" end add_fragment schema: column.table.schema, table: column.table, migration_method: :change_column, # this will be nil if the column is not an enum dependent_enum: column.enum, object: column, code_comment: code_comment, migration: <<~RUBY change_column :#{column.table.name}, :#{column.name}, :#{data_type}, #{} RUBY end |
#remove_column(column, code_comment = nil) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dynamic_migrations/postgres/generator/column.rb', line 99 def remove_column column, code_comment = nil add_fragment schema: column.table.schema, table: column.table, migration_method: :remove_column, object: column, code_comment: code_comment, migration: <<~RUBY remove_column :#{column.table.name}, :#{column.name} RUBY end |
#remove_column_comment(column, code_comment = nil) ⇒ Object
remove the comment from a column
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dynamic_migrations/postgres/generator/column.rb', line 131 def remove_column_comment column, code_comment = nil add_fragment schema: column.table.schema, table: column.table, migration_method: :remove_column_comment, object: column, code_comment: code_comment, migration: <<~RUBY remove_column_comment :#{column.table.name}, :#{column.name} RUBY end |
#set_column_comment(column, code_comment = nil) ⇒ Object
add a comment to a column
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dynamic_migrations/postgres/generator/column.rb', line 111 def set_column_comment column, code_comment = nil description = column.description if description.nil? raise MissingDescriptionError, "Missing required description for column `#{column.name}` in table `#{column.table.schema.name}.#{column.table.name}`" end add_fragment schema: column.table.schema, table: column.table, migration_method: :set_column_comment, object: column, code_comment: code_comment, migration: <<~RUBY set_column_comment :#{column.table.name}, :#{column.name}, <<~COMMENT #{indent description} COMMENT RUBY end |