From e4c5d9b404c2b20cf113607a80ee928b810e6f05 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Fri, 6 May 2022 17:20:06 +0200 Subject: [PATCH] fix(MySQL): creating temporary tables on export to overcome view dependency errors --- src/common/data-types/mysql.js | 2 +- src/main/libs/clients/MySQLClient.ts | 2 +- src/main/libs/exporters/sql/MysqlExporter.ts | 28 ++++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/common/data-types/mysql.js b/src/common/data-types/mysql.js index 14b61229..f2e3a1b6 100644 --- a/src/common/data-types/mysql.js +++ b/src/common/data-types/mysql.js @@ -58,7 +58,7 @@ module.exports = [ }, { name: 'DOUBLE', - length: true, + length: false, collation: false, unsigned: false, zerofill: false diff --git a/src/main/libs/clients/MySQLClient.ts b/src/main/libs/clients/MySQLClient.ts index 3d12d8c9..6f49ccca 100644 --- a/src/main/libs/clients/MySQLClient.ts +++ b/src/main/libs/clients/MySQLClient.ts @@ -102,7 +102,7 @@ export class MySQLClient extends AntaresCore { return { name, length }; } - private _getTypeInfo (type: string) { + _getTypeInfo (type: string) { return dataTypes .reduce((acc, group) => [...acc, ...group.types], []) .filter((_type) => _type.name === type.toUpperCase())[0]; diff --git a/src/main/libs/exporters/sql/MysqlExporter.ts b/src/main/libs/exporters/sql/MysqlExporter.ts index ce87db0b..9c97f5f6 100644 --- a/src/main/libs/exporters/sql/MysqlExporter.ts +++ b/src/main/libs/exporters/sql/MysqlExporter.ts @@ -197,11 +197,35 @@ ${footer} ); let sqlString = ''; + sqlString += this.buildComment('Creating temporary tables to overcome VIEW dependency errors'); + + // Temporary tables for (const view of views) { - sqlString += `DROP VIEW IF EXISTS \`${view.Name}\`;\n`; + const viewFields = await this._client.getTableColumns({ schema: this.schemaName, table: view.Name }); + const tableFields: string[] = []; + + for (const field of viewFields) { + const typeInfo = this._client._getTypeInfo(field.type); + const length = typeInfo.length ? field.enumValues || field.numLength || field.charLength || field.datePrecision : false; + + tableFields.push(`\`${field.name}\` ${field.type.toUpperCase()}${length ? `(${length}${field.numScale ? `,${field.numScale}` : ''})` : ''} ${field.unsigned ? 'UNSIGNED' : ''} ${field.zerofill ? 'ZEROFILL' : ''} ${field.nullable ? 'NULL' : 'NOT NULL'} ${field.autoIncrement ? 'AUTO_INCREMENT' : ''} ${field.collation ? `COLLATE ${field.collation}` : ''}`); + } + sqlString += +` +CREATE TABLE \`${view.Name}\`( + ${tableFields.join(',\n\t')} +);`; + + sqlString += '\n'; + } + + sqlString += '\n'; + + for (const view of views) { + sqlString += `DROP TABLE IF EXISTS \`${view.Name}\`;\n`; const viewSyntax = await this.getCreateTable(view.Name); sqlString += viewSyntax.replaceAll('`' + this.schemaName + '`.', ''); - sqlString += '\n'; + sqlString += '\n\n'; } return sqlString;