Raw SQL In Migrations

813 views
Skip to first unread message

Paul Tiseo

unread,
Mar 5, 2015, 8:40:14 AM3/5/15
to sequ...@googlegroups.com
I posted it as an issue (https://github.com/sequelize/cli/issues/105) but not sure it is. The documentation on migrations seems weak on this common migrations function, and so in googling for a way to run raw SQL in Sequelize migrations most people seem to suggest/use this:

migration.migrator.sequelize.query(theSql)

But, in my case, it seems to error. Seems like migration does not have a migrator, as it tells me that it "cannot read property 'sequelize' of undefined". Looking at a console dump of the migration object, I don't see a migrator. My guess is that something changed in some recent version? If so, how does one run raw sql now?

Mick Hansen

unread,
Mar 5, 2015, 9:57:01 AM3/5/15
to Paul Tiseo, sequ...@googlegroups.com
migrations should have the sequelize property directly, i.e: migration.sequelize rather than migration.migrator.sequelize
--
Mick Hansen
@mhansendev
mhansen.io

Paul Tiseo

unread,
Mar 5, 2015, 12:23:19 PM3/5/15
to sequ...@googlegroups.com, paulx...@gmail.com
Ok, I have confirmed that.

Now, what is the proper way to handle database errors to stop migrations? Seems like if I call migration.sequelize.query(theSql).then(done) with bad SQL, I see the error in the console, but it blissfully continues on with subsequent migrations. Does one need to catch() and throw it?

Paul Tiseo

unread,
Mar 5, 2015, 1:31:54 PM3/5/15
to sequ...@googlegroups.com, paulx...@gmail.com
Sorry, it blissfully continued because I had a finally(done), not a then(done). When I use then(done), I get a "Possibly unhandled error: , [object Object]" error. Adding a catch(function(e){}) doesn't actually seem to catch the error. In fact, the SQL seems to have executed, but the execution was not recorded in sequelizemeta.

Mick Hansen

unread,
Mar 5, 2015, 3:48:13 PM3/5/15
to Paul Tiseo, sequ...@googlegroups.com
Depending on the migration library version you can either just return the promise or use .nodeify(done) to get err, result semantics which callbacks need.

Paul Tiseo

unread,
Mar 6, 2015, 4:51:43 PM3/6/15
to sequ...@googlegroups.com, paulx...@gmail.com
The versions are: Node: 0.12.0, CLI: 1.3.1, ORM: 2.0.3

Do you mean return migration.sequelize.query(theSql).then(done)?

Mick Hansen

unread,
Mar 6, 2015, 5:01:11 PM3/6/15
to Paul Tiseo, sequ...@googlegroups.com
If you return a promise you should remove the done callback entirely.
If you'd like to use the done callback you should use nodeify(done)

Mick Hansen

unread,
Mar 6, 2015, 5:01:35 PM3/6/15
to Paul Tiseo, sequ...@googlegroups.com
Of course that depends on the migrator you're using, only Umzug supports promises afair.

Paul Tiseo

unread,
Mar 6, 2015, 5:25:57 PM3/6/15
to sequ...@googlegroups.com, paulx...@gmail.com
Beautiful, nodeify() was what I needed.

Paul Tiseo

unread,
Mar 7, 2015, 12:12:44 PM3/7/15
to sequ...@googlegroups.com
Ok, there must be something I'm not doing right. If the migration fails, be it a built-in like createTable() or raw query, how does one ensure that the the migration halts? For example, I had a createTable() that failed because of using an UNSIGNED option (then called done() separately), but it just kept going through subsequent migration files.

Mick Hansen

unread,
Mar 7, 2015, 12:45:43 PM3/7/15
to Paul Tiseo, sequ...@googlegroups.com
As far as i know it should halt on error.
I'm not too familiar with the cli/migrations, you'll have to open an issue against the CLI or Umzug
Reply all
Reply to author
Forward
0 new messages