Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing the default value of a boolean column in SQLite #5319

Merged
merged 1 commit into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dialects/sqlite3/schema/sqlite-tablecompiler.js
Expand Up @@ -51,7 +51,7 @@ class TableCompiler_SQLite3 extends TableCompiler {
const type = col.getColumnType();

const defaultTo = col.modified['defaultTo']
? formatDefault(col.modified['defaultTo'][0], type, this.client)
? formatDefault(col.modified['defaultTo'][0], col.type, this.client)
: null;

const notNull =
Expand Down
34 changes: 29 additions & 5 deletions test/integration2/schema/alter.spec.js
Expand Up @@ -34,6 +34,7 @@ describe('Schema', () => {
table.dateTime('column_datetime');

table.integer('column_defaultTo').defaultTo(0);
table.boolean('column_boolean_defaultTo').defaultTo(false);
table.string('column_notNullable').notNullable();
table
.dateTime('column_defaultToAndNotNullable')
Expand Down Expand Up @@ -96,7 +97,7 @@ describe('Schema', () => {
expect(item_one.column_string).to.be.a('number');
expect(item_one.column_datetime).to.be.a('number');
expect(tableAfter).to.equal(
"CREATE TABLE \"alter_table\" (`column_integer` varchar(255), `column_string` integer, `column_datetime` date, `column_defaultTo` integer DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)"
"CREATE TABLE \"alter_table\" (`column_integer` varchar(255), `column_string` integer, `column_datetime` date, `column_defaultTo` integer DEFAULT '0', `column_boolean_defaultTo` boolean DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)"
);
});

Expand Down Expand Up @@ -128,13 +129,14 @@ describe('Schema', () => {
"insert into `alter_table` (`column_notNullable`) values ('text') - SQLITE_CONSTRAINT: NOT NULL constraint failed: alter_table.column_string"
);
expect(tableAfter).to.equal(
"CREATE TABLE \"alter_table\" (`column_integer` integer DEFAULT '0', `column_string` varchar(255) NOT NULL, `column_datetime` datetime NOT NULL DEFAULT '0', `column_defaultTo` integer DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)"
"CREATE TABLE \"alter_table\" (`column_integer` integer DEFAULT '0', `column_string` varchar(255) NOT NULL, `column_datetime` datetime NOT NULL DEFAULT '0', `column_defaultTo` integer DEFAULT '0', `column_boolean_defaultTo` boolean DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)"
);
});

it('removes not specified default and not null constraints', async () => {
await knex.schema.alterTable('alter_table', (table) => {
table.integer('column_defaultTo').alter();
table.boolean('column_boolean_defaultTo').alter();
table.string('column_notNullable').alter();
table.dateTime('column_defaultToAndNotNullable').alter();
});
Expand All @@ -145,10 +147,11 @@ describe('Schema', () => {
const tableAfter = (await knex.raw(QUERY_TABLE))[0].sql;

expect(item_two.column_defaultTo).to.be.null;
expect(item_two.column_boolean_defaultTo).to.be.null;
expect(item_two.column_notNullable).to.be.null;
expect(item_two.column_defaultToAndNotNullable).to.be.null;
expect(tableAfter).to.equal(
'CREATE TABLE "alter_table" (`column_integer` integer, `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer, `column_notNullable` varchar(255), `column_defaultToAndNotNullable` datetime, `column_nullable` boolean NULL)'
'CREATE TABLE "alter_table" (`column_integer` integer, `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer, `column_boolean_defaultTo` boolean, `column_notNullable` varchar(255), `column_defaultToAndNotNullable` datetime, `column_nullable` boolean NULL)'
);
});

Expand All @@ -166,7 +169,28 @@ describe('Schema', () => {
"insert into `alter_table` (`column_notNullable`) values ('text') - SQLITE_CONSTRAINT: NOT NULL constraint failed: alter_table.column_nullable"
);
expect(tableAfter).to.equal(
"CREATE TABLE \"alter_table\" (`column_integer` integer, `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NOT NULL)"
"CREATE TABLE \"alter_table\" (`column_integer` integer, `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_boolean_defaultTo` boolean DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NOT NULL)"
);
});

it('properly changes the default value of a boolean column', async () => {
await knex.schema.alterTable('alter_table', (table) => {
table
.boolean('column_boolean_defaultTo')
.defaultTo(true)
.alter();
});

await knex('alter_table').insert({
column_notNullable: 'text',
});

const item_two = (await knex('alter_table'))[1];
const tableAfter = (await knex.raw(QUERY_TABLE))[0].sql;

expect(item_two.column_boolean_defaultTo).to.equal(1);
expect(tableAfter).to.equal(
"CREATE TABLE \"alter_table\" (`column_integer` integer, `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_boolean_defaultTo` boolean DEFAULT '1', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)"
);
});

Expand All @@ -178,7 +202,7 @@ describe('Schema', () => {
const queries = await builder.generateDdlCommands();

expect(queries.sql).to.deep.equal([
"CREATE TABLE `_knex_temp_alter111` (`column_integer` varchar(255), `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)",
"CREATE TABLE `_knex_temp_alter111` (`column_integer` varchar(255), `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_boolean_defaultTo` boolean DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0', `column_nullable` boolean NULL)",
'INSERT INTO "_knex_temp_alter111" SELECT * FROM "alter_table";',
'DROP TABLE "alter_table"',
'ALTER TABLE "_knex_temp_alter111" RENAME TO "alter_table"',
Expand Down