-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrei Zhigalkin
committed
Nov 14, 2023
1 parent
51300a8
commit d097489
Showing
10 changed files
with
467 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
lib/generators/logidze/install/templates/sequel/hstore.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Sequel.migration do | ||
up do | ||
run "CREATE EXTENSION IF NOT EXISTS hstore" | ||
end | ||
|
||
down do | ||
run "DROP EXTENSION IF EXISTS hstore CASCADE" | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
lib/generators/logidze/install/templates/sequel/migration.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Sequel.migration do | ||
up do | ||
<%- if update? -%> | ||
# Drop legacy functions (<1.0) | ||
run <<~SQL | ||
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb); | ||
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb); | ||
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, text[]); | ||
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text[]); | ||
DROP FUNCTION IF EXISTS logidze_version(bigint, jsonb, timestamp with time zone, text[]); | ||
DROP FUNCTION IF EXISTS logidze_snapshot(jsonb, text, text[]); | ||
DROP FUNCTION IF EXISTS logidze_exclude_keys(jsonb, VARIADIC text[]); | ||
DROP FUNCTION IF EXISTS logidze_compact_history(jsonb); | ||
SQL | ||
|
||
<%- end -%> | ||
run <<~SQL | ||
<%- function_definitions.each do |f| -%> | ||
<%= inject_sql("#{f.name}.sql", indent: 6) %> | ||
<%- end -%> | ||
SQL | ||
end | ||
|
||
down do | ||
<%- unless update? -%> | ||
run <<~SQL | ||
<%- function_definitions.each do |f| -%> | ||
DROP FUNCTION IF EXISTS <%= f.name %>(<%= f.signature %>) CASCADE; | ||
<%- end -%> | ||
SQL | ||
<%- end -%> | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
lib/generators/logidze/model/templates/sequel/migration.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Sequel.migration do | ||
up do | ||
<%- unless update? || only_trigger? -%> | ||
add_column :<%= table_name %>, :log_data, :jsonb | ||
<%- end -%> | ||
<%- if update? -%> | ||
run <<~SQL | ||
DROP TRIGGER IF EXISTS "logidze_on_<%= table_name %>" on "<%= table_name %>"; | ||
SQL | ||
<%- end -%> | ||
run <<~SQL | ||
<%= inject_sql("logidze.sql", indent: 6) %> | ||
SQL | ||
<%- if backfill? -%> | ||
run <<~SQL | ||
UPDATE "<%= table_name %>" as t | ||
SET log_data = logidze_snapshot(<%= logidze_snapshot_parameters %>); | ||
SQL | ||
<%- end -%> | ||
end | ||
|
||
down do | ||
<%- if update? -%> | ||
# NOTE: We have no idea on how to revert the migration | ||
# ('cause we don't know the previous trigger params), | ||
# but you can do that on your own. | ||
# | ||
# Uncomment this line if you want to raise an error. | ||
# raise Sequel::Error | ||
<%- else -%> | ||
run <<~SQL | ||
DROP TRIGGER IF EXISTS "logidze_on_<%= table_name %>" on "<%= table_name %>"; | ||
SQL | ||
<%- end -%> | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Logidze | ||
module Generators | ||
# Adds --sequel option and provides #sequel? method | ||
module SequelHelper | ||
def self.included(base) | ||
base.class_option :sequel, type: :boolean, optional: true, | ||
desc: "Define whether to generate Sequel install migrations" | ||
end | ||
|
||
def sequel? | ||
options.fetch(:sequel, false) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require "generators/logidze/install/install_generator" | ||
|
||
describe Logidze::Generators::InstallGenerator, type: :generator, sequel: true do | ||
destination File.expand_path("../../../tmp", __dir__) | ||
|
||
let(:base_args) { [] } | ||
let(:sequel_args) { ["--sequel"] } | ||
let(:args) { base_args + sequel_args } | ||
|
||
before do | ||
prepare_destination | ||
end | ||
|
||
describe "hstore migration" do | ||
subject { migration_file("db/migrate/enable_hstore.rb") } | ||
|
||
it "creates migration", :aggregate_failures do | ||
run_generator(args) | ||
|
||
is_expected.to exist | ||
is_expected.to contain "Sequel.migration" | ||
is_expected.to contain "up do" | ||
is_expected.to contain "CREATE EXTENSION IF NOT EXISTS hstore" | ||
is_expected.to contain "down do" | ||
is_expected.to contain "DROP EXTENSION IF EXISTS hstore CASCADE" | ||
end | ||
end | ||
|
||
describe "trigger migration" do | ||
subject { migration_file("db/migrate/logidze_install.rb") } | ||
|
||
it "creates migration", :aggregate_failures do | ||
run_generator(args) | ||
|
||
is_expected.to exist | ||
is_expected.to contain "Sequel.migration" | ||
is_expected.to contain "up do" | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_logger()/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_snapshot/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_version/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_filter_keys/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_compact_history/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_capture_exception/i) | ||
is_expected.to contain "down do" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_logger" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_snapshot" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_version" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_filter_keys" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_compact_history" | ||
is_expected.to contain "DROP FUNCTION IF EXISTS logidze_capture_exception" | ||
end | ||
end | ||
|
||
context "update migration" do | ||
let(:version) { Logidze::VERSION.delete(".") } | ||
let(:base_args) { ["--update"] } | ||
|
||
subject { migration_file("db/migrate/logidze_update_#{version}.rb") } | ||
|
||
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 | ||
|
||
is_expected.to exist | ||
is_expected.to contain "Sequel.migration" | ||
is_expected.to contain "up do" | ||
is_expected.to contain(/Drop legacy functions/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_logger()/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_snapshot/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_version/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_filter_keys/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_compact_history/i) | ||
is_expected.to contain(/CREATE OR REPLACE FUNCTION logidze_capture_exception/i) | ||
is_expected.to contain "down do" | ||
end | ||
end | ||
end |
Oops, something went wrong.