-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.go
120 lines (105 loc) · 2.98 KB
/
migrations.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
package sqlite
import (
"context"
"database/sql"
"github.com/boreq/errors"
"github.com/planetary-social/nos-event-service/internal/migrations"
)
func NewMigrations(fns *MigrationFns) (migrations.Migrations, error) {
return migrations.NewMigrations([]migrations.Migration{
migrations.MustNewMigration("initial", fns.Initial),
migrations.MustNewMigration("create_pubsub_tables", fns.CreatePubsubTables),
})
}
type MigrationFns struct {
db *sql.DB
pubsub *PubSub
}
func NewMigrationFns(db *sql.DB, pubsub *PubSub) *MigrationFns {
return &MigrationFns{db: db, pubsub: pubsub}
}
func (m *MigrationFns) Initial(ctx context.Context, state migrations.State, saveStateFunc migrations.SaveStateFunc) error {
_, err := m.db.Exec(`
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY,
event_id TEXT UNIQUE,
payload BLOB
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the events table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS events_to_relays (
event_id INT,
relay_id INT,
PRIMARY KEY(event_id, relay_id),
FOREIGN KEY(event_id) REFERENCES events(id),
FOREIGN KEY(relay_id) REFERENCES relays(id)
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the events_to_relays table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS relays (
id INTEGER PRIMARY KEY,
address TEXT UNIQUE
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the relays table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS public_keys (
id INTEGER PRIMARY KEY,
public_key TEXT UNIQUE
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the public_keys table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS contacts_events (
follower_id INTEGER PRIMARY KEY,
event_id INTEGER UNIQUE,
FOREIGN KEY(follower_id) REFERENCES public_keys(id),
FOREIGN KEY(event_id) REFERENCES events(id)
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the contacts_events table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS contacts_followees (
follower_id INTEGER,
followee_id INTEGER,
PRIMARY KEY(follower_id, followee_id),
FOREIGN KEY(follower_id) REFERENCES public_keys(id),
FOREIGN KEY(followee_id) REFERENCES public_keys(id)
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the contacts_followees table")
}
_, err = m.db.Exec(`
CREATE TABLE IF NOT EXISTS public_keys_to_monitor (
id INTEGER PRIMARY KEY,
public_key TEXT UNIQUE,
created_at INTEGER,
updated_at INTEGER
);`,
)
if err != nil {
return errors.Wrap(err, "error creating the public_keys_to_monitor table")
}
return nil
}
func (m *MigrationFns) CreatePubsubTables(ctx context.Context, state migrations.State, saveStateFunc migrations.SaveStateFunc) error {
for _, query := range m.pubsub.InitializingQueries() {
if _, err := m.db.Exec(query); err != nil {
return errors.Wrapf(err, "error initializing pubsub")
}
}
return nil
}