Skip to content

Commit e58a1a1

Browse files
committed
Tweak specific db schema
Follow up to rails#52781. `ActiveRecord::Schema` responds connection's methods.
1 parent 2ae883c commit e58a1a1

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

activerecord/test/schema/mysql2_specific_schema.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,17 @@
8080
SELECT * FROM topics LIMIT num;
8181
END
8282
SQL
83-
end
8483

85-
if ActiveRecord::Base.lease_connection.supports_insert_returning?
86-
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
87-
t.integer :id, null: false
88-
end
84+
if supports_insert_returning?
85+
create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
86+
t.integer :id, null: false
87+
end
8988

90-
ActiveRecord::Base.lease_connection.execute(
91-
<<-SQL
89+
execute <<~SQL
9290
CREATE TRIGGER before_insert_trigger
9391
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
9492
FOR EACH ROW
9593
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
9694
SQL
97-
)
95+
end
9896
end

activerecord/test/schema/postgresql_specific_schema.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# frozen_string_literal: true
22

33
ActiveRecord::Schema.define do
4-
ActiveRecord::TestCase.enable_extension!("uuid-ossp", ActiveRecord::Base.lease_connection)
5-
ActiveRecord::TestCase.enable_extension!("pgcrypto", ActiveRecord::Base.lease_connection) if ActiveRecord::Base.lease_connection.supports_pgcrypto_uuid?
4+
ActiveRecord::TestCase.enable_extension!("uuid-ossp", connection)
5+
ActiveRecord::TestCase.enable_extension!("pgcrypto", connection) if supports_pgcrypto_uuid?
66

7-
uuid_default = connection.supports_pgcrypto_uuid? ? {} : { default: "uuid_generate_v4()" }
7+
uuid_default = supports_pgcrypto_uuid? ? {} : { default: "uuid_generate_v4()" }
88

99
create_table :chat_messages, id: :uuid, force: true, **uuid_default do |t|
1010
t.text :content
@@ -196,15 +196,13 @@
196196
end
197197

198198
add_index(:companies, [:firm_id, :type], name: "company_include_index", include: [:name, :account_id])
199-
end
200199

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
200+
if supports_insert_returning?
201+
create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
202+
t.integer :id, null: false
203+
end
205204

206-
ActiveRecord::Base.lease_connection.execute(
207-
<<-SQL
205+
execute <<~SQL
208206
CREATE OR REPLACE FUNCTION populate_column()
209207
RETURNS TRIGGER AS $$
210208
DECLARE
@@ -221,5 +219,5 @@
221219
FOR EACH ROW
222220
EXECUTE FUNCTION populate_column();
223221
SQL
224-
)
222+
end
225223
end

activerecord/test/schema/trilogy_specific_schema.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,17 @@
7979
SELECT * FROM topics LIMIT num;
8080
END
8181
SQL
82-
end
8382

84-
if ActiveRecord::Base.lease_connection.supports_insert_returning?
85-
ActiveRecord::Base.lease_connection.create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
86-
t.integer :id, null: false
87-
end
83+
if supports_insert_returning?
84+
create_table :pk_autopopulated_by_a_trigger_records, force: true, id: false do |t|
85+
t.integer :id, null: false
86+
end
8887

89-
ActiveRecord::Base.lease_connection.execute(
90-
<<-SQL
88+
execute <<~SQL
9189
CREATE TRIGGER before_insert_trigger
9290
BEFORE INSERT ON pk_autopopulated_by_a_trigger_records
9391
FOR EACH ROW
9492
SET NEW.id = (SELECT COALESCE(MAX(id), 0) + 1 FROM pk_autopopulated_by_a_trigger_records);
9593
SQL
96-
)
94+
end
9795
end

0 commit comments

Comments
 (0)