Skip to content

Instantly share code, notes, and snippets.

@eloop
Created March 31, 2014 23:36
Show Gist options
  • Save eloop/9904762 to your computer and use it in GitHub Desktop.
Save eloop/9904762 to your computer and use it in GitHub Desktop.
var dbm = require('db-migrate');
var type = dbm.dataType;
var async = require ('async');
exports.up = function(db, callback) {
var tasks = [];
// tagging
tasks.push(db.runSql.bind(db, "ALTER TABLE volumes ADD tags text[] DEFAULT '{}'"));
// sharing
tasks.push(db.runSql.bind(db, "ALTER TABLE volumes ADD shares text[] DEFAULT '{}'"));
// add some foreign key constraints to previous tables
tasks.push(db.runSql.bind(db, "ALTER TABLE volumes ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) MATCH FULL"));
tasks.push(db.runSql.bind(db, "ALTER TABLE images ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) MATCH FULL"));
tasks.push(db.runSql.bind(db, "ALTER TABLE bookmarks ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) MATCH FULL"));
tasks.push(db.runSql.bind(db, "ALTER TABLE ingests ADD CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES users (id) MATCH FULL"));
async.series (tasks, callback);
};
exports.down = function(db, callback) {
var opts = {ifExists: true};
var tasks = [];
// sharing
tasks.push(db.removeColumn.bind(db, 'volumes', 'shares'));
tasks.push(db.removeColumn.bind(db, 'volumes', 'tags'));
// remove constraints added
tasks.push(db.runSql.bind(db, "ALTER TABLE volumes DROP CONSTRAINT IF EXISTS fk_user_id "));
tasks.push(db.runSql.bind(db, "ALTER TABLE images DROP CONSTRAINT IF EXISTS fk_user_id "));
tasks.push(db.runSql.bind(db, "ALTER TABLE volumes DROP CONSTRAINT IF EXISTS fk_user_id "));
tasks.push(db.runSql.bind(db, "ALTER TABLE ingests DROP CONSTRAINT IF EXISTS fk_user_id "));
tasks.push(db.runSql.bind(db, "ALTER TABLE bookmarks DROP CONSTRAINT IF EXISTS fk_user_id "));
async.series (tasks, callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment