mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-24 22:16:00 +00:00
sql dumper - create index
This commit is contained in:
@@ -1,10 +1,23 @@
|
|||||||
const stableStringify = require('json-stable-stringify');
|
const stableStringify = require('json-stable-stringify');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const fp = require('lodash/fp');
|
||||||
const uuidv1 = require('uuid/v1');
|
const uuidv1 = require('uuid/v1');
|
||||||
const { testWrapper, checkTableStructure } = require('../tools');
|
const { testWrapper } = require('../tools');
|
||||||
const engines = require('../engines');
|
const engines = require('../engines');
|
||||||
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
|
const { getAlterTableScript, extendDatabaseInfo, generateDbPairingId } = require('dbgate-tools');
|
||||||
|
|
||||||
|
function pickImportantTableInfo(table) {
|
||||||
|
return {
|
||||||
|
pureName: table.pureName,
|
||||||
|
columns: table.columns.map(fp.pick(['columnName', 'notNull', 'autoIncrement'])),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkTableStructure(t1, t2) {
|
||||||
|
// expect(t1.pureName).toEqual(t2.pureName)
|
||||||
|
expect(pickImportantTableInfo(t1)).toEqual(pickImportantTableInfo(t2));
|
||||||
|
}
|
||||||
|
|
||||||
async function testTableDiff(conn, driver, mangle) {
|
async function testTableDiff(conn, driver, mangle) {
|
||||||
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,32 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
|
const fp = require('lodash/fp');
|
||||||
const engines = require('../engines');
|
const engines = require('../engines');
|
||||||
const { testWrapper, checkTableStructure } = require('../tools');
|
const { testWrapper } = require('../tools');
|
||||||
const { extendDatabaseInfo } = require('dbgate-tools');
|
const { extendDatabaseInfo } = require('dbgate-tools');
|
||||||
|
|
||||||
|
function createExpector(value) {
|
||||||
|
return _.cloneDeepWith(value, x => {
|
||||||
|
if (_.isPlainObject(x)) {
|
||||||
|
return expect.objectContaining(_.mapValues(x, y => createExpector(y)));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function omitTableSpecificInfo(table) {
|
||||||
|
return {
|
||||||
|
...table,
|
||||||
|
columns: table.columns.map(fp.omit(['dataType'])),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkTableStructure2(t1, t2) {
|
||||||
|
// expect(t1.pureName).toEqual(t2.pureName)
|
||||||
|
expect(t2).toEqual(createExpector(omitTableSpecificInfo(t1)));
|
||||||
|
}
|
||||||
|
|
||||||
async function testTableCreate(conn, driver, table) {
|
async function testTableCreate(conn, driver, table) {
|
||||||
|
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
||||||
|
|
||||||
const dmp = driver.createDumper();
|
const dmp = driver.createDumper();
|
||||||
const table1 = {
|
const table1 = {
|
||||||
...table,
|
...table,
|
||||||
@@ -11,17 +35,17 @@ async function testTableCreate(conn, driver, table) {
|
|||||||
dmp.createTable(table1);
|
dmp.createTable(table1);
|
||||||
|
|
||||||
console.log('RUNNING CREATE SQL', driver.engine, ':', dmp.s);
|
console.log('RUNNING CREATE SQL', driver.engine, ':', dmp.s);
|
||||||
await driver.query(conn, dmp.s);
|
await driver.script(conn, dmp.s);
|
||||||
|
|
||||||
const db = extendDatabaseInfo(await driver.analyseFull(conn));
|
const db = extendDatabaseInfo(await driver.analyseFull(conn));
|
||||||
const table2 = db.tables.find(x => x.pureName == 'tested');
|
const table2 = db.tables.find(x => x.pureName == 'tested');
|
||||||
|
|
||||||
checkTableStructure(table1, table2);
|
checkTableStructure2(table1, table2);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Table create', () => {
|
describe('Table create', () => {
|
||||||
test.each(engines.map(engine => [engine.label, engine]))(
|
test.each(engines.map(engine => [engine.label, engine]))(
|
||||||
'Table structure - full analysis - %s',
|
'Simple table - %s',
|
||||||
testWrapper(async (conn, driver, engine) => {
|
testWrapper(async (conn, driver, engine) => {
|
||||||
await testTableCreate(conn, driver, {
|
await testTableCreate(conn, driver, {
|
||||||
columns: [
|
columns: [
|
||||||
@@ -29,7 +53,6 @@ describe('Table create', () => {
|
|||||||
columnName: 'col1',
|
columnName: 'col1',
|
||||||
dataType: 'int',
|
dataType: 'int',
|
||||||
notNull: true,
|
notNull: true,
|
||||||
autoIncrement: false,
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
primaryKey: {
|
primaryKey: {
|
||||||
@@ -38,4 +61,34 @@ describe('Table create', () => {
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
test.each(engines.map(engine => [engine.label, engine]))(
|
||||||
|
'Table with index - %s',
|
||||||
|
testWrapper(async (conn, driver, engine) => {
|
||||||
|
await testTableCreate(conn, driver, {
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
columnName: 'col1',
|
||||||
|
dataType: 'int',
|
||||||
|
notNull: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
columnName: 'col2',
|
||||||
|
dataType: 'int',
|
||||||
|
notNull: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
primaryKey: {
|
||||||
|
columns: [{ columnName: 'col1' }],
|
||||||
|
},
|
||||||
|
indexes: [
|
||||||
|
{
|
||||||
|
constraintName: 'ix1',
|
||||||
|
pureName: 'tested',
|
||||||
|
columns: [{ columnName: 'col2' }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
global.DBGATE_TOOLS = require('dbgate-tools');
|
global.DBGATE_TOOLS = require('dbgate-tools');
|
||||||
const requireEngineDriver = require('dbgate-api/src/utility/requireEngineDriver');
|
const requireEngineDriver = require('dbgate-api/src/utility/requireEngineDriver');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const fp = require('lodash/fp');
|
|
||||||
|
|
||||||
function randomDbName() {
|
function randomDbName() {
|
||||||
const generatedKey = crypto.randomBytes(6);
|
const generatedKey = crypto.randomBytes(6);
|
||||||
@@ -56,22 +55,9 @@ const testWrapper = body => async (label, ...other) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function pickImportantTableInfo(table) {
|
|
||||||
return {
|
|
||||||
pureName: table.pureName,
|
|
||||||
columns: table.columns.map(fp.pick(['columnName', 'notNull', 'autoIncrement'])),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkTableStructure(t1, t2) {
|
|
||||||
// expect(t1.pureName).toEqual(t2.pureName)
|
|
||||||
expect(pickImportantTableInfo(t1)).toEqual(pickImportantTableInfo(t2));
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
randomDbName,
|
randomDbName,
|
||||||
connect,
|
connect,
|
||||||
extractConnection,
|
extractConnection,
|
||||||
testWrapper,
|
testWrapper,
|
||||||
checkTableStructure,
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -249,10 +249,9 @@ export class SqlDumper implements AlterProcessor {
|
|||||||
// }
|
// }
|
||||||
this.put('&<&n)');
|
this.put('&<&n)');
|
||||||
this.endCommand();
|
this.endCommand();
|
||||||
// foreach (var ix in table.Indexes)
|
for (const ix of table.indexes) {
|
||||||
// {
|
this.createIndex(ix);
|
||||||
// CreateIndex(ix);
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
createForeignKeyFore(fk: ForeignKeyInfo) {
|
createForeignKeyFore(fk: ForeignKeyInfo) {
|
||||||
|
|||||||
Reference in New Issue
Block a user