PeerTube/server/initializers/database.ts

142 lines
4.5 KiB
TypeScript
Raw Normal View History

2017-12-12 19:53:50 +03:00
import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
import { isTestInstance } from '../helpers/core-utils'
import { logger } from '../helpers/logger'
2015-06-09 18:41:40 +03:00
2017-12-12 19:53:50 +03:00
import { AccountModel } from '../models/account/account'
import { AccountVideoRateModel } from '../models/account/account-video-rate'
import { UserModel } from '../models/account/user'
2017-12-14 19:38:41 +03:00
import { ActorModel } from '../models/activitypub/actor'
import { ActorFollowModel } from '../models/activitypub/actor-follow'
2017-12-12 19:53:50 +03:00
import { ApplicationModel } from '../models/application/application'
import { AvatarModel } from '../models/avatar/avatar'
import { OAuthClientModel } from '../models/oauth/oauth-client'
import { OAuthTokenModel } from '../models/oauth/oauth-token'
import { ServerModel } from '../models/server/server'
import { TagModel } from '../models/video/tag'
import { VideoModel } from '../models/video/video'
import { VideoAbuseModel } from '../models/video/video-abuse'
import { VideoBlacklistModel } from '../models/video/video-blacklist'
import { VideoChannelModel } from '../models/video/video-channel'
import { VideoCommentModel } from '../models/video/video-comment'
2017-12-12 19:53:50 +03:00
import { VideoFileModel } from '../models/video/video-file'
import { VideoShareModel } from '../models/video/video-share'
import { VideoTagModel } from '../models/video/video-tag'
2017-05-15 23:22:03 +03:00
import { CONFIG } from './constants'
import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update'
2018-07-12 20:02:00 +03:00
import { VideoCaptionModel } from '../models/video/video-caption'
import { VideoImportModel } from '../models/video/video-import'
2017-09-07 16:27:35 +03:00
2017-12-12 19:53:50 +03:00
require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
2015-06-09 18:41:40 +03:00
2017-05-15 23:22:03 +03:00
const dbname = CONFIG.DATABASE.DBNAME
const username = CONFIG.DATABASE.USERNAME
const password = CONFIG.DATABASE.PASSWORD
2017-12-15 10:21:00 +03:00
const host = CONFIG.DATABASE.HOSTNAME
const port = CONFIG.DATABASE.PORT
2018-07-28 22:02:26 +03:00
const poolMax = CONFIG.DATABASE.POOL.MAX
2015-06-09 18:41:40 +03:00
2017-12-12 19:53:50 +03:00
const sequelizeTypescript = new SequelizeTypescript({
database: dbname,
2016-12-11 23:50:51 +03:00
dialect: 'postgres',
2017-12-15 10:21:00 +03:00
host,
port,
2017-12-12 19:53:50 +03:00
username,
password,
2018-07-28 22:02:26 +03:00
pool: {
max: poolMax
},
2017-05-15 23:22:03 +03:00
benchmark: isTestInstance(),
2017-12-12 19:53:50 +03:00
isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
2017-10-26 17:59:02 +03:00
operatorsAliases: false,
2017-07-11 18:04:57 +03:00
logging: (message: string, benchmark: number) => {
if (process.env.NODE_DB_LOG === 'false') return
2016-12-24 18:59:17 +03:00
let newMessage = message
2017-10-10 10:00:50 +03:00
if (isTestInstance() === true && benchmark !== undefined) {
2016-12-24 18:59:17 +03:00
newMessage += ' | ' + benchmark + 'ms'
}
logger.debug(newMessage)
}
2016-12-11 23:50:51 +03:00
})
2017-12-13 19:46:23 +03:00
async function initDatabaseModels (silent: boolean) {
2017-12-12 19:53:50 +03:00
sequelizeTypescript.addModels([
ApplicationModel,
2017-12-14 19:38:41 +03:00
ActorModel,
ActorFollowModel,
2017-12-12 19:53:50 +03:00
AvatarModel,
AccountModel,
OAuthClientModel,
OAuthTokenModel,
ServerModel,
TagModel,
AccountVideoRateModel,
UserModel,
VideoAbuseModel,
VideoChannelModel,
VideoShareModel,
VideoFileModel,
2018-07-12 20:02:00 +03:00
VideoCaptionModel,
2017-12-12 19:53:50 +03:00
VideoBlacklistModel,
VideoTagModel,
VideoModel,
VideoCommentModel,
ScheduleVideoUpdateModel,
VideoImportModel
2017-12-12 19:53:50 +03:00
])
2016-12-25 11:44:57 +03:00
2018-07-19 17:17:54 +03:00
// Check extensions exist in the database
await checkPostgresExtensions()
// Create custom PostgreSQL functions
await createFunctions()
if (!silent) logger.info('Database %s is ready.', dbname)
2017-10-25 17:52:01 +03:00
return
2016-12-25 11:44:57 +03:00
}
2017-05-15 23:22:03 +03:00
// ---------------------------------------------------------------------------
2017-05-22 21:58:25 +03:00
export {
2017-12-13 19:46:23 +03:00
initDatabaseModels,
2017-12-12 19:53:50 +03:00
sequelizeTypescript
2017-06-16 10:45:46 +03:00
}
2018-07-19 17:17:54 +03:00
// ---------------------------------------------------------------------------
async function checkPostgresExtensions () {
const extensions = [
'pg_trgm',
'unaccent'
]
for (const extension of extensions) {
const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
const [ res ] = await sequelizeTypescript.query(query, { raw: true })
if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
// Try to create the extension ourself
try {
await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
} catch {
const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
`You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
throw new Error(errorMessage)
}
}
}
}
async function createFunctions () {
2018-07-26 11:45:10 +03:00
const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
RETURNS text AS
$func$
SELECT public.unaccent('public.unaccent', $1::text)
$func$ LANGUAGE sql IMMUTABLE;`
2018-07-19 17:17:54 +03:00
return sequelizeTypescript.query(query, { raw: true })
}