From 2cb3a6b446f3bc56d5f2a345f271a6c7a09246d5 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 5 Sep 2021 09:44:40 +0200 Subject: [PATCH] create table dumper --- .../__tests__/table-create.spec.js | 59 +++++++++++++++++++ packages/tools/src/SqlDumper.ts | 36 +++++------ 2 files changed, 75 insertions(+), 20 deletions(-) diff --git a/integration-tests/__tests__/table-create.spec.js b/integration-tests/__tests__/table-create.spec.js index 492a9b46..618c39ae 100644 --- a/integration-tests/__tests__/table-create.spec.js +++ b/integration-tests/__tests__/table-create.spec.js @@ -91,4 +91,63 @@ describe('Table create', () => { }); }) ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Table with foreign key - %s', + testWrapper(async (conn, driver, engine) => { + await testTableCreate(conn, driver, { + columns: [ + { + columnName: 'col1', + dataType: 'int', + notNull: true, + }, + { + columnName: 'col2', + dataType: 'int', + notNull: true, + }, + ], + primaryKey: { + columns: [{ columnName: 'col1' }], + }, + foreignKeys: [ + { + pureName: 'tested', + refTableName: 't0', + columns: [{ columnName: 'col2', refColumnName: 'id' }], + }, + ], + }); + }) + ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Table with unique - %s', + testWrapper(async (conn, driver, engine) => { + await testTableCreate(conn, driver, { + columns: [ + { + columnName: 'col1', + dataType: 'int', + notNull: true, + }, + { + columnName: 'col2', + dataType: 'int', + notNull: true, + }, + ], + primaryKey: { + columns: [{ columnName: 'col1' }], + }, + uniques: [ + { + pureName: 'tested', + columns: [{ columnName: 'col2' }], + }, + ], + }); + }) + ); }); diff --git a/packages/tools/src/SqlDumper.ts b/packages/tools/src/SqlDumper.ts index ddd52091..3e3a08f0 100644 --- a/packages/tools/src/SqlDumper.ts +++ b/packages/tools/src/SqlDumper.ts @@ -229,29 +229,25 @@ export class SqlDumper implements AlterProcessor { table.primaryKey.columns.map(x => x.columnName) ); } - if (table.foreignKeys) { - table.foreignKeys.forEach(fk => { - this.put(',&n'); - this.createForeignKeyFore(fk); - }); - } - // foreach (var cnt in table.Uniques) - // { - // if (!first) this.put(", &n"); - // first = false; - // CreateUniqueCore(cnt); - // } - // foreach (var cnt in table.Checks) - // { - // if (!first) this.put(", &n"); - // first = false; - // CreateCheckCore(cnt); - // } + + (table.foreignKeys || []).forEach(fk => { + this.put(',&n'); + this.createForeignKeyFore(fk); + }); + (table.uniques || []).forEach(uq => { + this.put(',&n'); + this.createUniqueCore(uq); + }); + (table.checks || []).forEach(chk => { + this.put(',&n'); + this.createCheckCore(chk); + }); + this.put('&<&n)'); this.endCommand(); - for (const ix of table.indexes) { + (table.indexes || []).forEach(ix => { this.createIndex(ix); - } + }); } createForeignKeyFore(fk: ForeignKeyInfo) {