Skip to content

Commit 36b0150

Browse files
committed
WEB-8347: rename vars and add comments for clarity
1 parent 4327cd9 commit 36b0150

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/generators/declare_schema/migration/migrator.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,21 +374,21 @@ def change_table(model, current_table_name)
374374
::DeclareSchema::SchemaChange::ColumnRename.new(new_table_name, old_name, new_name)
375375
end
376376

377-
to_add.sort_by! { |c| model.field_specs[c]&.position || 0 }
377+
to_add.sort_by! { model.field_specs[_1]&.position || 0 }
378378

379-
adds = to_add.map do |c|
379+
adds = to_add.map do |col_name_to_add|
380380
type, options =
381-
if (spec = model.field_specs[c])
382-
[spec.type, spec.sql_options.merge(fk_field_options(model, c)).compact]
381+
if (spec = model.field_specs[col_name_to_add])
382+
[spec.type, spec.sql_options.merge(fk_field_options(model, col_name_to_add)).compact]
383383
else
384384
[:integer, {}]
385385
end
386-
::DeclareSchema::SchemaChange::ColumnAdd.new(new_table_name, c, type, **options)
386+
::DeclareSchema::SchemaChange::ColumnAdd.new(new_table_name, col_name_to_add, type, **options)
387387
end
388388

389-
removes = to_remove.map do |c|
390-
old_type, old_options = add_column_back(model, current_table_name, c)
391-
::DeclareSchema::SchemaChange::ColumnRemove.new(new_table_name, c, old_type, **old_options)
389+
removes = to_remove.map do |col_name_to_remove|
390+
old_type, old_options = add_column_back(model, current_table_name, col_name_to_remove)
391+
::DeclareSchema::SchemaChange::ColumnRemove.new(new_table_name, col_name_to_remove, old_type, **old_options)
392392
end
393393

394394
old_names = to_rename.invert
@@ -544,11 +544,14 @@ def foreign_key_changes_due_to_column_renames(fks_to_drop, fks_to_add, to_rename
544544
end
545545
end
546546

547+
# TODO: switch this to depend on _infer_foreign_key_field_spec instead
547548
def fk_field_options(model, field_name)
548-
if (foreign_key = model.constraint_definitions.find { |fk| field_name == fk.foreign_key_column })
549+
# check if the field_name is a foreign key
550+
if (foreign_key = model.constraint_definitions.find { field_name == _1.foreign_key_column })
551+
# if so, look up the target table's primary key column to get its limit (note: this is looking in the DB, not the spec)
549552
parent_columns = connection.columns(foreign_key.parent_table_name) rescue []
550553
pk_limit =
551-
if (pk_column = parent_columns.find { |column| column.name.to_s == "id" }) # right now foreign keys assume id is the target
554+
if (pk_column = parent_columns.find { _1.name.to_s == "id" }) # right now foreign keys assume id is the target
552555
pk_column.limit
553556
else
554557
8

0 commit comments

Comments
 (0)