2014-06-12 19:25:55 +04:00
|
|
|
|
var _ = require('lodash'),
|
|
|
|
|
when = require('when'),
|
2014-05-16 06:29:42 +04:00
|
|
|
|
config = require('../../config'),
|
2014-06-12 19:25:55 +04:00
|
|
|
|
schema = require('../schema').tables,
|
2014-07-10 19:10:00 +04:00
|
|
|
|
clients = require('./clients');
|
|
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function addTableColumn(tablename, table, columnname) {
|
|
|
|
|
var column;
|
|
|
|
|
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
|
|
|
|
if (schema[tablename][columnname].type === 'text' && schema[tablename][columnname].hasOwnProperty('fieldtype')) {
|
|
|
|
|
column = table[schema[tablename][columnname].type](columnname, schema[tablename][columnname].fieldtype);
|
|
|
|
|
} else if (schema[tablename][columnname].type === 'string' && schema[tablename][columnname].hasOwnProperty('maxlength')) {
|
|
|
|
|
column = table[schema[tablename][columnname].type](columnname, schema[tablename][columnname].maxlength);
|
|
|
|
|
} else {
|
|
|
|
|
column = table[schema[tablename][columnname].type](columnname);
|
|
|
|
|
}
|
2014-06-10 00:37:44 +04:00
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('nullable') && schema[tablename][columnname].nullable === true) {
|
|
|
|
|
column.nullable();
|
|
|
|
|
} else {
|
|
|
|
|
column.notNullable();
|
|
|
|
|
}
|
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('primary') && schema[tablename][columnname].primary === true) {
|
|
|
|
|
column.primary();
|
|
|
|
|
}
|
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('unique') && schema[tablename][columnname].unique) {
|
|
|
|
|
column.unique();
|
|
|
|
|
}
|
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('unsigned') && schema[tablename][columnname].unsigned) {
|
|
|
|
|
column.unsigned();
|
|
|
|
|
}
|
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('references')) {
|
|
|
|
|
//check if table exists?
|
|
|
|
|
column.references(schema[tablename][columnname].references);
|
|
|
|
|
}
|
|
|
|
|
if (schema[tablename][columnname].hasOwnProperty('defaultTo')) {
|
|
|
|
|
column.defaultTo(schema[tablename][columnname].defaultTo);
|
|
|
|
|
}
|
2014-06-10 00:37:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function addColumn(table, column) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
return config().database.knex.schema.table(table, function (t) {
|
2014-06-12 19:25:55 +04:00
|
|
|
|
addTableColumn(table, t, column);
|
|
|
|
|
});
|
2014-06-10 00:37:44 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function addUnique(table, column) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
return config().database.knex.schema.table(table, function (table) {
|
2014-06-12 19:25:55 +04:00
|
|
|
|
table.unique(column);
|
2014-06-10 00:37:44 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function dropUnique(table, column) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
return config().database.knex.schema.table(table, function (table) {
|
2014-06-12 19:25:55 +04:00
|
|
|
|
table.dropUnique(column);
|
2014-06-10 00:37:44 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function createTable(table) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
return config().database.knex.schema.createTable(table, function (t) {
|
2014-06-12 19:25:55 +04:00
|
|
|
|
var columnKeys = _.keys(schema[table]);
|
|
|
|
|
_.each(columnKeys, function (column) {
|
|
|
|
|
return addTableColumn(table, t, column);
|
|
|
|
|
});
|
2014-06-10 00:37:44 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
|
function deleteTable(table) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
return config().database.knex.schema.dropTableIfExists(table);
|
2014-06-12 19:25:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
|
function getTables() {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
var client = config().database.client;
|
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
|
|
|
return clients[client].getTables();
|
2014-06-12 19:25:55 +04:00
|
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
|
|
return when.reject('No support for database client ' + client);
|
2014-06-12 19:25:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getIndexes(table) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
var client = config().database.client;
|
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
|
|
|
return clients[client].getIndexes(table);
|
2014-06-12 19:25:55 +04:00
|
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
|
|
return when.reject('No support for database client ' + client);
|
2014-06-12 19:25:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getColumns(table) {
|
2014-05-16 06:29:42 +04:00
|
|
|
|
var client = config().database.client;
|
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
|
|
|
return clients[client].getColumns(table);
|
2014-06-10 00:37:44 +04:00
|
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
|
|
return when.reject('No support for database client ' + client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function checkTables() {
|
|
|
|
|
var client = config().database.client;
|
|
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
|
if (client === 'mysql') {
|
2014-07-10 19:10:00 +04:00
|
|
|
|
return clients[client].checkPostTable();
|
2014-06-10 00:37:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2014-07-10 19:10:00 +04:00
|
|
|
|
checkTables: checkTables,
|
2014-06-10 00:37:44 +04:00
|
|
|
|
createTable: createTable,
|
|
|
|
|
deleteTable: deleteTable,
|
2014-06-12 19:25:55 +04:00
|
|
|
|
getTables: getTables,
|
|
|
|
|
getIndexes: getIndexes,
|
|
|
|
|
addUnique: addUnique,
|
|
|
|
|
dropUnique: dropUnique,
|
|
|
|
|
addColumn: addColumn,
|
|
|
|
|
getColumns: getColumns
|
2014-06-10 00:37:44 +04:00
|
|
|
|
};
|