select scope identity test

This commit is contained in:
Jan Prochazka
2024-12-11 12:08:50 +01:00
parent 545e9863b6
commit 3f45bfcdd0
3 changed files with 26 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ const engines = require('../engines');
const stream = require('stream'); const stream = require('stream');
const { testWrapper } = require('../tools'); const { testWrapper } = require('../tools');
const dataDuplicator = require('dbgate-api/src/shell/dataDuplicator'); const dataDuplicator = require('dbgate-api/src/shell/dataDuplicator');
const { runCommandOnDriver } = require('dbgate-tools'); const { runCommandOnDriver, runQueryOnDriver } = require('dbgate-tools');
describe('Data duplicator', () => { describe('Data duplicator', () => {
test.each(engines.filter(x => !x.skipDataDuplicator).map(engine => [engine.label, engine]))( test.each(engines.filter(x => !x.skipDataDuplicator).map(engine => [engine.label, engine]))(
@@ -84,10 +84,10 @@ describe('Data duplicator', () => {
], ],
}); });
const res1 = await driver.query(conn, `select count(*) as cnt from t1`); const res1 = await runQueryOnDriver(conn, driver, dmp => dmp.put(`select count(*) as ~cnt from ~t1`));
expect(res1.rows[0].cnt.toString()).toEqual('6'); expect(res1.rows[0].cnt.toString()).toEqual('6');
const res2 = await driver.query(conn, `select count(*) as cnt from t2`); const res2 = await runQueryOnDriver(conn, driver, dmp => dmp.put(`select count(*) as ~cnt from ~t2`));
expect(res2.rows[0].cnt.toString()).toEqual('6'); expect(res2.rows[0].cnt.toString()).toEqual('6');
}) })
); );
@@ -145,13 +145,15 @@ describe('Data duplicator', () => {
}, },
}); });
const res1 = await driver.query(conn, `select count(*) as cnt from t1`); const res1 = await runQueryOnDriver(conn, driver, dmp => dmp.put(`select count(*) as ~cnt from ~t1`));
expect(res1.rows[0].cnt.toString()).toEqual('1'); expect(res1.rows[0].cnt.toString()).toEqual('1');
const res2 = await driver.query(conn, `select count(*) as cnt from t2`); const res2 = await runQueryOnDriver(conn, driver, dmp => dmp.put(`select count(*) as ~cnt from ~t2`));
expect(res2.rows[0].cnt.toString()).toEqual('2'); expect(res2.rows[0].cnt.toString()).toEqual('2');
const res3 = await driver.query(conn, `select count(*) as cnt from t2 where valfk is not null`); const res3 = await runQueryOnDriver(conn, driver, dmp =>
dmp.put(`select count(*) as ~cnt from ~t2 where ~valfk is not null`)
);
expect(res3.rows[0].cnt.toString()).toEqual('1'); expect(res3.rows[0].cnt.toString()).toEqual('1');
}) })
); );

View File

@@ -180,7 +180,7 @@ describe('Query', () => {
}) })
); );
test.each(engines.map(engine => [engine.label, engine]))( test.each(engines.filter(x => !x.skipDataDuplicator).map(engine => [engine.label, engine]))(
'Select scope identity - %s', 'Select scope identity - %s',
testWrapper(async (conn, driver, engine) => { testWrapper(async (conn, driver, engine) => {
const table = { const table = {
@@ -194,11 +194,24 @@ describe('Query', () => {
}, },
}; };
await runCommandOnDriver(conn, driver, dmp => dmp.createTable(table)); await runCommandOnDriver(conn, driver, dmp => dmp.createTable(table));
await runCommandOnDriver(conn, driver, dmp => dmp.put("INSERT INTO ~t1 (~val) VALUES ('aaa')")); let res;
const res = await runQueryOnDriver(conn, driver, dmp => dmp.selectScopeIdentity(table)); if (driver.dialect.requireStandaloneSelectForScopeIdentity) {
await runCommandOnDriver(conn, driver, dmp => dmp.put("INSERT INTO ~t1 (~val) VALUES ('aaa')"));
res = await runQueryOnDriver(conn, driver, dmp => dmp.selectScopeIdentity(table));
} else {
res = await runQueryOnDriver(conn, driver, dmp => {
dmp.putCmd("INSERT INTO ~t1 (~val) VALUES ('aaa')");
dmp.selectScopeIdentity(table);
});
}
const row = res.rows[0]; const row = res.rows[0];
// console.log('*******************');
// console.log(JSON.stringify(res, null, 2));
// console.log(JSON.stringify(row, null, 2));
const keys = Object.keys(row); const keys = Object.keys(row);
// console.log(JSON.stringify(row, null, 2));
expect(keys.length).toEqual(1); expect(keys.length).toEqual(1);
// console.log(JSON.stringify(row[keys[0]], null, 2));
expect(row[keys[0]] == 1).toBeTruthy(); expect(row[keys[0]] == 1).toBeTruthy();
}) })
); );

View File

@@ -66,7 +66,7 @@ class DuplicatorItemHolder {
this.autoColumn = this.table.columns.find(x => x.autoIncrement)?.columnName; this.autoColumn = this.table.columns.find(x => x.autoIncrement)?.columnName;
if ( if (
this.table.primaryKey?.columns?.length != 1 || this.table.primaryKey?.columns?.length != 1 ||
this.table.primaryKey?.columns?.[0].columnName != this.autoColumn this.table.primaryKey?.columns?.[0]?.columnName != this.autoColumn
) { ) {
this.autoColumn = null; this.autoColumn = null;
} }
@@ -194,6 +194,7 @@ class DuplicatorItemHolder {
res = await runQueryOnDriver(pool, driver, dmp => dmp.selectScopeIdentity(this.table)); res = await runQueryOnDriver(pool, driver, dmp => dmp.selectScopeIdentity(this.table));
} }
// console.log('IDRES', JSON.stringify(res)); // console.log('IDRES', JSON.stringify(res));
// console.log('*********** ENTRIES OF', res?.rows?.[0]);
const resId = Object.entries(res?.rows?.[0])?.[0]?.[1]; const resId = Object.entries(res?.rows?.[0])?.[0]?.[1];
if (resId != null) { if (resId != null) {
this.idMap[chunk[this.autoColumn]] = resId; this.idMap[chunk[this.autoColumn]] = resId;