Add integration test for specifictype (#4211)

This commit is contained in:
Igor Savin 2021-01-06 21:50:13 +02:00 committed by GitHub
parent 41606d799c
commit bf287be13a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View File

@ -106,6 +106,11 @@ class ColumnCompiler {
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`; return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
} }
// Used to support custom types
specifictype(type) {
return type;
}
// Modifiers // Modifiers
// ------- // -------
@ -154,7 +159,6 @@ ColumnCompiler.prototype.timestamp = 'timestamp';
ColumnCompiler.prototype.enu = 'varchar'; ColumnCompiler.prototype.enu = 'varchar';
ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text'; ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
ColumnCompiler.prototype.uuid = 'char(36)'; ColumnCompiler.prototype.uuid = 'char(36)';
ColumnCompiler.prototype.specifictype = (type) => type;
ColumnCompiler.prototype.increments = ColumnCompiler.prototype.increments =
'integer not null primary key autoincrement'; 'integer not null primary key autoincrement';
ColumnCompiler.prototype.bigincrements = ColumnCompiler.prototype.bigincrements =

View File

@ -0,0 +1,57 @@
const { expect } = require('chai');
const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider');
describe('Schema', () => {
describe('customType', () => {
getAllDbs().forEach((db) => {
describe(db, () => {
let knex;
const tblName = 'table_with_custom_varchar1';
const colName = 'varchar_col1';
before(async () => {
knex = getKnexForDb(db);
await knex.schema.dropTableIfExists(tblName);
await knex.schema.createTable(tblName, (table) => {
table.specificType(colName, 'varchar(42)');
});
});
after(async () => {
await knex.schema.dropTable(tblName);
return knex.destroy();
});
it('Allows to specify custom type params', async () => {
let res;
switch (db) {
case 'sqlite3':
res = await knex.schema.raw(`PRAGMA table_info(${tblName})`);
expect(res.find((c) => c.name === colName).type).to.equal(
'varchar(42)'
);
break;
case 'postgres':
res = await knex
.select(['data_type', 'character_maximum_length'])
.from('information_schema.columns')
.where({ table_name: tblName, column_name: colName });
expect(res[0].data_type).to.equal('character varying');
expect(res[0].character_maximum_length).to.equal(42);
break;
case 'mssql':
case 'mysql':
case 'mysql2':
res = await knex
.select(['DATA_TYPE', 'CHARACTER_MAXIMUM_LENGTH'])
.from('INFORMATION_SCHEMA.COLUMNS')
.where({ table_name: tblName, column_name: colName });
expect(res[0].DATA_TYPE).to.equal('varchar');
expect(res[0].CHARACTER_MAXIMUM_LENGTH).to.equal(42);
break;
}
});
});
});
});
});