use migration helpers in spec
This commit is contained in:
@@ -76,18 +76,19 @@ module ActiveRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Main DSL method for defining auxiliary tables
|
# Main DSL method for defining auxiliary tables
|
||||||
sig { params(table_name: T.any(String, Symbol)).returns(AuxTableConfig) }
|
sig { params(aux_name: T.any(String, Symbol)).returns(AuxTableConfig) }
|
||||||
def aux_table(table_name)
|
def aux_table(aux_name)
|
||||||
table_name = table_name.to_sym
|
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,
|
Kernel.raise ArgumentError,
|
||||||
"Auxiliary table '#{table_name}' is already defined"
|
"Auxiliary '#{aux_name}' (table '#{aux_table_name}') is already defined"
|
||||||
end
|
end
|
||||||
|
|
||||||
aux_table_configurations[table_name] = config =
|
aux_table_configurations[aux_table_name] = config =
|
||||||
generate_aux_model_class(table_name)
|
generate_aux_model_class(aux_table_name)
|
||||||
setup_schema_loading_hook(table_name)
|
setup_schema_loading_hook(aux_table_name)
|
||||||
setup_auto_join_queries(config)
|
setup_auto_join_queries(config)
|
||||||
|
|
||||||
config
|
config
|
||||||
|
|||||||
@@ -48,11 +48,11 @@ RSpec.describe ActiveRecord::AuxTable do
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Car < Vehicle
|
class Car < Vehicle
|
||||||
aux_table :vehicles_car_aux
|
aux_table :car
|
||||||
end
|
end
|
||||||
|
|
||||||
class Boat < Vehicle
|
class Boat < Vehicle
|
||||||
aux_table :vehicles_boat_aux
|
aux_table :boat
|
||||||
end
|
end
|
||||||
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
|
it "raises error when auxiliary table defines column that exists in main table" do
|
||||||
# Create a test schema with overlapping columns
|
# Create a test schema with overlapping columns
|
||||||
ActiveRecord::Schema.define do
|
ActiveRecord::Schema.define do
|
||||||
create_table :test_overlap_mains do |t|
|
create_base_table :test_overlap_mains do |t|
|
||||||
t.string :type, null: false
|
|
||||||
t.string :name
|
t.string :name
|
||||||
t.string :description
|
t.string :description
|
||||||
t.timestamps
|
|
||||||
end
|
|
||||||
|
|
||||||
create_table :test_overlap_auxes do |t|
|
t.create_aux :overlap do |t|
|
||||||
t.references :test_overlap_main,
|
t.string :name
|
||||||
null: false,
|
t.string :extra_data
|
||||||
foreign_key: {
|
end
|
||||||
to_table: :test_overlap_mains
|
|
||||||
}
|
|
||||||
t.string :name # This overlaps with main table
|
|
||||||
t.string :extra_data
|
|
||||||
t.timestamps
|
t.timestamps
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -530,58 +524,25 @@ RSpec.describe ActiveRecord::AuxTable do
|
|||||||
|
|
||||||
expect {
|
expect {
|
||||||
class TestOverlapChild < TestOverlapMain
|
class TestOverlapChild < TestOverlapMain
|
||||||
aux_table :test_overlap_auxes
|
aux_table :overlap
|
||||||
end
|
end
|
||||||
|
|
||||||
# Trigger schema loading to activate validation
|
# Trigger schema loading to activate validation
|
||||||
TestOverlapChild.load_schema
|
TestOverlapChild.load_schema
|
||||||
}.to raise_error(
|
}.to raise_error(ArgumentError, /defines column\(s\) 'name'/)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "ignores system columns and foreign keys when checking for overlaps" do
|
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)
|
# Create a test schema where system columns are duplicated (which should be allowed)
|
||||||
ActiveRecord::Schema.define do
|
ActiveRecord::Schema.define do
|
||||||
create_table :test_system_cols_main do |t|
|
create_base_table :test_system_cols_main do |t|
|
||||||
t.string :type, null: false
|
|
||||||
t.string :name
|
t.string :name
|
||||||
t.timestamps
|
|
||||||
end
|
|
||||||
|
|
||||||
create_table :test_system_cols_aux do |t|
|
t.create_aux :has_timestamps do |t|
|
||||||
t.references :test_system_cols_main, null: false, foreign_key: true
|
t.timestamps
|
||||||
t.string :special_data
|
end
|
||||||
t.timestamps # These are system columns and should be ignored
|
|
||||||
|
t.timestamps
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -591,7 +552,7 @@ RSpec.describe ActiveRecord::AuxTable do
|
|||||||
|
|
||||||
expect {
|
expect {
|
||||||
class TestSystemColsChild < TestSystemColsMain
|
class TestSystemColsChild < TestSystemColsMain
|
||||||
aux_table :test_system_cols_aux
|
aux_table :has_timestamps
|
||||||
end
|
end
|
||||||
}.not_to raise_error
|
}.not_to raise_error
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user