Skip to content

Commit

Permalink
Add Sequel generators back
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Zhigalkin committed Nov 14, 2023
1 parent 51300a8 commit d097489
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/generators/logidze/install/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ Examples:

This will generate schema.rb compatible migration with `create_function` definitions and separate SQL files.
The fx gem must be installed.

rails generate logidze:install --sequel

This will generate the Sequel supported core migration file with trigger function defined.
10 changes: 8 additions & 2 deletions lib/generators/logidze/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
require "logidze/utils/function_definitions"
require_relative "../inject_sql"
require_relative "../fx_helper"
require_relative "../sequel_helper"

module Logidze
module Generators
class InstallGenerator < ::Rails::Generators::Base # :nodoc:
include Rails::Generators::Migration
include InjectSql
include FxHelper
include SequelHelper

source_root File.expand_path("templates", __dir__)
source_paths << File.expand_path("functions", __dir__)
Expand All @@ -20,14 +22,14 @@ class InstallGenerator < ::Rails::Generators::Base # :nodoc:
desc: "Define whether this is an update migration"

def generate_migration
migration_template = fx? ? "migration_fx.rb.erb" : "migration.rb.erb"
migration_template = fx? ? "migration_fx.rb.erb" : with_adapter("migration.rb.erb")
migration_template migration_template, "db/migrate/#{migration_name}.rb"
end

def generate_hstore_migration
return if update?

migration_template "hstore.rb.erb", "db/migrate/enable_hstore.rb"
migration_template with_adapter("hstore.rb.erb"), "db/migrate/enable_hstore.rb"
end

def generate_fx_functions
Expand All @@ -41,6 +43,10 @@ def generate_fx_functions
end

no_tasks do
def with_adapter(migration_name)
[("sequel" if sequel?), migration_name].compact.join("/")
end

def migration_name
if update?
"logidze_update_#{Logidze::VERSION.delete(".")}"
Expand Down
9 changes: 9 additions & 0 deletions lib/generators/logidze/install/templates/sequel/hstore.rb.erb
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 lib/generators/logidze/install/templates/sequel/migration.rb.erb
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
9 changes: 9 additions & 0 deletions lib/generators/logidze/model/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ Examples:
rails generate logidze:model User --update --whitelist=name,age

This will generate the migration to update existing trigger (drop and create).

rails generate logidze:model User --sequel

This will generate the Sequel supported migration to add log column and trigger.
This will also add `plugin :logidze` to model.

rails generate logidze:model User --sequel --update --whitelist=name,age

This will generate the Sequel supported migration to update existing trigger (drop and create).
14 changes: 12 additions & 2 deletions lib/generators/logidze/model/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
require "rails/generators/active_record/migration/migration_generator"
require_relative "../inject_sql"
require_relative "../fx_helper"
require_relative "../sequel_helper"

module Logidze
module Generators
class ModelGenerator < ::ActiveRecord::Generators::Base # :nodoc:
include InjectSql
include FxHelper
include SequelHelper

source_root File.expand_path("templates", __dir__)
source_paths << File.expand_path("triggers", __dir__)
Expand Down Expand Up @@ -45,7 +47,7 @@ def generate_migration
warn "Use only one: --only or --except"
exit(1)
end
migration_template "migration.rb.erb", "db/migrate/#{migration_name}.rb"
migration_template with_adapter("migration.rb.erb"), "db/migrate/#{migration_name}.rb"
end

def generate_fx_trigger
Expand All @@ -61,10 +63,16 @@ def inject_logidze_to_model

indents = " " * (class_name.scan("::").count + 1)

inject_into_class(model_file_path, class_name.demodulize, "#{indents}has_logidze\n")
include_logidze = sequel? ? "plugin :logidze" : "has_logidze"

inject_into_class(model_file_path, class_name.demodulize, "#{indents}#{include_logidze}\n")
end

no_tasks do
def with_adapter(migration_name)
[("sequel" if sequel?), migration_name].compact.join("/")
end

def migration_name
return options[:name] if options[:name].present?

Expand All @@ -76,6 +84,8 @@ def migration_name
end

def full_table_name
return table_name if sequel?

config = ActiveRecord::Base
"#{config.table_name_prefix}#{table_name}#{config.table_name_suffix}"
end
Expand Down
36 changes: 36 additions & 0 deletions lib/generators/logidze/model/templates/sequel/migration.rb.erb
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
17 changes: 17 additions & 0 deletions lib/generators/logidze/sequel_helper.rb
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
81 changes: 81 additions & 0 deletions spec/sequel/generators/install_generator_spec.rb
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
Loading

0 comments on commit d097489

Please sign in to comment.