-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
64 lines (49 loc) · 1.2 KB
/
lock.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
package gooseplus
import (
"context"
"database/sql"
"fmt"
"github.com/pressly/goose/v3"
)
const lockTableSuffix = "_lock"
func (m Migrator) ensureLockTable(ctx context.Context) error {
tableName := fmt.Sprintf("%s%s", goose.TableName(), lockTableSuffix)
db := m.DB
if _, err := db.ExecContext(ctx, fmt.Sprintf(
`CREATE TABLE IF NOT EXISTS %s (status INTEGER NOT NULL DEFAULT 0)`, tableName,
)); err != nil {
return err
}
cancellable, cancel := context.WithCancel(ctx)
defer cancel()
tx, err := db.BeginTx(cancellable, nil)
if err != nil {
return err
}
if _, err = tx.ExecContext(cancellable, fmt.Sprintf(
`DELETE FROM %s`, tableName,
)); err != nil {
return err
}
if _, err = tx.ExecContext(ctx, fmt.Sprintf(
`INSERT INTO %s(status) VALUES(0)`, tableName,
)); err != nil {
return err
}
return tx.Commit()
}
func (m Migrator) acquireLock(ctx context.Context) (*sql.Tx, error) {
db := m.DB
tableName := fmt.Sprintf("%s%s", goose.TableName(), lockTableSuffix)
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
row := tx.QueryRowContext(ctx,
fmt.Sprintf(
`SELECT status FROM %s LIMIT 1 FOR UPDATE`, tableName,
),
)
var n int
return tx, row.Scan(&n)
}