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