]>
Commit | Line | Data |
---|---|---|
1735c825 | 1 | import { QueryTypes, Sequelize } from 'sequelize' |
48f07b4a | 2 | import { ServerInfo } from '../server/servers' |
df66d815 | 3 | |
a1587156 | 4 | const sequelizes: { [ id: number ]: Sequelize } = {} |
c8000975 | 5 | |
48f07b4a C |
6 | function getSequelize (internalServerNumber: number) { |
7 | if (sequelizes[internalServerNumber]) return sequelizes[internalServerNumber] | |
c8000975 | 8 | |
48f07b4a | 9 | const dbname = 'peertube_test' + internalServerNumber |
df66d815 C |
10 | const username = 'peertube' |
11 | const password = 'peertube' | |
2284f202 | 12 | const host = process.env.GITLAB_CI ? 'postgres' : 'localhost' |
df66d815 C |
13 | const port = 5432 |
14 | ||
c8000975 | 15 | const seq = new Sequelize(dbname, username, password, { |
df66d815 C |
16 | dialect: 'postgres', |
17 | host, | |
18 | port, | |
df66d815 C |
19 | logging: false |
20 | }) | |
c8000975 | 21 | |
48f07b4a | 22 | sequelizes[internalServerNumber] = seq |
c8000975 C |
23 | |
24 | return seq | |
df66d815 C |
25 | } |
26 | ||
48f07b4a C |
27 | function setActorField (internalServerNumber: number, to: string, field: string, value: string) { |
28 | const seq = getSequelize(internalServerNumber) | |
df66d815 | 29 | |
1735c825 | 30 | const options = { type: QueryTypes.UPDATE } |
df66d815 C |
31 | |
32 | return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options) | |
33 | } | |
34 | ||
48f07b4a C |
35 | function setVideoField (internalServerNumber: number, uuid: string, field: string, value: string) { |
36 | const seq = getSequelize(internalServerNumber) | |
a130f33c | 37 | |
1735c825 | 38 | const options = { type: QueryTypes.UPDATE } |
a130f33c C |
39 | |
40 | return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
41 | } | |
42 | ||
48f07b4a C |
43 | function setPlaylistField (internalServerNumber: number, uuid: string, field: string, value: string) { |
44 | const seq = getSequelize(internalServerNumber) | |
9f79ade6 | 45 | |
1735c825 | 46 | const options = { type: QueryTypes.UPDATE } |
9f79ade6 C |
47 | |
48 | return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | |
49 | } | |
50 | ||
48f07b4a C |
51 | async function countVideoViewsOf (internalServerNumber: number, uuid: string) { |
52 | const seq = getSequelize(internalServerNumber) | |
cda03765 C |
53 | |
54 | // tslint:disable | |
a1587156 C |
55 | const query = 'SELECT SUM("videoView"."views") AS "total" FROM "videoView" ' + |
56 | `INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` | |
cda03765 | 57 | |
1735c825 C |
58 | const options = { type: QueryTypes.SELECT as QueryTypes.SELECT } |
59 | const [ { total } ] = await seq.query<{ total: number }>(query, options) | |
cda03765 C |
60 | |
61 | if (!total) return 0 | |
62 | ||
1735c825 | 63 | return parseInt(total + '', 10) |
cda03765 C |
64 | } |
65 | ||
48f07b4a C |
66 | async function closeAllSequelize (servers: ServerInfo[]) { |
67 | for (const server of servers) { | |
a1587156 C |
68 | if (sequelizes[server.internalServerNumber]) { |
69 | await sequelizes[server.internalServerNumber].close() | |
70 | // eslint-disable-next-line | |
71 | delete sequelizes[server.internalServerNumber] | |
c8000975 C |
72 | } |
73 | } | |
74 | } | |
75 | ||
09071200 C |
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 | ||
6b9c966f C |
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 | ||
df66d815 | 92 | export { |
a130f33c | 93 | setVideoField, |
9f79ade6 | 94 | setPlaylistField, |
c8000975 | 95 | setActorField, |
cda03765 | 96 | countVideoViewsOf, |
09071200 | 97 | setPluginVersion, |
6b9c966f | 98 | setActorFollowScores, |
c8000975 | 99 | closeAllSequelize |
df66d815 | 100 | } |