Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Flash Messages #81

Merged
merged 10 commits into from
Jul 22, 2024
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
39 changes: 39 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
document.addEventListener('DOMContentLoaded', function () {
const migrationActions = document.querySelectorAll('.migration-action');

migrationActions.forEach(button => {
button.addEventListener('click', function (event) {
const originalText = button.value;
button.value = 'Loading...';
disableButtons();

fetch(event.target.form.action, { method: 'POST'})
.then(response => {
if (response.ok) {
window.location.reload();
} else {
throw new Error('Network response was not ok.');
}
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
enableButtons();
button.value = originalText;
});

event.preventDefault();
});
});

function disableButtons() {
migrationActions.forEach(button => {
button.disabled = true;
});
}

function enableButtons() {
migrationActions.forEach(button => {
button.disabled = false;
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ table {
background-color: #000;
}

.button:disabled, .button:hover:disabled {
background-color: transparent;
color: #666;
cursor: not-allowed;
}

.button-container {
display: flex;
}
Expand All @@ -90,3 +96,19 @@ pre {
overflow: hidden;
text-overflow: ellipsis;
}

.flash {
padding: 10px;
margin-bottom: 10px;
border-radius: 5px;
}

.flash.notice {
background-color: #d4edda;
color: #155724;
}

.flash.alert {
background-color: #f8d7da;
color: #721c24;
}
21 changes: 19 additions & 2 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
module ActualDbSchema
# Controller to display the list of migrations for each database connection.
class MigrationsController < ActionController::Base
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token

def index; end

def show
render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found unless migration
end

def rollback
ActualDbSchema::Migration.instance.rollback(params[:id], params[:database])
handle_rollback(params[:id], params[:database])
redirect_to migrations_path
end

def migrate
ActualDbSchema::Migration.instance.migrate(params[:id], params[:database])
handle_migrate(params[:id], params[:database])
redirect_to migrations_path
end

private

def handle_rollback(id, database)
ActualDbSchema::Migration.instance.rollback(id, database)
flash[:notice] = "Migration #{id} was successfully rolled back."
rescue StandardError => e
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] = e.message
end

helper_method def migrations
@migrations ||= ActualDbSchema::Migration.instance.all
end
Expand Down
21 changes: 19 additions & 2 deletions app/controllers/actual_db_schema/phantom_migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
module ActualDbSchema
# Controller to display the list of phantom migrations for each database connection.
class PhantomMigrationsController < ActionController::Base
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token

def index; end

def show
render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found unless phantom_migration
end

def rollback
ActualDbSchema::Migration.instance.rollback(params[:id], params[:database])
handle_rollback(params[:id], params[:database])
redirect_to phantom_migrations_path
end

def rollback_all
ActualDbSchema::Migration.instance.rollback_all
handle_rollback_all
redirect_to phantom_migrations_path
end

private

def handle_rollback(id, database)
ActualDbSchema::Migration.instance.rollback(id, database)
flash[:notice] = "Migration #{id} was successfully rolled back."
rescue StandardError => e
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] = e.message
end

helper_method def phantom_migrations
@phantom_migrations ||= ActualDbSchema::Migration.instance.all_phantom
end
Expand Down
10 changes: 7 additions & 3 deletions app/views/actual_db_schema/migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
<html>
<head>
<title>Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
<h2>Migrations</h2>
<p>
<span style="background-color: #ffe6e6; padding: 0 5px;">Red rows</span> represent phantom migrations.
Expand Down Expand Up @@ -45,12 +49,12 @@
<%= button_to '⎌ Rollback',
rollback_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button',
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "down") %>
<%= button_to '⬆ Migrate',
migrate_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button',
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
</div>
</td>
Expand Down
10 changes: 7 additions & 3 deletions app/views/actual_db_schema/migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
<html>
<head>
<title>Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
<h2>Migration <%= migration[:name] %> Details</h2>
<table>
<tbody>
Expand Down Expand Up @@ -41,12 +45,12 @@
<%= button_to '⎌ Rollback',
rollback_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button',
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "down") %>
<%= button_to '⬆ Migrate',
migrate_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button',
class: 'button migration-action',
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
</div>
</div>
Expand Down
20 changes: 16 additions & 4 deletions app/views/actual_db_schema/phantom_migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
<html>
<head>
<title>Phantom Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
<h2>Phantom Migrations</h2>
<div class="top-buttons">
<%= link_to 'All Migrations', migrations_path, class: "top-button" %>
<% if phantom_migrations.present? %>
<%= button_to '⎌ Rollback all', rollback_all_phantom_migrations_path, method: :post, class: 'button' %>
<%= button_to '⎌ Rollback all',
rollback_all_phantom_migrations_path,
method: :post,
class: 'button migration-action' %>
<% end %>
</div>
<% if phantom_migrations.present? %>
Expand Down Expand Up @@ -39,8 +46,13 @@
<td><%= migration[:database] %></td>
<td>
<div class='button-container'>
<%= link_to '👁 Show', phantom_migration_path(id: migration[:version], database: migration[:database]), class: 'button' %>
<%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: migration[:version], database: migration[:database]), method: :post, class: 'button' %>
<%= link_to '👁 Show',
phantom_migration_path(id: migration[:version], database: migration[:database]),
class: 'button' %>
<%= button_to '⎌ Rollback',
rollback_phantom_migration_path(id: migration[:version], database: migration[:database]),
method: :post,
class: 'button migration-action' %>
</div>
</td>
</tr>
Expand Down
11 changes: 9 additions & 2 deletions app/views/actual_db_schema/phantom_migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
<html>
<head>
<title>Phantom Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
<h2>Phantom Migration <%= phantom_migration[:name] %> Details</h2>
<table>
<tbody>
Expand Down Expand Up @@ -38,7 +42,10 @@
</div>
<div class='button-container'>
<%= link_to '← Back', phantom_migrations_path, class: 'button' %>
<%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
<%= button_to '⎌ Rollback',
rollback_phantom_migration_path(id: params[:id], database: params[:database]),
method: :post,
class: 'button migration-action' %>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion lib/actual_db_schema/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Engine < ::Rails::Engine
mount ActualDbSchema::Engine => "/rails"
end

app.config.assets.precompile += %w[actual_db_schema/styles.css]
app.config.assets.precompile += %w[styles.css application.js]
end
end
end
Expand Down
23 changes: 23 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 All @@ -113,6 +135,7 @@ def active_record_setup
end
end
end
assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back."
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ def active_record_setup
end
end
end
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
Expand Down
Loading