mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
1ad7a91f4d
refs #6301 - In the migration folder, commands.js changed to builder.js to resolve conflict with the 'commands' inside data/utils/clients/. - a new data/schema/ folder has been added to hold all the code related to the database schema - data/utils/clients have been moved to data/schema/clients - data/utils/index.js has become data/schema/commands.js - data/schema.js has been split, the definition of the DB schema stays put, the additional checks have moved to data/schema/checks.js - data/validation/index.js has become data/schema/versioning.js - data/fixtures has moved to data/migration/fixtures - data/default-settings.json has moved to data/schema/default-settings.json
135 lines
3.9 KiB
JavaScript
135 lines
3.9 KiB
JavaScript
var _ = require('lodash'),
|
|
Promise = require('bluebird'),
|
|
config = require('../../config'),
|
|
i18n = require('../../i18n'),
|
|
schema = require('./schema'),
|
|
clients = require('./clients'),
|
|
|
|
dbConfig;
|
|
|
|
function addTableColumn(tablename, table, columnname) {
|
|
var column,
|
|
columnSpec = schema[tablename][columnname];
|
|
|
|
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
|
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);
|
|
} else {
|
|
column = table[columnSpec.type](columnname);
|
|
}
|
|
|
|
if (columnSpec.hasOwnProperty('nullable') && columnSpec.nullable === true) {
|
|
column.nullable();
|
|
} else {
|
|
column.notNullable();
|
|
}
|
|
if (columnSpec.hasOwnProperty('primary') && columnSpec.primary === true) {
|
|
column.primary();
|
|
}
|
|
if (columnSpec.hasOwnProperty('unique') && columnSpec.unique) {
|
|
column.unique();
|
|
}
|
|
if (columnSpec.hasOwnProperty('unsigned') && columnSpec.unsigned) {
|
|
column.unsigned();
|
|
}
|
|
if (columnSpec.hasOwnProperty('references')) {
|
|
// check if table exists?
|
|
column.references(columnSpec.references);
|
|
}
|
|
if (columnSpec.hasOwnProperty('defaultTo')) {
|
|
column.defaultTo(columnSpec.defaultTo);
|
|
}
|
|
}
|
|
|
|
function addColumn(table, column) {
|
|
dbConfig = dbConfig || config.database;
|
|
return dbConfig.knex.schema.table(table, function (t) {
|
|
addTableColumn(table, t, column);
|
|
});
|
|
}
|
|
|
|
function addUnique(table, column) {
|
|
dbConfig = dbConfig || config.database;
|
|
return dbConfig.knex.schema.table(table, function (table) {
|
|
table.unique(column);
|
|
});
|
|
}
|
|
|
|
function dropUnique(table, column) {
|
|
dbConfig = dbConfig || config.database;
|
|
return dbConfig.knex.schema.table(table, function (table) {
|
|
table.dropUnique(column);
|
|
});
|
|
}
|
|
|
|
function createTable(table) {
|
|
dbConfig = dbConfig || config.database;
|
|
return dbConfig.knex.schema.createTable(table, function (t) {
|
|
var columnKeys = _.keys(schema[table]);
|
|
_.each(columnKeys, function (column) {
|
|
return addTableColumn(table, t, column);
|
|
});
|
|
});
|
|
}
|
|
|
|
function deleteTable(table) {
|
|
dbConfig = dbConfig || config.database;
|
|
return dbConfig.knex.schema.dropTableIfExists(table);
|
|
}
|
|
|
|
function getTables() {
|
|
dbConfig = dbConfig || config.database;
|
|
var client = dbConfig.client;
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
return clients[client].getTables();
|
|
}
|
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function getIndexes(table) {
|
|
dbConfig = dbConfig || config.database;
|
|
var client = dbConfig.client;
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
return clients[client].getIndexes(table);
|
|
}
|
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function getColumns(table) {
|
|
dbConfig = dbConfig || config.database;
|
|
var client = dbConfig.client;
|
|
|
|
if (_.contains(_.keys(clients), client)) {
|
|
return clients[client].getColumns(table);
|
|
}
|
|
|
|
return Promise.reject(i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function checkTables() {
|
|
dbConfig = dbConfig || config.database;
|
|
var client = dbConfig.client;
|
|
|
|
if (client === 'mysql') {
|
|
return clients[client].checkPostTable();
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
checkTables: checkTables,
|
|
createTable: createTable,
|
|
deleteTable: deleteTable,
|
|
getTables: getTables,
|
|
getIndexes: getIndexes,
|
|
addUnique: addUnique,
|
|
dropUnique: dropUnique,
|
|
addColumn: addColumn,
|
|
getColumns: getColumns
|
|
};
|