forked from cosyverif/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrations.lua
More file actions
144 lines (142 loc) · 6.03 KB
/
migrations.lua
File metadata and controls
144 lines (142 loc) · 6.03 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
local Schema = require "lapis.db.schema"
local Database = require "lapis.db"
local identifier = [[ bigint PRIMARY KEY DEFAULT nextval('identifiers') ]]
local foreign = [[ bigint NOT NULL ]]
local foreign_null = [[ bigint DEFAULT NULL ]]
local identity = [[ identity NOT NULL ]]
local permission = [[ permission NOT NULL ]]
return {
function ()
Database.query [[
CREATE SEQUENCE identifiers
]]
end,
function ()
Database.query [[
CREATE TYPE identity AS ENUM ('user', 'project')
]]
Schema.create_table ("identities", {
{ "identifier", Schema.types.text { null = true } },
{ "id" , [[ bigint UNIQUE NOT NULL ]] },
{ "type" , identity },
})
end,
function ()
Schema.create_table ("users", {
{ "id" , identifier },
{ "path" , Schema.types.text { null = true } },
{ "email" , Schema.types.text { null = true } },
{ "name" , Schema.types.text { null = true } },
{ "picture" , Schema.types.text { null = true } },
{ "nickname" , Schema.types.text },
{ "reputation", Schema.types.integer },
{ "created_at", Schema.types.time },
{ "updated_at", Schema.types.time },
})
end,
function ()
Database.query [[
CREATE TYPE permission AS ENUM ('none', 'read', 'write', 'admin')
]]
Schema.create_table ("projects", {
{ "id" , identifier },
{ "path" , Schema.types.text { null = true } },
{ "name" , Schema.types.text { null = true } },
{ "description" , Schema.types.text { null = true } },
{ "permission_anonymous", permission },
{ "permission_user" , permission },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
})
Schema.create_table ("tags", {
{ "id" , Schema.types.text },
{ "user_id" , foreign },
{ "project_id", foreign },
{ "created_at", Schema.types.time },
{ "updated_at", Schema.types.time },
[[ PRIMARY KEY ("id", "user_id", "project_id") ]],
[[ FOREIGN KEY ("user_id" ) REFERENCES "users" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ]],
})
Schema.create_table ("stars", {
{ "user_id" , foreign },
{ "project_id", foreign },
{ "created_at", Schema.types.time },
{ "updated_at", Schema.types.time },
[[ PRIMARY KEY ("user_id", "project_id") ]],
[[ FOREIGN KEY ("user_id" ) REFERENCES "users" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ]],
})
end,
function ()
Schema.create_table ("permissions", {
{ "identity_id", foreign },
{ "project_id" , foreign },
{ "permission" , permission },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
[[ PRIMARY KEY ("identity_id", "project_id") ]],
[[ FOREIGN KEY ("project_id" ) REFERENCES "projects" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("identity_id") REFERENCES "identities" ("id") ON DELETE CASCADE ]],
})
end,
function ()
Schema.create_table ("services", {
{ "id" , identifier },
{ "launched" , Schema.types.boolean { default = false } },
{ "path" , Schema.types.text },
{ "docker_url", Schema.types.text { null = true } },
{ "editor_url", Schema.types.text { null = true } },
{ "qless_job" , Schema.types.text { null = true } },
{ "created_at", Schema.types.time },
{ "updated_at", Schema.types.time },
})
end,
function ()
Schema.create_table ("resources", {
{ "id" , identifier },
{ "path" , Schema.types.text { null = true } },
{ "project_id" , foreign },
{ "name" , Schema.types.text { null = true } },
{ "description" , Schema.types.text { null = true } },
{ "service_id" , foreign_null },
{ "data" , Schema.types.text },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
[[ FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("service_id") REFERENCES "services" ("id") ON DELETE SET NULL ]],
})
Schema.create_table ("histories", {
{ "id" , identifier },
{ "user_id" , foreign },
{ "resource_id", foreign },
{ "data" , Schema.types.text },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
[[ FOREIGN KEY ("user_id" ) REFERENCES "users" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("resource_id") REFERENCES "resources" ("id") ON DELETE CASCADE ]],
})
Schema.create_table ("aliases", {
{ "id" , Schema.types.text { primary_key = true } },
{ "resource_id", foreign },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
[[ FOREIGN KEY ("resource_id") REFERENCES "resources" ("id") ON DELETE CASCADE ]],
})
end,
function ()
Schema.create_table ("executions", {
{ "id" , identifier },
{ "resource_id" , foreign },
{ "image" , Schema.types.text },
{ "path" , Schema.types.text { null = true } },
{ "name" , Schema.types.text { null = true } },
{ "description" , Schema.types.text { null = true } },
{ "service_id" , foreign_null },
{ "created_at" , Schema.types.time },
{ "updated_at" , Schema.types.time },
[[ FOREIGN KEY ("resource_id") REFERENCES "resources" ("id") ON DELETE CASCADE ]],
[[ FOREIGN KEY ("service_id" ) REFERENCES "services" ("id") ON DELETE SET NULL ]],
})
end,
}