]>
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 C |
50 | const host = CONFIG.DATABASE.HOSTNAME |
51 | const port = CONFIG.DATABASE.PORT | |
1c3386e8 | 52 | const poolMax = CONFIG.DATABASE.POOL.MAX |
8c308c2b | 53 | |
3fd3ab2d C |
54 | const sequelizeTypescript = new SequelizeTypescript({ |
55 | database: dbname, | |
feb4bdfd | 56 | dialect: 'postgres', |
228077ef C |
57 | host, |
58 | port, | |
3fd3ab2d C |
59 | username, |
60 | password, | |
1c3386e8 RK |
61 | pool: { |
62 | max: poolMax | |
63 | }, | |
65fcc311 | 64 | benchmark: isTestInstance(), |
1735c825 | 65 | isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE, |
075f16ca | 66 | logging: (message: string, benchmark: number) => { |
165cdc75 C |
67 | if (process.env.NODE_DB_LOG === 'false') return |
68 | ||
7920c273 | 69 | let newMessage = message |
769d3321 | 70 | if (isTestInstance() === true && benchmark !== undefined) { |
7920c273 C |
71 | newMessage += ' | ' + benchmark + 'ms' |
72 | } | |
73 | ||
74 | logger.debug(newMessage) | |
75 | } | |
feb4bdfd C |
76 | }) |
77 | ||
91fea9fc | 78 | async function initDatabaseModels (silent: boolean) { |
3fd3ab2d C |
79 | sequelizeTypescript.addModels([ |
80 | ApplicationModel, | |
50d6de9c C |
81 | ActorModel, |
82 | ActorFollowModel, | |
3fd3ab2d C |
83 | AvatarModel, |
84 | AccountModel, | |
3fd3ab2d C |
85 | OAuthClientModel, |
86 | OAuthTokenModel, | |
87 | ServerModel, | |
88 | TagModel, | |
89 | AccountVideoRateModel, | |
3fd3ab2d | 90 | UserModel, |
edbc9325 | 91 | AbuseMessageModel, |
d95d1559 C |
92 | AbuseModel, |
93 | VideoCommentAbuseModel, | |
3fd3ab2d | 94 | VideoAbuseModel, |
b876eaf1 | 95 | VideoModel, |
74d63469 | 96 | VideoChangeOwnershipModel, |
3fd3ab2d | 97 | VideoChannelModel, |
3fd3ab2d C |
98 | VideoShareModel, |
99 | VideoFileModel, | |
40e87e9e | 100 | VideoCaptionModel, |
3fd3ab2d C |
101 | VideoBlacklistModel, |
102 | VideoTagModel, | |
2baea0c7 | 103 | VideoCommentModel, |
fbad87b0 | 104 | ScheduleVideoUpdateModel, |
6b616860 | 105 | VideoImportModel, |
c48e82b5 | 106 | VideoViewModel, |
6e46de09 | 107 | VideoRedundancyModel, |
7ad9b984 C |
108 | UserVideoHistoryModel, |
109 | AccountBlocklistModel, | |
cef534ed C |
110 | ServerBlocklistModel, |
111 | UserNotificationModel, | |
09209296 | 112 | UserNotificationSettingModel, |
418d092a C |
113 | VideoStreamingPlaylistModel, |
114 | VideoPlaylistModel, | |
e8bafea3 | 115 | VideoPlaylistElementModel, |
f023a19c C |
116 | ThumbnailModel, |
117 | PluginModel | |
3fd3ab2d | 118 | ]) |
b769007f | 119 | |
57c36b27 C |
120 | // Check extensions exist in the database |
121 | await checkPostgresExtensions() | |
122 | ||
123 | // Create custom PostgreSQL functions | |
124 | await createFunctions() | |
125 | ||
f5028693 | 126 | if (!silent) logger.info('Database %s is ready.', dbname) |
b769007f | 127 | } |
65fcc311 C |
128 | |
129 | // --------------------------------------------------------------------------- | |
130 | ||
e02643f3 | 131 | export { |
91fea9fc | 132 | initDatabaseModels, |
3fd3ab2d | 133 | sequelizeTypescript |
74889a71 | 134 | } |
57c36b27 C |
135 | |
136 | // --------------------------------------------------------------------------- | |
137 | ||
138 | async function checkPostgresExtensions () { | |
0b2f03d3 C |
139 | const promises = [ |
140 | checkPostgresExtension('pg_trgm'), | |
141 | checkPostgresExtension('unaccent') | |
57c36b27 C |
142 | ] |
143 | ||
0b2f03d3 C |
144 | return Promise.all(promises) |
145 | } | |
146 | ||
147 | async function checkPostgresExtension (extension: string) { | |
3acc5084 | 148 | const query = `SELECT 1 FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;` |
1735c825 C |
149 | const options = { |
150 | type: QueryTypes.SELECT as QueryTypes.SELECT, | |
151 | raw: true | |
152 | } | |
153 | ||
3acc5084 | 154 | const res = await sequelizeTypescript.query<object>(query, options) |
57c36b27 | 155 | |
3acc5084 | 156 | if (!res || res.length === 0) { |
1735c825 | 157 | // Try to create the extension ourselves |
0b2f03d3 C |
158 | try { |
159 | await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true }) | |
57c36b27 | 160 | |
0b2f03d3 C |
161 | } catch { |
162 | const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` + | |
163 | `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.` | |
164 | throw new Error(errorMessage) | |
57c36b27 C |
165 | } |
166 | } | |
167 | } | |
168 | ||
f0ad4710 | 169 | function createFunctions () { |
2cebd797 C |
170 | const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text) |
171 | RETURNS text AS | |
172 | $func$ | |
173 | SELECT public.unaccent('public.unaccent', $1::text) | |
174 | $func$ LANGUAGE sql IMMUTABLE;` | |
57c36b27 C |
175 | |
176 | return sequelizeTypescript.query(query, { raw: true }) | |
177 | } |