Skip to content

Commit

Permalink
Merge branch 'master' into ts
Browse files Browse the repository at this point in the history
* master:
  Implement missing schema support for mssql dialect
  • Loading branch information
tgriesser committed Jul 10, 2018
2 parents c2421b5 + e08c14b commit 4866973
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/dialects/mssql/schema/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ class SchemaCompiler_MSSQL extends SchemaCompiler {
renameTable(tableName, to) {
this.pushQuery(
`exec sp_rename ${this.formatter.parameter(
tableName
prefixedTableName(this.schema, tableName)
)}, ${this.formatter.parameter(to)}`
);
}

// Check whether a table exists on the query.
hasTable(tableName) {
const formattedTable = this.formatter.parameter(
this.formatter.wrap(tableName)
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);

const sql =
`select object_id from sys.tables ` +
`where object_id = object_id(${formattedTable})`;
Expand All @@ -38,7 +39,7 @@ class SchemaCompiler_MSSQL extends SchemaCompiler {
hasColumn(tableName, column) {
const formattedColumn = this.formatter.parameter(column);
const formattedTable = this.formatter.parameter(
this.formatter.wrap(tableName)
this.formatter.wrap(prefixedTableName(this.schema, tableName))
);
const sql =
`select object_id from sys.columns ` +
Expand Down
84 changes: 84 additions & 0 deletions test/integration/schema/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,90 @@ module.exports = function(knex) {
});
});

describe('withSchema', function() {
describe('mssql only', function() {
if (!knex || !knex.client || !/mssql/i.test(knex.client.dialect)) {
return Promise.resolve();
}

const columnName = 'test';

function checkTable(schema, tableName, expected) {
return knex.schema
.withSchema(schema)
.hasTable(tableName)
.then(function(exists) {
return expect(exists).to.equal(expected);
});
}

function createTable(schema, tableName) {
return knex.schema
.withSchema(schema)
.createTable(tableName, (table) => {
table.string(columnName);
});
}

function checkColumn(schema, tableName) {
return knex.schema
.withSchema(schema)
.hasColumn(tableName, columnName)
.then((exists) => {
return expect(exists).to.equal(true);
});
}

function renameTable(schema, from, to) {
return knex.schema.withSchema(schema).renameTable(from, to);
}

function createSchema(schema) {
return knex.schema.raw('CREATE SCHEMA ' + schema);
}

const defaultSchemaName = 'public';
const testSchemaName = 'test';

before(function() {
return createSchema(testSchemaName);
});

after(function() {
return knex.schema.raw('DROP SCHEMA ' + testSchemaName);
});

it('should not find non-existent tables', function() {
return checkTable(testSchemaName, 'test', false).then(() =>
checkTable(defaultSchemaName, 'test', false)
);
});

it('should create and drop tables', function() {
return createTable(testSchemaName, 'test')
.then(() => checkColumn(testSchemaName, 'test'))
.then(() => checkTable(testSchemaName, 'test', true))
.then(() => checkTable(defaultSchemaName, 'test', false))
.then(() =>
knex.schema.withSchema(testSchemaName).dropTableIfExists('test')
)
.then(() => checkTable(testSchemaName, 'test', false));
});

it('should rename tables', function() {
return createTable(testSchemaName, 'test')
.then(() => renameTable(testSchemaName, 'test', 'test2'))
.then(() => checkColumn(testSchemaName, 'test2'))
.then(() => checkTable(defaultSchemaName, 'test2', false))
.then(() => checkTable(testSchemaName, 'test', false))
.then(() => checkTable(testSchemaName, 'test2', true))
.then(() =>
knex.schema.withSchema(testSchemaName).dropTableIfExists('test2')
);
});
});
});

it('should warn attempting to create primary from nonexistent columns', function() {
// Redshift only
if (!knex || !knex.client || !/redshift/i.test(knex.client.driverName)) {
Expand Down
69 changes: 69 additions & 0 deletions test/unit/schema/mssql.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,75 @@ describe('MSSQL SchemaBuilder', function() {
expect(tableSql[0].bindings[1]).to.equal('foo');
});

it('test has table', function() {
tableSql = client
.schemaBuilder()
.hasTable('users')
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('[users]');
});

it('test has table with schema', function() {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.hasTable('users')
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.tables where object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('[schema].[users]');
});

it('test rename table with schema', function() {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.renameTable('users', 'foo')
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal('exec sp_rename ?, ?');
expect(tableSql[0].bindings[0]).to.equal('schema.users');
expect(tableSql[0].bindings[1]).to.equal('foo');
});

it('test has column', function() {
tableSql = client
.schemaBuilder()
.hasColumn('users', 'foo')
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('foo');
expect(tableSql[0].bindings[1]).to.equal('[users]');
});

it('test has column with schema', function() {
tableSql = client
.schemaBuilder()
.withSchema('schema')
.hasColumn('users', 'foo')
.toSQL();

equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
'select object_id from sys.columns where name = ? and object_id = object_id(?)'
);
expect(tableSql[0].bindings[0]).to.equal('foo');
expect(tableSql[0].bindings[1]).to.equal('[schema].[users]');
});

it('test adding primary key', function() {
tableSql = client
.schemaBuilder()
Expand Down

0 comments on commit 4866973

Please sign in to comment.