fix(mssql): columnInfo() support case-sensitive database collations (#4633)

This commit is contained in:
Jeremy W. Sherman 2021-08-21 14:26:54 -04:00 committed by GitHub
parent cafc10182b
commit 52950c652b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 4 deletions

View File

@ -429,7 +429,8 @@ class QueryCompiler_MSSQL extends QueryCompiler {
schema = this.client.customWrapIdentifier(schema, identity);
}
let sql = `select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from information_schema.columns where table_name = ? and table_catalog = ?`;
// GOTCHA: INFORMATION_SCHEMA.COLUMNS must be capitalized to work when the database has a case-sensitive collation. [#4573]
let sql = `select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from INFORMATION_SCHEMA.COLUMNS where table_name = ? and table_catalog = ?`;
const bindings = [table, this.client.database()];
if (schema) {

View File

@ -300,7 +300,7 @@ module.exports = function (knex) {
[drivers.SQLite]: "SELECT name FROM sqlite_master WHERE type='table';",
[drivers.Oracle]: 'select TABLE_NAME from USER_TABLES',
[drivers.MsSQL]:
"SELECT table_name FROM information_schema.tables WHERE table_schema='dbo'",
"SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='dbo'",
};
return knex
.raw(tables[knex.client.driverName])
@ -417,7 +417,7 @@ module.exports = function (knex) {
);
tester(
'mssql',
"select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from information_schema.columns where table_name = ? and table_catalog = ? and table_schema = 'dbo'",
"select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from INFORMATION_SCHEMA.COLUMNS where table_name = ? and table_catalog = ? and table_schema = 'dbo'",
['datatype_test', 'knex_test'],
{
enum_value: {
@ -493,7 +493,7 @@ module.exports = function (knex) {
);
tester(
'mssql',
"select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from information_schema.columns where table_name = ? and table_catalog = ? and table_schema = 'dbo'",
"select [COLUMN_NAME], [COLUMN_DEFAULT], [DATA_TYPE], [CHARACTER_MAXIMUM_LENGTH], [IS_NULLABLE] from INFORMATION_SCHEMA.COLUMNS where table_name = ? and table_catalog = ? and table_schema = 'dbo'",
null,
{
defaultValue: null,

View File

@ -1463,6 +1463,49 @@ module.exports = (knex) => {
.then(() =>
knex.schema.withSchema(testSchemaName).dropTableIfExists('test2')
));
describe('case-sensitive collation support', () => {
let k;
const databaseName = 'knex_test_CS_AS_SC';
const collation = 'Latin1_General_100_CS_AS_SC';
const tableName = 'Test';
before(async () => {
await knex.schema.raw(
`IF EXISTS(SELECT name FROM sys.databases WHERE name = :databaseName) DROP DATABASE :databaseName:; CREATE DATABASE :databaseName: COLLATE ${collation}`,
{ databaseName }
);
const Knex = require('../../../knex');
const config = require('../../knexfile');
k = Knex({
client: 'mssql',
connection: {
...config.mssql.connection,
database: databaseName,
},
});
// Verify configuration is using the correct database.
const [{ name }] = await k.raw('SELECT DB_NAME() AS name');
expect(name).to.equal(databaseName);
await k.schema.createTable(tableName, function () {
this.increments();
});
});
after(async () => {
await k.schema.dropTable(tableName);
await k.destroy();
await knex.schema.raw(
`IF EXISTS(SELECT name FROM sys.databases WHERE name = :databaseName) DROP DATABASE :databaseName:`,
{ databaseName }
);
});
it('should get columnInfo from a case-sensitive database', async () => {
const info = await k(tableName).columnInfo();
expect(info).not.to.equal(undefined);
});
});
});
});