diff options
Diffstat (limited to 'server/initializers')
-rw-r--r-- | server/initializers/constants.ts | 4 | ||||
-rw-r--r-- | server/initializers/database.ts | 43 |
2 files changed, 46 insertions, 1 deletions
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index ba48399de..b966c0acb 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts | |||
@@ -35,7 +35,9 @@ const SORTABLE_COLUMNS = { | |||
35 | VIDEO_COMMENT_THREADS: [ 'createdAt' ], | 35 | VIDEO_COMMENT_THREADS: [ 'createdAt' ], |
36 | BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ], | 36 | BLACKLISTS: [ 'id', 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid', 'createdAt' ], |
37 | FOLLOWERS: [ 'createdAt' ], | 37 | FOLLOWERS: [ 'createdAt' ], |
38 | FOLLOWING: [ 'createdAt' ] | 38 | FOLLOWING: [ 'createdAt' ], |
39 | |||
40 | VIDEOS_SEARCH: [ 'bestmatch', 'name', 'duration', 'createdAt', 'publishedAt', 'views', 'likes' ] | ||
39 | } | 41 | } |
40 | 42 | ||
41 | const OAUTH_LIFETIME = { | 43 | const OAUTH_LIFETIME = { |
diff --git a/server/initializers/database.ts b/server/initializers/database.ts index 434d7ef19..045f41a96 100644 --- a/server/initializers/database.ts +++ b/server/initializers/database.ts | |||
@@ -80,6 +80,14 @@ async function initDatabaseModels (silent: boolean) { | |||
80 | ScheduleVideoUpdateModel | 80 | ScheduleVideoUpdateModel |
81 | ]) | 81 | ]) |
82 | 82 | ||
83 | // Check extensions exist in the database | ||
84 | await checkPostgresExtensions() | ||
85 | |||
86 | // Create custom PostgreSQL functions | ||
87 | await createFunctions() | ||
88 | |||
89 | await sequelizeTypescript.query('CREATE EXTENSION IF NOT EXISTS pg_trgm', { raw: true }) | ||
90 | |||
83 | if (!silent) logger.info('Database %s is ready.', dbname) | 91 | if (!silent) logger.info('Database %s is ready.', dbname) |
84 | 92 | ||
85 | return | 93 | return |
@@ -91,3 +99,38 @@ export { | |||
91 | initDatabaseModels, | 99 | initDatabaseModels, |
92 | sequelizeTypescript | 100 | sequelizeTypescript |
93 | } | 101 | } |
102 | |||
103 | // --------------------------------------------------------------------------- | ||
104 | |||
105 | async function checkPostgresExtensions () { | ||
106 | const extensions = [ | ||
107 | 'pg_trgm', | ||
108 | 'unaccent' | ||
109 | ] | ||
110 | |||
111 | for (const extension of extensions) { | ||
112 | const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;` | ||
113 | const [ res ] = await sequelizeTypescript.query(query, { raw: true }) | ||
114 | |||
115 | if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) { | ||
116 | // Try to create the extension ourself | ||
117 | try { | ||
118 | await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true }) | ||
119 | |||
120 | } catch { | ||
121 | const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` + | ||
122 | `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.` | ||
123 | throw new Error(errorMessage) | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | async function createFunctions () { | ||
130 | const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(varchar) | ||
131 | RETURNS text AS $$ | ||
132 | SELECT unaccent($1) | ||
133 | $$ LANGUAGE sql IMMUTABLE;` | ||
134 | |||
135 | return sequelizeTypescript.query(query, { raw: true }) | ||
136 | } | ||