diff --git a/app/controllers/actual_db_schema/migrations_controller.rb b/app/controllers/actual_db_schema/migrations_controller.rb index 556a379..ea9b88d 100644 --- a/app/controllers/actual_db_schema/migrations_controller.rb +++ b/app/controllers/actual_db_schema/migrations_controller.rb @@ -5,7 +5,9 @@ module ActualDbSchema class MigrationsController < ActionController::Base def index; end - def show; end + def show + render :not_found, status: 404 unless migration + end def rollback rollback_migration(params[:id], params[:database]) @@ -18,15 +20,8 @@ def rollback ActualDbSchema::Migration.instance.all end - helper_method def load_migration(version, database) - ActualDbSchema.for_each_db_connection do - next unless ActualDbSchema.db_config[:database] == database - - context = ActualDbSchema.prepare_context - migration = find_migration_in_context(context, version) - return migration if migration - end - nil + helper_method def migration + ActualDbSchema::Migration.find(params[:id], params[:database]) end def rollback_migration(version, database) @@ -51,13 +46,5 @@ def build_migration_struct(status, migration) filename: migration.filename ) end - - def find_migration_in_context(context, version) - migration = context.migrations.detect { |m| m.version.to_s == version } - return unless migration - - status = context.migrations_status.detect { |_s, v| v.to_s == version }&.first || "unknown" - build_migration_struct(status, migration) - end end end diff --git a/app/views/actual_db_schema/migrations/show.html.erb b/app/views/actual_db_schema/migrations/show.html.erb index 150a833..1a88150 100644 --- a/app/views/actual_db_schema/migrations/show.html.erb +++ b/app/views/actual_db_schema/migrations/show.html.erb @@ -6,45 +6,40 @@
- <% if migration = load_migration(params[:id], params[:database]) %> -

Migration <%= migration[:name] %> Details

- - - - - - - - - - - - - - - - - - - - - - - -
Status<%= migration[:status] %>
Migration ID<%= migration[:version] %>
Branch<%= migration[:branch] %>
Database<%= migration[:database] %>
Path<%= migration[:filename] %>
+

Migration <%= migration[:name] %> Details

+ + + + + + + + + + + + + + + + + + + + + + + +
Status<%= migration[:status] %>
Migration ID<%= migration[:version] %>
Branch<%= migration[:branch] %>
Database<%= migration[:database] %>
Path<%= migration[:filename] %>
-

Migration Code

-
-
<%= File.read(migration[:filename]) %>
-
-
- <%= link_to 'Back', migrations_path, class: 'button' %> - <%= button_to 'Rollback', rollback_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %> -
- <% else %> -

Migration not found

+

Migration Code

+
+
<%= File.read(migration[:filename]) %>
+
+
<%= link_to 'Back', migrations_path, class: 'button' %> - <% end %> + <%= button_to 'Rollback', rollback_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %> +
diff --git a/lib/actual_db_schema/migration.rb b/lib/actual_db_schema/migration.rb index dcc530f..95df341 100644 --- a/lib/actual_db_schema/migration.rb +++ b/lib/actual_db_schema/migration.rb @@ -11,6 +11,10 @@ def self.all instance.all end + def self.find(version, database) + instance.find(version, database) + end + def all migrations = [] @@ -27,6 +31,17 @@ def all migrations end + def find(version, database) + ActualDbSchema.for_each_db_connection do + next unless ActualDbSchema.db_config[:database] == database + + context = ActualDbSchema.prepare_context + migration = find_migration_in_context(context, version) + return migration if migration + end + nil + end + private def build_migration_struct(status, migration) @@ -40,6 +55,14 @@ def build_migration_struct(status, migration) ) end + def find_migration_in_context(context, version) + migration = context.migrations.detect { |m| m.version.to_s == version } + return unless migration + + status = context.migrations_status.detect { |_s, v| v.to_s == version }&.first || "unknown" + build_migration_struct(status, migration) + end + def branch_for(version) metadata.fetch(version.to_s, {})[:branch] || "unknown" end