Sequelize-typescript: Dealing with migrations?

Created on 19 Jan 2018  路  24Comments  路  Source: RobinBuschmann/sequelize-typescript

I'm guessing this means crafting migrations by hand?

Most helpful comment

Do have a look at the below article describing how I was able to use Sequelize CLI with migration files written in Typescript.

Sequelize Cli migrations with typescript

Looking forward to feedback and critique.

All 24 comments

Hey @SamD, sequelize provides tooling for that: http://docs.sequelizejs.com/manual/tutorial/migrations.html

If you don鈥檛 want to use a query builder for your migrations but pure sql queries instead I can recommend this library: https://www.npmjs.com/package/db-migrate

@RobinBuschmann, the sequelize CLI tool looks at model files to create migrations. As far as I can tell, the model files used by sequelize-typescript won't work with that, before or after compilation to JS. Am I missing something here?

As for db-migrate, since you need to create migrations yourself, how can we produce them programatically or better, automatically?

Hey @ljjb , from my understanding the cli can also be used to create sequelize model files next to their actual migrations. I admit that this is not very useful for sequelize-typescript, but you don鈥榯 need to use this feature, or am I wrong? As stated here http://docs.sequelizejs.com/manual/tutorial/migrations.html#advance-topics you are able to create migrations as usual (up, down)

There is no way I know of to make migrations automatically the same way the sequelize cli can do in vanilla sequelize, the structure of our ts models differ slightly.

You may get more mileage by using sequelize-decorators. It's quite different from this library, but try it out and see if that decorator library works with the CLI's migration tooling - it could be just what ya need.

I think our deal in this library is to grant freedom to users to use migrations, but that comes at the cost of the tooling.

@snewell92 So, you mean it is not possible to use the normal migration tooling with vanilla sequelize is not possible? If so, why not? Thank you :)

@RobinBuschmann
With vanilla sequelize the normal migration tooling works.

With Sequelize Typescript you can only use a tool that scaffolds the skeleton of up/down migrations, and then we have to manually fill it out.

With vanillas Sequelize, the migration tooling can inspect model definition files and fill in migration up/down for you.

The reason is because the tooling inspects the model definition file, and I _believe_ our js emit isn't compatible with that, as this library does extra processing. I actually haven't tried it though. (maybe we can compile our projects, and have the tooling point to the js model file folder?)

Actually I'm not sure - I could be totally wrong. 馃惐

I was just setting up a nodeJS feathersJS application with sequelize, and the sequelize migration tool just needs the model definitions in memory after initialization, not the source files to be a certain format. I'm going to play with it... I suspect it will likely work now.

Hey @snewell92 any progress on this? 馃檭

I figured out that the sequelize CLI only supports v3 of Sequelize, not v4.

@SamD If you are using v3 of Sequelize, there's no reasons the Sequelize CLI can't be used. If you are using v4 of Sequelize then refer to this issue: https://github.com/sequelize/cli/issues/441#issuecomment-365488402

Oh, the day after my previous comment this happened: https://github.com/sequelize/cli/issues/441#issuecomment-365920223

So now the sequelize cli (which has migration support) is compatible with sequelize v4, and published to npm. Perhaps it will work now? I followed the docs and it seems like it just needs the model definitions loaded into memory, which you can do pretty easily.

I do want to reiterate that both @RobinBuschmann and I use the aforementioned db-migrate library. It works, and it is helping me understand sequelize too.

@RobinBuschmann I think we can close this issue now. We might need to open another issue to track a migrations 'guide' or docs about migrations(?) - unless that's out of scope of this project...? I could perhaps write a blog post about it, would that suffice?

@snewell92 Perfect. Thanks you for the information. A blog post would be great as well :)

@snewell92 any update on the blog post or guide for migrations?

I can do that this weekend. It could be hosted on my personal blog but I would be willing to just put it on a wiki here on this github, but idk what people prefer. I'll start working on it and put it up on my blog, just let me know if y'all want it here as a wiki (I don't really know how github wiki's work all that much, always seemed to be a little clunky imo).

Update - started workin' on documentation in a branch on my fork. It's just a root-level md file.

I would be interested to see other people's work flows with the sequelize cli, I'm going to fill in the rest of my workflow, but after getting into it, I'm not really seeing a lot of benefit from the sequelize cli that standalone (and lighter weight) cli tools like db-migrate provide.

It's not like the sequelize cli can look at a model definition and generate most of the up/down scripts for us (like a mature tool like EF in the .NET world can) by comparing the current schema to the changed model definitions - which is fine, but I was hoping an integrated tool would be able to do something like that to speed up writing migrations. Unless I'm mistaken, again, here - which I would welcome! Because having sequelize-cli do more work would be a very strong reason to use it!

Oh, sounds a little bit disappointing :( So sequelize-cli does not provide more than a query builder via its queryInterface, right?

But anyway: Thanks for working on that :)

Do have a look at the below article describing how I was able to use Sequelize CLI with migration files written in Typescript.

Sequelize Cli migrations with typescript

Looking forward to feedback and critique.

@samratshaw is there an article that ISN'T behind a paywall?

@samratshaw is there an article that ISN'T behind a paywall?

I agree this can be annoying. But to circumvent the pay wall you can always open the link on an incognito browser

@farisT thanks I tried using the browser tools to delete the overlay to no avail, didn't think it would be that easy :P

Just an update for those who didn't see. The latest sequelize-cli supports typescript: 6+

This library looks interesting: https://github.com/kimjbstar/sequelize-typescript-migration

There is code in there to parse the migrations from the Typescript models.

How should we reuse the model definition written using decorators? Using Model.rawAttributes just adds the timestamp and id column. @RobinBuschmann Do we need to define the schema and relation in migration.ts too.

@RobinBuschmann @snewell92 I am using the following command for migrations:

tsc src/database/migrations/* --outdir dist/migrations && npx sequelize-cli db:migrate --url 'postgres://'$(dotenv -p DB_USERNAME)':'$(dotenv -p DB_PASSWORD)'@'$(dotenv -p DB_HOST)':'$(dotenv -p DB_PORT)'/'$(dotenv -p DATABASE)'' --migrations-path dist/migrations

As I am using dotenv-cli along with dotenv package for reading .env file, I can read the database credentials from .env and create the postgresql url easily. We do not need a configuration file if --url flag is used. What do you guys think about this solution?

Sample migration script (src/database/migrations/16085945884-test.ts)
`import { DataTypes, QueryInterface } from 'sequelize';

export async function up(qi: QueryInterface) {
try {
await qi.addColumn('PaymentMethods', 'number', {
allowNull: true,
type: DataTypes.DOUBLE
})
// .......
} catch (e) {
console.error(e);
throw e;
}
}

export async function down(qi: QueryInterface) {
try {
await qi.removeColumn('PaymentMethods', 'number')
// .......
} catch (e) {
console.error(e);
throw e;
}
}`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ReneHollander picture ReneHollander  路  3Comments

libvirtadept picture libvirtadept  路  4Comments

nandox5 picture nandox5  路  3Comments

mikew picture mikew  路  4Comments

thestrayed picture thestrayed  路  5Comments