add error management

This commit is contained in:
Pierre Noël 2022-11-18 23:57:46 +01:00
parent 11d6f7803c
commit 0c76c5ae1a
5 changed files with 42 additions and 16 deletions

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { MARIADB, MYSQL } = require('../../utils/constants'); const { MARIADB, MYSQL, UNKNOWN } = require('../../utils/constants');
const SQL_QUERIES = { const SQL_QUERIES = {
VERSION: `SELECT version() as version`, VERSION: `SELECT version() as version`,
@ -12,11 +12,20 @@ class MysqlDatabaseInspector {
} }
async getInformation() { async getInformation() {
const [results] = await this.db.connection.raw(SQL_QUERIES.VERSION); let database;
const version = results[0].version; let versionNumber;
try {
const [versionNumber, databaseName] = version.split('-'); const [results] = await this.db.connection.raw(SQL_QUERIES.VERSION);
const database = databaseName && databaseName.toLowerCase() === 'mariadb' ? MARIADB : MYSQL; const versionSplit = results[0].version.split('-');
const databaseName = versionSplit[1];
versionNumber = versionSplit[0];
database = databaseName && databaseName.toLowerCase() === 'mariadb' ? MARIADB : MYSQL;
throw new Error('oups');
} catch (e) {
database = UNKNOWN;
versionNumber = UNKNOWN;
strapi.log.warn(`Database version couldn't be retrieved: ${e.message}`);
}
return { return {
database, database,

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { POSTGRES } = require('../../utils/constants'); const { POSTGRES, UNKNOWN } = require('../../utils/constants');
const SQL_QUERIES = { const SQL_QUERIES = {
VERSION: `SELECT current_setting('server_version') as version`, VERSION: `SELECT current_setting('server_version') as version`,
@ -12,12 +12,18 @@ class PostgresqlDatabaseInspector {
} }
async getInformation() { async getInformation() {
const { rows } = await this.db.connection.raw(SQL_QUERIES.VERSION); let version;
const version = rows[0].version; try {
const { rows } = await this.db.connection.raw(SQL_QUERIES.VERSION);
version = rows[0].version.split(' ')[0];
} catch (e) {
version = UNKNOWN;
strapi.log.warn(`Database version couldn't be retrieved: ${e.message}`);
}
return { return {
database: POSTGRES, database: POSTGRES,
version: version.split(' ')[0], version,
}; };
} }
} }

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { SQLITE } = require('../../utils/constants'); const { SQLITE, UNKNOWN } = require('../../utils/constants');
const SQL_QUERIES = { const SQL_QUERIES = {
VERSION: `SELECT sqlite_version() as version`, VERSION: `SELECT sqlite_version() as version`,
@ -12,9 +12,14 @@ class SqliteDatabaseInspector {
} }
async getInformation() { async getInformation() {
const results = await this.db.connection.raw(SQL_QUERIES.VERSION); let version;
const version = results[0].version; try {
const results = await this.db.connection.raw(SQL_QUERIES.VERSION);
version = results[0].version;
} catch (e) {
version = UNKNOWN;
strapi.log.warn(`Database version couldn't be retrieved: ${e.message}`);
}
return { return {
database: SQLITE, database: SQLITE,
version, version,

View File

@ -2,7 +2,7 @@
const { map, isEmpty } = require('lodash/fp'); const { map, isEmpty } = require('lodash/fp');
const semver = require('semver'); const semver = require('semver');
const { MYSQL } = require('../utils/constants'); const { MYSQL, UNKNOWN } = require('../utils/constants');
const { const {
isBidirectional, isBidirectional,
isOneToAny, isOneToAny,
@ -199,7 +199,12 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
// Handle databases that don't support window function ROW_NUMBER // Handle databases that don't support window function ROW_NUMBER
const { database, version } = strapi.db.getDatabaseInformation(); const { database, version } = strapi.db.getDatabaseInformation();
if (database === MYSQL && semver.lt(version, '8.0.0')) { if (
strapi.db.dialect.client === 'mysql' &&
[MYSQL, UNKNOWN].includes(database) &&
(!semver.valid(version) || semver.lt(version, '8.0.0'))
) {
console.log('DEDANS');
await cleanOrderColumnsForOldDatabases({ id, attribute, db, inverseRelIds, transaction: trx }); await cleanOrderColumnsForOldDatabases({ id, attribute, db, inverseRelIds, transaction: trx });
return; return;
} }

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
module.exports = { module.exports = {
UNKNOWN: 'unknown',
SQLITE: 'SQLite', SQLITE: 'SQLite',
POSTGRES: 'Postgres', POSTGRES: 'Postgres',
MYSQL: 'MySQL', MYSQL: 'MySQL',