-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
213 lines (175 loc) · 5.21 KB
/
sql.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package doltswarm
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/bokwoon95/sq"
)
type Queryer interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
}
type TAG struct {
sq.TableStruct `sq:"dolt_tags"`
TAG_NAME sq.StringField
TAG_HASH sq.StringField
TAGGER sq.StringField
EMAIL sq.StringField
DATE sq.TimeField
MESSAGE sq.StringField
}
type Tag struct {
Name string
Hash string
Tagger string
Email string
Date time.Time
Message string
}
func tagMapper(row *sq.Row) Tag {
tag := Tag{
Name: row.String("tag_name"),
Hash: row.String("tag_hash"),
Tagger: row.String("tagger"),
Email: row.String("email"),
Date: row.Time("date"),
Message: row.String("message"),
}
return tag
}
type Commit struct {
Hash string
Committer string
Email string
Date time.Time
Message string
}
type ExecFunc func(*sql.Tx) error
func commitMapper(row *sq.Row) Commit {
commit := Commit{
Hash: row.String("commit_hash"),
Committer: row.String("committer"),
Email: row.String("email"),
Date: row.Time("date"),
Message: row.String("message"),
}
return commit
}
func doCommit(tx *sql.Tx, msg string, signer Signer) (string, error) {
if signer == nil {
return "", fmt.Errorf("no signer available")
}
// commit
var commitHash string
err := tx.QueryRow(fmt.Sprintf("CALL DOLT_COMMIT('-A', '-m', '%s', '--author', '%s <%s@doltswarm>', '--date', '%s');", msg, signer.GetID(), signer.GetID(), time.Now().Format(time.RFC3339Nano))).Scan(&commitHash)
if err != nil {
return "", fmt.Errorf("failed to run commit procedure: %w", err)
}
// create commit signature and add it to a tag
signature, err := signer.Sign(commitHash)
if err != nil {
return "", fmt.Errorf("failed to sign commit '%s': %w", commitHash, err)
}
tagcmd := fmt.Sprintf("CALL DOLT_TAG('-m', '%s', '--author', '%s <%s@doltswarm>', '%s', '%s');", signer.PublicKey(), signer.GetID(), signer.GetID(), signature, commitHash)
_, err = tx.Exec(tagcmd)
if err != nil {
return "", fmt.Errorf("failed to create signature tag (%s) : %w", signature, err)
}
return commitHash, nil
}
func getChangedTables(q Queryer, from string, to string) ([]string, error) {
query := fmt.Sprintf("SELECT to_table_name FROM DOLT_DIFF_SUMMARY('%s', '%s')", from, to)
rows, err := q.Query(query)
if err != nil {
return nil, fmt.Errorf("failed to query changed tables: %w", err)
}
defer rows.Close()
var changedTables []string
for rows.Next() {
var tableName string
err := rows.Scan(&tableName)
if err != nil {
return nil, fmt.Errorf("failed to scan table name: %w", err)
}
changedTables = append(changedTables, tableName)
}
return changedTables, nil
}
func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
return db.sqldb.ExecContext(ctx, query, args...)
}
func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
return db.sqldb.QueryContext(ctx, query, args...)
}
func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
return db.sqldb.PrepareContext(ctx, query)
}
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
return db.sqldb.BeginTx(ctx, opts)
}
// Commit
func (db *DB) Commit(commitMsg string) (string, error) {
tx, err := db.BeginTx(context.TODO(), nil)
if err != nil {
return "", fmt.Errorf("failed to start transaction: %w", err)
}
defer tx.Rollback()
commitHash, err := doCommit(tx, commitMsg, db.signer)
if err != nil {
return "", fmt.Errorf("failed to commit: %w", err)
}
// find out which tables changed
changedTables, err := getChangedTables(tx, "HEAD^", "HEAD")
if err != nil {
return "", fmt.Errorf("failed to get changed tables: %w", err)
}
err = tx.Commit()
if err != nil {
return "", fmt.Errorf("failed to commit transaction: %w", err)
}
// trigger table change callbacks after successful commit
for _, tableName := range changedTables {
db.triggerTableChangeCallbacks(tableName)
}
// advertise new head
db.AdvertiseHead()
return commitHash, nil
}
func (db *DB) ExecAndCommit(execFunc ExecFunc, commitMsg string) (string, error) {
tx, err := db.BeginTx(context.TODO(), nil)
if err != nil {
return "", fmt.Errorf("failed to start transaction: %w", err)
}
defer tx.Rollback()
_, err = tx.Exec("CALL DOLT_CHECKOUT('main');")
if err != nil {
return "", fmt.Errorf("failed to checkout main branch: %w", err)
}
// exec the sql func
err = execFunc(tx)
if err != nil {
return "", fmt.Errorf("failed to run exec function: %w", err)
}
// find out which tables changed
changedTables, err := getChangedTables(tx, "WORKING", "HEAD")
if err != nil {
return "", err
}
// dolt commit
commitHash, err := doCommit(tx, commitMsg, db.signer)
if err != nil {
return "", fmt.Errorf("failed to commit: %w", err)
}
// finish sql transaction
err = tx.Commit()
if err != nil {
return "", fmt.Errorf("failed to commit transaction: %w", err)
}
// trigger table change callbacks after successful commit
for _, tableName := range changedTables {
db.triggerTableChangeCallbacks(tableName)
}
// advertise new head
db.AdvertiseHead()
return commitHash, nil
}