From b31b477f497daab00554d8ee54f1cb4902b63816 Mon Sep 17 00:00:00 2001 From: Nicola Krumschmidt Date: Thu, 1 Sep 2022 15:50:12 +0200 Subject: [PATCH] Fix changing the default value of a boolean column in SQLite The `formatDefault()` function expects the type parameter to be a Knex internal type instead of a SQL type. --- .../sqlite3/schema/sqlite-tablecompiler.js | 2 +- test/integration2/schema/alter.spec.js | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js b/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js index 930693f182..2d57e06821 100644 --- a/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js +++ b/lib/dialects/sqlite3/schema/sqlite-tablecompiler.js @@ -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 = diff --git a/test/integration2/schema/alter.spec.js b/test/integration2/schema/alter.spec.js index cbfbbaaeb7..2fff3444a9 100644 --- a/test/integration2/schema/alter.spec.js +++ b/test/integration2/schema/alter.spec.js @@ -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') @@ -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)" ); }); @@ -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(); }); @@ -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)' ); }); @@ -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)" ); }); @@ -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"',