-function getModelFiles (modelDirectory: string) {
- return readdirPromise(modelDirectory)
- .then(files => {
- const directories: string[] = files.filter(function (directory) {
- // Find directories
- if (
- directory.endsWith('.js.map') ||
- directory === 'index.js' || directory === 'index.ts' ||
- directory === 'utils.js' || directory === 'utils.ts'
- ) return false
-
- return true
- })
-
- return directories
- })
- .then(directories => {
- const tasks = []
-
- // For each directory we read it and append model in the modelFilePaths array
- directories.forEach(directory => {
- const modelDirectoryPath = join(modelDirectory, directory)
-
- const promise = readdirPromise(modelDirectoryPath).then(files => {
- const filteredFiles = files.filter(file => {
- if (
- file === 'index.js' || file === 'index.ts' ||
- file === 'utils.js' || file === 'utils.ts' ||
- file.endsWith('-interface.js') || file.endsWith('-interface.ts') ||
- file.endsWith('.js.map')
- ) return false
-
- return true
- }).map(file => join(modelDirectoryPath, file))
-
- return filteredFiles
- })
-
- tasks.push(promise)
- })
-
- return Promise.all(tasks)
- })
- .then((filteredFiles: string[][]) => {
- return flattenDepth<string>(filteredFiles, 1)
- })
+async function checkPostgresExtensions () {
+ const extensions = [
+ 'pg_trgm',
+ 'unaccent'
+ ]
+
+ for (const extension of extensions) {
+ const query = `SELECT true AS enabled FROM pg_available_extensions WHERE name = '${extension}' AND installed_version IS NOT NULL;`
+ const [ res ] = await sequelizeTypescript.query(query, { raw: true })
+
+ if (!res || res.length === 0 || res[ 0 ][ 'enabled' ] !== true) {
+ // Try to create the extension ourself
+ try {
+ await sequelizeTypescript.query(`CREATE EXTENSION ${extension};`, { raw: true })
+
+ } catch {
+ const errorMessage = `You need to enable ${extension} extension in PostgreSQL. ` +
+ `You can do so by running 'CREATE EXTENSION ${extension};' as a PostgreSQL super user in ${CONFIG.DATABASE.DBNAME} database.`
+ throw new Error(errorMessage)
+ }
+ }
+ }
+}
+
+async function createFunctions () {
+ const query = `CREATE OR REPLACE FUNCTION immutable_unaccent(text)
+ RETURNS text AS
+$func$
+SELECT public.unaccent('public.unaccent', $1::text)
+$func$ LANGUAGE sql IMMUTABLE;`
+
+ return sequelizeTypescript.query(query, { raw: true })