forked from snap-cloud/snapCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrations.lua
More file actions
184 lines (169 loc) · 6.41 KB
/
migrations.lua
File metadata and controls
184 lines (169 loc) · 6.41 KB
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
-- Database migrations
-- ===================
--
-- Run migrations by running bin/migrations.sh
--
-- Do not modify a migration once it has been run or commited!
-- To change what a migration does, create a new one.
--
-- Add a new migration with the key 'YYYY-MM-DD:X'
-- Where X is a value [0-9]
-- NOTES:
-- use _at for timestamps, and always add { timezone = true }
--
-- Written by Bernat Romagosa and Michael Ball
--
-- Copyright (C) 2019 by Bernat Romagosa and Michael Ball
--
-- This file is part of Snap Cloud.
--
-- Snap Cloud is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local db = require("lapis.db")
local schema = require("lapis.db.schema")
local types = schema.types
-- You MUST recreate the views modifying the users table.
local update_user_views = function ()
db.query([[
CREATE OR REPLACE VIEW active_users AS (
SELECT * FROM users WHERE deleted is null
)
]])
db.query([[
CREATE OR REPLACE VIEW deleted_users AS (
SELECT * FROM users WHERE deleted is not null
)
]])
end
local update_project_views = function ()
db.query([[
CREATE OR REPLACE VIEW active_projects AS (
SELECT * FROM projects WHERE deleted is null
)
]])
db.query([[
CREATE OR REPLACE VIEW deleted_projects AS (
SELECT * FROM projects WHERE deleted is not null
)
]])
end
return {
-- TODO: We will eventually create migrations for the other tables.
-- Create Collections and CollectionMemberships
['2019-01-04:0'] = function ()
schema.create_table("collections", {
{ 'id', types.serial({ primary_key = true }) },
{ 'name', types.text },
{ 'creator_id', types.foreign_key },
{ 'created_at', types.time({ timezone = true }) },
{ 'updated_at', types.time({ timezone = true }) },
{ 'description', types.text({ null = true }) },
{ 'published', types.boolean },
{ 'published_at', types.time({ timezone = true, null = true }) },
{ 'shared', types.boolean },
{ 'shared_at', types.time({ timezone = true, null = true }) },
{ 'thumbnail_id', types.foreign_key({ null = true }) }
})
schema.create_index('collections', 'creator_id')
schema.create_table("collection_memberships", {
{ 'id', types.serial({ primary_key = true }) },
{ 'collection_id', types.foreign_key },
{ 'project_id', types.foreign_key },
{ 'created_at', types.time({ timezone = true }) },
{ 'updated_at', types.time({ timezone = true }) }
})
schema.create_index('collection_memberships', 'collection_id')
schema.create_index('collection_memberships', 'project_id')
end,
-- Update CollectionMemberships to store a user
['2019-01-29:0'] = function ()
schema.add_column(
'collection_memberships', 'user_id', types.foreign_key
)
schema.create_index('collection_memberships',
'collection_id', 'project_id', 'user_id',
{ unique = true })
end,
-- Create and views for handling deleted items.
['2019-02-01:0'] = function ()
schema.add_column('users',
'deleted',
types.time({ timezone = true, null = true }))
update_user_views()
schema.add_column('projects',
'deleted',
types.time({ timezone = true, null = true }))
update_project_views()
end,
-- Add an editor_ids[] field to collections
['2019-02-04:0'] = function ()
schema.add_column(
'collections',
'editor_ids',
types.foreign_key({ array = true, null = true })
)
end,
-- Add a table to store spambot IPs and ban them
['2019-02-05:0'] = function ()
schema.create_table("banned_ips", {
{ 'ip', types.text({ primary_key = true }) },
{ 'created_at', types.time({ timezone = true }) },
{ 'updated_at', types.time({ timezone = true }) },
{ 'offense_count', types.integer, { default = 1 } }
})
end,
['2020-10-22:0'] = function ()
schema.add_column('users', 'unique_email', types.text({ null = true, unique = true }))
-- We use an index on *non-unique* emails to be able to search related accounts.
-- We will rarely query by unique_email and thus no index is necessary.
schema.create_index('users', 'email', { unique = false })
update_user_views()
end,
-- Update Collections to include a "free for all" flag
['2020-11-03:0'] = function ()
schema.add_column(
'collections',
'free_for_all',
types.boolean
)
-- The "flagged" collection is free for all
db.update(
'collections',
{ free_for_all = true },
{ id = 0 }
)
end,
-- Create a FlaggedProjects table
['2020-11-09:0'] = function ()
schema.create_table("flagged_projects", {
{ 'id', types.serial({ primary_key = true }) },
{ 'flagger_id', types.foreign_key },
{ 'project_id', types.foreign_key },
{ 'reason', types.text },
{ 'created_at', types.time({ timezone = true }) },
{ 'updated_at', types.time({ timezone = true }) }
})
-- One flag per user and project
schema.create_index('flagged_projects',
'flagger_id', 'project_id',
{ unique = true })
end,
-- Add a notes column to FlaggedProjects
['2020-11-10:0'] = function ()
schema.add_column(
'flagged_projects',
'notes',
types.text({ null = true })
)
end
}