2019-04-18 04:28:17 -05:00
import { QueryTypes , Sequelize } from 'sequelize'
2019-04-26 01:50:52 -05:00
import { ServerInfo } from '../server/servers'
2018-10-23 04:38:48 -05:00
2019-04-18 04:28:17 -05:00
let sequelizes : { [ id : number ] : Sequelize } = { }
2019-02-21 08:44:12 -06:00
2019-04-26 01:50:52 -05:00
function getSequelize ( internalServerNumber : number ) {
if ( sequelizes [ internalServerNumber ] ) return sequelizes [ internalServerNumber ]
2019-02-21 08:44:12 -06:00
2019-04-26 01:50:52 -05:00
const dbname = 'peertube_test' + internalServerNumber
2018-10-23 04:38:48 -05:00
const username = 'peertube'
const password = 'peertube'
2019-07-29 04:59:29 -05:00
const host = process . env . GITLAB_CI ? 'postgres' : 'localhost'
2018-10-23 04:38:48 -05:00
const port = 5432
2019-02-21 08:44:12 -06:00
const seq = new Sequelize ( dbname , username , password , {
2018-10-23 04:38:48 -05:00
dialect : 'postgres' ,
host ,
port ,
logging : false
} )
2019-02-21 08:44:12 -06:00
2019-04-26 01:50:52 -05:00
sequelizes [ internalServerNumber ] = seq
2019-02-21 08:44:12 -06:00
return seq
2018-10-23 04:38:48 -05:00
}
2019-04-26 01:50:52 -05:00
function setActorField ( internalServerNumber : number , to : string , field : string , value : string ) {
const seq = getSequelize ( internalServerNumber )
2018-10-23 04:38:48 -05:00
2019-04-18 04:28:17 -05:00
const options = { type : QueryTypes . UPDATE }
2018-10-23 04:38:48 -05:00
return seq . query ( ` UPDATE actor SET " ${ field } " = ' ${ value } ' WHERE url = ' ${ to } ' ` , options )
}
2019-04-26 01:50:52 -05:00
function setVideoField ( internalServerNumber : number , uuid : string , field : string , value : string ) {
const seq = getSequelize ( internalServerNumber )
2018-11-14 09:32:12 -06:00
2019-04-18 04:28:17 -05:00
const options = { type : QueryTypes . UPDATE }
2018-11-14 09:32:12 -06:00
return seq . query ( ` UPDATE video SET " ${ field } " = ' ${ value } ' WHERE uuid = ' ${ uuid } ' ` , options )
}
2019-04-26 01:50:52 -05:00
function setPlaylistField ( internalServerNumber : number , uuid : string , field : string , value : string ) {
const seq = getSequelize ( internalServerNumber )
2019-03-19 08:13:53 -05:00
2019-04-18 04:28:17 -05:00
const options = { type : QueryTypes . UPDATE }
2019-03-19 08:13:53 -05:00
return seq . query ( ` UPDATE "videoPlaylist" SET " ${ field } " = ' ${ value } ' WHERE uuid = ' ${ uuid } ' ` , options )
}
2019-04-26 01:50:52 -05:00
async function countVideoViewsOf ( internalServerNumber : number , uuid : string ) {
const seq = getSequelize ( internalServerNumber )
2019-04-11 10:33:36 -05:00
// tslint:disable
const query = ` SELECT SUM("videoView"."views") AS "total" FROM "videoView" INNER JOIN "video" ON "video"."id" = "videoView"."videoId" WHERE "video"."uuid" = ' ${ uuid } ' `
2019-04-18 04:28:17 -05:00
const options = { type : QueryTypes . SELECT as QueryTypes . SELECT }
const [ { total } ] = await seq . query < { total : number } > ( query , options )
2019-04-11 10:33:36 -05:00
if ( ! total ) return 0
2019-04-18 04:28:17 -05:00
// FIXME: check if we really need parseInt
return parseInt ( total + '' , 10 )
2019-04-11 10:33:36 -05:00
}
2019-04-26 01:50:52 -05:00
async function closeAllSequelize ( servers : ServerInfo [ ] ) {
for ( const server of servers ) {
if ( sequelizes [ server . internalServerNumber ] ) {
await sequelizes [ server . internalServerNumber ] . close ( )
delete sequelizes [ server . internalServerNumber ]
2019-02-21 08:44:12 -06:00
}
}
}
2019-07-19 07:36:04 -05:00
function setPluginVersion ( internalServerNumber : number , pluginName : string , newVersion : string ) {
const seq = getSequelize ( internalServerNumber )
const options = { type : QueryTypes . UPDATE }
return seq . query ( ` UPDATE "plugin" SET "version" = ' ${ newVersion } ' WHERE "name" = ' ${ pluginName } ' ` , options )
}
2018-10-23 04:38:48 -05:00
export {
2018-11-14 09:32:12 -06:00
setVideoField ,
2019-03-19 08:13:53 -05:00
setPlaylistField ,
2019-02-21 08:44:12 -06:00
setActorField ,
2019-04-11 10:33:36 -05:00
countVideoViewsOf ,
2019-07-19 07:36:04 -05:00
setPluginVersion ,
2019-02-21 08:44:12 -06:00
closeAllSequelize
2018-10-23 04:38:48 -05:00
}