Skip to content

Instantly share code, notes, and snippets.

@wlaurance
Last active March 7, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wlaurance/10756732 to your computer and use it in GitHub Desktop.
Save wlaurance/10756732 to your computer and use it in GitHub Desktop.
users_projects
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