import models tests fixed

This commit is contained in:
Jan Prochazka
2025-02-25 13:58:03 +01:00
parent ec02743f83
commit a4d3189dac
9 changed files with 60 additions and 14 deletions

View File

@@ -3,7 +3,7 @@ const fs = require('fs-extra');
const executeQuery = require('./executeQuery');
const { connectUtility } = require('../utility/connectUtility');
const requireEngineDriver = require('../utility/requireEngineDriver');
const { getAlterDatabaseScript, DatabaseAnalyser } = require('dbgate-tools');
const { getAlterDatabaseScript, DatabaseAnalyser, runCommandOnDriver } = require('dbgate-tools');
const importDbModel = require('../utility/importDbModel');
const jsonLinesReader = require('./jsonLinesReader');
const tableWriter = require('./tableWriter');
@@ -41,17 +41,38 @@ async function importDbFromFolder({ connection, systemConnection, driver, folder
})),
};
// const plan = createAlterDatabasePlan(
// DatabaseAnalyser.createEmptyStructure(),
// driver.dialect.enableAllForeignKeys ? modelAdapted : modelNoFk,
// {},
// DatabaseAnalyser.createEmptyStructure(),
// driver.dialect.enableAllForeignKeys ? modelAdapted : modelNoFk,
// driver
// );
// const dmp1 = driver.createDumper({ useHardSeparator: true });
// if (driver.dialect.enableAllForeignKeys) {
// dmp1.enableAllForeignKeys(false);
// }
// plan.run(dmp1);
// if (driver.dialect.enableAllForeignKeys) {
// dmp1.enableAllForeignKeys(true);
// }
const { sql } = getAlterDatabaseScript(
DatabaseAnalyser.createEmptyStructure(),
modelNoFk,
driver.dialect.enableAllForeignKeys ? modelAdapted : modelNoFk,
{},
DatabaseAnalyser.createEmptyStructure(),
modelNoFk,
driver.dialect.enableAllForeignKeys ? modelAdapted : modelNoFk,
driver
);
// console.log('CREATING STRUCTURE:', sql);
await executeQuery({ connection, systemConnection: dbhan, driver, sql, logScriptItems: true });
if (driver.dialect.enableAllForeignKeys) {
await runCommandOnDriver(dbhan, driver, dmp => dmp.enableAllForeignKeys(false));
}
for (const table of modelAdapted.tables) {
const fileName = path.join(folder, `${table.pureName}.jsonl`);
if (await fs.exists(fileName)) {
@@ -66,14 +87,19 @@ async function importDbFromFolder({ connection, systemConnection, driver, folder
}
}
const dmp = driver.createDumper();
for (const table of modelAdapted.tables) {
for (const fk of table.foreignKeys) {
dmp.createForeignKey(fk);
if (driver.dialect.enableAllForeignKeys) {
await runCommandOnDriver(dbhan, driver, dmp => dmp.enableAllForeignKeys(true));
} else if (driver.dialect.createForeignKey) {
const dmp = driver.createDumper();
for (const table of modelAdapted.tables) {
for (const fk of table.foreignKeys) {
dmp.createForeignKey(fk);
}
}
// create foreign keys
await executeQuery({ connection, systemConnection: dbhan, driver, sql: dmp.s, logScriptItems: true });
}
// create foreign keys
await executeQuery({ connection, systemConnection: dbhan, driver, sql: dmp.s, logScriptItems: true });
} finally {
if (!systemConnection) {
await driver.close(dbhan);

View File

@@ -350,7 +350,9 @@ export class SqlDumper implements AlterProcessor {
}
createForeignKeyFore(fk: ForeignKeyInfo) {
if (fk.constraintName != null) this.put('^constraint %i ', fk.constraintName);
if (fk.constraintName != null && !this.dialect.anonymousForeignKey) {
this.put('^constraint %i ', fk.constraintName);
}
this.put(
'^foreign ^key (%,i) ^references %f (%,i)',
fk.columns.map(x => x.columnName),
@@ -367,6 +369,7 @@ export class SqlDumper implements AlterProcessor {
allowIdentityInsert(table: NamedObjectInfo, allow: boolean) {}
enableConstraints(table: NamedObjectInfo, enabled: boolean) {}
enableAllForeignKeys(enabled: boolean) {}
comment(value: string) {
if (!value) return;

View File

@@ -13,8 +13,10 @@ export interface SqlDialect {
fallbackDataType?: string;
explicitDropConstraint?: boolean;
anonymousPrimaryKey?: boolean;
anonymousForeignKey?: boolean;
defaultSchemaName?: string;
enableConstraintsPerTable?: boolean;
enableAllForeignKeys?: boolean;
requireStandaloneSelectForScopeIdentity?: boolean;
allowMultipleValuesInsert?: boolean;

View File

@@ -39,6 +39,7 @@ export type TestEngineInfo = {
skipNonPkRename?: boolean;
skipPkDrop?: boolean;
skipOrderBy?: boolean;
skipImportModel?: boolean;
forceSortResults?: boolean;
forceSortStructureColumns?: boolean;