-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract patches to separate files (#29)
- Loading branch information
Showing
6 changed files
with
88 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Patches | ||
# Add new command to roll back the phantom migrations | ||
module MigrationContext | ||
def rollback_branches | ||
ActualDbSchema.failed = [] | ||
migrations.reverse_each do |migration| | ||
migrator = down_migrator_for(migration) | ||
migrator.extend(ActualDbSchema::Patches::Migrator) | ||
migrator.migrate | ||
rescue StandardError => e | ||
raise unless e.message.include?("ActiveRecord::IrreversibleMigration") | ||
|
||
ActualDbSchema.failed << migration | ||
end | ||
end | ||
|
||
private | ||
|
||
def down_migrator_for(migration) | ||
if ActiveRecord::Migration.current_version < 6 | ||
ActiveRecord::Migrator.new(:down, [migration], migration.version) | ||
else | ||
ActiveRecord::Migrator.new(:down, [migration], schema_migration, migration.version) | ||
end | ||
end | ||
|
||
def migration_files | ||
paths = Array(migrations_paths) | ||
current_branch_files = Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] | ||
other_branches_files = Dir["#{ActualDbSchema.migrated_folder}/**/[0-9]*_*.rb"] | ||
|
||
current_branch_file_names = current_branch_files.map { |f| ActualDbSchema.migration_filename(f) } | ||
other_branches_files.reject { |f| ActualDbSchema.migration_filename(f).in?(current_branch_file_names) } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Patches | ||
# Records the migration file into the tmp folder after it's been migrated | ||
module MigrationProxy | ||
def migrate(direction) | ||
super(direction) | ||
FileUtils.copy(filename, ActualDbSchema.migrated_folder.join(basename)) if direction == :up | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Patches | ||
# Run only one migration that's being rolled back | ||
module Migrator | ||
def runnable | ||
migration = migrations.first # there is only one migration, because we pass only one here | ||
ran?(migration) ? [migration] : [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters