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-21 14:50:00 +04:00
|
|
|
clients = require('./clients'),
|
|
|
|
|
|
|
|
dbConfig;
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
function addTableColumn(tablename, table, columnname) {
|
2014-07-18 21:58:29 +04:00
|
|
|
var column,
|
|
|
|
columnSpec = schema[tablename][columnname];
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.type === 'text' && columnSpec.hasOwnProperty('fieldtype')) {
|
|
|
|
column = table[columnSpec.type](columnname, columnSpec.fieldtype);
|
|
|
|
} else if (columnSpec.type === 'string' && columnSpec.hasOwnProperty('maxlength')) {
|
|
|
|
column = table[columnSpec.type](columnname, columnSpec.maxlength);
|
2014-06-12 19:25:55 +04:00
|
|
|
} else {
|
2014-07-18 21:58:29 +04:00
|
|
|
column = table[columnSpec.type](columnname);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
2014-06-10 00:37:44 +04:00
|
|
|
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('nullable') && columnSpec.nullable === true) {
|
2014-06-12 19:25:55 +04:00
|
|
|
column.nullable();
|
|
|
|
} else {
|
|
|
|
column.notNullable();
|
|
|
|
}
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('primary') && columnSpec.primary === true) {
|
2014-06-12 19:25:55 +04:00
|
|
|
column.primary();
|
|
|
|
}
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('unique') && columnSpec.unique) {
|
2014-06-12 19:25:55 +04:00
|
|
|
column.unique();
|
|
|
|
}
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('unsigned') && columnSpec.unsigned) {
|
2014-06-12 19:25:55 +04:00
|
|
|
column.unsigned();
|
|
|
|
}
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('references')) {
|
2014-06-12 19:25:55 +04:00
|
|
|
//check if table exists?
|
2014-07-18 21:58:29 +04:00
|
|
|
column.references(columnSpec.references);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
2014-07-18 21:58:29 +04:00
|
|
|
if (columnSpec.hasOwnProperty('defaultTo')) {
|
|
|
|
column.defaultTo(columnSpec.defaultTo);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
2014-06-10 00:37:44 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
function addColumn(table, column) {
|
2014-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
return dbConfig.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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
return dbConfig.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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
return dbConfig.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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
return dbConfig.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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
return dbConfig.knex.schema.dropTableIfExists(table);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
function getTables() {
|
2014-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
var client = dbConfig.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
var client = dbConfig.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
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-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
var client = dbConfig.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
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() {
|
2014-07-21 14:50:00 +04:00
|
|
|
dbConfig = dbConfig || config().database;
|
|
|
|
var client = dbConfig.client;
|
2014-07-10 19:10:00 +04:00
|
|
|
|
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
|
|
|
};
|