Skip to content

Commit

Permalink
fixup! Add Migration class
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 4, 2024
1 parent 7d8e928 commit 80e81cf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 55 deletions.
23 changes: 5 additions & 18 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand All @@ -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
69 changes: 32 additions & 37 deletions app/views/actual_db_schema/migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,40 @@
</head>
<body>
<div>
<% if migration = load_migration(params[:id], params[:database]) %>
<h2>Migration <%= migration[:name] %> Details</h2>
<table>
<tbody>
<tr>
<th>Status</th>
<td><%= migration[:status] %></td>
</tr>
<tr>
<th>Migration ID</th>
<td><%= migration[:version] %></td>
</tr>
<tr>
<th>Branch</th>
<td><%= migration[:branch] %></td>
</tr>
<tr>
<th>Database</th>
<td><%= migration[:database] %></td>
</tr>
<tr>
<th>Path</th>
<td><%= migration[:filename] %></td>
</tr>
</tbody>
</table>
<h2>Migration <%= migration[:name] %> Details</h2>
<table>
<tbody>
<tr>
<th>Status</th>
<td><%= migration[:status] %></td>
</tr>
<tr>
<th>Migration ID</th>
<td><%= migration[:version] %></td>
</tr>
<tr>
<th>Branch</th>
<td><%= migration[:branch] %></td>
</tr>
<tr>
<th>Database</th>
<td><%= migration[:database] %></td>
</tr>
<tr>
<th>Path</th>
<td><%= migration[:filename] %></td>
</tr>
</tbody>
</table>

<h3>Migration Code</h3>
<div>
<pre><%= File.read(migration[:filename]) %></pre>
</div>
<div class='button-container'>
<%= link_to 'Back', migrations_path, class: 'button' %>
<%= button_to 'Rollback', rollback_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
</div>
<% else %>
<h2>Migration not found</h2>
<h3>Migration Code</h3>
<div>
<pre><%= File.read(migration[:filename]) %></pre>
</div>
<div class='button-container'>
<%= link_to 'Back', migrations_path, class: 'button' %>
<% end %>
<%= button_to 'Rollback', rollback_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
</div>
</div>
</body>
</html>
23 changes: 23 additions & 0 deletions lib/actual_db_schema/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def self.all
instance.all
end

def self.find(version, database)
instance.find(version, database)
end

def all
migrations = []

Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 80e81cf

Please sign in to comment.