Skip to content

Commit

Permalink
Minor refactors and add documentation for TimestampMigrator.run_single
Browse files Browse the repository at this point in the history
Add a private apply_migration method that both run and run_single
can use.

Set the default direction in .run_single, so that #run_single can
rely on it. Simplify the already applied or not check.
  • Loading branch information
jeremyevans committed Nov 13, 2023
1 parent 422f160 commit 4637356
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
=== master

* Add TimestampMigrator.run_single to run a single migration file up or down (opya, jeremyevans) (#2093)

* Support INSERT RETURNING on MariaDB 10.5+, and use it when saving new model objects (jeremyevans)

* Add Database#{defer,immediate}_constraints on PostgreSQL for changing handling of deferrable constraints in a transaction (jeremyevans)
Expand Down
35 changes: 19 additions & 16 deletions lib/sequel/extensions/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,11 @@ def initialize(db, directory, opts=OPTS)
@migration_tuples = get_migration_tuples
end

# Apply the migration in the given file path. See Migrator.run for the
# available options. Additionally, this method supports the :direction
# option for whether to run the migration up (default) or down.
def self.run_single(db, path, opts=OPTS)
new(db, File.dirname(path), opts).run_single(path, opts[:direction])
new(db, File.dirname(path), opts).run_single(path, opts[:direction] || :up)
end

# The timestamp migrator is current if there are no migrations to apply
Expand All @@ -706,29 +709,31 @@ def is_current?
# Apply all migration tuples on the database
def run
migration_tuples.each do |m, f, direction|
t = Time.now
db.log_info("Begin applying migration #{f}, direction: #{direction}")
checked_transaction(m) do
m.apply(db, direction)
fi = f.downcase
direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
end
db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
apply_migration(m, f, direction)
end
nil
end

# Apply single migration tuple on the database
# Apply single migration tuple at the given path with the given direction
# on the database.
def run_single(path, direction)
migration = load_migration_file(path)
file_name = File.basename(path)
fi = file_name.downcase
direction ||= :up
already_applied = applied_migrations.include?(file_name.downcase)

return if (applied_migrations.include?(fi) && direction == :up) ||
(!applied_migrations.include?(fi) && direction == :down)
return if direction == :up ? already_applied : !already_applied

apply_migration(migration, file_name, direction)
nil
end

private

# Apply a single migration with the given filename in the given direction.
def apply_migration(migration, file_name, direction)
fi = file_name.downcase
t = Time.now

db.log_info("Begin applying migration #{file_name}, direction: #{direction}")
checked_transaction(migration) do
migration.apply(db, direction)
Expand All @@ -737,8 +742,6 @@ def run_single(path, direction)
db.log_info("Finished applying migration #{file_name}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
end

private

# Convert the schema_info table to the new schema_migrations table format,
# using the version of the schema_info table and the current migration files.
def convert_from_schema_info
Expand Down

0 comments on commit 4637356

Please sign in to comment.