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