Skip to content

Commit

Permalink
lib: Fix bug with columns with a default value
Browse files Browse the repository at this point in the history
"SHOW CREATE TABLE" formats numbers and booleans in a specific way
so mimic this formatting so that columns with a default value
don't get modified during every sync.
  • Loading branch information
nwoltman committed Feb 17, 2017
1 parent 7daccb9 commit 8f1e963
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
13 changes: 12 additions & 1 deletion lib/ColumnDefinitions/ColumnDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

const mysql = require('mysql');

function escapeDefault(value) {
if (typeof value === 'boolean') {
return value ? "'1'" : "'0'";
}
if (typeof value === 'number') {
return "'" + value + "'";
}

return mysql.escape(value);
}

class ColumnDefinition {
constructor(type, m, d) {
this._baseType = type;
Expand Down Expand Up @@ -42,7 +53,7 @@ class ColumnDefinition {
}

default(value) {
this._default = mysql.escape(value);
this._default = escapeDefault(value);
return this;
}

Expand Down
8 changes: 4 additions & 4 deletions test/integration/MySQLPlus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('MySQLPlus', function() {
const columnsTableName = 'columns_table';
const columnsTableSchema = {
columns: {
id: ColTypes.int().unsigned().notNull().primaryKey(),
id: ColTypes.int().unsigned().notNull().primaryKey().default(1),
uuid: ColTypes.char(44).unique(),
email: ColTypes.char(255),
fp: ColTypes.float(7, 4),
Expand All @@ -146,7 +146,7 @@ describe('MySQLPlus', function() {
};
const columnsTableExpectedSQL =
'CREATE TABLE `columns_table` (\n' +
' `id` int(10) unsigned NOT NULL,\n' +
' `id` int(10) unsigned NOT NULL DEFAULT \'1\',\n' +
' `uuid` char(44) DEFAULT NULL,\n' +
' `email` char(255) DEFAULT NULL,\n' +
' `fp` float(7,4) DEFAULT NULL,\n' +
Expand All @@ -163,7 +163,7 @@ describe('MySQLPlus', function() {

const columnsTableMigratedSchema = {
columns: {
id: ColTypes.bigint(5).unsigned().notNull().primaryKey(),
id: ColTypes.bigint(5).unsigned().notNull().primaryKey().default(2),
uuid: ColTypes.char(44).unique(),
email: ColTypes.varchar(255).notNull(),
fp: ColTypes.float(8, 3),
Expand All @@ -178,7 +178,7 @@ describe('MySQLPlus', function() {
};
const columnsTableMigratedExpectedSQL =
'CREATE TABLE `columns_table` (\n' +
' `id` bigint(5) unsigned NOT NULL,\n' +
' `id` bigint(5) unsigned NOT NULL DEFAULT \'2\',\n' +
' `uuid` char(44) DEFAULT NULL,\n' +
' `email` varchar(255) NOT NULL,\n' +
' `fp` float(8,3) DEFAULT NULL,\n' +
Expand Down
28 changes: 25 additions & 3 deletions test/unit/ColumnDefinitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ describe('ColumnDefinitions', () => {
b = ColumnDefinitions.blob().default('a');
a.$equals(b).should.be.true();

a = ColumnDefinitions.smallint().default(1);
b = ColumnDefinitions.smallint().default(1);
a.$equals(b).should.be.true();

a = ColumnDefinitions.bool().default(true);
b = ColumnDefinitions.bool().default(true);
a.$equals(b).should.be.true();

a = ColumnDefinitions.blob().notNull();
b = ColumnDefinitions.blob().notNull().default('a');
a.$equals(b).should.be.false();
Expand All @@ -42,6 +50,14 @@ describe('ColumnDefinitions', () => {
b = ColumnDefinitions.blob().notNull().default('b');
a.$equals(b).should.be.false();

a = ColumnDefinitions.smallint().notNull().default(1);
b = ColumnDefinitions.smallint().notNull().default(2);
a.$equals(b).should.be.false();

a = ColumnDefinitions.bool().notNull().default(true);
b = ColumnDefinitions.bool().notNull().default(false);
a.$equals(b).should.be.false();

a = ColumnDefinitions.int().unsigned().zerofill().notNull().default(2).autoIncrement();
b = ColumnDefinitions.int().unsigned().zerofill().notNull().default(2).autoIncrement();
a.$equals(b).should.be.true();
Expand Down Expand Up @@ -367,8 +383,14 @@ describe('ColumnDefinitions', () => {
describe('all data types', () => {

it('should be able to generate SQL with the DEFAULT or NOT NULL attributes', () => {
ColumnDefinitions.bool().notNull().default(true)
.$toSQL().should.equal('bool NOT NULL DEFAULT \'1\'');

ColumnDefinitions.bool().notNull().default(false)
.$toSQL().should.equal('bool NOT NULL DEFAULT \'0\'');

ColumnDefinitions.int().notNull().default(1)
.$toSQL().should.equal('int NOT NULL DEFAULT 1');
.$toSQL().should.equal('int NOT NULL DEFAULT \'1\'');

ColumnDefinitions.char().notNull().default('1')
.$toSQL().should.equal('char NOT NULL DEFAULT \'1\'');
Expand All @@ -385,7 +407,7 @@ describe('ColumnDefinitions', () => {

it('should allow the columns to be defined as keys, but not change the SQL', () => {
ColumnDefinitions.int().notNull().default(1).index()
.$toSQL().should.equal('int NOT NULL DEFAULT 1');
.$toSQL().should.equal('int NOT NULL DEFAULT \'1\'');

ColumnDefinitions.char().notNull().default('1').index()
.$toSQL().should.equal('char NOT NULL DEFAULT \'1\'');
Expand All @@ -411,7 +433,7 @@ describe('ColumnDefinitions', () => {

it('should allow the columns to be defined as primary keys, but not change the SQL, except for forcing the column to be NOT NULL', () => {
ColumnDefinitions.int().notNull().default(1).primaryKey()
.$toSQL().should.equal('int NOT NULL DEFAULT 1');
.$toSQL().should.equal('int NOT NULL DEFAULT \'1\'');

ColumnDefinitions.char().notNull().default('1').primaryKey()
.$toSQL().should.equal('char NOT NULL DEFAULT \'1\'');
Expand Down

0 comments on commit 8f1e963

Please sign in to comment.