88 "github.com/muir/libschema"
99 "github.com/muir/libschema/classifysql"
1010 "github.com/muir/libschema/internal"
11+ "github.com/muir/sqltoken"
1112 "github.com/pkg/errors"
1213)
1314
@@ -16,7 +17,42 @@ type CanExecContext interface {
1617}
1718
1819func RunSQL (ctx context.Context , log * internal.Log , tx CanExecContext , statements classifysql.Statements , rowsAffected * int64 , m libschema.Migration , d * libschema.Database ) error {
19- for _ , commandSQL := range statements .TokensList ().Strings () {
20+ for _ , tokens := range statements .TokensList () {
21+ if ! m .Base ().PreserveComments () {
22+ tokens = tokens .Strip ()
23+ }
24+ // Strip leading DelimiterStatement (e.g., "DELIMITER //\n")
25+ if len (tokens ) > 0 && tokens [0 ].Type == sqltoken .DelimiterStatement {
26+ log .Debug ("Stripping leading DelimiterStatement from migration" , map [string ]any {
27+ "name" : m .Base ().Name .Name ,
28+ "library" : m .Base ().Name .Library ,
29+ })
30+ tokens = tokens [1 :]
31+ }
32+ // Strip trailing DelimiterStatement (e.g., "DELIMITER ;\n") and any whitespace before it
33+ for len (tokens ) > 0 && (tokens [len (tokens )- 1 ].Type == sqltoken .DelimiterStatement || tokens [len (tokens )- 1 ].Type == sqltoken .Whitespace ) {
34+ if tokens [len (tokens )- 1 ].Type == sqltoken .DelimiterStatement {
35+ log .Debug ("Stripping trailing DelimiterStatement from migration" , map [string ]any {
36+ "name" : m .Base ().Name .Name ,
37+ "library" : m .Base ().Name .Library ,
38+ })
39+ }
40+ tokens = tokens [:len (tokens )- 1 ]
41+ }
42+ // Strip trailing Delimiter (e.g., "//") and any whitespace before it
43+ for len (tokens ) > 0 && (tokens [len (tokens )- 1 ].Type == sqltoken .Delimiter || tokens [len (tokens )- 1 ].Type == sqltoken .Whitespace ) {
44+ if tokens [len (tokens )- 1 ].Type == sqltoken .Delimiter {
45+ log .Debug ("Stripping trailing Delimiter from migration" , map [string ]any {
46+ "name" : m .Base ().Name .Name ,
47+ "library" : m .Base ().Name .Library ,
48+ })
49+ }
50+ tokens = tokens [:len (tokens )- 1 ]
51+ }
52+ if len (tokens ) == 0 {
53+ continue
54+ }
55+ commandSQL := tokens .String ()
2056 result , err := tx .ExecContext (ctx , commandSQL )
2157 if d .Options .DebugLogging {
2258 log .Debug ("Executed SQL" , map [string ]any {
0 commit comments