Skip to content

Commit

Permalink
Merge pull request rails#52781 from BuonOmo/main
Browse files Browse the repository at this point in the history
Separate specific db schema
  • Loading branch information
guilleiguaran authored Sep 3, 2024
2 parents 5f71da1 + 4b968de commit d5c2ff8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
15 changes: 15 additions & 0 deletions activerecord/test/schema/mysql2_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 26 additions & 0 deletions activerecord/test/schema/postgresql_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 0 additions & 37 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/schema/trilogy_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d5c2ff8

Please sign in to comment.