Add migrations
[oweals/peertube.git] / server / initializers / migrations / 0370-thumbnail.ts
1 import * as Sequelize from 'sequelize'
2
3 async function up (utils: {
4   transaction: Sequelize.Transaction,
5   queryInterface: Sequelize.QueryInterface,
6   sequelize: Sequelize.Sequelize,
7   db: any
8 }): Promise<void> {
9   {
10     const query = `
11 CREATE TABLE IF NOT EXISTS "thumbnail"
12 (
13   "id"              SERIAL,
14   "filename"        VARCHAR(255)             NOT NULL,
15   "height"          INTEGER DEFAULT NULL,
16   "width"           INTEGER DEFAULT NULL,
17   "type"            INTEGER                  NOT NULL,
18   "fileUrl"             VARCHAR(255),
19   "videoId"         INTEGER REFERENCES "video" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
20   "videoPlaylistId" INTEGER REFERENCES "videoPlaylist" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
21   "createdAt"       TIMESTAMP WITH TIME ZONE NOT NULL,
22   "updatedAt"       TIMESTAMP WITH TIME ZONE NOT NULL,
23   PRIMARY KEY ("id")
24 );`
25     await utils.sequelize.query(query)
26   }
27
28   {
29     // All video thumbnails
30     const query = 'INSERT INTO "thumbnail" ("filename", "type", "videoId", "height", "width", "createdAt", "updatedAt")' +
31       'SELECT uuid || \'.jpg\', 1, id, 110, 200, NOW(), NOW() FROM "video"'
32     await utils.sequelize.query(query)
33   }
34
35   {
36     // All video previews
37     const query = 'INSERT INTO "thumbnail" ("filename", "type", "videoId", "height", "width", "createdAt", "updatedAt")' +
38       'SELECT uuid || \'.jpg\', 2, id, 315, 560, NOW(), NOW() FROM "video"'
39     await utils.sequelize.query(query)
40   }
41 }
42
43 function down (options) {
44   throw new Error('Not implemented.')
45 }
46
47 export {
48   up,
49   down
50 }