diff --git a/chat_sdk/migration.nim b/chat_sdk/migration.nim index 45ea614..a23f740 100644 --- a/chat_sdk/migration.nim +++ b/chat_sdk/migration.nim @@ -32,10 +32,15 @@ proc runMigrations*(db: DbConn, dir = "migrations") = else: info "Applying migration", file let sqlContent = readFile(file) - # Split by semicolon and execute each statement separately - let statements = sqlContent.split(';') - for stmt in statements: - let trimmedStmt = stmt.strip() - if trimmedStmt.len > 0: - db.exec(sql(trimmedStmt)) - markMigrationRun(db, file) + db.exec(sql"BEGIN TRANSACTION") + try: + # Split by semicolon and execute each statement separately + for stmt in sqlContent.split(';'): + let trimmedStmt = stmt.strip() + if trimmedStmt.len > 0: + db.exec(sql(trimmedStmt)) + markMigrationRun(db, file) + db.exec(sql"COMMIT") + except: + db.exec(sql"ROLLBACK") + raise newException(ValueError, "Migration failed: " & file & " - " & getCurrentExceptionMsg())