mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-30 15:03:57 +00:00
driver tests
This commit is contained in:
@@ -4,7 +4,7 @@ const { testWrapper, testWrapperPrepareOnly } = require('../tools');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const engines = require('../engines');
|
const engines = require('../engines');
|
||||||
const deployDb = require('dbgate-api/src/shell/deployDb');
|
const deployDb = require('dbgate-api/src/shell/deployDb');
|
||||||
const { databaseInfoFromYamlModel } = require('dbgate-tools');
|
const { databaseInfoFromYamlModel, runQueryOnDriver, formatQueryWithoutParams } = require('dbgate-tools');
|
||||||
const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql');
|
const generateDeploySql = require('dbgate-api/src/shell/generateDeploySql');
|
||||||
const connectUtility = require('dbgate-api/src/utility/connectUtility');
|
const connectUtility = require('dbgate-api/src/utility/connectUtility');
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ async function testDatabaseDeploy(engine, conn, driver, dbModelsYaml, options) {
|
|||||||
|
|
||||||
for (const loadedDbModel of dbModelsYaml) {
|
for (const loadedDbModel of dbModelsYaml) {
|
||||||
if (_.isString(loadedDbModel)) {
|
if (_.isString(loadedDbModel)) {
|
||||||
await driver.script(conn, loadedDbModel);
|
await driver.script(conn, formatQueryWithoutParams(driver, loadedDbModel));
|
||||||
} else {
|
} else {
|
||||||
const { sql, isEmpty } = await generateDeploySql({
|
const { sql, isEmpty } = await generateDeploySql({
|
||||||
systemConnection: conn.isPreparedOnly ? undefined : conn,
|
systemConnection: conn.isPreparedOnly ? undefined : conn,
|
||||||
@@ -339,7 +339,7 @@ describe('Deploy database', () => {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const res = await driver.query(conn, `select count(*) as cnt from t1`);
|
const res = await runQueryOnDriver(conn, driver, `select count(*) as ~cnt from ~t1`);
|
||||||
expect(res.rows[0].cnt.toString()).toEqual('3');
|
expect(res.rows[0].cnt.toString()).toEqual('3');
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -386,7 +386,7 @@ describe('Deploy database', () => {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const res = await driver.query(conn, `select val from t1 where id = 2`);
|
const res = await runQueryOnDriver(conn, driver, `select ~val from ~t1 where ~id = 2`);
|
||||||
expect(res.rows[0].val.toString()).toEqual('5');
|
expect(res.rows[0].val.toString()).toEqual('5');
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -414,8 +414,8 @@ describe('Deploy database', () => {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await driver.query(conn, `insert into t1 (id) values (1)`);
|
await runQueryOnDriver(conn, driver, `insert into ~t1 (~id) values (1)`);
|
||||||
const res = await driver.query(conn, ` select val from t1 where id = 1`);
|
const res = await runQueryOnDriver(conn, driver, ` select ~val from ~t1 where ~id = 1`);
|
||||||
expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20');
|
expect(res.rows[0].val.toString().substring(0, 2)).toEqual('20');
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -438,7 +438,7 @@ describe('Deploy database', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
'insert into t1 (id, val) values (1, 1); insert into t1 (id) values (2)',
|
'insert into ~t1 (~id, ~val) values (1, 1); insert into ~t1 (~id) values (2)',
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
name: 't1.table.yaml',
|
name: 't1.table.yaml',
|
||||||
@@ -452,16 +452,16 @@ describe('Deploy database', () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
'insert into t1 (id) values (3);',
|
'insert into ~t1 (~id) values (3);',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const res1 = await driver.query(conn, `select val from t1 where id = 1`);
|
const res1 = await runQueryOnDriver(conn, driver, `select ~val from ~t1 where ~id = 1`);
|
||||||
expect(res1.rows[0].val).toEqual(1);
|
expect(res1.rows[0].val).toEqual(1);
|
||||||
|
|
||||||
const res2 = await driver.query(conn, `select val from t1 where id = 2`);
|
const res2 = await runQueryOnDriver(conn, driver, `select ~val from ~t1 where ~id = 2`);
|
||||||
expect(res2.rows[0].val).toEqual(20);
|
expect(res2.rows[0].val).toEqual(20);
|
||||||
|
|
||||||
const res3 = await driver.query(conn, `select val from t1 where id = 3`);
|
const res3 = await runQueryOnDriver(conn, driver, `select ~val from ~t1 where ~id = 3`);
|
||||||
expect(res2.rows[0].val).toEqual(20);
|
expect(res2.rows[0].val).toEqual(20);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -687,10 +687,10 @@ describe('Deploy database', () => {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const res1 = await driver.query(conn, 'SELECT COUNT(*) AS cnt FROM t1');
|
const res1 = await runQueryOnDriver(conn, driver, 'SELECT COUNT(*) AS ~cnt FROM ~t1');
|
||||||
expect(res1.rows[0].cnt == 1).toBeTruthy();
|
expect(res1.rows[0].cnt == 1).toBeTruthy();
|
||||||
|
|
||||||
const res2 = await driver.query(conn, 'SELECT COUNT(*) AS cnt FROM dbgate_deploy_journal');
|
const res2 = await runQueryOnDriver(conn, driver, 'SELECT COUNT(*) AS ~cnt FROM ~dbgate_deploy_journal');
|
||||||
expect(res2.rows[0].cnt == 1).toBeTruthy();
|
expect(res2.rows[0].cnt == 1).toBeTruthy();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -729,21 +729,26 @@ describe('Deploy database', () => {
|
|||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const res1 = await driver.query(conn, 'SELECT val from t1 where id = 1');
|
const res1 = await runQueryOnDriver(conn, driver, 'SELECT ~val from ~t1 where ~id = 1');
|
||||||
expect(res1.rows[0].val == 11).toBeTruthy();
|
expect(res1.rows[0].val == 11).toBeTruthy();
|
||||||
|
|
||||||
const res2 = await driver.query(conn, 'SELECT COUNT(*) AS cnt FROM t2');
|
const res2 = await runQueryOnDriver(conn, driver, 'SELECT COUNT(*) AS ~cnt FROM ~t2');
|
||||||
expect(res2.rows[0].cnt == 1).toBeTruthy();
|
expect(res2.rows[0].cnt == 1).toBeTruthy();
|
||||||
|
|
||||||
const res3 = await driver.query(conn, 'SELECT COUNT(*) AS cnt FROM dbgate_deploy_journal');
|
const res3 = await runQueryOnDriver(conn, driver, 'SELECT COUNT(*) AS ~cnt FROM ~dbgate_deploy_journal');
|
||||||
expect(res3.rows[0].cnt == 3).toBeTruthy();
|
expect(res3.rows[0].cnt == 3).toBeTruthy();
|
||||||
|
|
||||||
const res4 = await driver.query(conn, "SELECT run_count from dbgate_deploy_journal where name = 't2.once.sql'");
|
const res4 = await runQueryOnDriver(
|
||||||
|
conn,
|
||||||
|
driver,
|
||||||
|
"SELECT ~run_count from ~dbgate_deploy_journal where ~name = 't2.once.sql'"
|
||||||
|
);
|
||||||
expect(res4.rows[0].run_count == 1).toBeTruthy();
|
expect(res4.rows[0].run_count == 1).toBeTruthy();
|
||||||
|
|
||||||
const res5 = await driver.query(
|
const res5 = await runQueryOnDriver(
|
||||||
conn,
|
conn,
|
||||||
"SELECT run_count from dbgate_deploy_journal where name = 't1.install.sql'"
|
driver,
|
||||||
|
"SELECT ~run_count from ~dbgate_deploy_journal where ~name = 't1.install.sql'"
|
||||||
);
|
);
|
||||||
expect(res5.rows[0].run_count == 2).toBeTruthy();
|
expect(res5.rows[0].run_count == 2).toBeTruthy();
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import _compact from 'lodash/compact';
|
import _compact from 'lodash/compact';
|
||||||
|
import _isString from 'lodash/isString';
|
||||||
import { SqlDumper } from './SqlDumper';
|
import { SqlDumper } from './SqlDumper';
|
||||||
import { splitQuery } from 'dbgate-query-splitter';
|
import { splitQuery } from 'dbgate-query-splitter';
|
||||||
import { dumpSqlSelect } from 'dbgate-sqltree';
|
import { dumpSqlSelect } from 'dbgate-sqltree';
|
||||||
@@ -26,9 +27,17 @@ const dialect = {
|
|||||||
defaultSchemaName: null,
|
defaultSchemaName: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function runCommandOnDriver(pool, driver: EngineDriver, cmd: (dmp: SqlDumper) => void): Promise<void> {
|
export async function runCommandOnDriver(
|
||||||
|
pool,
|
||||||
|
driver: EngineDriver,
|
||||||
|
cmd: (dmp: SqlDumper) => void | string
|
||||||
|
): Promise<void> {
|
||||||
const dmp = driver.createDumper();
|
const dmp = driver.createDumper();
|
||||||
cmd(dmp as any);
|
if (_isString(cmd)) {
|
||||||
|
dmp.put(cmd);
|
||||||
|
} else {
|
||||||
|
cmd(dmp as any);
|
||||||
|
}
|
||||||
// console.log('CMD:', dmp.s);
|
// console.log('CMD:', dmp.s);
|
||||||
await driver.query(pool, dmp.s, { discardResult: true });
|
await driver.query(pool, dmp.s, { discardResult: true });
|
||||||
}
|
}
|
||||||
@@ -39,7 +48,11 @@ export async function runQueryOnDriver(
|
|||||||
cmd: (dmp: SqlDumper) => void
|
cmd: (dmp: SqlDumper) => void
|
||||||
): Promise<QueryResult> {
|
): Promise<QueryResult> {
|
||||||
const dmp = driver.createDumper();
|
const dmp = driver.createDumper();
|
||||||
cmd(dmp as any);
|
if (_isString(cmd)) {
|
||||||
|
dmp.put(cmd);
|
||||||
|
} else {
|
||||||
|
cmd(dmp as any);
|
||||||
|
}
|
||||||
// console.log('QUERY:', dmp.s);
|
// console.log('QUERY:', dmp.s);
|
||||||
return await driver.query(pool, dmp.s);
|
return await driver.query(pool, dmp.s);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,7 +132,9 @@ class MsSqlDumper extends SqlDumper {
|
|||||||
} else {
|
} else {
|
||||||
this.dropDefault(oldcol);
|
this.dropDefault(oldcol);
|
||||||
if (oldcol.columnName != newcol.columnName) this.renameColumn(oldcol, newcol.columnName);
|
if (oldcol.columnName != newcol.columnName) this.renameColumn(oldcol, newcol.columnName);
|
||||||
this.fillNewNotNullDefaults(newcol);
|
if (!oldcol.notNull) {
|
||||||
|
this.fillNewNotNullDefaults(newcol);
|
||||||
|
}
|
||||||
this.put('^alter ^table %f ^alter ^column %i ', oldcol, oldcol.columnName, newcol.columnName);
|
this.put('^alter ^table %f ^alter ^column %i ', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
this.columnDefinition(newcol, { includeDefault: false });
|
this.columnDefinition(newcol, { includeDefault: false });
|
||||||
this.endCommand();
|
this.endCommand();
|
||||||
|
|||||||
@@ -32,10 +32,12 @@ class Dumper extends SqlDumper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
changeColumn(oldcol, newcol, constraints) {
|
changeColumn(oldcol, newcol, constraints) {
|
||||||
this.fillNewNotNullDefaults({
|
if (!oldcol.notNull) {
|
||||||
...newcol,
|
this.fillNewNotNullDefaults({
|
||||||
columnName: oldcol.columnName,
|
...newcol,
|
||||||
});
|
columnName: oldcol.columnName,
|
||||||
|
});
|
||||||
|
}
|
||||||
this.put('^alter ^table %f ^change ^column %i %i ', oldcol, oldcol.columnName, newcol.columnName);
|
this.put('^alter ^table %f ^change ^column %i %i ', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
this.columnDefinition(newcol);
|
this.columnDefinition(newcol);
|
||||||
this.inlineConstraints(constraints);
|
this.inlineConstraints(constraints);
|
||||||
|
|||||||
@@ -92,6 +92,10 @@ class Dumper extends SqlDumper {
|
|||||||
this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', oldcol, oldcol.columnName, newcol.columnName);
|
this.putCmd('^alter ^table %f ^rename ^column %i ^to %i', oldcol, oldcol.columnName, newcol.columnName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!oldcol.notNull) {
|
||||||
|
this.fillNewNotNullDefaults(newcol);
|
||||||
|
}
|
||||||
|
|
||||||
if (!testEqualTypes(oldcol, newcol) || oldcol.notNull != newcol.notNull) {
|
if (!testEqualTypes(oldcol, newcol) || oldcol.notNull != newcol.notNull) {
|
||||||
this.putCmd(
|
this.putCmd(
|
||||||
'^alter ^table %f ^modify (%i %s %k)',
|
'^alter ^table %f ^modify (%i %s %k)',
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ class Dumper extends SqlDumper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (oldcol.notNull != newcol.notNull) {
|
if (oldcol.notNull != newcol.notNull) {
|
||||||
this.fillNewNotNullDefaults(newcol);
|
if (!oldcol.notNull) {
|
||||||
|
this.fillNewNotNullDefaults(newcol);
|
||||||
|
}
|
||||||
if (newcol.notNull) this.putCmd('^alter ^table %f ^alter ^column %i ^set ^not ^null', newcol, newcol.columnName);
|
if (newcol.notNull) this.putCmd('^alter ^table %f ^alter ^column %i ^set ^not ^null', newcol, newcol.columnName);
|
||||||
else this.putCmd('^alter ^table %f ^alter ^column %i ^drop ^not ^null', newcol, newcol.columnName);
|
else this.putCmd('^alter ^table %f ^alter ^column %i ^drop ^not ^null', newcol, newcol.columnName);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user