-
Notifications
You must be signed in to change notification settings - Fork 948
/
Copy pathschema.go
57 lines (51 loc) · 2.08 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
package reminders
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS reminders (
id SERIAL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
deleted_at TIMESTAMP WITH TIME ZONE,
-- text instead of bigint for legacy compatibility
user_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
guild_id BIGINT NOT NULL,
message TEXT NOT NULL,
"when" BIGINT NOT NULL
);
`, `
CREATE INDEX IF NOT EXISTS idx_reminders_deleted_at ON reminders(deleted_at);
`, `
-- Previous versions of the reputation module used gorm instead of sqlboiler,
-- which does not add NOT NULL constraints by default. Therefore, ensure the
-- NOT NULL constraints are present in existing tables as well.
-- The first few columns below have always been set since the reminders plugin was
-- added, so barring the presence of invalid entries, we can safely add NOT NULL
-- constraints without error.
ALTER TABLE reminders ALTER COLUMN created_at SET NOT NULL;
`, `
ALTER TABLE reminders ALTER COLUMN updated_at SET NOT NULL;
`, `
ALTER TABLE reminders ALTER COLUMN user_id SET NOT NULL;
`, `
ALTER TABLE reminders ALTER COLUMN channel_id SET NOT NULL;
`, `
ALTER TABLE reminders ALTER COLUMN message SET NOT NULL;
`, `
ALTER TABLE reminders ALTER COLUMN "when" SET NOT NULL;
`, `
DO $$
BEGIN
-- The guild_id column is more annoying to deal with. When the reminders plugin
-- was first created, the reminders table did not have a guild_id column -- it
-- was added later, in October 2018 (9f5ef28). So reminders before then could
-- plausibly have guild_id = NULL, meaning directly adding the NOT NULL
-- constraint would fail. But since the maximum offset of a reminder is 1 year,
-- all such reminders have now expired and so we can just delete them before
-- adding the constraint.
-- Only run if we haven't added the NOT NULL constraint yet.
IF EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='reminders' AND column_name='guild_id' AND is_nullable='YES') THEN
DELETE FROM reminders WHERE guild_id IS NULL;
ALTER TABLE reminders ALTER COLUMN guild_id SET NOT NULL;
END IF;
END $$;
`}