Skip to content

Commit

Permalink
Update migration template naming in upgrade generator
Browse files Browse the repository at this point in the history
  • Loading branch information
amkisko committed Oct 29, 2024
1 parent 1a16b59 commit 08e45f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/generators/audited/upgrade_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ class UpgradeGenerator < Rails::Generators::Base
source_root File.expand_path("../templates", __FILE__)

def copy_templates
migrations_to_be_applied do |m|
migration_template "#{m}.rb", "db/migrate/#{m}.rb"
migrations_to_be_applied do |template_name|
name = "db/migrate/#{template_name}.rb"
if options[:audited_table_name] != "audits"
name = name.gsub("_to_audits", "_to_#{options[:audited_table_name]}")
end
migration_template "#{template_name}.rb", name
end
end

Expand Down
20 changes: 20 additions & 0 deletions test/upgrade_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,24 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
assert_includes(content, "class AddCommentToAudits < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]\n")
end
end

test "generate migration with context column change" do
load_schema 6

run_generator %w[upgrade]

assert_migration "db/migrate/add_context_to_audits.rb" do |content|
assert_match(/add_column :audits, :context, :jsonb/, content)
end
end

test "generate migration with context column change for custom table name" do
load_schema 6

run_generator %w[upgrade --audited_table_name=custom_audits]

assert_migration "db/migrate/add_context_to_custom_audits.rb" do |content|
assert_match(/add_column :custom_audits, :context, :jsonb/, content)
end
end
end

0 comments on commit 08e45f5

Please sign in to comment.