aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-03-06 11:32:53 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-03-18 11:17:59 +0100
commitd4c9f45b31eda0b7a391ddc83eb290ca5cba311f (patch)
tree657a47aafc71f67d86718d2de8d4c0f878367331 /server/initializers/migrations
parent9c6ca37fc1512a99d420ea90707cebcd06cdc970 (diff)
downloadPeerTube-d4c9f45b31eda0b7a391ddc83eb290ca5cba311f.tar.gz
PeerTube-d4c9f45b31eda0b7a391ddc83eb290ca5cba311f.tar.zst
PeerTube-d4c9f45b31eda0b7a391ddc83eb290ca5cba311f.zip
Add server migrations
Diffstat (limited to 'server/initializers/migrations')
-rw-r--r--server/initializers/migrations/0345-video-playlists.ts86
1 files changed, 86 insertions, 0 deletions
diff --git a/server/initializers/migrations/0345-video-playlists.ts b/server/initializers/migrations/0345-video-playlists.ts
new file mode 100644
index 000000000..11670b11d
--- /dev/null
+++ b/server/initializers/migrations/0345-video-playlists.ts
@@ -0,0 +1,86 @@
1import * as Sequelize from 'sequelize'
2import { CONFIG } from '../constants'
3import { VideoPlaylistPrivacy, VideoPlaylistType } from '../../../shared/models/videos'
4import * as uuidv4 from 'uuid/v4'
5
6async function up (utils: {
7 transaction: Sequelize.Transaction,
8 queryInterface: Sequelize.QueryInterface,
9 sequelize: Sequelize.Sequelize
10}): Promise<void> {
11 const transaction = utils.transaction
12
13 {
14 const query = `
15CREATE TABLE IF NOT EXISTS "videoPlaylist"
16(
17 "id" SERIAL,
18 "name" VARCHAR(255) NOT NULL,
19 "description" VARCHAR(255),
20 "privacy" INTEGER NOT NULL,
21 "url" VARCHAR(2000) NOT NULL,
22 "uuid" UUID NOT NULL,
23 "type" INTEGER NOT NULL DEFAULT 1,
24 "ownerAccountId" INTEGER NOT NULL REFERENCES "account" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
25 "videoChannelId" INTEGER REFERENCES "videoChannel" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
26 "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
27 "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
28 PRIMARY KEY ("id")
29);`
30 await utils.sequelize.query(query, { transaction })
31 }
32
33 {
34 const query = `
35CREATE TABLE IF NOT EXISTS "videoPlaylistElement"
36(
37 "id" SERIAL,
38 "url" VARCHAR(2000) NOT NULL,
39 "position" INTEGER NOT NULL DEFAULT 1,
40 "startTimestamp" INTEGER,
41 "stopTimestamp" INTEGER,
42 "videoPlaylistId" INTEGER NOT NULL REFERENCES "videoPlaylist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
43 "videoId" INTEGER NOT NULL REFERENCES "video" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
44 "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
45 "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
46 PRIMARY KEY ("id")
47);`
48
49 await utils.sequelize.query(query, { transaction })
50 }
51
52 {
53 const userQuery = 'SELECT "username" FROM "user";'
54 const userResult = await utils.sequelize.query(userQuery, { transaction, type: Sequelize.QueryTypes.SELECT })
55 const usernames = userResult.map(r => r.username)
56
57 for (const username of usernames) {
58 const uuid = uuidv4()
59
60 const baseUrl = CONFIG.WEBSERVER.URL + '/video-playlists/' + uuid
61 const query = `
62 INSERT INTO "videoPlaylist" ("url", "uuid", "name", "privacy", "type", "ownerAccountId", "createdAt", "updatedAt")
63 SELECT '${baseUrl}' AS "url",
64 '${uuid}' AS "uuid",
65 'Watch later' AS "name",
66 ${VideoPlaylistPrivacy.PRIVATE} AS "privacy",
67 ${VideoPlaylistType.WATCH_LATER} AS "type",
68 "account"."id" AS "ownerAccountId",
69 NOW() as "createdAt",
70 NOW() as "updatedAt"
71 FROM "user" INNER JOIN "account" ON "user"."id" = "account"."userId"
72 WHERE "user"."username" = '${username}'`
73
74 await utils.sequelize.query(query, { transaction })
75 }
76 }
77}
78
79function down (options) {
80 throw new Error('Not implemented.')
81}
82
83export {
84 up,
85 down
86}