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