Last active
March 9, 2016 20:03
-
-
Save SPY/e83f8747dd07eb13d3bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}) | |
); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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