Merge branch 'release/1.4.0' into develop
[oweals/peertube.git] / server / initializers / database.ts
1 import { Sequelize as SequelizeTypescript } from 'sequelize-typescript'
2 import { isTestInstance } from '../helpers/core-utils'
3 import { logger } from '../helpers/logger'
4
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 './config'
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'
28 import { VideoViewModel } from '../models/video/video-views'
29 import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership'
30 import { VideoRedundancyModel } from '../models/redundancy/video-redundancy'
31 import { UserVideoHistoryModel } from '../models/account/user-video-history'
32 import { AccountBlocklistModel } from '../models/account/account-blocklist'
33 import { ServerBlocklistModel } from '../models/server/server-blocklist'
34 import { UserNotificationModel } from '../models/account/user-notification'
35 import { UserNotificationSettingModel } from '../models/account/user-notification-setting'
36 import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
37 import { VideoPlaylistModel } from '../models/video/video-playlist'
38 import { VideoPlaylistElementModel } from '../models/video/video-playlist-element'
39 import { ThumbnailModel } from '../models/video/thumbnail'
40 import { PluginModel } from '../models/server/plugin'
41 import { QueryTypes, Transaction } from 'sequelize'
42
43 require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string
44
45 const dbname = CONFIG.DATABASE.DBNAME
46 const username = CONFIG.DATABASE.USERNAME
47 const password = CONFIG.DATABASE.PASSWORD
48 const host = CONFIG.DATABASE.HOSTNAME
49 const port = CONFIG.DATABASE.PORT
50 const poolMax = CONFIG.DATABASE.POOL.MAX
51
52 const sequelizeTypescript = new SequelizeTypescript({
53   database: dbname,
54   dialect: 'postgres',
55   host,
56   port,
57   username,
58   password,
59   pool: {
60     max: poolMax
61   },
62   benchmark: isTestInstance(),
63   isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE,
64   logging: (message: string, benchmark: number) => {
65     if (process.env.NODE_DB_LOG === 'false') return
66
67     let newMessage = message
68     if (isTestInstance() === true && benchmark !== undefined) {
69       newMessage += ' | ' + benchmark + 'ms'
70     }
71
72     logger.debug(newMessage)
73   }
74 })
75
76 async function initDatabaseModels (silent: boolean) {
77   sequelizeTypescript.addModels([
78     ApplicationModel,
79     ActorModel,
80     ActorFollowModel,
81     AvatarModel,
82     AccountModel,
83     OAuthClientModel,
84     OAuthTokenModel,
85     ServerModel,
86     TagModel,
87     AccountVideoRateModel,
88     UserModel,
89     VideoAbuseModel,
90     VideoModel,
91     VideoChangeOwnershipModel,
92     VideoChannelModel,
93     VideoShareModel,
94     VideoFileModel,
95     VideoCaptionModel,
96     VideoBlacklistModel,
97     VideoTagModel,
98     VideoCommentModel,
99     ScheduleVideoUpdateModel,
100     VideoImportModel,
101     VideoViewModel,
102     VideoRedundancyModel,
103     UserVideoHistoryModel,
104     AccountBlocklistModel,
105     ServerBlocklistModel,
106     UserNotificationModel,
107     UserNotificationSettingModel,
108     VideoStreamingPlaylistModel,
109     VideoPlaylistModel,
110     VideoPlaylistElementModel,
111     ThumbnailModel,
112     PluginModel
113   ])
114
115   // Check extensions exist in the database
116   await checkPostgresExtensions()
117
118   // Create custom PostgreSQL functions
119   await createFunctions()
120
121   if (!silent) logger.info('Database %s is ready.', dbname)
122
123   return
124 }
125
126 // ---------------------------------------------------------------------------
127
128 export {
129   initDatabaseModels,
130   sequelizeTypescript
131 }
132
133 // ---------------------------------------------------------------------------
134
135 async function checkPostgresExtensions () {
136   const promises = [
137     checkPostgresExtension('pg_trgm'),
138     checkPostgresExtension('unaccent')
139   ]
140
141   return Promise.all(promises)
142 }
143
144 async function checkPostgresExtension (extension: string) {
145   const query = `SELECT 1 FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
146   const options = {
147     type: QueryTypes.SELECT as QueryTypes.SELECT,
148     raw: true
149   }
150
151   const res = await sequelizeTypescript.query<object>(query, options)
152
153   if (!res || res.length === 0) {
154     // Try to create the extension ourselves
155     try {
156       await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
157
158     } catch {
159       const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
160         `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
161       throw new Error(errorMessage)
162     }
163   }
164 }
165
166 async function createFunctions () {
167   const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
168   RETURNS text AS
169 $func$
170 SELECT public.unaccent('public.unaccent', $1::text)
171 $func$  LANGUAGE sql IMMUTABLE;`
172
173   return sequelizeTypescript.query(query, { raw: true })
174 }