This repository has been archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
48b961caa.go
122 lines (116 loc) · 3.33 KB
/
48b961caa.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
121
122
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package migration
import (
"github.com/jmoiron/sqlx"
)
type revision_48b961caa struct {
}
func (r *revision_48b961caa) Version() string { return "48b961caa" }
func (r *revision_48b961caa) Up(tx *sqlx.Tx) error {
stmts := []string{
`
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
CREATE TABLE IF NOT EXISTS public.pending_notification (
id SERIAL NOT NULL PRIMARY KEY,
op text NOT NULL,
appname text NOT NULL,
recordtype text NOT NULL,
record jsonb NOT NULL
);
`,
`
CREATE OR REPLACE FUNCTION public.notify_record_change() RETURNS TRIGGER AS $$
DECLARE
affected_record RECORD;
inserted_id integer;
BEGIN
IF (TG_OP = 'DELETE') THEN
affected_record := OLD;
ELSE
affected_record := NEW;
END IF;
INSERT INTO pending_notification (op, appname, recordtype, record)
VALUES (TG_OP, TG_TABLE_SCHEMA, TG_TABLE_NAME, row_to_json(affected_record)::jsonb)
RETURNING id INTO inserted_id;
PERFORM pg_notify('record_change', inserted_id::TEXT);
RETURN affected_record;
END;
$$ LANGUAGE plpgsql;
`,
`
CREATE TABLE _user (
id text PRIMARY KEY,
email text,
password text,
auth jsonb
);
`,
`
CREATE TABLE _asset (
id text PRIMARY KEY,
content_type text NOT NULL,
size bigint NOT NULL
);
`,
`
CREATE TABLE _device (
id text PRIMARY KEY,
user_id text REFERENCES _user (id),
type text NOT NULL,
token text NOT NULL,
last_registered_at timestamp without time zone NOT NULL,
UNIQUE (user_id, type, token)
);
`,
`
CREATE INDEX ON _device (token, last_registered_at);
`,
`
CREATE TABLE _subscription (
id text NOT NULL,
user_id text NOT NULL,
device_id text REFERENCES _device (id) ON DELETE CASCADE NOT NULL,
type text NOT NULL,
notification_info jsonb,
query jsonb,
PRIMARY KEY(user_id, device_id, id)
);
`,
`
CREATE TABLE _friend (
left_id text NOT NULL,
right_id text REFERENCES _user (id) NOT NULL,
PRIMARY KEY(left_id, right_id)
);
`,
`
CREATE TABLE _follow (
left_id text NOT NULL,
right_id text REFERENCES _user (id) NOT NULL,
PRIMARY KEY(left_id, right_id)
);
`,
}
for _, stmt := range stmts {
_, err := tx.Exec(stmt)
if err != nil {
return err
}
}
return nil
}
func (r *revision_48b961caa) Down(tx *sqlx.Tx) error {
panic("cannot downgrade from a base revision")
}