diff --git a/lib/db/setup.js b/lib/db/setup.js index d1f6e091..ff9c885c 100644 --- a/lib/db/setup.js +++ b/lib/db/setup.js @@ -26,6 +26,42 @@ module.exports = function(conn) { }) } + function createIndex(table, index, options) { + var args = [index] + , rTable = r.table(table) + + if (options) { + if (options.indexFunction) { + args.push(options.indexFunction) + } + if (options.options) { + args.push(options.options) + } + } + + return rTable.indexCreate.apply(rTable, args).run(conn) + .then(function() { + log.info('Index "%s"."%s" created', table, index) + }) + .catch(alreadyExistsError, function() { + log.info('Index "%s"."%s" already exists', table, index) + return Promise.resolve() + }) + .then(function() { + log.info('Waiting for index "%s"."%s"', table, index) + return r.table(table).indexWait(index).run(conn) + }) + .then(function() { + log.info('Index "%s"."%s" is ready', table, index) + return Promise.resolve() + }) + .catch(noMasterAvailableError, function() { + return Promise.delay(1000).then(function() { + return createIndex(table, index, options) + }) + }) + } + function createTable(table, options) { var tableOptions = { primaryKey: options.primaryKey @@ -52,34 +88,6 @@ module.exports = function(conn) { }) } - function createIndex(table, index, options) { - var args = [index] - , rTable = r.table(table) - - if (options) { - if (options.indexFunction) { - args.push(options.indexFunction) - } - if (options.options) { - args.push(options.options) - } - } - - return rTable.indexCreate.apply(rTable, args).run(conn) - .then(function() { - log.info('Index "%s"."%s" created', table, index) - }) - .catch(alreadyExistsError, function() { - log.info('Index "%s"."%s" already exists', table, index) - return Promise.resolve() - }) - .catch(noMasterAvailableError, function() { - return Promise.delay(1000).then(function() { - return createIndex(table, index, options) - }) - }) - } - return createDatabase() .then(function() { return Promise.all(Object.keys(tables).map(function(table) {