2021-09-05 07:38:38 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const fp = require('lodash/fp');
|
2021-09-05 06:42:56 +00:00
|
|
|
const engines = require('../engines');
|
2021-09-05 07:38:38 +00:00
|
|
|
const { testWrapper } = require('../tools');
|
2021-09-05 06:42:56 +00:00
|
|
|
const { extendDatabaseInfo } = require('dbgate-tools');
|
|
|
|
|
2021-09-05 07:38:38 +00:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2021-09-05 06:42:56 +00:00
|
|
|
async function testTableCreate(conn, driver, table) {
|
2021-09-05 07:38:38 +00:00
|
|
|
await driver.query(conn, `create table t0 (id int not null primary key)`);
|
|
|
|
|
2021-09-05 06:42:56 +00:00
|
|
|
const dmp = driver.createDumper();
|
|
|
|
const table1 = {
|
|
|
|
...table,
|
|
|
|
pureName: 'tested',
|
|
|
|
};
|
|
|
|
dmp.createTable(table1);
|
|
|
|
|
|
|
|
console.log('RUNNING CREATE SQL', driver.engine, ':', dmp.s);
|
2021-09-05 07:38:38 +00:00
|
|
|
await driver.script(conn, dmp.s);
|
2021-09-05 06:42:56 +00:00
|
|
|
|
|
|
|
const db = extendDatabaseInfo(await driver.analyseFull(conn));
|
|
|
|
const table2 = db.tables.find(x => x.pureName == 'tested');
|
|
|
|
|
2021-09-05 07:38:38 +00:00
|
|
|
checkTableStructure2(table1, table2);
|
2021-09-05 06:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('Table create', () => {
|
|
|
|
test.each(engines.map(engine => [engine.label, engine]))(
|
2021-09-05 07:38:38 +00:00
|
|
|
'Simple table - %s',
|
2021-09-05 06:42:56 +00:00
|
|
|
testWrapper(async (conn, driver, engine) => {
|
|
|
|
await testTableCreate(conn, driver, {
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
columnName: 'col1',
|
|
|
|
dataType: 'int',
|
|
|
|
notNull: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
primaryKey: {
|
|
|
|
columns: [{ columnName: 'col1' }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2021-09-05 07:38:38 +00:00
|
|
|
|
2024-09-16 07:50:36 +00:00
|
|
|
test.each(engines.filter(x => !x.skipIndexes).map(engine => [engine.label, engine]))(
|
2021-09-05 07:38:38 +00:00
|
|
|
'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' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2021-09-05 07:44:40 +00:00
|
|
|
|
2024-09-16 07:50:36 +00:00
|
|
|
test.each(engines.filter(x => !x.skipReferences).map(engine => [engine.label, engine]))(
|
2021-09-05 07:44:40 +00:00
|
|
|
'Table with foreign key - %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' }],
|
|
|
|
},
|
|
|
|
foreignKeys: [
|
|
|
|
{
|
|
|
|
pureName: 'tested',
|
|
|
|
refTableName: 't0',
|
|
|
|
columns: [{ columnName: 'col2', refColumnName: 'id' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-09-16 07:50:36 +00:00
|
|
|
test.each(engines.filter(x => !x.skipUnique).map(engine => [engine.label, engine]))(
|
2021-09-05 07:44:40 +00:00
|
|
|
'Table with unique - %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' }],
|
|
|
|
},
|
|
|
|
uniques: [
|
|
|
|
{
|
|
|
|
pureName: 'tested',
|
|
|
|
columns: [{ columnName: 'col2' }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2021-09-05 06:42:56 +00:00
|
|
|
});
|