Skip to content

Commit

Permalink
Show a loader for running migrations (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov authored Jul 22, 2024
1 parent 0203e2f commit ac634fa
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 13 deletions.
47 changes: 47 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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();

const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

fetch(event.target.form.action, {
method: 'POST',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': csrfToken
}
})
.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 Down
8 changes: 5 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,7 +2,9 @@
<html>
<head>
<title>Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -45,12 +47,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
8 changes: 5 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,7 +2,9 @@
<html>
<head>
<title>Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -41,12 +43,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
18 changes: 14 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,20 @@
<html>
<head>
<title>Phantom Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
<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 +44,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
9 changes: 7 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,7 +2,9 @@
<html>
<head>
<title>Phantom Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -38,7 +40,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>
<% flash.each do |key, message| %>
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

0 comments on commit ac634fa

Please sign in to comment.