2014-09-01 17:18:45 +02:00
|
|
|
/*global describe, expect, it*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2013-09-12 01:00:44 -04:00
|
|
|
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 () {
|
2013-09-12 01:00:44 -04:00
|
|
|
|
|
|
|
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');
|
2014-02-23 15:38:53 -05:00
|
|
|
tester('sqlite3', "delete from sqlite_sequence where name = \"test_table_two\"");
|
2014-08-11 12:25:39 +02:00
|
|
|
tester('oracle', "truncate table \"test_table_two\"");
|
2015-02-04 14:56:41 -05:00
|
|
|
tester('fdbsql', 'truncate table \"test_table_two\"');
|
2013-12-27 14:44:21 -05:00
|
|
|
})
|
2013-09-12 01:00:44 -04: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',
|
2014-06-09 16:27:03 -04:00
|
|
|
mysql2: 'SHOW TABLES',
|
2014-06-03 23:27:37 -04:00
|
|
|
mariasql: 'SHOW TABLES',
|
2013-09-12 01:00:44 -04:00
|
|
|
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';",
|
2015-02-04 14:56:41 -05:00
|
|
|
oracle: "select TABLE_NAME from USER_TABLES",
|
|
|
|
fdbsql: "SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
|
2013-09-12 01:00:44 -04:00
|
|
|
};
|
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]);
|
|
|
|
});
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow using the primary table as a raw statement', function() {
|
2014-03-14 16:56:55 -04:00
|
|
|
expect(knex(knex.raw("raw_table_name")).toQuery()).to.equal('select * from raw_table_name');
|
2014-04-21 23:08:59 -04:00
|
|
|
});
|
2013-09-12 01:00:44 -04:00
|
|
|
|
2014-07-03 11:59:29 +02:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2014-04-21 23:08:59 -04:00
|
|
|
it('gets the columnInfo', function() {
|
|
|
|
return knex('datatype_test').columnInfo().testSql(function(tester) {
|
|
|
|
tester('mysql',
|
2014-05-04 23:53:44 -04:00
|
|
|
'select * from information_schema.columns where table_name = ? and table_schema = ?',
|
2014-04-27 18:53:25 -04:00
|
|
|
null, {
|
2014-04-21 23:08:59 -04:00
|
|
|
"enum_value": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": 1,
|
|
|
|
"nullable": true,
|
2014-04-21 23:08:59 -04:00
|
|
|
"type": "enum"
|
|
|
|
},
|
|
|
|
"uuid": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": 36,
|
|
|
|
"nullable": false,
|
2014-04-21 23:08:59 -04:00
|
|
|
"type": "char"
|
|
|
|
}
|
|
|
|
});
|
2014-05-04 23:53:44 -04:00
|
|
|
tester('postgresql', 'select * from information_schema.columns where table_name = ? and table_catalog = ?',
|
2014-04-27 18:53:25 -04:00
|
|
|
null, {
|
2014-04-21 23:08:59 -04:00
|
|
|
"enum_value": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": null,
|
|
|
|
"nullable": true,
|
2014-04-21 23:08:59 -04:00
|
|
|
"type": "text"
|
|
|
|
},
|
|
|
|
"uuid": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": null,
|
|
|
|
"nullable": false,
|
2014-04-21 23:08:59 -04:00
|
|
|
"type": "uuid"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tester('sqlite3', 'PRAGMA table_info(datatype_test)', [], {
|
|
|
|
"enum_value": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": null,
|
|
|
|
"nullable": true,
|
2014-04-21 23:08:59 -04:00
|
|
|
"type": "varchar"
|
|
|
|
},
|
|
|
|
"uuid": {
|
2014-05-04 23:53:44 -04:00
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": "36",
|
|
|
|
"nullable": false,
|
2014-04-21 23:08:59 -04:00
|
|
|
"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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-02-04 14:56:41 -05:00
|
|
|
tester(
|
|
|
|
'fdbsql',
|
|
|
|
'select * from information_schema.columns where table_name = ? and table_schema = ? and table_schema = CURRENT_SCHEMA',
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
"enum_value": {
|
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": 1,
|
|
|
|
"nullable": true,
|
|
|
|
"type": "VARCHAR"
|
|
|
|
},
|
|
|
|
"uuid": {
|
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": null,
|
|
|
|
"nullable": false,
|
|
|
|
"type": "GUID"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2014-04-21 23:08:59 -04:00
|
|
|
});
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
|
2014-06-03 14:21:31 -04:00
|
|
|
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"
|
2014-06-03 14:21:31 -04:00
|
|
|
});
|
2014-08-11 12:25:39 +02:00
|
|
|
tester('postgresql', 'select * from information_schema.columns where table_name = ? and table_catalog = ?',
|
|
|
|
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-06-03 14:21:31 -04:00
|
|
|
});
|
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"
|
|
|
|
}
|
|
|
|
);
|
2015-02-04 14:56:41 -05:00
|
|
|
tester('fdbsql', 'select * from information_schema.columns where table_name = ? and table_schema = ? and table_schema = CURRENT_SCHEMA',
|
|
|
|
null, {
|
|
|
|
"defaultValue": null,
|
|
|
|
"maxLength": null,
|
|
|
|
"nullable": false,
|
|
|
|
"type": "GUID"
|
|
|
|
});
|
2014-08-11 12:25:39 +02:00
|
|
|
});
|
|
|
|
});
|
2014-06-03 14:21:31 -04:00
|
|
|
|
2014-04-16 01:23:50 -04:00
|
|
|
it('should allow renaming a column', function() {
|
|
|
|
var count, inserts = [];
|
|
|
|
_.times(40, function() {
|
|
|
|
inserts.push({first_name: 'Test', last_name: 'Data'});
|
|
|
|
});
|
|
|
|
return knex('accounts').insert(inserts).then(function() {
|
|
|
|
return knex.count('*').from('accounts');
|
|
|
|
}).then(function(resp) {
|
|
|
|
count = resp['count(*)'];
|
|
|
|
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\""]);
|
2015-02-04 14:56:41 -05:00
|
|
|
tester('fdbsql', ["alter table \"accounts\" rename column \"about\" to \"about_col\""]);
|
2014-04-16 01:23:50 -04:00
|
|
|
});
|
2013-09-12 01:00:44 -04:00
|
|
|
}).then(function() {
|
2014-04-16 01:23:50 -04:00
|
|
|
return knex.count('*').from('accounts');
|
2013-09-12 01:00:44 -04:00
|
|
|
}).then(function(resp) {
|
2014-04-16 01:23:50 -04:00
|
|
|
expect(resp['count(*)']).to.equal(count);
|
|
|
|
}).then(function() {
|
|
|
|
return knex('accounts').select('about_col');
|
|
|
|
}).then(function() {
|
2013-09-12 01:00:44 -04:00
|
|
|
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['count(*)']).to.equal(count);
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
});
|
2014-06-09 15:28:22 -04:00
|
|
|
|
|
|
|
it('should allow dropping a column', function() {
|
|
|
|
var count;
|
2014-08-11 12:25:39 +02:00
|
|
|
|
|
|
|
if (knex.client.dialect === 'oracle') {
|
|
|
|
return knex.count('*').from('accounts').then(function (resp) {
|
|
|
|
count = resp[0]['COUNT(*)'];
|
|
|
|
}).then(function () {
|
|
|
|
return knex.schema.table('accounts', function (t) {
|
|
|
|
t.dropColumn('first_name');
|
|
|
|
}).testSql(function (tester) {
|
2014-08-21 00:17:49 +02:00
|
|
|
tester('oracle', ['alter table "accounts" drop ("first_name")']);
|
2014-08-11 12:25:39 +02:00
|
|
|
});
|
|
|
|
}).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]['COUNT(*)']).to.equal(count);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-09 15:28:22 -04:00
|
|
|
return knex.count('*').from('accounts').then(function(resp) {
|
|
|
|
count = resp['count(*)'];
|
2014-09-02 22:56:51 +02:00
|
|
|
}).then(function() {
|
2014-06-09 15:28:22 -04:00
|
|
|
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\")"]);
|
2015-02-04 14:56:41 -05:00
|
|
|
tester('fdbsql', ['alter table "accounts" drop column "first_name"']);
|
2014-06-09 15:28:22 -04:00
|
|
|
});
|
|
|
|
}).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['count(*)']).to.equal(count);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
|
2014-08-21 23:39:12 +02:00
|
|
|
};
|