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

Show a loader for running migrations #84

Merged
merged 1 commit 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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the web server support multiple requests? Always? Consider the web server is configured to serve only one request at a time. How will it behave if the user clicks to run one migration and clicks another one without waiting for its response? Is it an acceptable solution to disable just the pressed button then?

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 %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point in using csrf_meta_tags here? Do we really need it taking into account that these forms will be used only in development mode?

<%= 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
Loading