Skip to content

Commit

Permalink
fixup! 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 80e81cf commit a45030d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
27 changes: 2 additions & 25 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,18 @@ def show
end

def rollback
rollback_migration(params[:id], params[:database])
ActualDbSchema::Migration.rollback(params[:id], params[:database])
redirect_to migrations_path
end

private

helper_method def migrations
ActualDbSchema::Migration.instance.all
ActualDbSchema::Migration.all
end

helper_method def migration
ActualDbSchema::Migration.find(params[:id], params[:database])
end

def rollback_migration(version, database)
ActualDbSchema.for_each_db_connection do
next unless ActualDbSchema.db_config[:database] == database

context = ActualDbSchema.prepare_context
if context.migrations.detect { |m| m.version.to_s == version }
context.run(:down, version.to_i)
break
end
end
end

def build_migration_struct(status, migration)
MigrationStruct.new(
status: status,
version: migration.version.to_s,
name: migration.name,
branch: ActualDbSchema.branch_for(ActualDbSchema.metadata, migration.version),
database: ActualDbSchema.db_config[:database],
filename: migration.filename
)
end
end
end
16 changes: 16 additions & 0 deletions lib/actual_db_schema/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def self.find(version, database)
instance.find(version, database)
end

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

def all
migrations = []

Expand Down Expand Up @@ -42,6 +46,18 @@ def find(version, database)
nil
end

def rollback(version, database)
ActualDbSchema.for_each_db_connection do
next unless ActualDbSchema.db_config[:database] == database

context = ActualDbSchema.prepare_context
if context.migrations.detect { |m| m.version.to_s == version }
context.run(:down, version.to_i)
break
end
end
end

private

def build_migration_struct(status, migration)
Expand Down

0 comments on commit a45030d

Please sign in to comment.