-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmigrations_sqlite.go
More file actions
91 lines (83 loc) · 2 KB
/
Copy pathmigrations_sqlite.go
File metadata and controls
91 lines (83 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package floxy
import (
"context"
"database/sql"
"embed"
"fmt"
"io/fs"
"sort"
"strings"
)
//go:embed migrations_sqlite/*.sql
var sqliteMigrationFiles embed.FS
// RunSQLiteMigrations executes embedded SQLite migrations in lexical order within a single transaction.
func RunSQLiteMigrations(ctx context.Context, db *sql.DB) error {
entries, err := fs.ReadDir(sqliteMigrationFiles, "migrations_sqlite")
if err != nil {
return fmt.Errorf("read migrations dir: %w", err)
}
if len(entries) == 0 {
return nil
}
// Sort by filename to ensure deterministic order (e.g., 0001_..., 0002_...)
sort.Slice(entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() })
tx, err := db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
defer func() {
if tx != nil {
_ = tx.Rollback()
}
}()
for _, e := range entries {
if e.IsDir() {
continue
}
b, err := sqliteMigrationFiles.ReadFile("migrations_sqlite/" + e.Name())
if err != nil {
return fmt.Errorf("read migration %s: %w", e.Name(), err)
}
content := string(b)
// Very simple split by semicolon; adequate for our DDL files
stmts := splitSQLStatements(content)
for _, stmt := range stmts {
if _, err := tx.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("exec migration %s: %w", e.Name(), err)
}
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit migrations: %w", err)
}
tx = nil
return nil
}
func splitSQLStatements(sqlText string) []string {
parts := strings.Split(sqlText, ";")
res := make([]string, 0, len(parts))
for _, p := range parts {
stmt := strings.TrimSpace(p)
if stmt == "" {
continue
}
// Skip pure comment-only lines
lines := strings.Split(stmt, "\n")
allComment := true
for _, ln := range lines {
l := strings.TrimSpace(ln)
if l == "" {
continue
}
if !strings.HasPrefix(l, "--") {
allComment = false
break
}
}
if allComment {
continue
}
res = append(res, stmt)
}
return res
}