fix(MySQL): creating temporary tables on export to overcome view dependency errors

This commit is contained in:
Fabio Di Stasio 2022-05-06 17:20:06 +02:00
parent bfdb463390
commit e4c5d9b404
3 changed files with 28 additions and 4 deletions

View File

@ -58,7 +58,7 @@ module.exports = [
},
{
name: 'DOUBLE',
length: true,
length: false,
collation: false,
unsigned: false,
zerofill: false

View File

@ -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];

View File

@ -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;