2021-08-28 20:41:54 +03:00
|
|
|
const chai = require('chai');
|
|
|
|
chai.use(require('chai-as-promised'));
|
|
|
|
const expect = chai.expect;
|
2021-09-19 17:14:06 +02:00
|
|
|
const sinon = require('sinon');
|
2021-09-03 15:25:22 -04:00
|
|
|
const {
|
|
|
|
isSQLite,
|
|
|
|
isPostgreSQL,
|
|
|
|
isMysql,
|
|
|
|
isOracle,
|
|
|
|
} = require('../../util/db-helpers');
|
2021-08-28 20:41:54 +03:00
|
|
|
const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider');
|
|
|
|
|
|
|
|
describe('Schema', () => {
|
|
|
|
describe('alter nullable', () => {
|
|
|
|
getAllDbs().forEach((db) => {
|
|
|
|
describe(db, () => {
|
|
|
|
let knex;
|
|
|
|
|
|
|
|
before(function () {
|
2021-09-19 17:14:06 +02:00
|
|
|
sinon.stub(Math, 'random').returns(0.1);
|
2021-08-28 20:41:54 +03:00
|
|
|
knex = getKnexForDb(db);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
2021-09-19 17:14:06 +02:00
|
|
|
sinon.restore();
|
2021-08-28 20:41:54 +03:00
|
|
|
return knex.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await knex.schema.createTable('primary_table', (table) => {
|
|
|
|
table.integer('id_nullable').nullable();
|
|
|
|
table.integer('id_not_nullable').notNull();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
await knex.schema.dropTable('primary_table');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setNullable', () => {
|
2021-09-19 17:14:06 +02:00
|
|
|
it('should generate correct SQL for set nullable operation', async () => {
|
|
|
|
const builder = knex.schema.table('primary_table', (table) => {
|
|
|
|
table.setNullable('id_not_nullable');
|
|
|
|
});
|
|
|
|
const queries = await builder.generateDdlCommands();
|
|
|
|
|
|
|
|
if (isSQLite(knex)) {
|
|
|
|
expect(queries.sql).to.eql([
|
|
|
|
'CREATE TABLE `_knex_temp_alter111` (`id_nullable` integer NULL, `id_not_nullable` integer)',
|
|
|
|
'INSERT INTO _knex_temp_alter111 SELECT * FROM primary_table;',
|
|
|
|
'DROP TABLE "primary_table"',
|
|
|
|
'ALTER TABLE "_knex_temp_alter111" RENAME TO "primary_table"',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPostgreSQL(knex)) {
|
|
|
|
expect(queries.sql).to.eql([
|
|
|
|
{
|
|
|
|
bindings: [],
|
|
|
|
sql: 'alter table "primary_table" alter column "id_not_nullable" drop not null',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-28 20:41:54 +03:00
|
|
|
it('sets column to be nullable', async () => {
|
|
|
|
await knex.schema.table('primary_table', (table) => {
|
|
|
|
table.setNullable('id_not_nullable');
|
|
|
|
});
|
|
|
|
|
|
|
|
await knex('primary_table').insert({
|
|
|
|
id_nullable: null,
|
|
|
|
id_not_nullable: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('dropNullable', () => {
|
|
|
|
it('sets column to be not nullable', async () => {
|
|
|
|
await knex.schema.table('primary_table', (table) => {
|
|
|
|
table.dropNullable('id_nullable');
|
|
|
|
});
|
|
|
|
|
|
|
|
let errorMessage;
|
2021-09-03 15:25:22 -04:00
|
|
|
if (isPostgreSQL(knex)) {
|
2021-08-28 20:41:54 +03:00
|
|
|
errorMessage = 'violates not-null constraint';
|
2021-09-03 15:25:22 -04:00
|
|
|
} else if (isMysql(knex)) {
|
2021-08-28 20:41:54 +03:00
|
|
|
errorMessage = 'cannot be null';
|
2021-09-03 15:25:22 -04:00
|
|
|
} else if (isOracle(knex)) {
|
|
|
|
errorMessage = 'ORA-01400: cannot insert NULL into';
|
2021-09-19 17:14:06 +02:00
|
|
|
} else if (isSQLite(knex)) {
|
|
|
|
errorMessage =
|
|
|
|
'insert into `primary_table` (`id_not_nullable`, `id_nullable`) values (1, NULL) - SQLITE_CONSTRAINT: NOT NULL constraint failed: primary_table.id_nullable';
|
2021-08-28 20:41:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
knex('primary_table').insert({
|
|
|
|
id_nullable: null,
|
|
|
|
id_not_nullable: 1,
|
|
|
|
})
|
|
|
|
).to.eventually.be.rejectedWith(errorMessage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|