-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_migrations.go
More file actions
163 lines (150 loc) · 4.25 KB
/
schema_migrations.go
File metadata and controls
163 lines (150 loc) · 4.25 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package sqlproc
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
const schemaMigrationsTable = "sqlproc_schema_migrations"
var migrationFilenamePattern = regexp.MustCompile(`^(\d+)[-_]?([A-Za-z0-9_-]*)\.sql$`)
// SchemaMigration represents a discrete schema change.
type SchemaMigration struct {
Version int64
Name string
File string
SQL string
}
// LoadSchemaMigrations reads raw SQL migration files and returns structured migrations.
func LoadSchemaMigrations(files []string) ([]*SchemaMigration, error) {
if len(files) == 0 {
return nil, fmt.Errorf("no schema migration files provided")
}
migrations := make([]*SchemaMigration, 0, len(files))
seen := make(map[int64]string)
for _, file := range files {
mig, err := parseSchemaMigration(file)
if err != nil {
return nil, err
}
if existing, ok := seen[mig.Version]; ok {
return nil, fmt.Errorf("duplicate schema migration version %d (%s and %s)", mig.Version, existing, file)
}
seen[mig.Version] = file
migrations = append(migrations, mig)
}
sort.Slice(migrations, func(i, j int) bool {
if migrations[i].Version == migrations[j].Version {
return migrations[i].Name < migrations[j].Name
}
return migrations[i].Version < migrations[j].Version
})
return migrations, nil
}
func parseSchemaMigration(path string) (*SchemaMigration, error) {
base := filepath.Base(path)
matches := migrationFilenamePattern.FindStringSubmatch(base)
if matches == nil {
return nil, fmt.Errorf("invalid migration filename %q (expected NN_description.sql)", base)
}
version, err := strconv.ParseInt(matches[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("parse migration version from %q: %w", base, err)
}
if version <= 0 {
return nil, fmt.Errorf("migration version must be positive in %q", base)
}
name := strings.Trim(matches[2], "-_ ")
if name == "" {
name = fmt.Sprintf("migration_%d", version)
}
sqlBytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read migration %s: %w", path, err)
}
sqlText := strings.TrimSpace(string(sqlBytes))
if sqlText == "" {
return nil, fmt.Errorf("migration %s is empty", path)
}
return &SchemaMigration{
Version: version,
Name: name,
File: path,
SQL: sqlText,
}, nil
}
// SchemaMigrator applies schema migrations with version tracking.
type SchemaMigrator struct {
db *sql.DB
}
// NewSchemaMigrator creates a schema migrator.
func NewSchemaMigrator(db *sql.DB) *SchemaMigrator {
return &SchemaMigrator{db: db}
}
// Migrate applies all pending schema migrations in order.
func (m *SchemaMigrator) Migrate(ctx context.Context, migrations []*SchemaMigration) error {
if len(migrations) == 0 {
return nil
}
if err := m.ensureTable(ctx); err != nil {
return err
}
applied, err := m.appliedVersions(ctx)
if err != nil {
return err
}
for _, migration := range migrations {
if applied[migration.Version] {
continue
}
if err := m.applyMigration(ctx, migration); err != nil {
return err
}
}
return nil
}
func (m *SchemaMigrator) ensureTable(ctx context.Context) error {
const createTable = `
CREATE TABLE IF NOT EXISTS ` + schemaMigrationsTable + ` (
version BIGINT PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);`
_, err := m.db.ExecContext(ctx, createTable)
return err
}
func (m *SchemaMigrator) appliedVersions(ctx context.Context) (map[int64]bool, error) {
rows, err := m.db.QueryContext(ctx, `SELECT version FROM `+schemaMigrationsTable)
if err != nil {
return nil, err
}
defer rows.Close()
applied := make(map[int64]bool)
for rows.Next() {
var version int64
if err := rows.Scan(&version); err != nil {
return nil, err
}
applied[version] = true
}
return applied, rows.Err()
}
func (m *SchemaMigrator) applyMigration(ctx context.Context, migration *SchemaMigration) error {
tx, err := m.db.BeginTx(ctx, nil)
if err != nil {
return err
}
if _, err := tx.ExecContext(ctx, migration.SQL); err != nil {
_ = tx.Rollback()
return fmt.Errorf("apply migration %s: %w", migration.File, err)
}
if _, err := tx.ExecContext(ctx, `INSERT INTO `+schemaMigrationsTable+` (version, name) VALUES ($1, $2)`, migration.Version, migration.Name); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}