Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: false
matrix:
ruby: ["3.2", "3.3", "3.4"]
rails: ["7.1", "7.2", "8.0", "8.1.0"]
rails: ["7.2", "8.0", "8.1"]
continue-on-error: [false]

services:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ changelog, see the [commits] for each version via the version links.
- Require Ruby >= 3.1 (#162)
- Drop EOL Ruby versions (3.0) (#162)
- Drop EOL Rails versions (7.0) (#162)
- Add Rails 8.1 to the test matrix (#175)
- Ensure multi-schema dumping in Rails 8.1.0 does not dump the same objects for
all schemas (#177)
- Drop Rails 7.1 due to EOL (#176)

## [0.9.0]

Expand Down
4 changes: 2 additions & 2 deletions lib/fx/adapters/postgres/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Postgres
class Functions
# The SQL query used by F(x) to retrieve the functions considered
# dumpable into `db/schema.rb`.
FUNCTIONS_WITH_DEFINITIONS_QUERY = <<~EOS.freeze
FUNCTIONS_WITH_DEFINITIONS_QUERY = <<~SQL.freeze
SELECT
pp.proname AS name,
pg_get_functiondef(pp.oid) AS definition
Expand All @@ -23,7 +23,7 @@ class Functions
AND pd.objid IS NULL
AND pa.aggfnoid IS NULL
ORDER BY pp.oid;
EOS
SQL

# Wraps #all as a static facade.
#
Expand Down
4 changes: 2 additions & 2 deletions lib/fx/adapters/postgres/triggers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Postgres
class Triggers
# The SQL query used by F(x) to retrieve the triggers considered
# dumpable into `db/schema.rb`.
TRIGGERS_WITH_DEFINITIONS_QUERY = <<~EOS.freeze
TRIGGERS_WITH_DEFINITIONS_QUERY = <<~SQL.freeze
SELECT
pt.tgname AS name,
pg_get_triggerdef(pt.oid) AS definition
Expand All @@ -23,7 +23,7 @@ class Triggers
AND pt.tgname NOT ILIKE '%constraint%'
AND pt.tgname NOT ILIKE 'pg%'
ORDER BY pc.oid;
EOS
SQL

# Wraps #all as a static facade.
#
Expand Down
12 changes: 6 additions & 6 deletions spec/acceptance/user_manages_functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
RSpec.describe "User manages functions" do
it "handles simple functions" do
successfully "rails generate fx:function test"
write_function_definition "test_v01", <<~EOS
write_function_definition "test_v01", <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
successfully "rake db:migrate"

result = execute("SELECT * FROM test() AS result")
Expand All @@ -21,14 +21,14 @@
"db/functions/test_v01.sql",
"db/functions/test_v02.sql"
)
write_function_definition "test_v02", <<~EOS
write_function_definition "test_v02", <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'testest';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
successfully "rake db:migrate"

result = execute("SELECT * FROM test() AS result")
Expand All @@ -37,14 +37,14 @@

it "handles functions with arguments" do
successfully "rails generate fx:function adder"
write_function_definition "adder_v01", <<~EOS
write_function_definition "adder_v01", <<~SQL
CREATE FUNCTION adder(x int, y int)
RETURNS int AS $$
BEGIN
RETURN $1 + $2;
END;
$$ LANGUAGE plpgsql;
EOS
SQL
successfully "rake db:migrate"

result = execute("SELECT * FROM adder(1, 2) AS result")
Expand Down
20 changes: 10 additions & 10 deletions spec/acceptance/user_manages_triggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,46 @@
it "handles simple triggers" do
successfully "rails generate model user name:string upper_name:string"
successfully "rails generate fx:function uppercase_users_name"
write_function_definition "uppercase_users_name_v01", <<~EOS
write_function_definition "uppercase_users_name_v01", <<~SQL
CREATE OR REPLACE FUNCTION uppercase_users_name()
RETURNS trigger AS $$
BEGIN
NEW.upper_name = UPPER(NEW.name);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
EOS
SQL
successfully "rails generate fx:trigger uppercase_users_name table_name:users"
write_trigger_definition "uppercase_users_name_v01", <<~EOS
write_trigger_definition "uppercase_users_name_v01", <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL
successfully "rake db:migrate"

execute <<~EOS
execute <<~SQL
INSERT INTO users
(name, created_at, updated_at)
VALUES
('Bob', NOW(), NOW());
EOS
SQL
result = execute("SELECT upper_name FROM users WHERE name = 'Bob';")
expect(result).to eq("upper_name" => "BOB")

successfully "rails generate fx:trigger uppercase_users_name table_name:users"
write_trigger_definition "uppercase_users_name_v02", <<~EOS
write_trigger_definition "uppercase_users_name_v02", <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL
successfully "rake db:migrate"
execute <<~EOS
execute <<~SQL
UPDATE users
SET name = 'Alice'
WHERE id = 1;
EOS
SQL

result = execute("SELECT upper_name FROM users WHERE name = 'Alice';")
expect(result).to eq("upper_name" => "ALICE")
Expand Down
8 changes: 4 additions & 4 deletions spec/features/functions/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

RSpec.describe "Function migrations", :db do
around do |example|
sql_definition = <<~EOS
sql_definition = <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
with_function_definition(name: :test, sql_definition: sql_definition) do
example.run
end
Expand Down Expand Up @@ -40,14 +40,14 @@ def up
it "can run migrations that updates functions" do
connection.create_function(:test)

sql_definition = <<~EOS
sql_definition = <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'testest';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
with_function_definition(
name: :test,
version: 2,
Expand Down
8 changes: 4 additions & 4 deletions spec/features/functions/revert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

RSpec.describe "Reverting migrations", :db do
around do |example|
sql_definition = <<~EOS
sql_definition = <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
with_function_definition(name: :test, sql_definition: sql_definition) do
example.run
end
Expand Down Expand Up @@ -50,14 +50,14 @@ def change
it "can run reversible migrations for updating functions" do
connection.create_function(:test)

sql_definition = <<~EOS
sql_definition = <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'bar';
END;
$$ LANGUAGE plpgsql;
EOS
SQL
with_function_definition(
name: :test,
version: 2,
Expand Down
12 changes: 6 additions & 6 deletions spec/features/triggers/migrations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

RSpec.describe "Trigger migrations", :db do
around do |example|
connection.execute <<~EOS
connection.execute <<~SQL
CREATE TABLE users (
id int PRIMARY KEY,
name varchar(256),
upper_name varchar(256)
);
EOS
Fx.database.create_function <<~EOS
SQL
Fx.database.create_function <<~SQL
CREATE OR REPLACE FUNCTION uppercase_users_name()
RETURNS trigger AS $$
BEGIN
NEW.upper_name = UPPER(NEW.name);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
EOS
sql_definition = <<~EOS
SQL
sql_definition = <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL
with_trigger_definition(
name: :uppercase_users_name,
sql_definition: sql_definition
Expand Down
16 changes: 8 additions & 8 deletions spec/features/triggers/revert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

RSpec.describe "Reverting migrations", :db do
around do |example|
connection.execute <<~EOS
connection.execute <<~SQL
CREATE TABLE users (
id int PRIMARY KEY,
name varchar(256),
upper_name varchar(256)
);
EOS
Fx.database.create_function <<~EOS
SQL
Fx.database.create_function <<~SQL
CREATE OR REPLACE FUNCTION uppercase_users_name()
RETURNS trigger AS $$
BEGIN
NEW.upper_name = UPPER(NEW.name);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
EOS
sql_definition = <<~EOS
SQL
sql_definition = <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL
with_trigger_definition(
name: :uppercase_users_name,
sql_definition: sql_definition
Expand Down Expand Up @@ -67,12 +67,12 @@ def change
it "can run reversible migrations for updating triggers" do
connection.create_trigger(:uppercase_users_name)

sql_definition = <<~EOS
sql_definition = <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL
with_trigger_definition(
name: :uppercase_users_name,
sql_definition: sql_definition,
Expand Down
8 changes: 4 additions & 4 deletions spec/fx/adapters/postgres/functions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
describe ".all" do
it "returns `Function` objects" do
connection = ActiveRecord::Base.connection
connection.execute <<~EOS
connection.execute <<~SQL
CREATE OR REPLACE FUNCTION test()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
SQL

functions = Fx::Adapters::Postgres::Functions.new(connection).all

first = functions.first
expect(functions.size).to eq(1)
expect(first.name).to eq("test")
expect(first.definition).to eq(<<~EOS)
expect(first.definition).to eq(<<~SQL)
CREATE OR REPLACE FUNCTION public.test()
RETURNS text
LANGUAGE plpgsql
Expand All @@ -27,7 +27,7 @@
RETURN 'test';
END;
$function$
EOS
SQL

connection.execute "CREATE SCHEMA IF NOT EXISTS other;"
connection.execute "SET search_path = 'other';"
Expand Down
12 changes: 6 additions & 6 deletions spec/fx/adapters/postgres/triggers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
describe ".all" do
it "returns `Trigger` objects" do
connection = ActiveRecord::Base.connection
connection.execute <<~EOS
connection.execute <<~SQL
CREATE TABLE users (
id int PRIMARY KEY,
name varchar(256),
upper_name varchar(256)
);
EOS
connection.execute <<~EOS
SQL
connection.execute <<~SQL
CREATE OR REPLACE FUNCTION uppercase_users_name()
RETURNS trigger AS $$
BEGIN
NEW.upper_name = UPPER(NEW.name);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
EOS
connection.execute <<~EOS
SQL
connection.execute <<~SQL
CREATE TRIGGER uppercase_users_name
BEFORE INSERT ON users
FOR EACH ROW
EXECUTE FUNCTION uppercase_users_name();
EOS
SQL

triggers = Fx::Adapters::Postgres::Triggers.new(connection).all

Expand Down
Loading