mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 06:12:03 +03:00
22e13acd65
- All var declarations are now const or let as per ES6 - All comma-separated lists / chained declarations are now one declaration per line - This is for clarity/readability but also made running the var-to-const/let switch smoother - ESLint rules updated to match How this was done: - npm install -g jscodeshift - git clone https://github.com/cpojer/js-codemod.git - git clone git@github.com:TryGhost/Ghost.git shallow-ghost - cd shallow-ghost - jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2 - jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2 - yarn - yarn test - yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode - grunt test-regression - sorted!
183 lines
5.8 KiB
JavaScript
183 lines
5.8 KiB
JavaScript
const _ = require('lodash');
|
|
const Promise = require('bluebird');
|
|
const common = require('../../lib/common');
|
|
const db = require('../db');
|
|
const schema = require('./schema');
|
|
const clients = require('./clients');
|
|
|
|
function addTableColumn(tableName, table, columnName, columnSpec = schema[tableName][columnName]) {
|
|
let column;
|
|
|
|
// creation distinguishes between text with fieldtype, string with maxlength and all others
|
|
if (columnSpec.type === 'text' && Object.prototype.hasOwnProperty.call(columnSpec, 'fieldtype')) {
|
|
column = table[columnSpec.type](columnName, columnSpec.fieldtype);
|
|
} else if (columnSpec.type === 'string') {
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'maxlength')) {
|
|
column = table[columnSpec.type](columnName, columnSpec.maxlength);
|
|
} else {
|
|
column = table[columnSpec.type](columnName, 191);
|
|
}
|
|
} else {
|
|
column = table[columnSpec.type](columnName);
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'nullable') && columnSpec.nullable === true) {
|
|
column.nullable();
|
|
} else {
|
|
column.nullable(false);
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'primary') && columnSpec.primary === true) {
|
|
column.primary();
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'unique') && columnSpec.unique) {
|
|
column.unique();
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'unsigned') && columnSpec.unsigned) {
|
|
column.unsigned();
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'references')) {
|
|
// check if table exists?
|
|
column.references(columnSpec.references);
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'defaultTo')) {
|
|
column.defaultTo(columnSpec.defaultTo);
|
|
}
|
|
if (Object.prototype.hasOwnProperty.call(columnSpec, 'index') && columnSpec.index === true) {
|
|
column.index();
|
|
}
|
|
}
|
|
|
|
function addColumn(tableName, column, transaction, columnSpec) {
|
|
return (transaction || db.knex).schema.table(tableName, function (table) {
|
|
addTableColumn(tableName, table, column, columnSpec);
|
|
});
|
|
}
|
|
|
|
function dropColumn(table, column, transaction) {
|
|
return (transaction || db.knex).schema.table(table, function (table) {
|
|
table.dropColumn(column);
|
|
});
|
|
}
|
|
|
|
function addUnique(table, column, transaction) {
|
|
return (transaction || db.knex).schema.table(table, function (table) {
|
|
table.unique(column);
|
|
});
|
|
}
|
|
|
|
function dropUnique(table, column, transaction) {
|
|
return (transaction || db.knex).schema.table(table, function (table) {
|
|
table.dropUnique(column);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* https://github.com/tgriesser/knex/issues/1303
|
|
* createTableIfNotExists can throw error if indexes are already in place
|
|
*/
|
|
function createTable(table, transaction) {
|
|
return (transaction || db.knex).schema.hasTable(table)
|
|
.then(function (exists) {
|
|
if (exists) {
|
|
return;
|
|
}
|
|
|
|
return (transaction || db.knex).schema.createTable(table, function (t) {
|
|
const columnKeys = _.keys(schema[table]);
|
|
_.each(columnKeys, function (column) {
|
|
return addTableColumn(table, t, column);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function deleteTable(table, transaction) {
|
|
return (transaction || db.knex).schema.dropTableIfExists(table);
|
|
}
|
|
|
|
function getTables(transaction) {
|
|
const client = (transaction || db.knex).client.config.client;
|
|
|
|
if (_.includes(_.keys(clients), client)) {
|
|
return clients[client].getTables(transaction);
|
|
}
|
|
|
|
return Promise.reject(common.i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function getIndexes(table, transaction) {
|
|
const client = (transaction || db.knex).client.config.client;
|
|
|
|
if (_.includes(_.keys(clients), client)) {
|
|
return clients[client].getIndexes(table, transaction);
|
|
}
|
|
|
|
return Promise.reject(common.i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function getColumns(table, transaction) {
|
|
const client = (transaction || db.knex).client.config.client;
|
|
|
|
if (_.includes(_.keys(clients), client)) {
|
|
return clients[client].getColumns(table);
|
|
}
|
|
|
|
return Promise.reject(common.i18n.t('notices.data.utils.index.noSupportForDatabase', {client: client}));
|
|
}
|
|
|
|
function checkTables(transaction) {
|
|
const client = (transaction || db.knex).client.config.client;
|
|
|
|
if (client === 'mysql') {
|
|
return clients[client].checkPostTable();
|
|
}
|
|
}
|
|
|
|
const createLog = type => msg => common.logging[type](msg);
|
|
|
|
function createColumnMigration(...migrations) {
|
|
async function runColumnMigration(conn, migration) {
|
|
const {
|
|
table,
|
|
column,
|
|
dbIsInCorrectState,
|
|
operation,
|
|
operationVerb,
|
|
columnDefinition
|
|
} = migration;
|
|
|
|
const hasColumn = await conn.schema.hasColumn(table, column);
|
|
const isInCorrectState = dbIsInCorrectState(hasColumn);
|
|
|
|
const log = createLog(isInCorrectState ? 'warn' : 'info');
|
|
|
|
log(`${operationVerb} ${table}.${column}`);
|
|
|
|
if (!isInCorrectState) {
|
|
await operation(table, column, conn, columnDefinition);
|
|
}
|
|
}
|
|
|
|
return async function columnMigration(options) {
|
|
const conn = options.transacting || options.connection;
|
|
|
|
for (const migration of migrations) {
|
|
await runColumnMigration(conn, migration);
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
checkTables: checkTables,
|
|
createTable: createTable,
|
|
deleteTable: deleteTable,
|
|
getTables: getTables,
|
|
getIndexes: getIndexes,
|
|
addUnique: addUnique,
|
|
dropUnique: dropUnique,
|
|
addColumn: addColumn,
|
|
dropColumn: dropColumn,
|
|
getColumns: getColumns,
|
|
createColumnMigration
|
|
};
|