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); mangle(structure2);
structure2 = extendDatabaseInfo(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); console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql); await driver.script(conn, sql);

View File

@@ -46,7 +46,7 @@ async function testTableDiff(conn, driver, mangle) {
mangle(tget(structure2)); mangle(tget(structure2));
structure2 = extendDatabaseInfo(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); console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql); await driver.script(conn, sql);

View File

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

View File

@@ -42,7 +42,14 @@ async function generateDeploySql({
// console.log('deployedModel', deployedModel.tables[0]); // console.log('deployedModel', deployedModel.tables[0]);
// console.log('currentModel', currentModel.tables[0]); // console.log('currentModel', currentModel.tables[0]);
// console.log('currentModelPaired', currentModelPaired.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; return res;
} }

View File

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

View File

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

View File

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

View File

@@ -70,7 +70,7 @@
} }
if (objectTypeField == 'tables') { if (objectTypeField == 'tables') {
return getAlterTableScript(oldObject, newObject, opts, db, driver); return getAlterTableScript(oldObject, newObject, opts, db, db, driver);
} }
const dmp = driver.createDumper(); const dmp = driver.createDumper();
if (oldObject) dmp.dropSqlObject(oldObject); if (oldObject) dmp.dropSqlObject(oldObject);
@@ -280,7 +280,7 @@
function getDeploySql() { function getDeploySql() {
return diffRows return diffRows
.filter(row => $values[`isChecked_${row.identifier}`]) .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'); .join('\n');
} }

View File

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

View File

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