Last active
March 7, 2016 18:14
-
-
Save wlaurance/10756732 to your computer and use it in GitHub Desktop.
users_projects
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
dbm = require("db-migrate") | |
async = require 'async' | |
_ = require 'underscore' | |
type = dbm.dataType | |
table = "users_projects" | |
exports.up = (db, callback) -> | |
t = """ | |
create table #{table}( | |
"userId" uuid NOT NULL, | |
"projectId" uuid NOT NULL | |
) | |
""" | |
r = [ | |
""" | |
add constraint #{table}_pkey PRIMARY KEY ("userId", "projectId") | |
""", | |
""" | |
add constraint "user_id_f_key" FOREIGN KEY ("userId") REFERENCES users("id") | |
""", | |
""" | |
add constraint "project_id_f_key" FOREIGN KEY ("projectId") REFERENCES projects("id") | |
""" | |
] | |
r = _.map r, (s) -> "alter table #{table} #{s}" | |
async.eachSeries _.flatten([t, r]), | |
(q, c) -> db.runSql(q, c) | |
, callback | |
exports.down = (db, callback) -> | |
if process.env.DROP_TABLES | |
db.dropTable table, callback | |
else | |
callback() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment