2018-06-29 10:47:06 +03:00
|
|
|
'use strict';
|
2018-07-09 08:10:34 -04:00
|
|
|
const tape = require('tape');
|
2020-02-25 03:24:30 +03:00
|
|
|
const { expect } = require('chai');
|
2021-03-08 07:16:07 -05:00
|
|
|
const { isOracle } = require('../util/db-helpers');
|
2018-06-29 10:47:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of tests for making sure that certain features are cross database compatible
|
|
|
|
*/
|
2020-04-19 00:40:23 +02:00
|
|
|
module.exports = function (knex) {
|
2018-07-08 14:10:51 +03:00
|
|
|
const driverName = knex.client.driverName;
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2021-03-08 07:16:07 -05:00
|
|
|
if (isOracle(knex)) {
|
2018-06-29 10:47:06 +03:00
|
|
|
// TODO: FIX ORACLE TO WORK THE SAME WAY WITH OTHER DIALECTS IF POSSIBLE
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-25 03:24:30 +03:00
|
|
|
tape(
|
|
|
|
driverName + ' - crossdb compatibility: setup test table',
|
2020-04-19 00:40:23 +02:00
|
|
|
async function (t) {
|
2020-02-25 03:24:30 +03:00
|
|
|
try {
|
|
|
|
await knex.schema
|
|
|
|
.dropTableIfExists('test_table')
|
2020-04-19 00:40:23 +02:00
|
|
|
.createTable('test_table', function (t) {
|
2020-02-25 03:24:30 +03:00
|
|
|
t.integer('id');
|
|
|
|
t.string('first');
|
|
|
|
t.string('second');
|
|
|
|
t.string('third').unique();
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
});
|
|
|
|
} finally {
|
2018-06-29 10:47:06 +03:00
|
|
|
t.end();
|
2020-02-25 03:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
tape(
|
|
|
|
driverName +
|
|
|
|
' - crossdb compatibility: table may have multiple nulls in unique constrainted column',
|
2020-02-25 03:24:30 +03:00
|
|
|
async (t) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(3);
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2020-02-25 03:24:30 +03:00
|
|
|
try {
|
|
|
|
await expect(
|
|
|
|
knex('test_table').insert([{ third: 'foo' }, { third: 'foo' }])
|
|
|
|
).to.be.eventually.rejected;
|
|
|
|
t.assert(true, 'unique constraint prevents adding rows');
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
knex('test_table').insert([
|
2018-07-09 08:10:34 -04:00
|
|
|
{ first: 'foo2', second: 'bar2' },
|
|
|
|
{ first: 'foo2', second: 'bar2' },
|
2020-02-25 03:24:30 +03:00
|
|
|
])
|
|
|
|
).to.be.eventually.rejected;
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2020-02-25 03:24:30 +03:00
|
|
|
t.assert(true, 'two column unique constraint prevents adding rows');
|
|
|
|
|
|
|
|
// even one null makes index to not match, thus allows adding the row
|
|
|
|
await knex('test_table').insert([
|
|
|
|
{ first: 'fo', second: null, third: null },
|
|
|
|
{ first: 'fo', second: null, third: null },
|
|
|
|
{ first: null, second: 'fo', third: null },
|
|
|
|
{ first: null, second: 'fo', third: null },
|
|
|
|
{ first: null, second: null, third: null },
|
|
|
|
]);
|
|
|
|
|
|
|
|
const res = await knex('test_table');
|
|
|
|
t.assert(
|
|
|
|
res.length === 5,
|
|
|
|
'multiple rows with nulls could be added despite of unique constraints'
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
t.end();
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
);
|
2018-06-29 10:47:06 +03:00
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
tape(
|
|
|
|
driverName + ' - create and drop index works in different cases',
|
2020-02-25 03:24:30 +03:00
|
|
|
async (t) => {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2020-02-25 03:24:30 +03:00
|
|
|
try {
|
|
|
|
await knex.schema
|
|
|
|
.dropTableIfExists('test_table_drop_unique')
|
|
|
|
.createTable('test_table_drop_unique', (t) => {
|
|
|
|
t.integer('id');
|
|
|
|
t.string('first');
|
|
|
|
t.string('second');
|
|
|
|
t.string('third').unique();
|
|
|
|
t.string('fourth');
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
t.unique('fourth');
|
|
|
|
})
|
|
|
|
.alterTable('test_table_drop_unique', (t) => {
|
|
|
|
t.dropUnique('third');
|
|
|
|
t.dropUnique('fourth');
|
|
|
|
t.dropUnique(['first', 'second']);
|
|
|
|
})
|
|
|
|
.alterTable('test_table_drop_unique', (t) => {
|
|
|
|
t.unique(['first', 'second']);
|
|
|
|
t.unique('third');
|
|
|
|
t.unique('fourth');
|
|
|
|
});
|
|
|
|
t.assert(
|
|
|
|
true,
|
|
|
|
'Creating / dropping / creating unique constraint was a success'
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
t.end();
|
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|