Class: Localtower::Generators::Migration
- Inherits:
-
Object
- Object
- Localtower::Generators::Migration
- Defined in:
- lib/localtower/generators/migration.rb
Constant Summary collapse
- TYPES =
[ { name: "string", example: "'foo'", }, { name: "text", example: "'long foo'", }, { name: "datetime", example: "2024-07-22 12:28:31.487318", }, { name: "date", example: "2024-09-24", }, { name: "uuid", example: "'860f5e00-0000-0000-0000-000000000000'", }, { name: "integer", example: "123", }, { name: "bigint", example: "123", }, { name: "float", example: "30.12", }, { name: "numeric", example: "30.12", }, { name: "decimal", example: "30.12", }, { name: "json", example: "{\"foo\": \"bar\"}", }, { name: "jsonb", example: "{\"foo\": \"bar\"}", }, { name: "binary", example: "01010001", }, { name: "boolean", example: "true/false", }, { name: "array", example: "[1, 2, 3]", }, { name: "blob", example: "x3F7F2A9", }, { name: "references", example: "MyModel", } ]
- ACTIONS =
[ { name: "add_column", label: "Add column", example: "add_column :users, :name, :string", }, { name: "add_index_to_column", label: "Add index", example: "add_index :users, :name", }, { name: "belongs_to", label: "Add reference", example: "add_reference :posts, :user", }, { name: "remove_index_to_column", label: "Remove index", example: "remove_index :users, :name", }, { name: "remove_column", label: "Remove column", example: "remove_column :users, :name", }, { name: "change_column_type", label: "Change column type", example: "change_column :users, :name, :string", }, { name: "rename_column", label: "Rename column", example: "rename_column :users, :name, :new_name", }, { name: "drop_table", label: "Drop table", example: "drop_table :users", } ]
Instance Attribute Summary collapse
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
-
#thor ⇒ Object
readonly
Returns the value of attribute thor.
Instance Method Summary collapse
-
#initialize(migrations, migration_name = nil) ⇒ Migration
constructor
A new instance of Migration.
-
#run ⇒ Object
const DEFAULT_LINE = { model_name: MODELS ? MODELS.value : ”, table_name: MODELS ? MODELS.table_name : ”, action_name: ‘add_column’, column_type: ‘string’, column_name: ”, new_column_name: ”, new_column_type: ”, default: ”, unique: false, foreign_key: false, index: ”, index_algorithm: ‘default’, index_name: ”, nullable: true };.
Constructor Details
#initialize(migrations, migration_name = nil) ⇒ Migration
Returns a new instance of Migration.
245 246 247 248 249 250 251 |
# File 'lib/localtower/generators/migration.rb', line 245 def initialize(migrations, migration_name = nil) @thor = ThorGeneratorMigration.new # stringify keys: @migrations = JSON[migrations.to_json] @migration_name = migration_name end |
Instance Attribute Details
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
309 310 311 |
# File 'lib/localtower/generators/migration.rb', line 309 def migrations @migrations end |
#thor ⇒ Object (readonly)
Returns the value of attribute thor.
309 310 311 |
# File 'lib/localtower/generators/migration.rb', line 309 def thor @thor end |
Instance Method Details
#run ⇒ Object
const DEFAULT_LINE =
model_name: MODELS[0] ? MODELS[0].value : '',
table_name: MODELS[0] ? MODELS[0].table_name : '',
action_name: 'add_column',
column_type: 'string',
column_name: '',
new_column_name: '',
new_column_type: '',
default: '',
unique: false,
foreign_key: false,
index: '',
index_algorithm: 'default',
index_name: '',
nullable: true
;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/localtower/generators/migration.rb', line 270 def run if Localtower::Tools.pending_migrations.any? # raise "There is a pending migration. You can't generate a migration until the pending migration is committed." else migration_name_final = if @migration_name.present? @migration_name else action_names = migrations.map { |line| line['action_name'].camelize }.uniq.take(1).join model_names = migrations.map { |line| line['model_name'].camelize }.uniq.take(2).join column_names = migrations.map { |line| (line['column_name'].presence || "").camelize }.uniq.take(2).join "#{action_names}#{column_names}For#{model_names}" end ::Localtower::Tools.perform_migration(migration_name_final) end migrations.each do |action_line| next if action_line['action_name'].blank? # action_line['table_name'] = action_line['model_name'].pluralize.underscore proc_var = { 'add_column' => -> { thor.migration_add_column(action_line) }, 'remove_column' => -> { thor.migration_remove_column(action_line) }, 'rename_column' => -> { thor.migration_rename_column(action_line) }, 'change_column_type' => -> { thor.migration_change_column_type(action_line) }, 'add_index_to_column' => -> { thor.migration_add_index_to_column(action_line) }, 'belongs_to' => -> { thor.migration_belongs_to(action_line) }, 'remove_index_to_column' => -> { thor.migration_remove_index_to_column(action_line) }, 'drop_table' => -> { thor.migration_drop_table(action_line) } }[action_line['action_name']] raise "No proc found for action_name #{action_line['action_name']}" unless proc_var proc_var.call end end |