Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 19, 2024
1 parent e7ca8a9 commit a567aee
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
6 changes: 2 additions & 4 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/controllers/actual_db_schema/migrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/dummy_app/config/database.yml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit a567aee

Please sign in to comment.