1 import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
5 import { AccountModel } from '../models/account/account'
6 import { AccountVideoRateModel } from '../models/account/account-video-rate'
7 import { UserModel } from '../models/account/user'
8 import { ActorModel } from '../models/activitypub/actor'
9 import { ActorFollowModel } from '../models/activitypub/actor-follow'
10 import { ApplicationModel } from '../models/application/application'
11 import { AvatarModel } from '../models/avatar/avatar'
12 import { OAuthClientModel } from '../models/oauth/oauth-client'
13 import { OAuthTokenModel } from '../models/oauth/oauth-token'
14 import { ServerModel } from '../models/server/server'
15 import { TagModel } from '../models/video/tag'
16 import { VideoModel } from '../models/video/video'
17 import { VideoAbuseModel } from '../models/video/video-abuse'
18 import { VideoBlacklistModel } from '../models/video/video-blacklist'
19 import { VideoChannelModel } from '../models/video/video-channel'
20 import { VideoCommentModel } from '../models/video/video-comment'
21 import { VideoFileModel } from '../models/video/video-file'
22 import { VideoShareModel } from '../models/video/video-share'
23 import { VideoTagModel } from '../models/video/video-tag'
24 import { CONFIG } from './constants'
25 import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update'
26 import { VideoCaptionModel } from '../models/video/video-caption'
27 import { VideoImportModel } from '../models/video/video-import'
29 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
31 const dbname = CONFIG.DATABASE.DBNAME
32 const username = CONFIG.DATABASE.USERNAME
33 const password = CONFIG.DATABASE.PASSWORD
34 const host = CONFIG.DATABASE.HOSTNAME
35 const port = CONFIG.DATABASE.PORT
36 const poolMax = CONFIG.DATABASE.POOL.MAX
38 const sequelizeTypescript = new SequelizeTypescript({
48 benchmark: isTestInstance(),
49 isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
50 operatorsAliases: false,
51 logging: (message: string, benchmark: number) => {
52 if (process.env.NODE_DB_LOG === 'false') return
54 let newMessage = message
55 if (isTestInstance() === true && benchmark !== undefined) {
56 newMessage += ' | ' + benchmark + 'ms'
59 logger.debug(newMessage)
63 async function initDatabaseModels (silent: boolean) {
64 sequelizeTypescript.addModels([
74 AccountVideoRateModel,
85 ScheduleVideoUpdateModel,
89 // Check extensions exist in the database
90 await checkPostgresExtensions()
92 // Create custom PostgreSQL functions
93 await createFunctions()
95 if (!silent) logger.info('Database %s is ready.', dbname)
100 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 async function checkPostgresExtensions () {
115 for (const extension of extensions) {
116 const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
117 const [ res ] = await sequelizeTypescript.query(query, { raw: true })
119 if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
120 // Try to create the extension ourself
122 await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
125 const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
126 `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
127 throw new Error(errorMessage)
133 async function createFunctions () {
134 const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
137 SELECT public.unaccent('public.unaccent', $1::text)
138 $func$ LANGUAGE sql IMMUTABLE;`
140 return sequelizeTypescript.query(query, { raw: true })