create column test

This commit is contained in:
Jan Prochazka 2021-06-28 08:00:28 +02:00
parent 67a793038b
commit 4c1ac0757c
3 changed files with 27 additions and 4 deletions

View File

@ -1,19 +1,33 @@
const engines = require('../engines'); const stableStringify = require('json-stable-stringify');
const _ = require('lodash');
const uuidv1 = require('uuid/v1'); const uuidv1 = require('uuid/v1');
const { testWrapper } = require('../tools'); const { testWrapper } = require('../tools');
const engines = require('../engines');
const { getAlterTableScript, extendDatabaseInfo } = require('dbgate-tools');
async function testTableDiff(conn, driver, mangle) { async function testTableDiff(conn, driver, mangle) {
await driver.query(conn, 'create table t1 (col1 int not null)'); await driver.query(conn, 'create table t1 (col1 int not null)');
const structure1 = await driver.analyseFull(conn); const structure1 = extendDatabaseInfo(await driver.analyseFull(conn));
mangle(structure1.tables[0]); 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', () => { describe('Alter processor', () => {
test.each(engines.map(engine => [engine.label, engine]))( test.each(engines.map(engine => [engine.label, engine]))(
'Add column - %s', 'Add column - %s',
testWrapper(async (conn, driver, engine) => { testWrapper(async (conn, driver, engine) => {
testTableDiff(conn, driver, tbl => await testTableDiff(conn, driver, tbl =>
tbl.columns.push({ tbl.columns.push({
columnName: 'added', columnName: 'added',
dataType: 'int', dataType: 'int',

View File

@ -158,6 +158,12 @@ export class AlterPlan {
newName, newName,
}); });
} }
run(processor: AlterProcessor) {
for (const op of this.operations) {
runAlterOperation(op, processor);
}
}
} }
export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) { export function runAlterOperation(op: AlterOperation, processor: AlterProcessor) {

View File

@ -281,4 +281,7 @@ export function getAlterTableScript(
driver: EngineDriver driver: EngineDriver
): string { ): string {
const plan = createAlterTablePlan(oldTable, newTable, opts, db); const plan = createAlterTablePlan(oldTable, newTable, opts, db);
const dmp = driver.createDumper();
plan.run(dmp);
return dmp.s;
} }