Skip to content

Commit 67795ae

Browse files
committed
db: Speed up application of empty down migrations
1 parent 276bff5 commit 67795ae

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

cli/src/statbus.cr

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,8 +1074,19 @@ class StatBus
10741074
private def execute_down_migration(db, down_path : Path, version : String)
10751075
Dir.cd(@project_directory) do
10761076
migration = MigrationFile.parse(down_path)
1077-
result = system("./devops/manage-statbus.sh psql --variable=ON_ERROR_STOP=on < #{down_path}")
1078-
if result
1077+
1078+
# Check if migration file is empty (size 0)
1079+
if File.size(down_path) == 0
1080+
puts "Skipping empty migration #{down_path}" if @verbose
1081+
# Remove the migration record without running the file
1082+
versions = db.query_all(<<-SQL, version, as: {version: String})
1083+
DELETE FROM db.migration
1084+
WHERE version = $1
1085+
RETURNING version;
1086+
SQL
1087+
else
1088+
result = system("./devops/manage-statbus.sh psql --variable=ON_ERROR_STOP=on < #{down_path}")
1089+
if result
10791090
# Remove the migration record(s)
10801091
versions = db.query_all(<<-SQL, version, as: {version: String})
10811092
DELETE FROM db.migration

0 commit comments

Comments
 (0)