Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Rails 7.2 in test suite #252

Merged
merged 10 commits into from
Dec 9, 2024
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
18 changes: 14 additions & 4 deletions .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
ruby: ["3.1"]
postgres: ["12"]
gemfile: [
"gemfiles/rails7.gemfile"
"gemfiles/rails72.gemfile"
]
fx: ["false"]
after_trigger: ["false"]
Expand All @@ -33,17 +33,27 @@ jobs:
after_trigger: "false"
- ruby: "3.2"
postgres: "15"
gemfile: "gemfiles/rails7.gemfile"
gemfile: "gemfiles/rails72.gemfile"
fx: "false"
after_trigger: "false"
- ruby: "3.2"
postgres: "15"
gemfile: "gemfiles/rails71.gemfile"
fx: "false"
after_trigger: "false"
- ruby: "3.2"
postgres: "15"
gemfile: "gemfiles/rails70.gemfile"
fx: "false"
after_trigger: "false"
- ruby: "3.1"
postgres: "12"
gemfile: "gemfiles/rails7.gemfile"
gemfile: "gemfiles/rails72.gemfile"
fx: "false"
after_trigger: "true"
- ruby: "3.0"
postgres: "13"
gemfile: "gemfiles/rails7.gemfile"
gemfile: "gemfiles/rails72.gemfile"
fx: "false"
after_trigger: "false"
- ruby: "2.7"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

- Support Rails 7.2 ([@atomaka][])

## 1.3.1 (2024-10-23)

- Fix `rails destroy logidze:model SomeModel` not deleting the `fx` trigger file file. ([@tylerhunt][])
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions gemfiles/rails71.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

rails_version = '~> 7.1.0'
gem 'railties', rails_version
gem 'activerecord', rails_version

eval_gemfile './shared.gemfile'

gemspec path: '..'
9 changes: 9 additions & 0 deletions gemfiles/rails72.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

rails_version = '~> 7.2.0'
gem 'railties', rails_version
gem 'activerecord', rails_version

eval_gemfile './shared.gemfile'

gemspec path: '..'
22 changes: 11 additions & 11 deletions spec/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
it "creates migration", :aggregate_failures do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "ActiveRecord::Migration[#{ar_version}]"
is_expected.to contain(/create or replace function logidze_logger()/i)
is_expected.to contain(/create or replace function logidze_logger_after()/i)
Expand All @@ -40,7 +40,7 @@
it "creates migration", :aggregate_failures do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "ActiveRecord::Migration[#{ar_version}]"
is_expected.to contain("create_function :logidze_logger, version: 4")
is_expected.to contain("create_function :logidze_logger_after, version: 4")
Expand All @@ -62,7 +62,7 @@
it "creates function files" do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
%w[
logidze_logger_v04.sql
logidze_logger_after_v04.sql
Expand All @@ -72,7 +72,7 @@
logidze_compact_history_v01.sql
logidze_capture_exception_v01.sql
].each do |path|
expect(file("db/functions/#{path}")).to exist
expect(file("db/functions/#{path}")).to be_a_file
end
end
end
Expand All @@ -84,7 +84,7 @@
it "creates migration", :aggregate_failures do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "ActiveRecord::Migration[#{ar_version}]"
is_expected.to contain(/enable_extension :hstore/i)
end
Expand All @@ -99,10 +99,10 @@
it "creates only functions", :aggregate_failures do
run_generator(args)

expect(migration_file("db/migrate/enable_hstore.rb")).not_to exist
expect(migration_file("db/migrate/logidze_install.rb")).not_to exist
expect(migration_file("db/migrate/enable_hstore.rb")).not_to be_a_file
expect(migration_file("db/migrate/logidze_install.rb")).not_to be_a_file

is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(/create or replace function logidze_logger()/i)
is_expected.to contain(/create or replace function logidze_logger_after()/i)
is_expected.to contain(/create or replace function logidze_snapshot/i)
Expand Down Expand Up @@ -144,7 +144,7 @@
it "creates migration", :aggregate_failures do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
is_expected.to contain("update_function :logidze_version, version: 2, revert_to_version: 3")
is_expected.to contain("update_function :logidze_snapshot, version: 3, revert_to_version: 4")
is_expected.not_to contain("update_function :logidze_filter_keys")
Expand All @@ -157,7 +157,7 @@
it "creates function files" do
run_generator(args)

is_expected.to exist
is_expected.to be_a_file
%w[
logidze_logger_v04.sql
logidze_logger_after_v04.sql
Expand All @@ -166,7 +166,7 @@
logidze_compact_history_v01.sql
logidze_capture_exception_v01.sql
].each do |path|
expect(file("db/functions/#{path}")).to exist
expect(file("db/functions/#{path}")).to be_a_file
end
end
end
Expand Down
52 changes: 26 additions & 26 deletions spec/generators/model_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class User < ActiveRecord::Base
end

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "ActiveRecord::Migration[#{ar_version}]"
is_expected.to contain "add_column :users, :log_data, :jsonb"
is_expected.to contain(/create trigger "logidze_on_#{full_table_name("users")}"/i)
Expand All @@ -65,21 +65,21 @@ class User < ActiveRecord::Base
let(:fx_args) { use_fx_args }

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain("create_trigger :logidze_on_users, on: :users")
end

it "creates a trigger file" do
is_expected.to exist
expect(file("db/triggers/logidze_on_users_v01.sql")).to exist
is_expected.to be_a_file
expect(file("db/triggers/logidze_on_users_v01.sql")).to be_a_file
end
end

context "with limit" do
let(:base_args) { ["user", "--limit=5", "--no-after-trigger"] }

it "creates trigger with limit" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(/execute procedure logidze_logger\(5, 'updated_at'\);/i)
end
end
Expand All @@ -88,7 +88,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--debounce_time=5000", "--no-after-trigger"] }

it "creates trigger with debounce_time" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(/execute procedure logidze_logger\(null, 'updated_at', null, null, 5000\);/i)
end
end
Expand All @@ -97,7 +97,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--except", "age", "active", "--no-after-trigger"] }

it "creates trigger with columns exclusion" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(
/execute procedure logidze_logger\(null, 'updated_at', '\{age, active\}'\);/i
)
Expand All @@ -108,7 +108,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--only", "age", "active", "--no-after-trigger"] }

it "creates trigger with columns inclusion" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(
/execute procedure logidze_logger\(null, 'updated_at', '\{age, active\}', true\);/i
)
Expand All @@ -119,7 +119,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--backfill", "--no-after-trigger"] }

it "creates backfill query" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(/update "#{full_table_name("users")}" as t/i)
is_expected.to contain(/set log_data = logidze_snapshot\(to_jsonb\(t\), 'updated_at'\);/i)
end
Expand All @@ -129,7 +129,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--only-trigger", "--no-after-trigger"] }

it "creates migration with trigger" do
is_expected.to exist
is_expected.to be_a_file
is_expected.not_to contain "add_column :users, :log_data, :jsonb"
is_expected.to contain(/create trigger "logidze_on_#{full_table_name("users")}"/i)
is_expected.to contain(/before update or insert on "#{full_table_name("users")}" for each row/i)
Expand All @@ -148,7 +148,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--update", "--no-after-trigger"] }

it "creates migration with drop and create trigger" do
is_expected.to exist
is_expected.to be_a_file
is_expected.not_to contain "add_column :users, :log_data, :jsonb"
is_expected.to contain(/drop trigger if exists "logidze_on_#{full_table_name("users")}" on "#{full_table_name("users")}"/i)
is_expected.to contain(/before update or insert on "#{full_table_name("users")}" for each row/i)
Expand All @@ -169,13 +169,13 @@ class User < ActiveRecord::Base
end

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain("update_trigger :logidze_on_users, on: :users, version: 2, revert_to_version: 1")
end

it "creates a trigger file" do
is_expected.to exist
expect(file("db/triggers/logidze_on_users_v02.sql")).to exist
is_expected.to be_a_file
expect(file("db/triggers/logidze_on_users_v02.sql")).to be_a_file
end
end

Expand All @@ -189,7 +189,7 @@ class User < ActiveRecord::Base
end

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "add_column :users, :log_data, :jsonb"
end
end
Expand All @@ -200,7 +200,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--timestamp_column", "time", "--no-after-trigger"] }

it "creates trigger with 'time' timestamp column" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(
/execute procedure logidze_logger\(null, 'time'\);/i
)
Expand All @@ -211,7 +211,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--timestamp_column", "nil", "--no-after-trigger"] }

it "creates trigger without timestamp column" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(
/execute procedure logidze_logger\(\);/i
)
Expand All @@ -223,7 +223,7 @@ class User < ActiveRecord::Base
let(:base_args) { ["user", "--after-trigger"] }

it "use after trigger" do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain(/after update or insert on "#{full_table_name("users")}" for each row/i)
is_expected.to contain(/execute procedure logidze_logger_after\(null, 'updated_at'\);/i)
end
Expand All @@ -232,8 +232,8 @@ class User < ActiveRecord::Base
let(:fx_args) { use_fx_args }

it "generates after trigger" do
is_expected.to exist
expect(file("db/triggers/logidze_on_users_v01.sql")).to exist
is_expected.to be_a_file
expect(file("db/triggers/logidze_on_users_v01.sql")).to be_a_file
expect(file("db/triggers/logidze_on_users_v01.sql")).to contain(/after update or insert on/i)
end
end
Expand Down Expand Up @@ -261,7 +261,7 @@ class Guest < ActiveRecord::Base
end

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "add_column :user_guests, :log_data, :jsonb"

expect(file("app/models/user/guest.rb")).to contain "has_logidze"
Expand Down Expand Up @@ -289,7 +289,7 @@ class Set < ActiveRecord::Base
end

it "creates migration", :aggregate_failures do
is_expected.to exist
is_expected.to be_a_file
is_expected.to contain "add_column :data_sets, :log_data, :jsonb"

expect(file("app/models/custom/data/set.rb")).to contain "has_logidze"
Expand Down Expand Up @@ -319,13 +319,13 @@ class User < ActiveRecord::Base
it "deletes migration file it created" do
migration_file = migration_file("db/migrate/add_logidze_to_users.rb")

expect(migration_file).to exist
expect(migration_file).to be_a_file

Rails::Generators.invoke "logidze:model", args,
behavior: :revoke,
destination_root: destination_root

expect(migration_file).not_to exist
expect(migration_file).not_to be_a_file
end

context "with fx" do
Expand All @@ -334,13 +334,13 @@ class User < ActiveRecord::Base
it "deletes trigger file it created" do
trigger_file = file("db/triggers/logidze_on_users_v01.sql")

expect(trigger_file).to exist
expect(trigger_file).to be_a_file

Rails::Generators.invoke "logidze:model", args,
behavior: :revoke,
destination_root: destination_root

expect(trigger_file).not_to exist
expect(trigger_file).not_to be_a_file
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
config.append_after(:each, db: true) do |ex|
ActiveRecord::Base.connection.rollback_transaction

raise "Migrations are pending: #{ex.metadata[:location]}" if ActiveRecord::Base.connection.migration_context.needs_migration?
migration_context = if Rails::VERSION::MAJOR >= 7
ActiveRecord::MigrationContext.new("db/migrate")
Comment on lines +42 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ActiveRecord::MigrationContext was made a public API in rails/rails@eb09f59 released with Rails 7. Prior to that, it was a non-public API (with a different interface)

else
ActiveRecord::Base.connection.migration_context
end

raise "Migrations are pending: #{ex.metadata[:location]}" if migration_context.needs_migration?
end
end
5 changes: 4 additions & 1 deletion spec/sql/snapshot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

describe "logidze_snapshot" do
let(:now) { Time.zone.local(1989, 7, 10, 18, 23, 33) }
let(:now_to_s) do
now.respond_to?(:to_fs) ? now.to_fs(:db) : now.to_s(:db)
end

let(:data) { %('{"title": "Feel me", "rating": 42, "name": "Jack", "extra": {"gender": "X"}, "updated_at": "#{now.to_s(:db)}"}'::jsonb) }
let(:data) { %('{"title": "Feel me", "rating": 42, "name": "Jack", "extra": {"gender": "X"}, "updated_at": "#{now_to_s}"}'::jsonb) }

specify "without optional args" do
res = sql "select logidze_snapshot(#{data})"
Expand Down
7 changes: 7 additions & 0 deletions spec/support/matchers/be_a_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec::Matchers.define :be_a_file do |expected|
match do |file_path|
File.exist?(file_path)
end
end
Loading