-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdb.go
356 lines (348 loc) · 8.08 KB
/
db.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package graphql
import (
"context"
"database/sql"
"github.com/GuiaBolso/darwin"
// Needed to talk to postgres
_ "github.com/lib/pq"
)
var (
db *sql.DB
migrations = []darwin.Migration{
{
Version: 1,
Description: "Creating table posts",
Script: `
CREATE TABLE posts (
id serial primary key,
title text,
content text,
date timestamp with time zone,
tags text[],
draft boolean,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 2,
Description: "Creating table stats",
Script: `
CREATE TABLE stats (
id serial primary key,
key text,
value text,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 3,
Description: "Creating table users",
Script: `
CREATE TABLE users(
id serial primary key,
role text,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 4,
Description: "Cleanup users",
Script: `
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS auth_identities;
CREATE TABLE users(
id text primary key,
role text,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 5,
Description: "Add links table",
Script: `
CREATE EXTENSION pgcrypto;
CREATE TABLE links(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title text,
uri text,
created timestamp with time zone,
description text,
screenshot text,
tags text[],
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 6,
Description: "Make things unique",
Script: `
ALTER TABLE links ADD CONSTRAINT links_uri_key UNIQUE (uri);
ALTER TABLE stats ADD CONSTRAINT stats_key_key UNIQUE (key);
`,
},
{
Version: 7,
Description: "Add API keys",
Script: `
ALTER TABLE users ADD COLUMN apikey UUID DEFAULT gen_random_uuid();
`,
},
{
Version: 8,
Description: "Add tweets table",
Script: `
CREATE TABLE tweets(
id text PRIMARY KEY NOT NULL,
text text,
hashtags text[],
symbols text[],
user_mentions text[],
urls text[],
screen_name text,
favorites bigint,
retweets bigint,
posted timestamp with time zone,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 9,
Description: "Add books table",
Script: `
CREATE TABLE books(
id text PRIMARY KEY NOT NULL,
title text,
goodreads_id text,
created_at timestamp with time zone,
modified_at timestamp with time zone
);
`,
},
{
Version: 10,
Description: "Add logs table",
Script: `
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE TABLE logs(
id text PRIMARY KEY NOT NULL,
code TEXT,
datetime TIMESTAMP WITH TIME ZONE,
description TEXT,
location GEOGRAPHY(POINT),
project TEXT,
user_id TEXT,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 11,
Description: "Add photos table",
Script: `
CREATE TABLE photos (
id TEXT PRIMARY KEY NOT NULL,
year INT,
user_id TEXT,
content_type TEXT,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 12,
Description: "Add pages table",
Script: `
CREATE TABLE pages (
id TEXT PRIMARY KEY NOT NULL,
slug TEXT,
title TEXT,
content TEXT,
category TEXT,
tags TEXT[],
user_id TEXT,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 13,
Description: "Add trgm",
Script: `
CREATE EXTENSION pg_trgm;
SELECT set_limit(0.6);
`,
},
{
Version: 14,
Description: "Add trgm index",
Script: `
CREATE INDEX content_gin_idx ON posts USING GIN(content gin_trgm_ops);
`,
},
{
Version: 15,
Description: "Add second trgm index",
Script: `
CREATE INDEX title_gin_idx ON posts USING GIN(title gin_trgm_ops);
`,
},
{
Version: 16,
Description: "Add comments table",
Script: `
CREATE TABLE comments (
id TEXT PRIMARY KEY NOT NULL,
post_id BIGINT,
user_id TEXT NOT NULL,
content TEXT,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 17,
Description: "Add cache table",
Script: `
CREATE TABLE cache (
key TEXT PRIMARY KEY NOT NULL,
value TEXT NOT NULL,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 18,
Description: "Add name to user",
Script: `
ALTER TABLE users ADD COLUMN name Text DEFAULT 'anonymous';
`,
},
{
Version: 19,
Description: "add serial to tweets",
Script: `ALTER TABLE tweets ADD COLUMN IF NOT EXISTS internal_id BIGSERIAL NOT NULL`,
},
{
Version: 20,
Description: "alter stats value",
Script: `ALTER TABLE stats ALTER COLUMN value TYPE float USING (value::float)`,
},
{
Version: 21,
Description: "new stats table",
Script: `
DROP TABLE stats;
CREATE TABLE stats (
id SERIAL PRIMARY KEY,
key TEXT,
value FLOAT,
inserted_at TIMESTAMP WITH TIME ZONE
);
CREATE INDEX stats_key_inserted_at_idx ON stats (key, inserted_at DESC);
`,
},
{
Version: 22,
Description: "add serial index to tweets",
Script: `CREATE INDEX ON tweets (internal_id);`,
},
{
Version: 23,
Description: "new page table",
Script: `
DROP TABLE logs;
DROP TABLE pages;
CREATE TABLE pages (
id SERIAL PRIMARY KEY,
slug TEXT NOT NULL,
content TEXT,
user_id TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 24,
Description: "unique constraint for pages",
Script: `ALTER TABLE pages ADD UNIQUE (slug, user_id);`,
},
{
Version: 25,
Description: "add meta to pages",
Script: `ALTER TABLE pages ADD COLUMN meta JSONB;`,
},
{
Version: 26,
Description: "no empty jsonb",
Script: `
ALTER TABLE pages ALTER COLUMN meta SET DEFAULT '[]'::jsonb;
ALTER TABLE pages ALTER COLUMN meta SET NOT NULL;
`,
},
{
Version: 27,
Description: "new jsonb org",
Script: `
ALTER TABLE pages ALTER COLUMN meta SET DEFAULT '{}'::jsonb;
`,
},
{
Version: 28,
Description: "New logs table",
Script: `
CREATE TABLE logs(
id text PRIMARY KEY NOT NULL,
description TEXT,
project TEXT,
sector TEXT,
user_id TEXT,
started TIMESTAMP WITH TIME ZONE,
stopped TIMESTAMP WITH TIME ZONE,
created_at TIMESTAMP WITH TIME ZONE,
modified_at TIMESTAMP WITH TIME ZONE
);
`,
},
{
Version: 29,
Description: "Cleanup pages",
Script: `DROP TABLE IF EXISTS pages;`,
},
}
)
// InitDB creates a package global db connection from a database string.
func InitDB(dataSourceName string) (*sql.DB, error) {
database, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
db = database
if err := db.PingContext(context.Background()); err != nil {
return nil, err
}
// Migrate
driver := darwin.NewGenericDriver(db, darwin.PostgresDialect{})
d := darwin.New(driver, migrations, nil)
if err := d.Migrate(); err != nil {
return nil, err
}
return db, nil
}