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;