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