]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/miscs/sql.ts
Translated using Weblate (Hungarian)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / sql.ts
1 import { QueryTypes, Sequelize } from 'sequelize'
2 import { ServerInfo } from '../server/servers'
3
4 const sequelizes: { [ id: number ]: Sequelize } = {}
5
6 function getSequelize (internalServerNumber: number) {
7 if (sequelizes[internalServerNumber]) return sequelizes[internalServerNumber]
8
9 const dbname = 'peertube_test' + internalServerNumber
10 const username = 'peertube'
11 const password = 'peertube'
12 const host = process.env.GITLAB_CI ? 'postgres' : 'localhost'
13 const port = 5432
14
15 const seq = new Sequelize(dbname, username, password, {
16 dialect: 'postgres',
17 host,
18 port,
19 logging: false
20 })
21
22 sequelizes[internalServerNumber] = seq
23
24 return seq
25 }
26
27 function setActorField (internalServerNumber: number, to: string, field: string, value: string) {
28 const seq = getSequelize(internalServerNumber)
29
30 const options = { type: QueryTypes.UPDATE }
31
32 return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options)
33 }
34
35 function setVideoField (internalServerNumber: number, uuid: string, field: string, value: string) {
36 const seq = getSequelize(internalServerNumber)
37
38 const options = { type: QueryTypes.UPDATE }
39
40 return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
41 }
42
43 function setPlaylistField (internalServerNumber: number, uuid: string, field: string, value: string) {
44 const seq = getSequelize(internalServerNumber)
45
46 const options = { type: QueryTypes.UPDATE }
47
48 return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options)
49 }
50
51 async function countVideoViewsOf (internalServerNumber: number, uuid: string) {
52 const seq = getSequelize(internalServerNumber)
53
54 // tslint:disable
55 const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' +
56 `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'`
57
58 const options = { type: QueryTypes.SELECT as QueryTypes.SELECT }
59 const [ { total } ] = await seq.query<{ total: number }>(query, options)
60
61 if (!total) return 0
62
63 return parseInt(total + '', 10)
64 }
65
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]
72 }
73 }
74 }
75
76 function setPluginVersion (internalServerNumber: number, pluginName: string, newVersion: string) {
77 const seq = getSequelize(internalServerNumber)
78
79 const options = { type: QueryTypes.UPDATE }
80
81 return seq.query(`UPDATE "plugin" SET "version" = '${newVersion}' WHERE "name" = '${pluginName}'`, options)
82 }
83
84 function setActorFollowScores (internalServerNumber: number, newScore: number) {
85 const seq = getSequelize(internalServerNumber)
86
87 const options = { type: QueryTypes.UPDATE }
88
89 return seq.query(`UPDATE "actorFollow" SET "score" = ${newScore}`, options)
90 }
91
92 export {
93 setVideoField,
94 setPlaylistField,
95 setActorField,
96 countVideoViewsOf,
97 setPluginVersion,
98 setActorFollowScores,
99 closeAllSequelize
100 }