Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def migrate
redirect_to migrations_path
end

def delete
handle_delete(params[:id], params[:database])
redirect_to migrations_path
end

private

def handle_rollback(id, database)
Expand All @@ -38,6 +43,13 @@ def handle_migrate(id, database)
flash[:alert] = e.message
end

def handle_delete(id, database)
ActualDbSchema::Migration.instance.delete(id, database)
flash[:notice] = "Migration #{id} was successfully deleted."
rescue StandardError => e
flash[:alert] = e.message
end

helper_method def migrations
@migrations ||= ActualDbSchema::Migration.instance.all
query = params[:query].to_s.strip.downcase
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/actual_db_schema/phantom_migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def rollback_all
redirect_to phantom_migrations_path
end

def delete
handle_delete(params[:id], params[:database])
redirect_to phantom_migrations_path
end

private

def handle_rollback(id, database)
Expand All @@ -38,6 +43,13 @@ def handle_rollback_all
flash[:alert] = e.message
end

def handle_delete(id, database)
ActualDbSchema::Migration.instance.delete(id, database)
flash[:notice] = "Migration #{id} was successfully deleted."
rescue StandardError => e
flash[:alert] = e.message
end

helper_method def phantom_migrations
@phantom_migrations ||= ActualDbSchema::Migration.instance.all_phantom
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/actual_db_schema/migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
method: :post,
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
<%= button_to '✖ Delete',
delete_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
data: { confirm: 'Are you sure you want to delete this migration?' },
class: 'button migration-action' %>
</div>
</td>
</tr>
Expand Down
5 changes: 5 additions & 0 deletions app/views/actual_db_schema/migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
method: :post,
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
<%= button_to '✖ Delete',
delete_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
data: { confirm: 'Are you sure you want to delete this migration?' },
class: 'button migration-action' %>
</div>
</div>
</body>
Expand Down
5 changes: 5 additions & 0 deletions app/views/actual_db_schema/phantom_migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
rollback_phantom_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button migration-action' %>
<%= button_to '✖ Delete',
delete_phantom_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
data: { confirm: 'Are you sure you want to delete this migration?' },
class: 'button migration-action' %>
</div>
</td>
</tr>
Expand Down
5 changes: 5 additions & 0 deletions app/views/actual_db_schema/phantom_migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
rollback_phantom_migration_path(id: params[:id], database: params[:database]),
method: :post,
class: 'button migration-action' %>
<%= button_to '✖ Delete',
delete_phantom_migration_path(id: params[:id], database: params[:database]),
method: :post,
data: { confirm: 'Are you sure you want to delete this migration?' },
class: 'button migration-action' %>
</div>
</div>
</body>
Expand Down
6 changes: 6 additions & 0 deletions app/views/actual_db_schema/shared/_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

migrationActions.forEach(button => {
button.addEventListener('click', function(event) {
const confirmMessage = button.dataset.confirm;
if (confirmMessage && !confirm(confirmMessage)) {
event.preventDefault();
return;
}

const originalText = button.value;
button.value = 'Loading...';
disableButtons();
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
member do
post :rollback
post :migrate
post :delete
end
end
resources :phantom_migrations, only: %i[index show] do
member do
post :rollback
post :delete
end
collection do
post :rollback_all
Expand Down
9 changes: 9 additions & 0 deletions lib/actual_db_schema/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ def migrate(version, database)
end
end

def delete(version, database)
MigrationContext.instance.each do
next unless ActualDbSchema.db_config[:database] == database

ActiveRecord::Base.connection.execute("DELETE FROM schema_migrations WHERE version = '#{version}'")
break
end
end

private

def build_migration_struct(status, migration)
Expand Down
17 changes: 17 additions & 0 deletions test/controllers/actual_db_schema/migrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def routes_setup
get "/rails/migration/:id" => "actual_db_schema/migrations#show", as: "migration"
post "/rails/migration/:id/rollback" => "actual_db_schema/migrations#rollback", as: "rollback_migration"
post "/rails/migration/:id/migrate" => "actual_db_schema/migrations#migrate", as: "migrate_migration"
post "/rails/migration/:id/delete" => "actual_db_schema/migrations#delete", as: "delete_migration"
end
ActualDbSchema::MigrationsController.include(@routes.url_helpers)
end
Expand Down Expand Up @@ -169,5 +170,21 @@ def down
end
assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back."
end

test "POST #delete removes migration record" do
version = "20130906111511"
sql = "SELECT version FROM schema_migrations WHERE version = '#{version}'"
ActiveRecord::Base.establish_connection(TestingState.db_config["primary"])

assert_not_nil ActiveRecord::Base.connection.select_value(sql)
post :delete, params: { id: version, database: @utils.primary_database }
assert_response :redirect
get :index
assert_select "table" do |table|
assert_no_match "20130906111511", table.text
end
assert_select ".flash", text: "Migration 20130906111511 was successfully deleted."
assert_nil ActiveRecord::Base.connection.select_value(sql)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def routes_setup
as: "rollback_phantom_migration"
post "/rails/phantom_migrations/rollback_all" => "actual_db_schema/phantom_migrations#rollback_all",
as: "rollback_all_phantom_migrations"
post "/rails/phantom_migrations/delete" => "actual_db_schema/phantom_migrations#delete",
as: "delete_phantom_migration"
end
ActualDbSchema::PhantomMigrationsController.include(@routes.url_helpers)
end
Expand Down Expand Up @@ -169,5 +171,21 @@ def down
get :index
assert_select "p", text: "No phantom migrations found."
end

test "POST #delete removes migration record" do
version = "20130906111511"
sql = "SELECT version FROM schema_migrations WHERE version = '#{version}'"
ActiveRecord::Base.establish_connection(TestingState.db_config["primary"])

assert_not_nil ActiveRecord::Base.connection.select_value(sql)
post :delete, params: { id: version, database: @utils.primary_database }
assert_response :redirect
get :index
assert_select "table" do |table|
assert_no_match "20130906111511", table.text
end
assert_select ".flash", text: "Migration 20130906111511 was successfully deleted."
assert_nil ActiveRecord::Base.connection.select_value(sql)
end
end
end