Skip to content

Commit 4ab4f37

Browse files
committed
[fix] support delimiter
1 parent 761aba8 commit 4ab4f37

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

internal/mhelp/run_sql.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
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,25 @@ type CanExecContext interface {
1617
}
1718

1819
func 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+
tokens = tokens.Strip()
22+
if len(tokens) == 0 {
23+
return nil
24+
}
25+
if tokens[0].Type == sqltoken.DelimiterStatement {
26+
log.Debug("Stripping leading delimiter statement from migration", map[string]any{
27+
"name": m.Base().Name.Name,
28+
"library": m.Base().Name.Library,
29+
})
30+
tokens = tokens[1:]
31+
}
32+
if tokens[len(tokens)-1].Type == sqltoken.DelimiterStatement {
33+
log.Debug("Stripping trailing delimiter statement from migration", map[string]any{
34+
"name": m.Base().Name.Name,
35+
"library": m.Base().Name.Library,
36+
})
37+
tokens = tokens[:len(tokens)-1]
38+
}
2039
result, err := tx.ExecContext(ctx, commandSQL)
2140
if d.Options.DebugLogging {
2241
log.Debug("Executed SQL", map[string]any{

lsmysql/mysql_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,47 @@ func testMysqlNotAllowed(t *testing.T, dsn string, createPostfix string, driverN
273273
}
274274
}
275275
}
276+
277+
func TestMigrationWithDelimiter(t *testing.T) {
278+
options, cleanup := lstesting.FakeSchema(t, "")
279+
280+
t.Log("Doing migrations in database/schema", options.SchemaOverride)
281+
282+
t.Log("We will error on unknown migrations")
283+
284+
options.DebugLogging = true
285+
t.Log("No opening the database...")
286+
db, err := sql.Open("mysql", dsn)
287+
require.NoError(t, err, "open database")
288+
defer func() {
289+
assert.NoError(t, db.Close())
290+
}()
291+
defer cleanup(db)
292+
293+
s := libschema.New(context.Background(), options)
294+
dbase, _, err := driverNew(t, "test", s, db)
295+
require.NoError(t, err, "libschema NewDatabase")
296+
297+
dbase.Migrations("L1",
298+
lsmysql.Script("SP", `
299+
DELIMITER //
300+
CREATE PROCEDURE charge_account(id BIGINT, amount DECIMAL(18,4)) AS
301+
DECLARE
302+
balance_tbl QUERY(bal DECIMAL(18,4)) =
303+
SELECT remaining_balance
304+
FROM account_balance
305+
WHERE account_id = id;
306+
balance DECIMAL(18,4) = SCALAR(balance_tbl);
307+
updated_balance DECIMAL(18,4) = balance - amount;
308+
BEGIN
309+
IF balance > amount THEN
310+
UPDATE account_balance
311+
SET remaining_balance = updated_balance
312+
WHERE account_id = id;
313+
END IF;
314+
END //
315+
DELIMITER ;
316+
`))
317+
err = s.Migrate(context.Background())
318+
assert.NoError(t, err)
319+
}

0 commit comments

Comments
 (0)