Skip to content

Commit

Permalink
db: Speed up application of empty down migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
jhf committed Jan 6, 2025
1 parent 276bff5 commit 67795ae
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cli/src/statbus.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1074,8 +1074,19 @@ class StatBus
private def execute_down_migration(db, down_path : Path, version : String)
Dir.cd(@project_directory) do
migration = MigrationFile.parse(down_path)
result = system("./devops/manage-statbus.sh psql --variable=ON_ERROR_STOP=on < #{down_path}")
if result

# Check if migration file is empty (size 0)
if File.size(down_path) == 0
puts "Skipping empty migration #{down_path}" if @verbose
# Remove the migration record without running the file
versions = db.query_all(<<-SQL, version, as: {version: String})
DELETE FROM db.migration
WHERE version = $1
RETURNING version;
SQL
else
result = system("./devops/manage-statbus.sh psql --variable=ON_ERROR_STOP=on < #{down_path}")
if result
# Remove the migration record(s)
versions = db.query_all(<<-SQL, version, as: {version: String})
DELETE FROM db.migration
Expand Down

0 comments on commit 67795ae

Please sign in to comment.