From 4b968debed92830f1af65e7e3047e49f17da876d Mon Sep 17 00:00:00 2001 From: Ulysse Buonomo Date: Mon, 2 Sep 2024 14:43:13 +0200 Subject: [PATCH] Separate specific db schema --- .../test/schema/mysql2_specific_schema.rb | 15 ++++++++ .../test/schema/postgresql_specific_schema.rb | 26 +++++++++++++ activerecord/test/schema/schema.rb | 37 ------------------- .../test/schema/trilogy_specific_schema.rb | 15 ++++++++ 4 files changed, 56 insertions(+), 37 deletions(-) diff --git a/activerecord/test/schema/mysql2_specific_schema.rb b/activerecord/test/schema/mysql2_specific_schema.rb index 5a5c30366d2d2..66006fc0e1dec 100644 --- a/activerecord/test/schema/mysql2_specific_schema.rb +++ b/activerecord/test/schema/mysql2_specific_schema.rb @@ -83,3 +83,18 @@ END SQL end + +if ActiveRecord::Base.lease_connection.supports_insert_returning? + ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t| + t.integer :id, null: false + end + + ActiveRecord::Base.lease_connection.execute( + <<-SQL + CREATE TRIGGER before_insert_trigger + BEFORE INSERT ON pk_autopopulated_by_a_trigger_records + FOR EACH ROW + SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records); + SQL + ) +end diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index e8be384b1830f..6893e744a8492 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -197,3 +197,29 @@ add_index(:companies, [:firm_id, :type], name: "company_include_index", include: [:name, :account_id]) end + +if ActiveRecord::Base.lease_connection.supports_insert_returning? + ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t| + t.integer :id, null: false + end + + ActiveRecord::Base.lease_connection.execute( + <<-SQL + CREATE OR REPLACE FUNCTION populate_column() + RETURNS TRIGGER AS $$ + DECLARE + max_value INTEGER; + BEGIN + SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records; + NEW.id = COALESCE(max_value, 0) + 1; + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + CREATE TRIGGER before_insert_trigger + BEFORE INSERT ON pk_autopopulated_by_a_trigger_records + FOR EACH ROW + EXECUTE FUNCTION populate_column(); + SQL + ) +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 3eb53417c0f37..18c5d6b4993b7 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -1476,43 +1476,6 @@ end end -if ActiveRecord::Base.lease_connection.supports_insert_returning? && !ActiveRecord::TestCase.current_adapter?(:SQLite3Adapter) - ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t| - t.integer :id, null: false - end - - if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter) - ActiveRecord::Base.lease_connection.execute( - <<-SQL - CREATE OR REPLACE FUNCTION populate_column() - RETURNS TRIGGER AS $$ - DECLARE - max_value INTEGER; - BEGIN - SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records; - NEW.id = COALESCE(max_value, 0) + 1; - RETURN NEW; - END; - $$ LANGUAGE plpgsql; - - CREATE TRIGGER before_insert_trigger - BEFORE INSERT ON pk_autopopulated_by_a_trigger_records - FOR EACH ROW - EXECUTE FUNCTION populate_column(); - SQL - ) - elsif ActiveRecord::TestCase.current_adapter?(:Mysql2Adapter, :TrilogyAdapter) - ActiveRecord::Base.lease_connection.execute( - <<-SQL - CREATE TRIGGER before_insert_trigger - BEFORE INSERT ON pk_autopopulated_by_a_trigger_records - FOR EACH ROW - SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records); - SQL - ) - end -end - Course.lease_connection.create_table :courses, force: true do |t| t.column :name, :string, null: false t.column :college_id, :integer, index: true diff --git a/activerecord/test/schema/trilogy_specific_schema.rb b/activerecord/test/schema/trilogy_specific_schema.rb index cb99f66f61536..4a6b355eaeaa8 100644 --- a/activerecord/test/schema/trilogy_specific_schema.rb +++ b/activerecord/test/schema/trilogy_specific_schema.rb @@ -82,3 +82,18 @@ END SQL end + +if ActiveRecord::Base.lease_connection.supports_insert_returning? + ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t| + t.integer :id, null: false + end + + ActiveRecord::Base.lease_connection.execute( + <<-SQL + CREATE TRIGGER before_insert_trigger + BEFORE INSERT ON pk_autopopulated_by_a_trigger_records + FOR EACH ROW + SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records); + SQL + ) +end