Skip to content

Commit

Permalink
Caching db config in migration utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ErisDS committed Jul 21, 2014
1 parent cf9839c commit 44cfcb6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions core/server/data/utils/index.js
Expand Up @@ -2,7 +2,9 @@ var _ = require('lodash'),
when = require('when'),
config = require('../../config'),
schema = require('../schema').tables,
clients = require('./clients');
clients = require('./clients'),

dbConfig;


function addTableColumn(tablename, table, columnname) {
Expand Down Expand Up @@ -42,25 +44,29 @@ function addTableColumn(tablename, table, columnname) {
}

function addColumn(table, column) {
return config().database.knex.schema.table(table, function (t) {
dbConfig = dbConfig || config().database;
return dbConfig.knex.schema.table(table, function (t) {
addTableColumn(table, t, column);
});
}

function addUnique(table, column) {
return config().database.knex.schema.table(table, function (table) {
dbConfig = dbConfig || config().database;
return dbConfig.knex.schema.table(table, function (table) {
table.unique(column);
});
}

function dropUnique(table, column) {
return config().database.knex.schema.table(table, function (table) {
dbConfig = dbConfig || config().database;
return dbConfig.knex.schema.table(table, function (table) {
table.dropUnique(column);
});
}

function createTable(table) {
return config().database.knex.schema.createTable(table, function (t) {
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);
Expand All @@ -69,11 +75,13 @@ function createTable(table) {
}

function deleteTable(table) {
return config().database.knex.schema.dropTableIfExists(table);
dbConfig = dbConfig || config().database;
return dbConfig.knex.schema.dropTableIfExists(table);
}

function getTables() {
var client = config().database.client;
dbConfig = dbConfig || config().database;
var client = dbConfig.client;

if (_.contains(_.keys(clients), client)) {
return clients[client].getTables();
Expand All @@ -83,7 +91,8 @@ function getTables() {
}

function getIndexes(table) {
var client = config().database.client;
dbConfig = dbConfig || config().database;
var client = dbConfig.client;

if (_.contains(_.keys(clients), client)) {
return clients[client].getIndexes(table);
Expand All @@ -93,7 +102,8 @@ function getIndexes(table) {
}

function getColumns(table) {
var client = config().database.client;
dbConfig = dbConfig || config().database;
var client = dbConfig.client;

if (_.contains(_.keys(clients), client)) {
return clients[client].getColumns(table);
Expand All @@ -103,7 +113,8 @@ function getColumns(table) {
}

function checkTables() {
var client = config().database.client;
dbConfig = dbConfig || config().database;
var client = dbConfig.client;

if (client === 'mysql') {
return clients[client].checkPostTable();
Expand Down

0 comments on commit 44cfcb6

Please sign in to comment.