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