Skip to content

Commit 6d100e5

Browse files
authored
Merge pull request rails#45643 from adrianna-chang-shopify/ac-extract-build-add-col-definition
Extract `#build_add_column_definition`
2 parents f3339df + e91b642 commit 6d100e5

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def visit_AlterTable(o)
2626
sql << o.foreign_key_drops.map { |fk| visit_DropForeignKey fk }.join(" ")
2727
sql << o.check_constraint_adds.map { |con| visit_AddCheckConstraint con }.join(" ")
2828
sql << o.check_constraint_drops.map { |con| visit_DropCheckConstraint con }.join(" ")
29+
o.ddl = sql
2930
end
3031

3132
def visit_ColumnDefinition(o)

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ class AlterTable # :nodoc:
580580
attr_reader :adds
581581
attr_reader :foreign_key_adds, :foreign_key_drops
582582
attr_reader :check_constraint_adds, :check_constraint_drops
583+
attr_accessor :ddl
583584

584585
def initialize(td)
585586
@td = td

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,24 @@ def drop_table(table_name, **options)
599599
# # Ignores the method call if the column exists
600600
# add_column(:shapes, :triangle, 'polygon', if_not_exists: true)
601601
def add_column(table_name, column_name, type, **options)
602+
add_column_def = build_add_column_definition(table_name, column_name, type, **options)
603+
return unless add_column_def
604+
605+
execute(add_column_def.ddl)
606+
end
607+
608+
def add_columns(table_name, *column_names, type:, **options) # :nodoc:
609+
column_names.each do |column_name|
610+
add_column(table_name, column_name, type, **options)
611+
end
612+
end
613+
614+
# Builds an AlterTable object for adding a column to a table.
615+
#
616+
# This definition object contains information about the column that would be created
617+
# if the same arguments were passed to #add_column. See #add_column for information about
618+
# passing a +table_name+, +column_name+, +type+ and other options that can be passed.
619+
def build_add_column_definition(table_name, column_name, type, **options) # :nodoc:
602620
return if options[:if_not_exists] == true && column_exists?(table_name, column_name)
603621

604622
if supports_datetime_with_precision?
@@ -607,15 +625,10 @@ def add_column(table_name, column_name, type, **options)
607625
end
608626
end
609627

610-
at = create_alter_table table_name
611-
at.add_column(column_name, type, **options)
612-
execute schema_creation.accept at
613-
end
614-
615-
def add_columns(table_name, *column_names, type:, **options) # :nodoc:
616-
column_names.each do |column_name|
617-
add_column(table_name, column_name, type, **options)
618-
end
628+
alter_table = create_alter_table(table_name)
629+
alter_table.add_column(column_name, type, **options)
630+
schema_creation.accept(alter_table)
631+
alter_table
619632
end
620633

621634
# Removes the given columns from the table definition.

activerecord/lib/active_record/connection_adapters/postgresql/schema_creation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def visit_AlterTable(o)
1010
sql << o.constraint_validations.map { |fk| visit_ValidateConstraint fk }.join(" ")
1111
sql << o.exclusion_constraint_adds.map { |con| visit_AddExclusionConstraint con }.join(" ")
1212
sql << o.exclusion_constraint_drops.map { |con| visit_DropExclusionConstraint con }.join(" ")
13+
o.ddl = sql
1314
end
1415

1516
def visit_AddForeignKey(o)

activerecord/test/cases/migration/schema_definitions_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ def test_build_create_index_definition_for_existing_index
6161
connection.drop_table(:test) if connection.table_exists?(:test)
6262
end
6363
end
64+
65+
def test_build_add_column_definition
66+
connection.create_table(:test)
67+
add_col_td = connection.build_add_column_definition(:test, :foo, :string)
68+
69+
assert_match "ALTER TABLE", add_col_td.ddl
70+
71+
add_cols = add_col_td.adds
72+
assert_equal 1, add_cols.size
73+
74+
add_col = add_cols.first.column
75+
assert_equal "foo", add_col.name
76+
assert add_col.type
77+
assert add_col.sql_type
78+
ensure
79+
connection.drop_table(:test) if connection.table_exists?(:test)
80+
end
6481
end
6582
end
6683
end

0 commit comments

Comments
 (0)