]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/initializers/database.ts
update tslint config and fix member ordering (#1279)
[github/Chocobozzz/PeerTube.git] / server / initializers / database.ts
CommitLineData
3fd3ab2d
C
1import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
2import { isTestInstance } from '../helpers/core-utils'
3import { logger } from '../helpers/logger'
8c308c2b 4
3fd3ab2d 5import { AccountModel } from '../models/account/account'
3fd3ab2d
C
6import { AccountVideoRateModel } from '../models/account/account-video-rate'
7import { UserModel } from '../models/account/user'
50d6de9c
C
8import { ActorModel } from '../models/activitypub/actor'
9import { ActorFollowModel } from '../models/activitypub/actor-follow'
3fd3ab2d
C
10import { ApplicationModel } from '../models/application/application'
11import { AvatarModel } from '../models/avatar/avatar'
3fd3ab2d
C
12import { OAuthClientModel } from '../models/oauth/oauth-client'
13import { OAuthTokenModel } from '../models/oauth/oauth-token'
14import { ServerModel } from '../models/server/server'
15import { TagModel } from '../models/video/tag'
16import { VideoModel } from '../models/video/video'
17import { VideoAbuseModel } from '../models/video/video-abuse'
18import { VideoBlacklistModel } from '../models/video/video-blacklist'
19import { VideoChannelModel } from '../models/video/video-channel'
6d852470 20import { VideoCommentModel } from '../models/video/video-comment'
3fd3ab2d
C
21import { VideoFileModel } from '../models/video/video-file'
22import { VideoShareModel } from '../models/video/video-share'
23import { VideoTagModel } from '../models/video/video-tag'
65fcc311 24import { CONFIG } from './constants'
2baea0c7 25import { ScheduleVideoUpdateModel } from '../models/video/schedule-video-update'
40e87e9e 26import { VideoCaptionModel } from '../models/video/video-caption'
fbad87b0 27import { VideoImportModel } from '../models/video/video-import'
6b616860 28import { VideoViewModel } from '../models/video/video-views'
74d63469 29import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership'
c48e82b5 30import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
6e46de09 31import { UserVideoHistoryModel } from '../models/account/user-video-history'
fdbda9e3 32
3fd3ab2d 33require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
8c308c2b 34
65fcc311
C
35const dbname = CONFIG.DATABASE.DBNAME
36const username = CONFIG.DATABASE.USERNAME
37const password = CONFIG.DATABASE.PASSWORD
228077ef
C
38const host = CONFIG.DATABASE.HOSTNAME
39const port = CONFIG.DATABASE.PORT
1c3386e8 40const poolMax = CONFIG.DATABASE.POOL.MAX
8c308c2b 41
3fd3ab2d
C
42const sequelizeTypescript = new SequelizeTypescript({
43 database: dbname,
feb4bdfd 44 dialect: 'postgres',
228077ef
C
45 host,
46 port,
3fd3ab2d
C
47 username,
48 password,
1c3386e8
RK
49 pool: {
50 max: poolMax
51 },
65fcc311 52 benchmark: isTestInstance(),
3fd3ab2d 53 isolationLevel: SequelizeTypescript.Transaction.ISOLATION_LEVELS.SERIALIZABLE,
c2962505 54 operatorsAliases: false,
075f16ca 55 logging: (message: string, benchmark: number) => {
165cdc75
C
56 if (process.env.NODE_DB_LOG === 'false') return
57
7920c273 58 let newMessage = message
769d3321 59 if (isTestInstance() === true && benchmark !== undefined) {
7920c273
C
60 newMessage += ' | ' + benchmark + 'ms'
61 }
62
63 logger.debug(newMessage)
64 }
feb4bdfd
C
65})
66
91fea9fc 67async function initDatabaseModels (silent: boolean) {
3fd3ab2d
C
68 sequelizeTypescript.addModels([
69 ApplicationModel,
50d6de9c
C
70 ActorModel,
71 ActorFollowModel,
3fd3ab2d
C
72 AvatarModel,
73 AccountModel,
3fd3ab2d
C
74 OAuthClientModel,
75 OAuthTokenModel,
76 ServerModel,
77 TagModel,
78 AccountVideoRateModel,
3fd3ab2d
C
79 UserModel,
80 VideoAbuseModel,
74d63469 81 VideoChangeOwnershipModel,
3fd3ab2d 82 VideoChannelModel,
3fd3ab2d
C
83 VideoShareModel,
84 VideoFileModel,
40e87e9e 85 VideoCaptionModel,
3fd3ab2d
C
86 VideoBlacklistModel,
87 VideoTagModel,
6d852470 88 VideoModel,
2baea0c7 89 VideoCommentModel,
fbad87b0 90 ScheduleVideoUpdateModel,
6b616860 91 VideoImportModel,
c48e82b5 92 VideoViewModel,
6e46de09
C
93 VideoRedundancyModel,
94 UserVideoHistoryModel
3fd3ab2d 95 ])
b769007f 96
57c36b27
C
97 // Check extensions exist in the database
98 await checkPostgresExtensions()
99
100 // Create custom PostgreSQL functions
101 await createFunctions()
102
f5028693
C
103 if (!silent) logger.info('Database %s is ready.', dbname)
104
d412e80e 105 return
b769007f 106}
65fcc311
C
107
108// ---------------------------------------------------------------------------
109
e02643f3 110export {
91fea9fc 111 initDatabaseModels,
3fd3ab2d 112 sequelizeTypescript
74889a71 113}
57c36b27
C
114
115// ---------------------------------------------------------------------------
116
117async function checkPostgresExtensions () {
118 const extensions = [
119 'pg_trgm',
120 'unaccent'
121 ]
122
123 for (const extension of extensions) {
124 const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
125 const [ res ] = await sequelizeTypescript.query(query, { raw: true })
126
127 if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
128 // Try to create the extension ourself
129 try {
130 await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
131
132 } catch {
133 const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
134 `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
135 throw new Error(errorMessage)
136 }
137 }
138 }
139}
140
141async function createFunctions () {
2cebd797
C
142 const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
143 RETURNS text AS
144$func$
145SELECT public.unaccent('public.unaccent', $1::text)
146$func$ LANGUAGE sql IMMUTABLE;`
57c36b27
C
147
148 return sequelizeTypescript.query(query, { raw: true })
149}