Skip to content

Commit d5c2ff8

Browse files
Merge pull request rails#52781 from BuonOmo/main
Separate specific db schema
2 parents 5f71da1 + 4b968de commit d5c2ff8

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

activerecord/test/schema/mysql2_specific_schema.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,18 @@
8383
END
8484
SQL
8585
end
86+
87+
if ActiveRecord::Base.lease_connection.supports_insert_returning?
88+
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
89+
t.integer :id, null: false
90+
end
91+
92+
ActiveRecord::Base.lease_connection.execute(
93+
<<-SQL
94+
CREATE TRIGGER before_insert_trigger
95+
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
96+
FOR EACH ROW
97+
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
98+
SQL
99+
)
100+
end

activerecord/test/schema/postgresql_specific_schema.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,29 @@
197197

198198
add_index(:companies, [:firm_id, :type], name: "company_include_index", include: [:name, :account_id])
199199
end
200+
201+
if ActiveRecord::Base.lease_connection.supports_insert_returning?
202+
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
203+
t.integer :id, null: false
204+
end
205+
206+
ActiveRecord::Base.lease_connection.execute(
207+
<<-SQL
208+
CREATE OR REPLACE FUNCTION populate_column()
209+
RETURNS TRIGGER AS $$
210+
DECLARE
211+
max_value INTEGER;
212+
BEGIN
213+
SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records;
214+
NEW.id = COALESCE(max_value, 0) + 1;
215+
RETURN NEW;
216+
END;
217+
$$ LANGUAGE plpgsql;
218+
219+
CREATE TRIGGER before_insert_trigger
220+
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
221+
FOR EACH ROW
222+
EXECUTE FUNCTION populate_column();
223+
SQL
224+
)
225+
end

activerecord/test/schema/schema.rb

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,43 +1476,6 @@
14761476
end
14771477
end
14781478

1479-
if ActiveRecord::Base.lease_connection.supports_insert_returning? && !ActiveRecord::TestCase.current_adapter?(:SQLite3Adapter)
1480-
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
1481-
t.integer :id, null: false
1482-
end
1483-
1484-
if ActiveRecord::TestCase.current_adapter?(:PostgreSQLAdapter)
1485-
ActiveRecord::Base.lease_connection.execute(
1486-
<<-SQL
1487-
CREATE OR REPLACE FUNCTION populate_column()
1488-
RETURNS TRIGGER AS $$
1489-
DECLARE
1490-
max_value INTEGER;
1491-
BEGIN
1492-
SELECT MAX(id) INTO max_value FROM pk_autopopulated_by_a_trigger_records;
1493-
NEW.id = COALESCE(max_value, 0) + 1;
1494-
RETURN NEW;
1495-
END;
1496-
$$ LANGUAGE plpgsql;
1497-
1498-
CREATE TRIGGER before_insert_trigger
1499-
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
1500-
FOR EACH ROW
1501-
EXECUTE FUNCTION populate_column();
1502-
SQL
1503-
)
1504-
elsif ActiveRecord::TestCase.current_adapter?(:Mysql2Adapter, :TrilogyAdapter)
1505-
ActiveRecord::Base.lease_connection.execute(
1506-
<<-SQL
1507-
CREATE TRIGGER before_insert_trigger
1508-
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
1509-
FOR EACH ROW
1510-
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
1511-
SQL
1512-
)
1513-
end
1514-
end
1515-
15161479
Course.lease_connection.create_table :courses, force: true do |t|
15171480
t.column :name, :string, null: false
15181481
t.column :college_id, :integer, index: true

activerecord/test/schema/trilogy_specific_schema.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@
8282
END
8383
SQL
8484
end
85+
86+
if ActiveRecord::Base.lease_connection.supports_insert_returning?
87+
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
88+
t.integer :id, null: false
89+
end
90+
91+
ActiveRecord::Base.lease_connection.execute(
92+
<<-SQL
93+
CREATE TRIGGER before_insert_trigger
94+
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
95+
FOR EACH ROW
96+
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
97+
SQL
98+
)
99+
end

0 commit comments

Comments
 (0)