-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
83 lines (66 loc) · 1.84 KB
/
schema.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
package bpm
import (
"context"
"github.com/blobcache/bpm/internal/sqlstores"
"github.com/jmoiron/sqlx"
"github.com/owlmessenger/owl/pkg/migrations"
)
var schema = func() *migrations.State {
x := migrations.InitialState()
x = sqlstores.Migration(x)
x = x.ApplyStmt(`CREATE TABLE assets (
id INTEGER,
store_id INTEGER REFERENCES stores(id),
root BLOB,
PRIMARY KEY(id)
)`)
x = x.ApplyStmt(`CREATE INDEX asset_root_idx ON assets (root, id)`)
x = x.ApplyStmt(`CREATE TABLE asset_labels (
asset_id INTEGER REFERENCES assets(id),
k TEXT,
v TEXT,
PRIMARY KEY(asset_id, k)
)`)
x = x.ApplyStmt(`CREATE INDEX asset_labels_idx_kv ON asset_labels (k, v, asset_id)`)
x = x.ApplyStmt(`CREATE TABLE upstreams (
scheme TEXT NOT NULL,
path TEXT NOT NULL,
remote_id TEXT NOT NULL,
asset_id INTEGER NOT NULL REFERENCES assets(id),
PRIMARY KEY(scheme, path, remote_id)
)`)
x = x.ApplyStmt(`CREATE INDEX upstream_idx_asset ON upstreams (asset_id, scheme, path, remote_id)`)
x = x.ApplyStmt(`CREATE TABLE snapshots (
id INTEGER NOT NULL,
cid BLOB NOT NULL,
UNIQUE (cid),
PRIMARY KEY (id)
)`)
x = x.ApplyStmt(`CREATE TABLE snapshot_tlds (
snapshot_id INTEGER NOT NULL REFERENCES snapshots(id),
path TEXT NOT NULL,
root BLOB NOT NULL REFERENCES assets(root),
PRIMARY KEY(snapshot_id, path)
)`)
x = x.ApplyStmt(`CREATE TABLE commits (
id INTEGER NOT NULL,
snapshot_id INTEGER NOT NULL REFERENCES snapshots(id),
created_at TIMESTAMP NOT NULL,
PRIMARY KEY(id)
)`)
x = x.ApplyStmt(`CREATE TABLE fs_cache (
path TEXT NOT NULL,
mtime INTEGER NOT NULL,
root BLOB NOT NULL,
PRIMARY KEY(path)
)`)
x = x.ApplyStmt(`CREATE TABLE webrefs (
blob_id BLOB,
ref BLOB,
PRIMARY KEY(blob_id, ref)
)`)
return x
}()
func setupDB(ctx context.Context, db *sqlx.DB) error {
return migrations.Migrate(ctx, db, schema)
}