oficina / home Public

Clone
git clone https://amadan.net/oficina/home

Plain git — no account needed to clone.

Download

Download this file

1package operator
2
3import (
4 "context"
5 "database/sql"
6
7 "amadan.net/rastrillo/rastrillo/migrate"
8)
9
10func VerifyLedger(ctx context.Context, c *sql.Conn, set *migrate.Set) error {
11 if c == nil || set == nil {
12 return ErrRefused
13 }
14 expected := map[string]string{}
15 for _, m := range set.All() {
16 if m.Fn != nil {
17 return ErrRefused
18 }
19 expected[m.ID] = migrate.Checksum(m.SQL)
20 }
21 rows, e := c.QueryContext(ctx, "SELECT id,checksum FROM rastrillo_migrations LIMIT 1001")
22 if e != nil {
23 return ErrRefused
24 }
25 for rows.Next() {
26 var id, sum string
27 if rows.Scan(&id, &sum) != nil || expected[id] == "" || expected[id] != sum {
28 rows.Close()
29 return ErrRefused
30 }
31 delete(expected, id)
32 }
33 if rows.Err() != nil || rows.Close() != nil || len(expected) != 0 {
34 return ErrRefused
35 }
36 return nil
37}
38