use migration helpers in spec

This commit is contained in:
Dylan Knutson
2025-07-14 17:13:58 +00:00
parent c870eef9bc
commit 81ec3d2902
2 changed files with 26 additions and 64 deletions

View File

@@ -76,18 +76,19 @@ module ActiveRecord
end
# Main DSL method for defining auxiliary tables
sig { params(table_name: T.any(String, Symbol)).returns(AuxTableConfig) }
def aux_table(table_name)
table_name = table_name.to_sym
sig { params(aux_name: T.any(String, Symbol)).returns(AuxTableConfig) }
def aux_table(aux_name)
base_table = self.table_name
aux_table_name = :"#{base_table}_#{aux_name}_aux"
if aux_table_configurations.key?(table_name)
if aux_table_configurations.key?(aux_table_name)
Kernel.raise ArgumentError,
"Auxiliary table '#{table_name}' is already defined"
"Auxiliary '#{aux_name}' (table '#{aux_table_name}') is already defined"
end
aux_table_configurations[table_name] = config =
generate_aux_model_class(table_name)
setup_schema_loading_hook(table_name)
aux_table_configurations[aux_table_name] = config =
generate_aux_model_class(aux_table_name)
setup_schema_loading_hook(aux_table_name)
setup_auto_join_queries(config)
config

View File

@@ -48,11 +48,11 @@ RSpec.describe ActiveRecord::AuxTable do
end
class Car < Vehicle
aux_table :vehicles_car_aux
aux_table :car
end
class Boat < Vehicle
aux_table :vehicles_boat_aux
aux_table :boat
end
end
@@ -504,21 +504,15 @@ RSpec.describe ActiveRecord::AuxTable do
it "raises error when auxiliary table defines column that exists in main table" do
# Create a test schema with overlapping columns
ActiveRecord::Schema.define do
create_table :test_overlap_mains do |t|
t.string :type, null: false
create_base_table :test_overlap_mains do |t|
t.string :name
t.string :description
t.timestamps
t.create_aux :overlap do |t|
t.string :name
t.string :extra_data
end
create_table :test_overlap_auxes do |t|
t.references :test_overlap_main,
null: false,
foreign_key: {
to_table: :test_overlap_mains
}
t.string :name # This overlaps with main table
t.string :extra_data
t.timestamps
end
end
@@ -530,58 +524,25 @@ RSpec.describe ActiveRecord::AuxTable do
expect {
class TestOverlapChild < TestOverlapMain
aux_table :test_overlap_auxes
aux_table :overlap
end
# Trigger schema loading to activate validation
TestOverlapChild.load_schema
}.to raise_error(
ArgumentError,
/Auxiliary table 'test_overlap_auxes' defines column\(s\) 'name'/
)
end
it "allows auxiliary table with non-overlapping columns" do
# Create a test schema with no overlapping columns
ActiveRecord::Schema.define do
create_table :test_no_overlap_main do |t|
t.string :type, null: false
t.string :name
t.timestamps
end
create_table :test_no_overlap_aux do |t|
t.references :test_no_overlap_main, null: false, foreign_key: true
t.string :fuel_type # No overlap
t.decimal :engine_size
t.timestamps
end
end
class TestNoOverlapMain < ActiveRecord::Base
include ActiveRecord::AuxTable
end
expect {
class TestNoOverlapChild < TestNoOverlapMain
aux_table :test_no_overlap_aux
end
}.not_to raise_error
}.to raise_error(ArgumentError, /defines column\(s\) 'name'/)
end
it "ignores system columns and foreign keys when checking for overlaps" do
# Create a test schema where system columns are duplicated (which should be allowed)
ActiveRecord::Schema.define do
create_table :test_system_cols_main do |t|
t.string :type, null: false
create_base_table :test_system_cols_main do |t|
t.string :name
t.create_aux :has_timestamps do |t|
t.timestamps
end
create_table :test_system_cols_aux do |t|
t.references :test_system_cols_main, null: false, foreign_key: true
t.string :special_data
t.timestamps # These are system columns and should be ignored
t.timestamps
end
end
@@ -591,7 +552,7 @@ RSpec.describe ActiveRecord::AuxTable do
expect {
class TestSystemColsChild < TestSystemColsMain
aux_table :test_system_cols_aux
aux_table :has_timestamps
end
}.not_to raise_error
end