Skip to content

Commit 0a30d2b

Browse files
authored
Merge pull request rails#50794 from Shopify/test-trigger-pk-population-against-more-dbs
[Tests only] Expand trigger populated PK test case to run against more DBs
2 parents 5703463 + 2457067 commit 0a30d2b

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

activerecord/test/cases/persistence_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ def test_model_with_no_auto_populated_fields_still_returns_primary_key_after_ins
15811581

15821582
assert_not_nil record.id
15831583
assert record.id > 0
1584-
end if current_adapter?(:PostgreSQLAdapter)
1584+
end if supports_insert_returning? && !current_adapter?(:SQLite3Adapter)
15851585
end
15861586

15871587
class QueryConstraintsTest < ActiveRecord::TestCase

activerecord/test/schema/schema.rb

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,30 +1483,41 @@
14831483
end
14841484
end
14851485

1486-
if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter)
1486+
if ActiveRecord::Base.connection.supports_insert_returning? && !ActiveRecord::TestCase.current_adapter?(:SQLite3Adapter)
14871487
ActiveRecord::Base.connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
14881488
t.integer :id, null: false
14891489
end
14901490

1491-
ActiveRecord::Base.connection.execute(
1492-
<<-SQL
1493-
CREATE OR REPLACE FUNCTION populate_column()
1494-
RETURNS TRIGGER AS $$
1495-
DECLARE
1496-
max_value INTEGER;
1497-
BEGIN
1498-
SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records;
1499-
NEW.id = COALESCE(max_value, 0) + 1;
1500-
RETURN NEW;
1501-
END;
1502-
$$ LANGUAGE plpgsql;
1503-
1504-
CREATE TRIGGER before_insert_trigger
1505-
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
1506-
FOR EACH ROW
1507-
EXECUTE FUNCTION populate_column();
1508-
SQL
1509-
)
1491+
if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter)
1492+
ActiveRecord::Base.connection.execute(
1493+
<<-SQL
1494+
CREATE OR REPLACE FUNCTION populate_column()
1495+
RETURNS TRIGGER AS $$
1496+
DECLARE
1497+
max_value INTEGER;
1498+
BEGIN
1499+
SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records;
1500+
NEW.id = COALESCE(max_value, 0) + 1;
1501+
RETURN NEW;
1502+
END;
1503+
$$ LANGUAGE plpgsql;
1504+
1505+
CREATE TRIGGER before_insert_trigger
1506+
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
1507+
FOR EACH ROW
1508+
EXECUTE FUNCTION populate_column();
1509+
SQL
1510+
)
1511+
elsif ActiveRecord::TestCase.current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
1512+
ActiveRecord::Base.connection.execute(
1513+
<<-SQL
1514+
CREATE TRIGGER before_insert_trigger
1515+
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
1516+
FOR EACH ROW
1517+
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
1518+
SQL
1519+
)
1520+
end
15101521
end
15111522

15121523
Course.connection.create_table :courses, force: true do |t|

0 commit comments

Comments
 (0)