db syn logical fix

This commit is contained in:
Jan Prochazka 2021-11-25 15:14:23 +01:00
parent a9216eda89
commit cec0130cba
10 changed files with 41 additions and 23 deletions

View File

@ -30,7 +30,7 @@ async function testDatabaseDiff(conn, driver, mangle, createObject = null) {
mangle(structure2);
structure2 = extendDatabaseInfo(structure2);
const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure2, driver);
const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure1, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql);

View File

@ -46,7 +46,7 @@ async function testTableDiff(conn, driver, mangle) {
mangle(tget(structure2));
structure2 = extendDatabaseInfo(structure2);
const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver);
const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure1, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql);

View File

@ -116,9 +116,9 @@ const engines = [
const filterLocal = [
// filter local testing
'-MySQL',
'-PostgreSQL',
'-SQL Server',
'MySQL',
'PostgreSQL',
'SQL Server',
'SQLite',
'-CockroachDB',
];

View File

@ -42,7 +42,14 @@ async function generateDeploySql({
// console.log('deployedModel', deployedModel.tables[0]);
// console.log('currentModel', currentModel.tables[0]);
// console.log('currentModelPaired', currentModelPaired.tables[0]);
const res = getAlterDatabaseScript(currentModelPaired, deployedModel, opts, deployedModel, driver);
const res = getAlterDatabaseScript(
currentModelPaired,
deployedModel,
opts,
currentModelPaired,
deployedModel,
driver
);
return res;
}

View File

@ -111,7 +111,12 @@ export class AlterPlan {
};
public operations: AlterOperation[] = [];
constructor(public db: DatabaseInfo, public dialect: SqlDialect, public opts: DbDiffOptions) {}
constructor(
public wholeOldDb: DatabaseInfo,
public wholeNewDb: DatabaseInfo,
public dialect: SqlDialect,
public opts: DbDiffOptions
) {}
createTable(table: TableInfo) {
this.operations.push({
@ -225,7 +230,7 @@ export class AlterPlan {
}
_getDependendColumnConstraints(column: ColumnInfo, dependencyDefinition) {
const table = this.db.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
const table = this.wholeOldDb.tables.find(x => x.pureName == column.pureName && x.schemaName == column.schemaName);
if (!table) return [];
const fks = dependencyDefinition?.includes('dependencies')
? table.dependencies.filter(fk => fk.columns.find(col => col.refColumnName == column.columnName))
@ -385,7 +390,7 @@ export class AlterPlan {
return [];
}
const table = this.db.tables.find(
const table = this.wholeNewDb.tables.find(
x => x.pureName == op[objectField].pureName && x.schemaName == op[objectField].schemaName
);
this.recreates.tables += 1;

View File

@ -83,7 +83,7 @@ export function computeDbDiffRows(
res.push(
..._.sortBy(
computeDiffRowsCore(sourceDb[objectTypeField], targetDb[objectTypeField], (a, b) =>
defs.test(a, b, opts, targetDb, driver)
defs.test(a, b, opts, sourceDb, targetDb, driver)
).map(row => ({
...row,
sourceSchemaName: row?.source?.schemaName,

View File

@ -373,10 +373,11 @@ export function testEqualTables(
a: TableInfo,
b: TableInfo,
opts: DbDiffOptions,
db: DatabaseInfo,
wholeOldDb: DatabaseInfo,
wholeNewDb: DatabaseInfo,
driver: EngineDriver
) {
const plan = new AlterPlan(db, driver.dialect, opts);
const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts);
planAlterTable(plan, a, b, opts);
// console.log('plan.operations', a, b, plan.operations);
return plan.operations.length == 0;
@ -390,10 +391,11 @@ export function createAlterTablePlan(
oldTable: TableInfo,
newTable: TableInfo,
opts: DbDiffOptions,
db: DatabaseInfo,
wholeOldDb: DatabaseInfo,
wholeNewDb: DatabaseInfo,
driver: EngineDriver
): AlterPlan {
const plan = new AlterPlan(db, driver.dialect, opts);
const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts);
if (oldTable == null) {
plan.createTable(newTable);
} else if (newTable == null) {
@ -409,10 +411,11 @@ export function createAlterDatabasePlan(
oldDb: DatabaseInfo,
newDb: DatabaseInfo,
opts: DbDiffOptions,
db: DatabaseInfo,
wholeOldDb: DatabaseInfo,
wholeNewDb: DatabaseInfo,
driver: EngineDriver
): AlterPlan {
const plan = new AlterPlan(db, driver.dialect, opts);
const plan = new AlterPlan(wholeOldDb, wholeNewDb, driver.dialect, opts);
for (const objectTypeField of ['tables', 'views', 'procedures', 'matviews', 'functions']) {
for (const oldobj of oldDb[objectTypeField] || []) {
@ -456,14 +459,15 @@ export function getAlterTableScript(
oldTable: TableInfo,
newTable: TableInfo,
opts: DbDiffOptions,
db: DatabaseInfo,
wholeOldDb: DatabaseInfo,
wholeNewDb: DatabaseInfo,
driver: EngineDriver
) {
if ((!oldTable && !newTable) || !driver) {
return { sql: '', recreates: [] };
}
const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver);
const plan = createAlterTablePlan(oldTable, newTable, opts, wholeOldDb, wholeNewDb, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);
@ -478,10 +482,11 @@ export function getAlterDatabaseScript(
oldDb: DatabaseInfo,
newDb: DatabaseInfo,
opts: DbDiffOptions,
db: DatabaseInfo,
wholeOldDb: DatabaseInfo,
wholeNewDb: DatabaseInfo,
driver: EngineDriver
) {
const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver);
const plan = createAlterDatabasePlan(oldDb, newDb, opts, wholeOldDb, wholeNewDb, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);

View File

@ -70,7 +70,7 @@
}
if (objectTypeField == 'tables') {
return getAlterTableScript(oldObject, newObject, opts, db, driver);
return getAlterTableScript(oldObject, newObject, opts, db, db, driver);
}
const dmp = driver.createDumper();
if (oldObject) dmp.dropSqlObject(oldObject);
@ -280,7 +280,7 @@
function getDeploySql() {
return diffRows
.filter(row => $values[`isChecked_${row.identifier}`])
.map(row => getAlterTableScript(row?.target, row?.source, dbDiffOptions, targetDb, driver).sql)
.map(row => getAlterTableScript(row?.target, row?.source, dbDiffOptions, sourceDb, targetDb, driver).sql)
.join('\n');
}

View File

@ -115,6 +115,7 @@
extendTableInfo(fillConstraintNames($editorValue.current, driver.dialect)),
{},
$dbInfo,
$dbInfo,
driver
);

View File

@ -15,7 +15,7 @@ export async function alterDatabaseDialog(conid, database, updateFunc) {
const dbUpdated = _.cloneDeep(db);
updateFunc(dbUpdated);
const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, driver);
const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, dbUpdated, driver);
showModal(ConfirmSqlModal, {
sql,