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 6 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
16 changes: 16 additions & 0 deletions app/assets/stylesheets/actual_db_schema/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,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;
}
18 changes: 16 additions & 2 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ def show
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
18 changes: 16 additions & 2 deletions app/controllers/actual_db_schema/phantom_migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ def show
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
3 changes: 3 additions & 0 deletions app/views/actual_db_schema/migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@
<p>No migrations found.</p>
<% end %>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
3 changes: 3 additions & 0 deletions app/views/actual_db_schema/migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
</div>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
3 changes: 3 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 @@ -51,5 +51,8 @@
<p>No phantom migrations found.</p>
<% end %>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
Copy link
Member

Choose a reason for hiding this comment

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

@VladislavSokov if there are too many migrations, will the user see this message? Is it ok? Please think about users

</body>
</html>
3 changes: 3 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 @@ -41,5 +41,8 @@
<%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
</div>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
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