Skip to content

Commit 2d305d0

Browse files
committed
Skip leading step comments.
1 parent 9573151 commit 2d305d0

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

file_migration.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io/fs"
1212
"log/slog"
1313
"os"
14+
"strings"
1415
)
1516

1617
// FileMigration implements the Migration interface. It helps to apply migrations from a file or fs.FS.
@@ -102,16 +103,27 @@ func migrationFromFileFS(name string, dir fs.FS, log *slog.Logger) FileMigration
102103
}
103104

104105
// applyStepsStream executes database migration steps read from an io.Reader, separated by semicolons, in a transaction.
105-
// Returns the corresponding error if any step execution fails.
106+
// Returns the corresponding error if any step execution fails. Also, as some database drivers or engines seem to not
107+
// support comments, leading comments are removed. This function does not undertake efforts to scan the SQL to find
108+
// other comments. So leading comments telling what a step is going to do, work. But comments in the middle of a
109+
// statement will not be removed. At least with SQLite this will lead to hard to find errors.
106110
func applyStepsStream(tx *sql.Tx, r io.Reader, id string, log *slog.Logger) error {
107111
buf := bytes.Buffer{}
108112

109113
scanner := bufio.NewScanner(r)
114+
newStep := true
110115
var i int
111116

112117
for i = 0; scanner.Scan(); {
113118
buf.Write(scanner.Bytes())
114119

120+
if newStep && strings.HasPrefix(scanner.Text(), "--") {
121+
// skip leading comments
122+
continue
123+
}
124+
125+
newStep = false
126+
115127
if scanner.Text() == ";" {
116128
log.Info("migration step",
117129
slog.String("id", id),

0 commit comments

Comments
 (0)