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