mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
db syn logical fix
This commit is contained in:
parent
a9216eda89
commit
cec0130cba
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -116,9 +116,9 @@ const engines = [
|
||||
|
||||
const filterLocal = [
|
||||
// filter local testing
|
||||
'-MySQL',
|
||||
'-PostgreSQL',
|
||||
'-SQL Server',
|
||||
'MySQL',
|
||||
'PostgreSQL',
|
||||
'SQL Server',
|
||||
'SQLite',
|
||||
'-CockroachDB',
|
||||
];
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
|
@ -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');
|
||||
}
|
||||
|
||||
|
@ -115,6 +115,7 @@
|
||||
extendTableInfo(fillConstraintNames($editorValue.current, driver.dialect)),
|
||||
{},
|
||||
$dbInfo,
|
||||
$dbInfo,
|
||||
driver
|
||||
);
|
||||
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user