Merge pull request #50794 from Shopify/test-trigger-pk-population-against-more-dbs

[Tests only] Expand trigger populated PK test case to run against more DBs
This commit is contained in:
Jean Boussier 2024-01-18 19:17:00 +01:00 committed by GitHub
commit 0a30d2bb3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 20 deletions

View File

@ -1581,7 +1581,7 @@ class PersistenceTest < ActiveRecord::TestCase
assert_not_nil record.id
assert record.id > 0
end if current_adapter?(:PostgreSQLAdapter)
end if supports_insert_returning? && !current_adapter?(:SQLite3Adapter)
end
class QueryConstraintsTest < ActiveRecord::TestCase

View File

@ -1483,30 +1483,41 @@ ActiveRecord::Schema.define do
end
end
if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter)
if ActiveRecord::Base.connection.supports_insert_returning? && !ActiveRecord::TestCase.current_adapter?(:SQLite3Adapter)
ActiveRecord::Base.connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
t.integer :id, null: false
end
ActiveRecord::Base.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;
if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter)
ActiveRecord::Base.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
)
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.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.connection.create_table :courses, force: true do |t|