Skip to content

Commit 79c5acb

Browse files
committed
ColumnDefinitions: Define certain numeric types as synonyms
This fixes a bug where using integer, dec, numeric, fixed, bool, or boolean would always cause the table to try to migrate if using the alter strategy.
1 parent 4f488bf commit 79c5acb

File tree

5 files changed

+86
-41
lines changed

5 files changed

+86
-41
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,17 +1133,17 @@ These schema properties configure table-level options. The options currently sup
11331133
+ `smallint([m])`
11341134
+ `mediumint([m])`
11351135
+ `int([m])`
1136-
+ `integer([m])`
1136+
+ `integer([m])` - synonym for `int`
11371137
+ `bigint([m])`
11381138
+ `float([m [, d]])`
11391139
+ `double([m [, d]])`
11401140
+ `decimal([m [, d]])`
1141-
+ `dec([m [, d]])`
1142-
+ `numeric([m [, d]])`
1143-
+ `fixed([m [, d]])`
1141+
+ `dec([m [, d]])` - synonym for `decimal`
1142+
+ `numeric([m [, d]])` - synonym for `decimal`
1143+
+ `fixed([m [, d]])` - synonym for `decimal`
11441144
+ `bit([m])`
1145-
+ `bool()`
1146-
+ `boolean()`
1145+
+ `bool()` - synonym for `tinyint(1)`
1146+
+ `boolean()` - synonym for `tinyint(1)`
11471147
+ `date()`
11481148
+ `datetime([m])`
11491149
+ `timestamp([m])`

jsdoc2md/README.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,17 +320,17 @@ These schema properties configure table-level options. The options currently sup
320320
+ `smallint([m])`
321321
+ `mediumint([m])`
322322
+ `int([m])`
323-
+ `integer([m])`
323+
+ `integer([m])` - synonym for `int`
324324
+ `bigint([m])`
325325
+ `float([m [, d]])`
326326
+ `double([m [, d]])`
327327
+ `decimal([m [, d]])`
328-
+ `dec([m [, d]])`
329-
+ `numeric([m [, d]])`
330-
+ `fixed([m [, d]])`
328+
+ `dec([m [, d]])` - synonym for `decimal`
329+
+ `numeric([m [, d]])` - synonym for `decimal`
330+
+ `fixed([m [, d]])` - synonym for `decimal`
331331
+ `bit([m])`
332-
+ `bool()`
333-
+ `boolean()`
332+
+ `bool()` - synonym for `tinyint(1)`
333+
+ `boolean()` - synonym for `tinyint(1)`
334334
+ `date()`
335335
+ `datetime([m])`
336336
+ `timestamp([m])`

lib/ColumnDefinitions/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ColumnDefinitions = {
2727
return new NumericColumnDefinition('int', m);
2828
},
2929
integer(m) {
30-
return new NumericColumnDefinition('integer', m);
30+
return new NumericColumnDefinition('int', m);
3131
},
3232
bigint(m) {
3333
return new NumericColumnDefinition('bigint', m);
@@ -42,22 +42,22 @@ const ColumnDefinitions = {
4242
return new NumericColumnDefinition('decimal', m, d);
4343
},
4444
dec(m, d) {
45-
return new NumericColumnDefinition('dec', m, d);
45+
return new NumericColumnDefinition('decimal', m, d);
4646
},
4747
numeric(m, d) {
48-
return new NumericColumnDefinition('numeric', m, d);
48+
return new NumericColumnDefinition('decimal', m, d);
4949
},
5050
fixed(m, d) {
51-
return new NumericColumnDefinition('fixed', m, d);
51+
return new NumericColumnDefinition('decimal', m, d);
5252
},
5353
bit(m) {
5454
return new ColumnDefinition('bit', m);
5555
},
5656
bool() {
57-
return new ColumnDefinition('bool');
57+
return new NumericColumnDefinition('tinyint', 1);
5858
},
5959
boolean() {
60-
return new ColumnDefinition('boolean');
60+
return new NumericColumnDefinition('tinyint', 1);
6161
},
6262
date() {
6363
return new ColumnDefinition('date');

test/integration/MySQLPlus.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,27 @@ describe('MySQLPlus', function() {
545545
" `e` timestamp NULL DEFAULT '2017-03-25 12:46:05'\n" +
546546
') ENGINE=InnoDB DEFAULT CHARSET=utf8';
547547

548+
const synonymTableName = 'synonym_table';
549+
const synonymTableSchema = {
550+
columns: {
551+
a: ColTypes.integer(),
552+
b: ColTypes.dec(),
553+
c: ColTypes.numeric(),
554+
d: ColTypes.fixed(),
555+
e: ColTypes.bool(),
556+
f: ColTypes.boolean(),
557+
},
558+
};
559+
const synonymTableExpectedSQL =
560+
'CREATE TABLE `synonym_table` (\n' +
561+
' `a` int(11) DEFAULT NULL,\n' +
562+
' `b` decimal(10,0) DEFAULT NULL,\n' +
563+
' `c` decimal(10,0) DEFAULT NULL,\n' +
564+
' `d` decimal(10,0) DEFAULT NULL,\n' +
565+
' `e` tinyint(1) DEFAULT NULL,\n' +
566+
' `f` tinyint(1) DEFAULT NULL\n' +
567+
') ENGINE=InnoDB DEFAULT CHARSET=utf8';
568+
548569

549570
describe('when creating new tables', () => {
550571

@@ -563,6 +584,7 @@ describe('MySQLPlus', function() {
563584
pool.defineTable(optionsTableName, optionsTableSchema);
564585
pool.defineTable(textTableName, textTableSchema);
565586
pool.defineTable(timestampTableName, timestampTableSchema);
587+
pool.defineTable(synonymTableName, synonymTableSchema);
566588
pool.sync(done);
567589
});
568590

@@ -656,6 +678,13 @@ describe('MySQLPlus', function() {
656678
result[0]['Create Table'].should.equal(timestampTableExpectedSQL);
657679
cbTimestamp();
658680
});
681+
682+
const cbSynonym = cbManager.registerCallback();
683+
pool.query(`SHOW CREATE TABLE \`${synonymTableName}\``, (err, result) => {
684+
if (err) throw err;
685+
result[0]['Create Table'].should.equal(synonymTableExpectedSQL);
686+
cbSynonym();
687+
});
659688
});
660689

661690
});
@@ -679,6 +708,7 @@ describe('MySQLPlus', function() {
679708
pool.defineTable(optionsTableName, optionsTableSchema);
680709
pool.defineTable(textTableName, textTableSchema);
681710
pool.defineTable(timestampTableName, timestampTableSchema);
711+
pool.defineTable(synonymTableName, synonymTableSchema);
682712
pool.sync(done);
683713
});
684714

@@ -777,6 +807,13 @@ describe('MySQLPlus', function() {
777807
result[0]['Create Table'].should.equal(timestampTableExpectedSQL);
778808
cbTimestamp();
779809
});
810+
811+
const cbSynonym = cbManager.registerCallback();
812+
pool.query(`SHOW CREATE TABLE \`${synonymTableName}\``, (err, result) => {
813+
if (err) throw err;
814+
result[0]['Create Table'].should.equal(synonymTableExpectedSQL);
815+
cbSynonym();
816+
});
780817
});
781818

782819
});
@@ -800,6 +837,7 @@ describe('MySQLPlus', function() {
800837
pool.defineTable(optionsTableName, optionsTableSchema);
801838
pool.defineTable(textTableName, textTableSchema);
802839
pool.defineTable(timestampTableName, timestampTableSchema);
840+
pool.defineTable(synonymTableName, synonymTableSchema);
803841
pool.sync(done);
804842
});
805843

@@ -893,6 +931,13 @@ describe('MySQLPlus', function() {
893931
result[0]['Create Table'].should.equal(timestampTableExpectedSQL);
894932
cbTimestamp();
895933
});
934+
935+
const cbSynonym = cbManager.registerCallback();
936+
pool.query(`SHOW CREATE TABLE \`${synonymTableName}\``, (err, result) => {
937+
if (err) throw err;
938+
result[0]['Create Table'].should.equal(synonymTableExpectedSQL);
939+
cbSynonym();
940+
});
896941
});
897942

898943
});

test/unit/ColumnDefinitions.test.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('ColumnDefinitions', () => {
175175
.$toSQL().should.equal('int');
176176

177177
ColumnDefinitions.integer()
178-
.$toSQL().should.equal('integer');
178+
.$toSQL().should.equal('int'); // synonym
179179

180180
ColumnDefinitions.bigint()
181181
.$toSQL().should.equal('bigint');
@@ -190,22 +190,22 @@ describe('ColumnDefinitions', () => {
190190
.$toSQL().should.equal('decimal');
191191

192192
ColumnDefinitions.dec()
193-
.$toSQL().should.equal('dec');
193+
.$toSQL().should.equal('decimal'); // synonym
194194

195195
ColumnDefinitions.numeric()
196-
.$toSQL().should.equal('numeric');
196+
.$toSQL().should.equal('decimal'); // synonym
197197

198198
ColumnDefinitions.fixed()
199-
.$toSQL().should.equal('fixed');
199+
.$toSQL().should.equal('decimal'); // synonym
200200

201201
ColumnDefinitions.bit()
202202
.$toSQL().should.equal('bit');
203203

204204
ColumnDefinitions.bool()
205-
.$toSQL().should.equal('bool');
205+
.$toSQL().should.equal('tinyint(1)'); // synonym
206206

207207
ColumnDefinitions.boolean()
208-
.$toSQL().should.equal('boolean');
208+
.$toSQL().should.equal('tinyint(1)'); // synonym
209209

210210
ColumnDefinitions.date()
211211
.$toSQL().should.equal('date');
@@ -307,7 +307,7 @@ describe('ColumnDefinitions', () => {
307307
.$toSQL().should.equal('int(1)');
308308

309309
ColumnDefinitions.integer(1)
310-
.$toSQL().should.equal('integer(1)');
310+
.$toSQL().should.equal('int(1)'); // synonym
311311

312312
ColumnDefinitions.bigint(1)
313313
.$toSQL().should.equal('bigint(1)');
@@ -322,13 +322,13 @@ describe('ColumnDefinitions', () => {
322322
.$toSQL().should.equal('decimal(1)');
323323

324324
ColumnDefinitions.dec(1)
325-
.$toSQL().should.equal('dec(1)');
325+
.$toSQL().should.equal('decimal(1)'); // synonym
326326

327327
ColumnDefinitions.numeric(1)
328-
.$toSQL().should.equal('numeric(1)');
328+
.$toSQL().should.equal('decimal(1)'); // synonym
329329

330330
ColumnDefinitions.fixed(1)
331-
.$toSQL().should.equal('fixed(1)');
331+
.$toSQL().should.equal('decimal(1)'); // synonym
332332

333333
ColumnDefinitions.float(1, 2)
334334
.$toSQL().should.equal('float(1,2)');
@@ -340,13 +340,13 @@ describe('ColumnDefinitions', () => {
340340
.$toSQL().should.equal('decimal(1,2)');
341341

342342
ColumnDefinitions.dec(1, 2)
343-
.$toSQL().should.equal('dec(1,2)');
343+
.$toSQL().should.equal('decimal(1,2)'); // synonym
344344

345345
ColumnDefinitions.numeric(1, 2)
346-
.$toSQL().should.equal('numeric(1,2)');
346+
.$toSQL().should.equal('decimal(1,2)'); // synonym
347347

348348
ColumnDefinitions.fixed(1, 2)
349-
.$toSQL().should.equal('fixed(1,2)');
349+
.$toSQL().should.equal('decimal(1,2)'); // synonym
350350

351351
ColumnDefinitions.bit(1)
352352
.$toSQL().should.equal('bit(1)');
@@ -384,10 +384,10 @@ describe('ColumnDefinitions', () => {
384384

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

389389
ColumnDefinitions.bool().notNull().default(false)
390-
.$toSQL().should.equal('bool NOT NULL DEFAULT \'0\'');
390+
.$toSQL().should.equal('tinyint(1) NOT NULL DEFAULT \'0\'');
391391

392392
ColumnDefinitions.int().notNull().default(1)
393393
.$toSQL().should.equal('int NOT NULL DEFAULT \'1\'');
@@ -471,17 +471,17 @@ describe('ColumnDefinitions', () => {
471471
describe('number data types', () => {
472472

473473
it('should provide number-specific definition methods', () => {
474-
ColumnDefinitions.integer().unsigned()
475-
.$toSQL().should.equal('integer unsigned');
474+
ColumnDefinitions.int().unsigned()
475+
.$toSQL().should.equal('int unsigned');
476476

477-
ColumnDefinitions.integer().unsigned().zerofill()
478-
.$toSQL().should.equal('integer unsigned zerofill');
477+
ColumnDefinitions.int().unsigned().zerofill()
478+
.$toSQL().should.equal('int unsigned zerofill');
479479

480-
ColumnDefinitions.integer().zerofill()
481-
.$toSQL().should.equal('integer unsigned zerofill');
480+
ColumnDefinitions.int().zerofill()
481+
.$toSQL().should.equal('int unsigned zerofill');
482482

483-
ColumnDefinitions.integer().unsigned().autoIncrement()
484-
.$toSQL().should.equal('integer unsigned AUTO_INCREMENT');
483+
ColumnDefinitions.int().unsigned().autoIncrement()
484+
.$toSQL().should.equal('int unsigned AUTO_INCREMENT');
485485
});
486486

487487
});

0 commit comments

Comments
 (0)