-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrator.go
114 lines (89 loc) · 2.6 KB
/
migrator.go
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
package db
import (
"context"
"database/sql"
"fmt"
// sqllite3 sql driver.
_ "github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
"github.com/seborama/pcloud-sdk/tracker/db/migrations"
)
type Migrator struct {
db *sql.DB
}
// NewMigrator creates a new initialised Migrator struct.
func NewMigrator(db *sql.DB) *Migrator {
return &Migrator{
db: db,
}
}
func (m *Migrator) MigrateUp(ctx context.Context) (err error) {
stmt := `
BEGIN;
CREATE TABLE IF NOT EXISTS "schema_migrations" ( "version" INTEGER PRIMARY KEY, "status" VARCHAR );
COMMIT;`
_, err = m.db.ExecContext(ctx, stmt)
if err != nil {
return errors.WithStack(err)
}
version := 0
var status migrationStatus
err = m.db.QueryRowContext(ctx, `SELECT version, status FROM "schema_migrations"`).Scan(&version, &status)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return errors.WithStack(err)
}
if status == migrationStatusInProgress {
return errors.Errorf("unable to apply database migration: migration version %d is recorded 'in progress', probably as the result of a previous failure", version)
}
if version == 0 {
version = -1
}
fmt.Printf("current migrations version: %d\n", version)
if version == len(migrations.SQLite3) {
fmt.Printf("migrations up-to-date - version: %d\n", version)
return nil
}
if version > len(migrations.SQLite3) {
return errors.Errorf("migrations corruption - last executed version: %d - highest available migration version: %d\n", version, len(migrations.SQLite3))
}
for version, stmt := range migrations.SQLite3[version+1:] {
fmt.Printf("applying migrations version: %d\n", version)
err = m.recordMigrationVersion(ctx, version, migrationStatusInProgress)
if err != nil {
return err
}
_, err = m.db.ExecContext(ctx, stmt)
if err != nil {
return errors.WithStack(err)
}
err = m.recordMigrationVersion(ctx, version, migrationStatusApplied)
if err != nil {
return err
}
}
return nil
}
type migrationStatus string
const (
migrationStatusInProgress migrationStatus = "in progress"
migrationStatusApplied migrationStatus = "applied"
)
func (m *Migrator) recordMigrationVersion(ctx context.Context, version int, status migrationStatus) error {
tx, err := m.db.BeginTx(ctx, nil)
if err != nil {
return errors.WithStack(err)
}
_, err = tx.ExecContext(ctx, `DELETE FROM "schema_migrations"`)
if err != nil {
return doRollback(tx, err)
}
_, err = tx.ExecContext(ctx, `INSERT INTO "schema_migrations" VALUES (?, ?)`, version, status)
if err != nil {
return doRollback(tx, err)
}
err = tx.Commit()
if err != nil {
return doRollback(tx, err)
}
return nil
}