2014-06-12 19:25:55 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
2016-02-12 14:56:27 +03:00
|
|
|
db = require('../db'),
|
2016-01-25 20:50:04 +03:00
|
|
|
schema = require('./schema'),
|
2016-02-12 14:56:27 +03:00
|
|
|
clients = require('./clients');
|
2014-07-10 19:10:00 +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-09-10 08:06:24 +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) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.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
|
|
|
}
|
|
|
|
|
2016-02-07 19:23:24 +03:00
|
|
|
function dropColumn(table, column) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex.schema.table(table, function (table) {
|
2016-02-07 19:23:24 +03:00
|
|
|
table.dropColumn(column);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:25:55 +04:00
|
|
|
function addUnique(table, column) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.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) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.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) {
|
2016-03-13 23:49:30 +03:00
|
|
|
return db.knex.schema.createTableIfNotExists(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) {
|
2016-02-12 14:56:27 +03:00
|
|
|
return db.knex.schema.dropTableIfExists(table);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 00:37:44 +04:00
|
|
|
function getTables() {
|
2016-02-12 14:56:27 +03:00
|
|
|
var client = db.knex.client.config.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
2016-06-11 21:23:27 +03:00
|
|
|
if (_.includes(_.keys(clients), client)) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return clients[client].getTables();
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getIndexes(table) {
|
2016-02-12 14:56:27 +03:00
|
|
|
var client = db.knex.client.config.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
2016-06-11 21:23:27 +03:00
|
|
|
if (_.includes(_.keys(clients), client)) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return clients[client].getIndexes(table);
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
2014-06-12 19:25:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getColumns(table) {
|
2016-02-12 14:56:27 +03:00
|
|
|
var client = db.knex.client.config.client;
|
2014-05-16 06:29:42 +04:00
|
|
|
|
2016-06-11 21:23:27 +03:00
|
|
|
if (_.includes(_.keys(clients), client)) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return clients[client].getColumns(table);
|
2014-06-10 00:37:44 +04:00
|
|
|
}
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
2014-07-10 19:10:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkTables() {
|
2016-02-12 14:56:27 +03:00
|
|
|
var client = db.knex.client.config.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,
|
2016-02-07 19:23:24 +03:00
|
|
|
dropColumn: dropColumn,
|
2014-06-12 19:25:55 +04:00
|
|
|
getColumns: getColumns
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|