Skip to content

Commit

Permalink
Fixed knex knex#1577
Browse files Browse the repository at this point in the history
Aslo corrected tests for primarykey
  • Loading branch information
statyan committed Jul 13, 2016
1 parent 4f28c99 commit c6d5897
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
22 changes: 16 additions & 6 deletions src/dialects/mssql/schema/columncompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@ assign(ColumnCompiler_MSSQL.prototype, {
bigint: 'bigint',

double(precision, scale) {
if (!precision) return 'double'
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`
if (!precision) return 'decimal'
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`
},

floating(precision, scale) {
if (!precision) return 'decimal'
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`
},

integer(length) {
length = length ? `(${this._num(length, 11)})` : ''
return `int${length}`
},

mediumint: 'mediumint',
mediumint: 'int',

smallint: 'smallint',

Expand Down Expand Up @@ -62,7 +67,10 @@ assign(ColumnCompiler_MSSQL.prototype, {
timestamp: 'datetime',

bit(length) {
return length ? `bit(${this._num(length)})` : 'bit'
if (length > 1) {
helpers.warn('Bit field is exactly 1 bit length for MSSQL');
}
return 'bit';
},

binary(length) {
Expand All @@ -83,11 +91,13 @@ assign(ColumnCompiler_MSSQL.prototype, {
},

first() {
return 'first'
helpers.warn('Column first modifier not available for MSSQL');
return '';
},

after(column) {
return `after ${this.formatter.wrap(column)}`
helpers.warn('Column after modifier not available for MSSQL');
return '';
},

comment(comment) {
Expand Down
7 changes: 4 additions & 3 deletions src/dialects/mssql/schema/tablecompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ assign(TableCompiler_MSSQL.prototype, {
},

primary (columns, constraintName) {
constraintName = constraintName ? this.formatter.wrap(constraintName) : this._indexCommand('primary', this.tableNameRaw, columns);
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
if (!this.forCreate) {
this.pushQuery(`ALTER TABLE ${this.tableName()} ADD CONSTRAINT ${constraintName} PRIMARY KEY (${this.formatter.columnize(columns)})`);
} else {
Expand Down Expand Up @@ -134,8 +134,9 @@ assign(TableCompiler_MSSQL.prototype, {
},

// Compile a drop primary key command.
dropPrimary () {
this.pushQuery(`ALTER TABLE ${this.tableName()} DROP PRIMARY KEY`);
dropPrimary (constraintName) {
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
this.pushQuery(`ALTER TABLE ${this.tableName()} DROP CONSTRAINT ${constraintName}`);
},

// Compile a drop unique key command.
Expand Down
2 changes: 1 addition & 1 deletion test/integration/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ module.exports = function(knex) {
.then(function() {
return tr.schema.createTable(tableName, function(table) {
table.string('test').primary(constraintName);
table.string('test2');
table.string('test2').notNullable();
})
})
.then(function() {
Expand Down
16 changes: 8 additions & 8 deletions test/unit/schema/mssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ describe("MSSQL SchemaBuilder", function() {

it('test drop primary', function() {
tableSql = client.schemaBuilder().table('users', function() {
this.dropPrimary();
this.dropPrimary('testconstraintname');
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP PRIMARY KEY');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] DROP CONSTRAINT [testconstraintname]');
});

it('test drop unique', function() {
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [name] nvarchar(255) after [foo]');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [name] nvarchar(255)');
});

it('test adding column on the first place', function() {
Expand All @@ -233,7 +233,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [first_name] nvarchar(255) first');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [first_name] nvarchar(255)');
});

it('test adding string', function() {
Expand Down Expand Up @@ -304,7 +304,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] mediumint');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] int');
});

it('test adding small integer', function() {
Expand All @@ -331,7 +331,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] float(5, 2)');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(5, 2)');
});

it('test adding double', function() {
Expand All @@ -340,7 +340,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] double');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal');
});

it('test adding double specifying precision', function() {
Expand All @@ -349,7 +349,7 @@ describe("MSSQL SchemaBuilder", function() {
}).toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] double(15, 8)');
expect(tableSql[0].sql).to.equal('ALTER TABLE [users] ADD [foo] decimal(15, 8)');
});

it('test adding decimal', function() {
Expand Down

0 comments on commit c6d5897

Please sign in to comment.