diff options
Diffstat (limited to 'shared/extra-utils/miscs/sql.ts')
-rw-r--r-- | shared/extra-utils/miscs/sql.ts | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/shared/extra-utils/miscs/sql.ts b/shared/extra-utils/miscs/sql.ts index 3cfae5c23..34477cb78 100644 --- a/shared/extra-utils/miscs/sql.ts +++ b/shared/extra-utils/miscs/sql.ts | |||
@@ -1,11 +1,12 @@ | |||
1 | import { QueryTypes, Sequelize } from 'sequelize' | 1 | import { QueryTypes, Sequelize } from 'sequelize' |
2 | import { ServerInfo } from '../server/servers' | ||
2 | 3 | ||
3 | let sequelizes: { [ id: number ]: Sequelize } = {} | 4 | let sequelizes: { [ id: number ]: Sequelize } = {} |
4 | 5 | ||
5 | function getSequelize (serverNumber: number) { | 6 | function getSequelize (internalServerNumber: number) { |
6 | if (sequelizes[serverNumber]) return sequelizes[serverNumber] | 7 | if (sequelizes[internalServerNumber]) return sequelizes[internalServerNumber] |
7 | 8 | ||
8 | const dbname = 'peertube_test' + serverNumber | 9 | const dbname = 'peertube_test' + internalServerNumber |
9 | const username = 'peertube' | 10 | const username = 'peertube' |
10 | const password = 'peertube' | 11 | const password = 'peertube' |
11 | const host = 'localhost' | 12 | const host = 'localhost' |
@@ -18,37 +19,37 @@ function getSequelize (serverNumber: number) { | |||
18 | logging: false | 19 | logging: false |
19 | }) | 20 | }) |
20 | 21 | ||
21 | sequelizes[serverNumber] = seq | 22 | sequelizes[internalServerNumber] = seq |
22 | 23 | ||
23 | return seq | 24 | return seq |
24 | } | 25 | } |
25 | 26 | ||
26 | function setActorField (serverNumber: number, to: string, field: string, value: string) { | 27 | function setActorField (internalServerNumber: number, to: string, field: string, value: string) { |
27 | const seq = getSequelize(serverNumber) | 28 | const seq = getSequelize(internalServerNumber) |
28 | 29 | ||
29 | const options = { type: QueryTypes.UPDATE } | 30 | const options = { type: QueryTypes.UPDATE } |
30 | 31 | ||
31 | return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options) | 32 | return seq.query(`UPDATE actor SET "${field}" = '${value}' WHERE url = '${to}'`, options) |
32 | } | 33 | } |
33 | 34 | ||
34 | function setVideoField (serverNumber: number, uuid: string, field: string, value: string) { | 35 | function setVideoField (internalServerNumber: number, uuid: string, field: string, value: string) { |
35 | const seq = getSequelize(serverNumber) | 36 | const seq = getSequelize(internalServerNumber) |
36 | 37 | ||
37 | const options = { type: QueryTypes.UPDATE } | 38 | const options = { type: QueryTypes.UPDATE } |
38 | 39 | ||
39 | return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | 40 | return seq.query(`UPDATE video SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) |
40 | } | 41 | } |
41 | 42 | ||
42 | function setPlaylistField (serverNumber: number, uuid: string, field: string, value: string) { | 43 | function setPlaylistField (internalServerNumber: number, uuid: string, field: string, value: string) { |
43 | const seq = getSequelize(serverNumber) | 44 | const seq = getSequelize(internalServerNumber) |
44 | 45 | ||
45 | const options = { type: QueryTypes.UPDATE } | 46 | const options = { type: QueryTypes.UPDATE } |
46 | 47 | ||
47 | return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) | 48 | return seq.query(`UPDATE "videoPlaylist" SET "${field}" = '${value}' WHERE uuid = '${uuid}'`, options) |
48 | } | 49 | } |
49 | 50 | ||
50 | async function countVideoViewsOf (serverNumber: number, uuid: string) { | 51 | async function countVideoViewsOf (internalServerNumber: number, uuid: string) { |
51 | const seq = getSequelize(serverNumber) | 52 | const seq = getSequelize(internalServerNumber) |
52 | 53 | ||
53 | // tslint:disable | 54 | // tslint:disable |
54 | const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` | 55 | const query = `SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = '${uuid}'` |
@@ -62,11 +63,11 @@ async function countVideoViewsOf (serverNumber: number, uuid: string) { | |||
62 | return parseInt(total + '', 10) | 63 | return parseInt(total + '', 10) |
63 | } | 64 | } |
64 | 65 | ||
65 | async function closeAllSequelize (servers: any[]) { | 66 | async function closeAllSequelize (servers: ServerInfo[]) { |
66 | for (let i = 1; i <= servers.length; i++) { | 67 | for (const server of servers) { |
67 | if (sequelizes[ i ]) { | 68 | if (sequelizes[ server.internalServerNumber ]) { |
68 | await sequelizes[ i ].close() | 69 | await sequelizes[ server.internalServerNumber ].close() |
69 | delete sequelizes[ i ] | 70 | delete sequelizes[ server.internalServerNumber ] |
70 | } | 71 | } |
71 | } | 72 | } |
72 | } | 73 | } |