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