Skip to content

Instantly share code, notes, and snippets.

@SPY
Last active March 9, 2016 20:03
Show Gist options
  • Save SPY/e83f8747dd07eb13d3bf to your computer and use it in GitHub Desktop.
Save SPY/e83f8747dd07eb13d3bf to your computer and use it in GitHub Desktop.
exports.up = function(db) {
return new Promise(function (resolve, reject) {
db.dropTable('companies')
}).then(
db.createTable('companies', {
company_id: { type: 'bigserial', primaryKey: true },
company_name: { type: 'string', unique: true },
domain: { type: 'string', unique: true },
owner: 'bigint'
})
).then(
db.createTable('users', {
user_id: { type: 'bigserial', primaryKey: true },
user_name: { type: 'string', unique: true },
mail: 'string',
password: 'string'
})
);
};
function promisify(scope, fn) {
return function() {
var args = [].slice.call(arguments, 0)
return new Promise(function(resolve, reject) {
args.push(function(err, res) {
if (err) { reject(err); return }
resolve(res)
})
fn.apply(scope, args)
})
}
}
function migrate(action) {
return (db, callback) => {
action(db)
.then(() => callback())
.catch(callback)
}
}
exports.up = migrate(db => {
var dropTable = promisify(db, db.dropTable)
var createTable = promisify(db. db.createTable)
return dropTable('companies')
.then(() =>
createTable('companies', {
company_id: { type: 'bigserial', primaryKey: true },
company_name: { type: 'string', unique: true },
domain: { type: 'string', unique: true },
owner: 'bigint'
})
)
.then(() =>
createTable('users', {
user_id: { type: 'bigserial', primaryKey: true },
user_name: { type: 'string', unique: true },
mail: 'string',
password: 'string'
})
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment