1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { ServerInfo } from '../server/servers'
4 const sequelizes: { [ id: number ]: Sequelize } = {}
6 function getSequelize (internalServerNumber: number) {
7 if (sequelizes[internalServerNumber]) return sequelizes[internalServerNumber]
9 const dbname = 'peertube_test' + internalServerNumber
10 const username = 'peertube'
11 const password = 'peertube'
12 const host = process.env.GITLAB_CI ? 'postgres' : 'localhost'
15 const seq = new Sequelize(dbname, username, password, {
22 sequelizes[internalServerNumber] = seq
27 function setActorField (internalServerNumber: number, to: string, field: string, value: string) {
28 const seq = getSequelize(internalServerNumber)
30 const options = { type: QueryTypes.UPDATE }
32 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
35 function setVideoField (internalServerNumber: number, uuid: string, field: string, value: string) {
36 const seq = getSequelize(internalServerNumber)
38 const options = { type: QueryTypes.UPDATE }
40 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
43 function setPlaylistField (internalServerNumber: number, uuid: string, field: string, value: string) {
44 const seq = getSequelize(internalServerNumber)
46 const options = { type: QueryTypes.UPDATE }
48 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
51 async function countVideoViewsOf (internalServerNumber: number, uuid: string) {
52 const seq = getSequelize(internalServerNumber)
55 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
56 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
58 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
59 const [ { total } ] = await seq.query<{ total: number }>(query, options)
63 return parseInt(total + '', 10)
66 async function closeAllSequelize (servers: ServerInfo[]) {
67 for (const server of servers) {
68 if (sequelizes[server.internalServerNumber]) {
69 await sequelizes[server.internalServerNumber].close()
70 // eslint-disable-next-line
71 delete sequelizes[server.internalServerNumber]
76 function setPluginVersion (internalServerNumber: number, pluginName: string, newVersion: string) {
77 const seq = getSequelize(internalServerNumber)
79 const options = { type: QueryTypes.UPDATE }
81 return seq.query(`UPDATE "plugin" SET "version" = '${newVersion}' WHERE "name" = '${pluginName}'`, options)
84 function setActorFollowScores (internalServerNumber: number, newScore: number) {
85 const seq = getSequelize(internalServerNumber)
87 const options = { type: QueryTypes.UPDATE }
89 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)