Skip to content

Commit

Permalink
Update defaultTo to stringify jsonb columns. Fixes #3167
Browse files Browse the repository at this point in the history
  • Loading branch information
lorefnon committed Jun 30, 2019
1 parent 141b202 commit 8158eb3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion scripts/docker-compose.yml
Expand Up @@ -22,7 +22,7 @@ services:
- 'until /opt/mssql-tools/bin/sqlcmd -S mssql -U sa -P S0meVeryHardPassword -d master -Q "CREATE DATABASE knex_test"; do sleep 5; done'

mysql:
image: mysql
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
ports:
- '23306:3306'
Expand Down
4 changes: 2 additions & 2 deletions src/schema/columncompiler.js
Expand Up @@ -154,8 +154,8 @@ ColumnCompiler.prototype.defaultTo = function(value) {
} else if (this.type === 'bool') {
if (value === 'false') value = 0;
value = `'${value ? 1 : 0}'`;
} else if (this.type === 'json' && isObject(value)) {
return JSON.stringify(value);
} else if ((this.type === 'json' || this.type === 'jsonb') && isObject(value)) {
value = `'${JSON.stringify(value)}'`;
} else {
value = `'${value}'`;
}
Expand Down
16 changes: 10 additions & 6 deletions test/integration/schema/index.js
Expand Up @@ -445,29 +445,33 @@ module.exports = function(knex) {
.notNullable()
.primary();
table.text('paragraph').defaultTo('Lorem ipsum Qui quis qui in.');
table.json('metadata').defaultTo({a: 10});
if (knex.client.driverName === 'pg') {
table.jsonb('details').defaultTo({b: {d: 20}});
}
})
.testSql(function(tester) {
tester('mysql', [
'create table `test_table_three` (`main` int not null, `paragraph` text) default character set utf8 engine = InnoDB',
'create table `test_table_three` (`main` int not null, `paragraph` text, `metadata` json default \'{"a":10}\') default character set utf8 engine = InnoDB',
'alter table `test_table_three` add primary key `test_table_three_pkey`(`main`)',
]);
tester('pg', [
'create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\')',
'create table "test_table_three" ("main" integer not null, "paragraph" text default \'Lorem ipsum Qui quis qui in.\', "metadata" json default \'{"a":10}\', "details" jsonb default \'{"b":{"d":20}}\')',
'alter table "test_table_three" add constraint "test_table_three_pkey" primary key ("main")',
]);
tester('pg-redshift', [
'create table "test_table_three" ("main" integer not null, "paragraph" varchar(max) default \'Lorem ipsum Qui quis qui in.\')',
'create table "test_table_three" ("main" integer not null, "paragraph" varchar(max) default \'Lorem ipsum Qui quis qui in.\', "metadata" json default \'{"a":10}\', "details" jsonb default \'{"b":{"d":20}}\')',
'alter table "test_table_three" add constraint "test_table_three_pkey" primary key ("main")',
]);
tester('sqlite3', [
"create table `test_table_three` (`main` integer not null, `paragraph` text default 'Lorem ipsum Qui quis qui in.', primary key (`main`))",
"create table `test_table_three` (`main` integer not null, `paragraph` text default 'Lorem ipsum Qui quis qui in.', `metadata` json default '{\"a\":10}', primary key (`main`))",
]);
tester('oracledb', [
'create table "test_table_three" ("main" integer not null, "paragraph" clob default \'Lorem ipsum Qui quis qui in.\')',
'create table "test_table_three" ("main" integer not null, "paragraph" clob default \'Lorem ipsum Qui quis qui in.\', "metadata" clob default \'{"a":10}\')',
'alter table "test_table_three" add constraint "test_table_three_pkey" primary key ("main")',
]);
tester('mssql', [
'CREATE TABLE [test_table_three] ([main] int not null, [paragraph] nvarchar(max), CONSTRAINT [test_table_three_pkey] PRIMARY KEY ([main]))',
'CREATE TABLE [test_table_three] ([main] int not null, [paragraph] nvarchar(max), [metadata] text default \'{"a":10}\', CONSTRAINT [test_table_three_pkey] PRIMARY KEY ([main]))',
]);
});
});
Expand Down
16 changes: 15 additions & 1 deletion test/unit/schema/postgres.js
Expand Up @@ -1162,7 +1162,21 @@ describe('PostgreSQL SchemaBuilder', function() {
})
.toSQL();
expect(tableSql[0].sql).to.equal(
'alter table "user" add column "preferences" json not null {}'
'alter table "user" add column "preferences" json not null default \'{}\''
);
});

it('allows adding default jsonb objects when the column is json', function() {
tableSql = client
.schemaBuilder()
.table('user', function(t) {
t.jsonb('preferences')
.defaultTo({})
.notNullable();
})
.toSQL();
expect(tableSql[0].sql).to.equal(
'alter table "user" add column "preferences" jsonb not null default \'{}\''
);
});

Expand Down
2 changes: 1 addition & 1 deletion test/unit/schema/redshift.js
Expand Up @@ -689,7 +689,7 @@ describe('Redshift SchemaBuilder', function() {
})
.toSQL();
expect(tableSql[0].sql).to.equal(
'alter table "user" add column "preferences" varchar(max) not null {}'
'alter table "user" add column "preferences" varchar(max) not null default \'{}\''
);
});

Expand Down

0 comments on commit 8158eb3

Please sign in to comment.