Skip to content

Commit

Permalink
Merge 9c6c99d into 0a85331
Browse files Browse the repository at this point in the history
  • Loading branch information
lorefnon committed Jun 30, 2019
2 parents 0a85331 + 9c6c99d commit 878883a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
11 changes: 0 additions & 11 deletions src/dialects/mssql/schema/columncompiler.js
Expand Up @@ -82,17 +82,6 @@ assign(ColumnCompiler_MSSQL.prototype, {
// Modifiers
// ------

defaultTo(value) {
const defaultVal = ColumnCompiler_MSSQL.super_.prototype.defaultTo.apply(
this,
arguments
);
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
return defaultVal;
}
return '';
},

first() {
this.client.logger.warn('Column first modifier not available for MSSQL');
return '';
Expand Down
5 changes: 4 additions & 1 deletion src/dialects/mysql/schema/columncompiler.js
Expand Up @@ -123,7 +123,10 @@ Object.assign(ColumnCompiler_MySQL.prototype, {
if (value === null || value === undefined) {
return;
}

if ((this.type === 'json' || this.type === 'jsonb') && isObject(value)) {
// Default value for json will work only it is an expression
return `default ('${JSON.stringify(value)}')`;
}
const defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(
this,
arguments
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
46 changes: 39 additions & 7 deletions test/integration/schema/index.js
Expand Up @@ -437,6 +437,8 @@ module.exports = function(knex) {
});

it('sets default values with defaultTo', function() {
const defaultMetadata = { a: 10 };
const defaultDetails = { b: { d: 20 } };
return knex.schema
.createTable('test_table_three', function(table) {
table.engine('InnoDB');
Expand All @@ -445,31 +447,61 @@ module.exports = function(knex) {
.notNullable()
.primary();
table.text('paragraph').defaultTo('Lorem ipsum Qui quis qui in.');
table.json('metadata').defaultTo(defaultMetadata);
if (knex.client.driverName === 'pg') {
table.jsonb('details').defaultTo(defaultDetails);
}
})
.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) default \'Lorem ipsum Qui quis qui in.\', [metadata] text default \'{"a":10}\', CONSTRAINT [test_table_three_pkey] PRIMARY KEY ([main]))',
]);
});
})
.then(function () {
return knex('test_table_three').insert([{
main: 1
}])
})
.then(function() {
return knex('test_table_three').where({main: 1}).first()
})
.then(function(result) {
expect(result.main).to.equal(1);
if (!knex.client.driverName.match(/^mysql/)) {
// MySQL doesn't support default values in text columns
expect(result.paragraph).to.eql(
'Lorem ipsum Qui quis qui in.'
);
return;
}
if (knex.client.driverName === 'pg') {
expect(result.metadata).to.eql(defaultMetadata);
expect(result.details).to.eql(defaultDetails);
} else if (_.isString(result.metadata)) {
expect(JSON.parse(result.metadata)).to.eql(defaultMetadata);
} else {
expect(result.metadata).to.eql(defaultMetadata);
}
})
});

it('handles numeric length correctly', function() {
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 878883a

Please sign in to comment.