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