-
Notifications
You must be signed in to change notification settings - Fork 106
/
ddl.go
75 lines (57 loc) · 1.62 KB
/
ddl.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
package ddl
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"fmt"
"log"
_ "embed"
)
//go:embed delist_statuses.sql
var delistStatusesDDL string
//go:embed add_delist_reasons.sql
var addDelistReasonsDDL string
//go:embed drop_blobs.sql
var dropBlobs string
var mediorumMigrationTable = `
create table if not exists mediorum_migrations (
"hash" text primary key,
"ts" timestamp
);
`
var qmSyncTable = `
create table if not exists qm_sync (
"host" text primary key
);
`
func Migrate(db *sql.DB, myHost string) {
mustExec(db, mediorumMigrationTable)
runMigration(db, delistStatusesDDL)
runMigration(db, addDelistReasonsDDL)
runMigration(db, dropBlobs)
runMigration(db, `create index if not exists uploads_ts_idx on uploads(created_at, transcoded_at)`)
runMigration(db, `drop table if exists "Files", "ClockRecords", "Tracks", "AudiusUsers", "CNodeUsers", "SessionTokens", "ContentBlacklists", "Playlists", "SequelizeMeta", blobs, cid_lookup, cid_log cascade`)
runMigration(db, qmSyncTable)
}
func runMigration(db *sql.DB, ddl string) {
h := md5string(ddl)
var alreadyRan bool
db.QueryRow(`select count(*) = 1 from mediorum_migrations where hash = $1`, h).Scan(&alreadyRan)
if alreadyRan {
fmt.Printf("hash %s exists skipping ddl \n", h)
return
}
mustExec(db, ddl)
mustExec(db, `insert into mediorum_migrations values ($1, now()) on conflict do nothing`, h)
}
func mustExec(db *sql.DB, ddl string, va ...interface{}) {
_, err := db.Exec(ddl, va...)
if err != nil {
fmt.Println(ddl)
log.Fatal(err)
}
}
func md5string(s string) string {
hash := md5.Sum([]byte(s))
return hex.EncodeToString(hash[:])
}