diff --git a/integration-tests/__tests__/alter-processor.spec.js b/integration-tests/__tests__/alter-processor.spec.js index 13db05b55..b88024b0d 100644 --- a/integration-tests/__tests__/alter-processor.spec.js +++ b/integration-tests/__tests__/alter-processor.spec.js @@ -19,21 +19,35 @@ function checkTableStructure(t1, t2) { } async function testTableDiff(conn, driver, mangle) { - await driver.query(conn, 'create table t1 (col1 int not null)'); + await driver.query(conn, `create table t0 (id int not null primary key)`); + await driver.query( + conn, + `create table t1 ( + id int not null primary key, + col_std int null, + col_def int null default 12, + col_fk int null references t0(id), + col_idx int null + )` + ); + + await driver.query(conn, `create index idx1 on t1(col_idx)`); + + const tget = x => x.tables.find(y => y.pureName == 't1'); const structure1 = generateDbPairingId(extendDatabaseInfo(await driver.analyseFull(conn))); let structure2 = _.cloneDeep(structure1); - mangle(structure2.tables[0]); + mangle(tget(structure2)); structure2 = extendDatabaseInfo(structure2); - const sql = getAlterTableScript(structure1.tables[0], structure2.tables[0], {}, structure2, driver); + const sql = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver); console.log('RUNNING ALTER SQL:', sql); await driver.query(conn, sql); const structure2Real = extendDatabaseInfo(await driver.analyseFull(conn)); - checkTableStructure(structure2Real.tables[0], structure2.tables[0]); + checkTableStructure(tget(structure2Real), tget(structure2)); // expect(stableStringify(structure2)).toEqual(stableStringify(structure2Real)); } @@ -50,14 +64,45 @@ describe('Alter processor', () => { autoIncrement: false, }) ); - // console.log('ENGINE', engine); - // for (const sql of initSql) await driver.query(conn, sql); + }) + ); - // await driver.query(conn, object.create1); - // const structure = await driver.analyseFull(conn); + test.each(engines.map(engine => [engine.label, engine]))( + 'Drop column - %s', + testWrapper(async (conn, driver, engine) => { + await testTableDiff(conn, driver, tbl => (tbl.columns = tbl.columns.filter(x => x.columnName != 'col_std'))); + }) + ); - // expect(structure[type].length).toEqual(1); - // expect(structure[type][0]).toEqual(type.includes('views') ? view1Match : obj1Match); + test.each(engines.map(engine => [engine.label, engine]))( + 'Drop column with default - %s', + testWrapper(async (conn, driver, engine) => { + await testTableDiff(conn, driver, tbl => (tbl.columns = tbl.columns.filter(x => x.columnName != 'col_def'))); + }) + ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Drop column with fk - %s', + testWrapper(async (conn, driver, engine) => { + await testTableDiff(conn, driver, tbl => (tbl.columns = tbl.columns.filter(x => x.columnName != 'col_fk'))); + }) + ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Drop column with index - %s', + testWrapper(async (conn, driver, engine) => { + await testTableDiff(conn, driver, tbl => (tbl.columns = tbl.columns.filter(x => x.columnName != 'col_idx'))); + }) + ); + + test.each(engines.map(engine => [engine.label, engine]))( + 'Change nullability - %s', + testWrapper(async (conn, driver, engine) => { + await testTableDiff( + conn, + driver, + tbl => (tbl.columns = tbl.columns.map(x => (x.columnName == 'col_std' ? { ...x, notNull: true } : x))) + ); }) ); }); diff --git a/packages/tools/src/diffTools.ts b/packages/tools/src/diffTools.ts index 3e04f48ea..d76033c38 100644 --- a/packages/tools/src/diffTools.ts +++ b/packages/tools/src/diffTools.ts @@ -217,7 +217,7 @@ function getTableConstraints(table: TableInfo) { function createPairs(oldList, newList, additionalCondition = null) { const res = []; for (const a of oldList) { - const b = newList.find(x => x.pairingId == a.pairingId || (additionalCondition && additionalCondition(a, b))); + const b = newList.find(x => x.pairingId == a.pairingId || (additionalCondition && additionalCondition(a, x))); if (b) { res.push([a, b]); } else {