From 4c1ac0757c4d352c3d2b8c2a851c051af64e5684 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Mon, 28 Jun 2021 08:00:28 +0200 Subject: [PATCH] create column test --- .../__tests__/alter-processor.spec.js | 22 +++++++++++++++---- packages/tools/src/alterPlan.ts | 6 +++++ packages/tools/src/diffTools.ts | 3 +++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/integration-tests/__tests__/alter-processor.spec.js b/integration-tests/__tests__/alter-processor.spec.js index d6caffa2..fc979ee5 100644 --- a/integration-tests/__tests__/alter-processor.spec.js +++ b/integration-tests/__tests__/alter-processor.spec.js @@ -1,19 +1,33 @@ -const engines = require('../engines'); +const stableStringify = require('json-stable-stringify'); +const _ = require('lodash'); const uuidv1 = require('uuid/v1'); const { testWrapper } = require('../tools'); +const engines = require('../engines'); +const { getAlterTableScript, extendDatabaseInfo } = require('dbgate-tools'); async function testTableDiff(conn, driver, mangle) { await driver.query(conn, 'create table t1 (col1 int not null)'); - const structure1 = await driver.analyseFull(conn); - mangle(structure1.tables[0]); + const structure1 = extendDatabaseInfo(await driver.analyseFull(conn)); + let structure2 = _.cloneDeep(structure1); + mangle(structure2.tables[0]); + structure2 = extendDatabaseInfo(structure2); + + const sql = getAlterTableScript(structure1.tables[0], structure2.tables[0], {}, structure2, driver); + console.log('RUNNING ALTER SQL:', sql); + + await driver.query(conn, sql); + + const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn)); + + expect(stableStringify(structure2)).toEqual(stableStringify(structure2Real)); } describe('Alter processor', () => { test.each(engines.map(engine => [engine.label, engine]))( 'Add column - %s', testWrapper(async (conn, driver, engine) => { - testTableDiff(conn, driver, tbl => + await testTableDiff(conn, driver, tbl => tbl.columns.push({ columnName: 'added', dataType: 'int', diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index ca57cf8e..3547e184 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -158,6 +158,12 @@ export class AlterPlan { newName, }); } + + run(processor: AlterProcessor) { + for (const op of this.operations) { + runAlterOperation(op, processor); + } + } } export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) { diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 87b7598d..02ce614d 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -281,4 +281,7 @@ export function getAlterTableScript( driver: EngineDriver ): string { const plan = createAlterTablePlan(oldTable, newTable, opts, db); + const dmp = driver.createDumper(); + plan.run(dmp); + return dmp.s; }