From a2a66300d99ed53d63a7f065ffbed53d2ce074e3 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 11 Apr 2023 11:25:29 +0200 Subject: [PATCH] Clean up the post deployment migration generator (#24233) --- .../post_deployment_migration/USAGE | 10 +++++++ .../post_deployment_migration_generator.rb | 17 ++++++++++++ .../templates/migration.erb | 8 ++++++ .../post_deployment_migration_generator.rb | 17 ------------ .../post_deployment_migration/migration.rb | 8 ------ ...ost_deployment_migration_generator_spec.rb | 27 +++++++++++++++++++ 6 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 lib/generators/post_deployment_migration/USAGE create mode 100644 lib/generators/post_deployment_migration/post_deployment_migration_generator.rb create mode 100644 lib/generators/post_deployment_migration/templates/migration.erb delete mode 100644 lib/generators/post_deployment_migration_generator.rb delete mode 100644 lib/templates/rails/post_deployment_migration/migration.rb create mode 100644 spec/generators/post_deployment_migration_generator_spec.rb diff --git a/lib/generators/post_deployment_migration/USAGE b/lib/generators/post_deployment_migration/USAGE new file mode 100644 index 000000000..57f3c6aa1 --- /dev/null +++ b/lib/generators/post_deployment_migration/USAGE @@ -0,0 +1,10 @@ +Description: + Generate a Rails migration in the db/post_migrate/ dir. + + Interacts with the post_deployment_migrations initializer. + +Example: + bin/rails generate post_deployment_migration IsolateChanges + + Creates a migration in db/post_migrate/_isolate_changes.rb + which will have `disable_ddl_transaction!` and a `change` method included. diff --git a/lib/generators/post_deployment_migration/post_deployment_migration_generator.rb b/lib/generators/post_deployment_migration/post_deployment_migration_generator.rb new file mode 100644 index 000000000..adb34a289 --- /dev/null +++ b/lib/generators/post_deployment_migration/post_deployment_migration_generator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +require 'rails/generators/active_record' + +class PostDeploymentMigrationGenerator < Rails::Generators::NamedBase + source_root File.expand_path('templates', __dir__) + + include Rails::Generators::Migration + + def create_post_deployment_migration + migration_template 'migration.erb', "db/post_migrate/#{file_name}.rb" + end + + def self.next_migration_number(path) + ActiveRecord::Generators::Base.next_migration_number(path) + end +end diff --git a/lib/generators/post_deployment_migration/templates/migration.erb b/lib/generators/post_deployment_migration/templates/migration.erb new file mode 100644 index 000000000..fd6a02a0f --- /dev/null +++ b/lib/generators/post_deployment_migration/templates/migration.erb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>] + disable_ddl_transaction! + + def change + end +end diff --git a/lib/generators/post_deployment_migration_generator.rb b/lib/generators/post_deployment_migration_generator.rb deleted file mode 100644 index 798c01b88..000000000 --- a/lib/generators/post_deployment_migration_generator.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -require 'rails/generators' - -module Rails - class PostDeploymentMigrationGenerator < Rails::Generators::NamedBase - def create_migration_file - timestamp = Time.zone.now.strftime('%Y%m%d%H%M%S') - - template 'migration.rb', "db/post_migrate/#{timestamp}_#{file_name}.rb" - end - - def migration_class_name - file_name.camelize - end - end -end diff --git a/lib/templates/rails/post_deployment_migration/migration.rb b/lib/templates/rails/post_deployment_migration/migration.rb deleted file mode 100644 index 503205b84..000000000 --- a/lib/templates/rails/post_deployment_migration/migration.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -class <%= migration_class_name %> < ActiveRecord::Migration[5.2] - disable_ddl_transaction! - - def change - end -end diff --git a/spec/generators/post_deployment_migration_generator_spec.rb b/spec/generators/post_deployment_migration_generator_spec.rb new file mode 100644 index 000000000..d552880e3 --- /dev/null +++ b/spec/generators/post_deployment_migration_generator_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'rails/generators/testing/behaviour' +require 'rails/generators/testing/assertions' + +require 'generators/post_deployment_migration/post_deployment_migration_generator' + +describe PostDeploymentMigrationGenerator, type: :generator do + include Rails::Generators::Testing::Behaviour + include Rails::Generators::Testing::Assertions + include FileUtils + + tests described_class + destination File.expand_path('../../tmp', __dir__) + before { prepare_destination } + after { rm_rf(destination_root) } + + describe 'the migration' do + it 'generates expected file' do + run_generator %w(Changes) + + assert_migration('db/post_migrate/changes.rb', /disable_ddl/) + assert_migration('db/post_migrate/changes.rb', /change/) + end + end +end