Skip to content

Commit

Permalink
improvement: migrations (#7000)
Browse files Browse the repository at this point in the history
closes #6972, #6574

- run each database version as top level transaction
- run migrations in correct order
  • Loading branch information
kirrg001 authored and ErisDS committed Jul 14, 2016
1 parent 71282a2 commit 6e1bd28
Show file tree
Hide file tree
Showing 42 changed files with 954 additions and 924 deletions.
4 changes: 4 additions & 0 deletions core/server/data/db/connection.js
Expand Up @@ -2,6 +2,10 @@ var knex = require('knex'),
config = require('../../config'),
knexInstance;

// @TODO:
// - if you require this file before config file was loaded,
// - then this file is cached and you have no chance to connect to the db anymore
// - bring dynamic into this file (db.connect())
function configure(dbConfig) {
var client = dbConfig.client,
pg;
Expand Down
44 changes: 23 additions & 21 deletions core/server/data/migration/004/01-add-tour-column-to-users.js
@@ -1,24 +1,26 @@
var commands = require('../../schema').commands,
db = require('../../db'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
table = 'users',
column = 'tour',
message = 'Adding column: ' + table + '.' + column;

table = 'users',
column = 'tour',
message = 'Adding column: ' + table + '.' + column;
module.exports = function addTourColumnToUsers(options, logger) {
var transaction = options.transacting;

module.exports = function addTourColumnToUsers(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return transaction.schema.hasColumn(table, column);
})
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
};
@@ -1,24 +1,26 @@
var commands = require('../../schema').commands,
db = require('../../db'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
table = 'posts_tags',
column = 'sort_order',
message = 'Adding column: ' + table + '.' + column;

table = 'posts_tags',
column = 'sort_order',
message = 'Adding column: ' + table + '.' + column;
module.exports = function addSortOrderColumnToPostsTags(options, logger) {
var transaction = options.transacting;

module.exports = function addSortOrderColumnToPostsTags(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return transaction.schema.hasColumn(table, column);
})
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
};
42 changes: 22 additions & 20 deletions core/server/data/migration/004/03-add-many-columns-to-clients.js
@@ -1,27 +1,29 @@
var Promise = require('bluebird'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
db = require('../../db'),
table = 'clients',
columns = ['redirection_uri', 'logo', 'status', 'type', 'description'];

table = 'clients',
columns = ['redirection_uri', 'logo', 'status', 'type', 'description'];
module.exports = function addManyColumnsToClients(options, logger) {
var transaction = options.transacting;

return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

module.exports = function addManyColumnsToClients(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return Promise.mapSeries(columns, function (column) {
var message = 'Adding column: ' + table + '.' + column;
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});

return transaction.schema.hasColumn(table, column)
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
});
} else {
// @TODO: this should probably be an error
logger.warn('Adding columns to table: ' + table);
}
});
});
};
25 changes: 13 additions & 12 deletions core/server/data/migration/004/04-add-clienttrusteddomains-table.js
@@ -1,16 +1,17 @@
var commands = require('../../schema').commands,
db = require('../../db'),
table = 'client_trusted_domains',
message = 'Creating table: ' + table;

table = 'client_trusted_domains',
message = 'Creating table: ' + table;
module.exports = function addClientTrustedDomainsTable(options, logger) {
var transaction = options.transacting;

module.exports = function addClientTrustedDomainsTable(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.createTable(table);
} else {
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.createTable(table, transaction);
} else {
logger.warn(message);
}
});
};
44 changes: 23 additions & 21 deletions core/server/data/migration/004/05-drop-unique-on-clients-secret.js
@@ -1,24 +1,26 @@
var commands = require('../../schema').commands,
db = require('../../db'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
table = 'clients',
column = 'secret',
message = 'Dropping unique on: ' + table + '.' + column;

table = 'clients',
column = 'secret',
message = 'Dropping unique on: ' + table + '.' + column;
module.exports = function dropUniqueOnClientsSecret(options, logger) {
var transaction = options.transacting;

module.exports = function dropUniqueOnClientsSecret(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return commands.getIndexes(table).then(function (indexes) {
if (indexes.indexOf(table + '_' + column + '_unique') > -1) {
logger.info(message);
return commands.dropUnique(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return commands.getIndexes(table, transaction);
})
.then(function (indexes) {
if (indexes.indexOf(table + '_' + column + '_unique') > -1) {
logger.info(message);
return commands.dropUnique(table, column, transaction);
} else {
logger.warn(message);
}
});
};
44 changes: 23 additions & 21 deletions core/server/data/migration/005/01-drop-hidden-column-from-tags.js
@@ -1,24 +1,26 @@
var commands = require('../../schema').commands,
db = require('../../db'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
table = 'tags',
column = 'hidden',
message = 'Removing column: ' + table + '.' + column;

table = 'tags',
column = 'hidden',
message = 'Removing column: ' + table + '.' + column;
module.exports = function dropHiddenColumnFromTags(options, logger) {
var transaction = options.transacting;

module.exports = function dropHiddenColumnFromTags(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (exists) {
logger.info(message);
return commands.dropColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return transaction.schema.hasColumn(table, column);
})
.then(function (exists) {
if (exists) {
logger.info(message);
return commands.dropColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
};
@@ -1,27 +1,29 @@
var Promise = require('bluebird'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
db = require('../../db'),
tables = ['posts', 'tags', 'users'],
column = 'visibility';

tables = ['posts', 'tags', 'users'],
column = 'visibility';
module.exports = function addVisibilityColumnToKeyTables(options, logger) {
var transaction = options.transacting;

module.exports = function addVisibilityColumnToKeyTables(logger) {
return Promise.mapSeries(tables, function (table) {
var message = 'Adding column: ' + table + '.' + column;
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
var message = 'Adding column: ' + table + '.' + column;

return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return transaction.schema.hasColumn(table, column);
})
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
});
};
44 changes: 23 additions & 21 deletions core/server/data/migration/005/03-add-mobiledoc-column-to-posts.js
@@ -1,24 +1,26 @@
var commands = require('../../schema').commands,
db = require('../../db'),
var Promise = require('bluebird'),
commands = require('../../schema').commands,
table = 'posts',
column = 'mobiledoc',
message = 'Adding column: ' + table + '.' + column;

table = 'posts',
column = 'mobiledoc',
message = 'Adding column: ' + table + '.' + column;
module.exports = function addMobiledocColumnToPosts(options, logger) {
var transaction = options.transacting;

module.exports = function addMobiledocColumnToPosts(logger) {
return db.knex.schema.hasTable(table).then(function (exists) {
if (exists) {
return db.knex.schema.hasColumn(table, column).then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column);
} else {
logger.warn(message);
}
});
} else {
// @TODO: this should probably be an error
logger.warn(message);
}
});
return transaction.schema.hasTable(table)
.then(function (exists) {
if (!exists) {
return Promise.reject(new Error('Table does not exist!'));
}

return transaction.schema.hasColumn(table, column);
})
.then(function (exists) {
if (!exists) {
logger.info(message);
return commands.addColumn(table, column, transaction);
} else {
logger.warn(message);
}
});
};

0 comments on commit 6e1bd28

Please sign in to comment.