diff options
Diffstat (limited to 'server/initializers/database.ts')
-rw-r--r-- | server/initializers/database.ts | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 482c03b31..fe296142d 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts | |||
@@ -29,6 +29,11 @@ import { VideoViewModel } from '../models/video/video-views' | |||
29 | import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership' | 29 | import { VideoChangeOwnershipModel } from '../models/video/video-change-ownership' |
30 | import { VideoRedundancyModel } from '../models/redundancy/video-redundancy' | 30 | import { VideoRedundancyModel } from '../models/redundancy/video-redundancy' |
31 | import { UserVideoHistoryModel } from '../models/account/user-video-history' | 31 | import { UserVideoHistoryModel } from '../models/account/user-video-history' |
32 | import { AccountBlocklistModel } from '../models/account/account-blocklist' | ||
33 | import { ServerBlocklistModel } from '../models/server/server-blocklist' | ||
34 | import { UserNotificationModel } from '../models/account/user-notification' | ||
35 | import { UserNotificationSettingModel } from '../models/account/user-notification-setting' | ||
36 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | ||
32 | 37 | ||
33 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string | 38 | require('pg').defaults.parseInt8 = true // Avoid BIGINT to be converted to string |
34 | 39 | ||
@@ -91,7 +96,12 @@ async function initDatabaseModels (silent: boolean) { | |||
91 | VideoImportModel, | 96 | VideoImportModel, |
92 | VideoViewModel, | 97 | VideoViewModel, |
93 | VideoRedundancyModel, | 98 | VideoRedundancyModel, |
94 | UserVideoHistoryModel | 99 | UserVideoHistoryModel, |
100 | AccountBlocklistModel, | ||
101 | ServerBlocklistModel, | ||
102 | UserNotificationModel, | ||
103 | UserNotificationSettingModel, | ||
104 | VideoStreamingPlaylistModel | ||
95 | ]) | 105 | ]) |
96 | 106 | ||
97 | // Check extensions exist in the database | 107 | // Check extensions exist in the database |
@@ -115,25 +125,27 @@ export { | |||
115 | // --------------------------------------------------------------------------- | 125 | // --------------------------------------------------------------------------- |
116 | 126 | ||
117 | async function checkPostgresExtensions () { | 127 | async function checkPostgresExtensions () { |
118 | const extensions = [ | 128 | const promises = [ |
119 | 'pg_trgm', | 129 | checkPostgresExtension('pg_trgm'), |
120 | 'unaccent' | 130 | checkPostgresExtension('unaccent') |
121 | ] | 131 | ] |
122 | 132 | ||
123 | for (const extension of extensions) { | 133 | return Promise.all(promises) |
124 | const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;` | 134 | } |
125 | const [ res ] = await sequelizeTypescript.query(query, { raw: true }) | 135 | |
136 | async function checkPostgresExtension (extension: string) { | ||
137 | const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;` | ||
138 | const [ res ] = await sequelizeTypescript.query(query, { raw: true }) | ||
126 | 139 | ||
127 | if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) { | 140 | if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) { |
128 | // Try to create the extension ourself | 141 | // Try to create the extension ourself |
129 | try { | 142 | try { |
130 | await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true }) | 143 | await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true }) |
131 | 144 | ||
132 | } catch { | 145 | } catch { |
133 | const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` + | 146 | const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` + |
134 | `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.` | 147 | `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.` |
135 | throw new Error(errorMessage) | 148 | throw new Error(errorMessage) |
136 | } | ||
137 | } | 149 | } |
138 | } | 150 | } |
139 | } | 151 | } |