knex/test/integration/builder/additional.js

261 lines
8.9 KiB
JavaScript
Raw Normal View History

/*global describe, expect, it*/
'use strict';
module.exports = function(knex) {
2014-04-16 01:23:50 -04:00
var _ = require('lodash');
2013-09-12 13:30:47 -04:00
describe('Additional', function () {
it('should truncate a table with truncate', function() {
return knex('test_table_two')
.truncate()
2013-12-27 14:44:21 -05:00
.testSql(function(tester) {
tester('mysql', 'truncate `test_table_two`');
tester('postgresql', 'truncate "test_table_two" restart identity');
tester('sqlite3', "delete from \"test_table_two\"");
2014-08-11 12:25:39 +02:00
tester('oracle', "truncate table \"test_table_two\"");
tester('mssql', 'truncate table [test_table_two]');
2013-12-27 14:44:21 -05:00
})
.then(function() {
return knex('test_table_two')
.select('*')
.then(function(resp) {
expect(resp).to.have.length(0);
});
});
});
it('should allow raw queries directly with `knex.raw`', function() {
var tables = {
mysql: 'SHOW TABLES',
mysql2: 'SHOW TABLES',
2015-04-29 15:14:41 -04:00
mariadb: 'SHOW TABLES',
postgresql: "SELECT table_name FROM information_schema.tables WHERE table_schema='public'",
2014-08-11 12:25:39 +02:00
sqlite3: "SELECT name FROM sqlite_master WHERE type='table';",
oracle: "select TABLE_NAME from USER_TABLES",
mssql: "SELECT table_name FROM information_schema.tables WHERE table_schema='dbo'"
};
2013-12-27 14:44:21 -05:00
return knex.raw(tables[knex.client.dialect]).testSql(function(tester) {
tester(knex.client.dialect, tables[knex.client.dialect]);
});
});
it('should allow using the primary table as a raw statement', function() {
expect(knex(knex.raw("raw_table_name")).toQuery()).to.equal('select * from raw_table_name');
});
it('should allow using .fn-methods to create raw statements', function() {
expect(knex.fn.now().prototype === knex.raw().prototype);
expect(knex.fn.now().toQuery()).to.equal('CURRENT_TIMESTAMP');
});
it('gets the columnInfo', function() {
return knex('datatype_test').columnInfo().testSql(function(tester) {
tester('mysql',
'select * from information_schema.columns where table_name = ? and table_schema = ?',
null, {
"enum_value": {
"defaultValue": null,
"maxLength": 1,
"nullable": true,
"type": "enum"
},
"uuid": {
"defaultValue": null,
"maxLength": 36,
"nullable": false,
"type": "char"
}
});
tester('postgresql', 'select * from information_schema.columns where table_name = ? and table_catalog = ? and table_schema = current_schema',
null, {
"enum_value": {
"defaultValue": null,
"maxLength": null,
"nullable": true,
"type": "text"
},
"uuid": {
"defaultValue": null,
"maxLength": null,
"nullable": false,
"type": "uuid"
}
});
tester('sqlite3', 'PRAGMA table_info(datatype_test)', [], {
"enum_value": {
"defaultValue": null,
"maxLength": null,
"nullable": true,
"type": "varchar"
},
"uuid": {
"defaultValue": null,
"maxLength": "36",
"nullable": false,
"type": "char"
}
});
2014-08-11 12:25:39 +02:00
tester(
'oracle',
"select COLUMN_NAME, DATA_TYPE, CHAR_COL_DECL_LENGTH, NULLABLE from USER_TAB_COLS where TABLE_NAME = :1",
['datatype_test'],
{
"enum_value": {
nullable: true,
maxLength: 1,
type: "VARCHAR2"
},
"uuid": {
nullable: false,
maxLength: 36,
type: "CHAR"
}
}
);
tester('mssql',
'select * from information_schema.columns where table_name = ? and table_schema = \'dbo\'',
['datatype_test'], {
"enum_value": {
"defaultValue": null,
"maxLength": 100,
"nullable": true,
"type": "nvarchar"
},
"uuid": {
"defaultValue": null,
"maxLength": null,
"nullable": false,
"type": "uniqueidentifier"
}
});
});
});
it('gets the columnInfo', function() {
2014-08-11 12:25:39 +02:00
return knex('datatype_test').columnInfo('uuid').testSql(function(tester) {
tester('mysql',
'select * from information_schema.columns where table_name = ? and table_schema = ?',
null, {
"defaultValue": null,
"maxLength": 36,
"nullable": false,
"type": "char"
});
tester('postgresql', 'select * from information_schema.columns where table_name = ? and table_catalog = ? and table_schema = current_schema',
2014-08-11 12:25:39 +02:00
null, {
"defaultValue": null,
"maxLength": null,
"nullable": false,
"type": "uuid"
});
tester('sqlite3', 'PRAGMA table_info(datatype_test)', [], {
"defaultValue": null,
"maxLength": "36",
"nullable": false,
"type": "char"
});
2014-08-11 12:25:39 +02:00
tester(
'oracle',
'select COLUMN_NAME, DATA_TYPE, CHAR_COL_DECL_LENGTH, NULLABLE from USER_TAB_COLS where TABLE_NAME = :1',
['datatype_test'],
{
"maxLength": 36,
"nullable": false,
"type": "CHAR"
}
);
tester('mssql',
'select * from information_schema.columns where table_name = ? and table_schema = \'dbo\'',
null, {
"defaultValue": null,
"maxLength": null,
"nullable": false,
"type": "uniqueidentifier"
});
2014-08-11 12:25:39 +02:00
});
});
2014-04-16 01:23:50 -04:00
it('should allow renaming a column', function() {
var countColumn
console.log(knex.client.dialect);
switch (knex.client.dialect) {
case 'oracle': countColumn = 'COUNT(*)'; break;
case 'mssql': countColumn = ''; break;
default: countColumn = 'count(*)'; break;
}
2014-04-16 01:23:50 -04:00
var count, inserts = [];
_.times(40, function(i) {
inserts.push({email: 'email'+ i, first_name: 'Test', last_name: 'Data'});
2014-04-16 01:23:50 -04:00
});
return knex('accounts').insert(inserts).then(function() {
return knex.count('*').from('accounts');
}).then(function(resp) {
count = resp[0][countColumn];
2014-04-16 01:23:50 -04:00
return knex.schema.table('accounts', function(t) {
t.renameColumn('about', 'about_col');
}).testSql(function(tester) {
tester('mysql', ["show fields from `accounts` where field = ?"]);
tester('postgresql', ["alter table \"accounts\" rename \"about\" to \"about_col\""]);
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
2014-08-11 12:25:39 +02:00
tester('oracle', ["alter table \"accounts\" rename column \"about\" to \"about_col\""]);
tester('mssql', ["exec sp_rename ?, ?, 'COLUMN'"]);
2014-04-16 01:23:50 -04:00
});
}).then(function() {
2014-04-16 01:23:50 -04:00
return knex.count('*').from('accounts');
}).then(function(resp) {
expect(resp[0][countColumn]).to.equal(count);
2014-04-16 01:23:50 -04:00
}).then(function() {
return knex('accounts').select('about_col');
}).then(function() {
return knex.schema.table('accounts', function(t) {
t.renameColumn('about_col', 'about');
});
2014-04-16 01:23:50 -04:00
}).then(function() {
return knex.count('*').from('accounts');
}).then(function(resp) {
expect(resp[0][countColumn]).to.equal(count);
});
});
it('should allow dropping a column', function() {
var countColumn
switch (knex.client.dialect) {
case 'oracle': countColumn = 'COUNT(*)'; break;
case 'mssql': countColumn = ''; break;
default: countColumn = 'count(*)'; break;
2014-08-11 12:25:39 +02:00
}
var count;
return knex.count('*').from('accounts').then(function(resp) {
count = resp[0][countColumn];
}).then(function() {
return knex.schema.table('accounts', function(t) {
t.dropColumn('first_name');
}).testSql(function(tester) {
tester('mysql', ["alter table `accounts` drop `first_name`"]);
tester('postgresql', ['alter table "accounts" drop column "first_name"']);
tester('sqlite3', ["PRAGMA table_info(\"accounts\")"]);
tester('oracle', ['alter table "accounts" drop ("first_name")']);
2015-12-14 09:56:53 -06:00
tester('mssql', ["ALTER TABLE [accounts] DROP COLUMN [first_name]"]);
});
}).then(function() {
return knex.select('*').from('accounts').first();
}).then(function(resp) {
expect(_.keys(resp).sort()).to.eql(["about", "created_at", "email", "id", "last_name", "logins", "phone", "updated_at"]);
}).then(function() {
return knex.count('*').from('accounts');
}).then(function(resp) {
expect(resp[0][countColumn]).to.equal(count);
});
});
});
};