From a567aeefe99fad1d87f2a138640d7759dc3bf055 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov Date: Fri, 19 Jul 2024 16:39:40 +0300 Subject: [PATCH] fix tests --- .../actual_db_schema/migrations_controller.rb | 6 ++--- .../phantom_migrations_controller.rb | 6 ++--- .../migrations_controller_test.rb | 22 +++++++++++++++++++ .../phantom_migrations_controller_test.rb | 22 +++++++++++++++++++ test/dummy_app/config/database.yml | 10 +++++++++ 5 files changed, 58 insertions(+), 8 deletions(-) diff --git a/app/controllers/actual_db_schema/migrations_controller.rb b/app/controllers/actual_db_schema/migrations_controller.rb index d223419..573855e 100644 --- a/app/controllers/actual_db_schema/migrations_controller.rb +++ b/app/controllers/actual_db_schema/migrations_controller.rb @@ -24,17 +24,15 @@ def migrate def handle_rollback(id, database) ActualDbSchema::Migration.instance.rollback(id, database) flash[:notice] = "Migration #{id} was successfully rolled back." - rescue ActiveRecord::IrreversibleMigration - flash[:alert] = "Migration #{id} cannot be rolled back because it is irreversible." rescue StandardError => e - flash[:alert] = "An error occurred while trying to roll back the migration #{id}: #{e.message}" + flash[:alert] = e.message end def handle_migrate(id, database) ActualDbSchema::Migration.instance.migrate(id, database) flash[:notice] = "Migration #{id} was successfully migrated." rescue StandardError => e - flash[:alert] = "An error occurred while migrating #{id}: #{e.message}" + flash[:alert] = e.message end helper_method def migrations diff --git a/app/controllers/actual_db_schema/phantom_migrations_controller.rb b/app/controllers/actual_db_schema/phantom_migrations_controller.rb index d7024b5..95469c8 100644 --- a/app/controllers/actual_db_schema/phantom_migrations_controller.rb +++ b/app/controllers/actual_db_schema/phantom_migrations_controller.rb @@ -24,17 +24,15 @@ def rollback_all def handle_rollback(id, database) ActualDbSchema::Migration.instance.rollback(id, database) flash[:notice] = "Migration #{id} was successfully rolled back." - rescue ActiveRecord::IrreversibleMigration - flash[:alert] = "Migration #{id} cannot be rolled back because it is irreversible." rescue StandardError => e - flash[:alert] = "An error occurred while trying to roll back the migration #{id}: #{e.message}" + flash[:alert] = e.message end def handle_rollback_all ActualDbSchema::Migration.instance.rollback_all flash[:notice] = "Migrations was successfully rolled back." rescue StandardError => e - flash[:alert] = "An error occurred while trying to roll back migrations: #{e.message}" + flash[:alert] = e.message end helper_method def phantom_migrations diff --git a/test/controllers/actual_db_schema/migrations_controller_test.rb b/test/controllers/actual_db_schema/migrations_controller_test.rb index e2afd87..2b4288c 100644 --- a/test/controllers/actual_db_schema/migrations_controller_test.rb +++ b/test/controllers/actual_db_schema/migrations_controller_test.rb @@ -100,6 +100,28 @@ def active_record_setup assert_response :not_found end + test "POST #rollback with irreversible migration returns error message" do + %w[primary secondary].each do |prefix| + @utils.define_migration_file("20130906111513_irreversible_#{prefix}.rb", <<~RUBY, prefix: prefix) + class Irreversible#{prefix.camelize} < ActiveRecord::Migration[6.0] + def up + TestingState.up << :irreversible_#{prefix} + end + + def down + raise ActiveRecord::IrreversibleMigration + end + end + RUBY + end + @utils.prepare_phantom_migrations(TestingState.db_config) + post :rollback, params: { id: "20130906111513", database: "tmp/primary.sqlite3" } + assert_response :redirect + get :index + message = "An error has occurred, this and all later migrations canceled:\n\nActiveRecord::IrreversibleMigration" + assert_select ".flash", text: message + end + test "POST #rollback changes migration status to down and hide migration with down status" do post :rollback, params: { id: "20130906111511", database: "tmp/primary.sqlite3" } assert_response :redirect diff --git a/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb b/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb index fda24bc..7f9274d 100644 --- a/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb +++ b/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb @@ -136,6 +136,28 @@ def active_record_setup assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back." end + test "POST #rollback with irreversible migration returns error message" do + %w[primary secondary].each do |prefix| + @utils.define_migration_file("20130906111513_irreversible_#{prefix}.rb", <<~RUBY, prefix: prefix) + class Irreversible#{prefix.camelize} < ActiveRecord::Migration[6.0] + def up + TestingState.up << :irreversible_#{prefix} + end + + def down + raise ActiveRecord::IrreversibleMigration + end + end + RUBY + end + @utils.prepare_phantom_migrations(TestingState.db_config) + post :rollback, params: { id: "20130906111513", database: "tmp/primary.sqlite3" } + assert_response :redirect + get :index + message = "An error has occurred, this and all later migrations canceled:\n\nActiveRecord::IrreversibleMigration" + assert_select ".flash", text: message + end + test "POST #rollback_all changes all phantom migrations status to down and hide migration with down status" do post :rollback_all assert_response :redirect diff --git a/test/dummy_app/config/database.yml b/test/dummy_app/config/database.yml index e69de29..e804c7c 100644 --- a/test/dummy_app/config/database.yml +++ b/test/dummy_app/config/database.yml @@ -0,0 +1,10 @@ +--- +test: + primary: + adapter: sqlite3 + database: tmp/primary.sqlite3 + migrations_paths: "/home/vlad/projects/actual_db_schema/test/dummy_app/db/migrate" + secondary: + adapter: sqlite3 + database: tmp/secondary.sqlite3 + migrations_paths: "/home/vlad/projects/actual_db_schema/test/dummy_app/db/migrate_secondary"