diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-19 16:17:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-24 14:04:05 +0200 |
commit | 57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6 (patch) | |
tree | 87ebcd623c06445b9b25a237addefc98a2d64afa /server/initializers/database.ts | |
parent | 7279b455811f4806dcb74a08d17b837bc22533c1 (diff) | |
download | PeerTube-57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6.tar.gz PeerTube-57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6.tar.zst PeerTube-57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6.zip |
Begin advanced search
Diffstat (limited to 'server/initializers/database.ts')
-rw-r--r-- | server/initializers/database.ts | 43 |
1 files changed, 43 insertions, 0 deletions
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 | } | ||