From 81ec3d29027655ee13acbc0e39eb727a6392382e Mon Sep 17 00:00:00 2001 From: Dylan Knutson Date: Mon, 14 Jul 2025 17:13:58 +0000 Subject: [PATCH] use migration helpers in spec --- lib/active_record/aux_table.rb | 17 ++++--- spec/active_record/aux_table_spec.rb | 73 +++++++--------------------- 2 files changed, 26 insertions(+), 64 deletions(-) diff --git a/lib/active_record/aux_table.rb b/lib/active_record/aux_table.rb index 82a21ac..d0e3e23 100644 --- a/lib/active_record/aux_table.rb +++ b/lib/active_record/aux_table.rb @@ -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 diff --git a/spec/active_record/aux_table_spec.rb b/spec/active_record/aux_table_spec.rb index 0585880..fcf66d2 100644 --- a/spec/active_record/aux_table_spec.rb +++ b/spec/active_record/aux_table_spec.rb @@ -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 - 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.create_aux :overlap do |t| + t.string :name + t.string :extra_data + end + 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.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.create_aux :has_timestamps do |t| + t.timestamps + end + + 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