Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-Create Migrations #257

Open
jgr3go opened this issue Jan 29, 2016 · 52 comments
Open

Auto-Create Migrations #257

jgr3go opened this issue Jan 29, 2016 · 52 comments
Labels

Comments

@jgr3go
Copy link

jgr3go commented Jan 29, 2016

I know this has been discussed before, but it would be really helpful if sequelize could auto-create migration scripts based on differences in sequelizer and the database.

I currently have a fork I'm testing out that attempts to solve this issue, and I wanted to open up the discussion again to make sure this would be accepted by sequelize/the community.

Right now the rules put into place for sequelize migration:create:auto does the following (note, this treats sequelize as canon, so it will attempt to resolve the migrations to create a state in the database that matches sequelizer imports):

  1. Read model.js files from a new config/models.json, and import the model/column definitions into sequelizer.
  2. Read all tables from the database and import the model/column definitions
  3. Compare:
    • If a column exists in the database and not sequelizer, create removeColumn() migration
    • If a column exists in sequelizer and not database, create addColumn() migration
    • If a table exists in database and not in sequelizer, print a WARNING, unless --drop flag is present, in which case create a dropTable() migration
    • If a table exists in sequelizer and not database, do nothing. It's currently assumed sequelizer.sync() will take care of this.
  4. Keep track of auto-migrations by filename convention: {timestamp}-{model}-auto-{version}, e.g. 20160129135511-users-auto-0002.js . If there were multiple changes, it will chain them all together in the same model file, relative to the models they're affecting. It will also clean any auto migration files that haven't been migrated to the database yet.

This works very similar to how Django's South works, but it's a little less fleshed out. It (currently) doesn't handle column alters, indexes, associations, etc. Before I (or someone else I suppose) continues development on it I'd like to start a discussion to see if this is worth pursuing and if the owners have any hesitations on this.

@Americas
Copy link
Collaborator

Americas commented Mar 9, 2016

I don't see myself using such a feature. I like having control over the architecture of the DB. As such, I like to have conscientious control over migrations, and not have them auto-generated.

@benirose
Copy link

benirose commented Jul 1, 2016

I'm a huge fan of migration-diff generation, and would like to help work to get this in. I'm not interested in duplicating model definitions across my migrations. I want to either write the migration or the model change, not both. To me that's one of the main benefits of an ORM.

I'm interested to know why you went with config/models.js rather than loading in the models/index.js that's supposed to represent the models as they exist in the project.

@bisubus
Copy link

bisubus commented Jul 19, 2016

Diffs are quite ambitious, generating a base migration from existing model would be great for starters.

This gist https://gist.github.com/manuelbieh/ae3b028286db10770c81 gives the gist of it.

@itzmattu
Copy link

@benirose Have you made any progress on this? Doesn't seem like sequelize is prioritizing it from their roadmap page, but there is some interest in the comments. Curious if this is the most developed attempt at an equivalent to Django's South auto migration feature for sequelize or any other NodeJS ORM module.

@benirose
Copy link

I sadly haven't had time/focus to work on it. My project needs also changed from MySQL to Mongo, so I'm not actively focused on using this library. I'd still like to contribute when I have some more time though.

@andyhennie
Copy link

+1 to auto-migration generator feature.

@iDVB
Copy link

iDVB commented Feb 16, 2017

+1

1 similar comment
@iagomelanias
Copy link

+1

@ilan-schemoul
Copy link

So @Americas what about this highly requested feature ?

@antoniovassell
Copy link

+1

@adaml881
Copy link

+1. This would be an awesome feature

@gtg092x
Copy link

gtg092x commented Apr 27, 2017

👍 - this is one of best things about Entity Framework as well. I'd be happy to help with this.

@fcpauldiaz
Copy link

+1

3 similar comments
@asdfjackal
Copy link

+1

@iknowmac
Copy link

iknowmac commented Jun 7, 2017

+1

@brud
Copy link

brud commented Jun 8, 2017

+1

@littlee
Copy link

littlee commented Jun 13, 2017

+10086

@sergmetelin
Copy link

+1

1 similar comment
@tonymayoral
Copy link

+1

@moiz-frost
Copy link

Are there any other alternate ORMs supporting auto migrations?

@zuhair-naqvi
Copy link

+1000000000000000

@flexxnn
Copy link

flexxnn commented Jul 18, 2017

Hi. I make my own module to do it. It works, but have some issues.
You can try https://github.com/flexxnn/sequelize-auto-migrations

@adaml881
Copy link

@flexxnn thank you it looks awesome. I'll give it a go soon

@SirWaithaka
Copy link

+1 definitely some feature I would love to have on the ORM

@aryeharmon
Copy link

aryeharmon commented Sep 28, 2017

@flexxnn you implement your own migrations, not using sequelize's method. maybe we can change this to use regular migrations?

@leodutra
Copy link

+1.
@flexxnn Using it on 3 microservices. I'd help on the development.

@jourdanrodrigues
Copy link

+1. Help @flexxnn's solution.

@johnny-dash
Copy link

johnny-dash commented Oct 16, 2017

+1.
I am currently using sequelize-auto-migrations npm package by @flexxnn . It solves my pain currently but still not stable enough and the generated migration file have some error need to be manually fixed. Hopefully, this feature could come out soon.

@Aknilam
Copy link

Aknilam commented Nov 25, 2017

+1k

@pythonscraping
Copy link

+1

I would love this feature that has existed for a long time in Django.

@murbanowicz
Copy link

murbanowicz commented Mar 13, 2018

Any progress on that? It is something which really should be implemented. Entity Framework, Django, Hibernate - all works nice with migrating DB which is not possible at the moment with sequelize.

@antl3x
Copy link

antl3x commented Mar 22, 2018

+1

@leodutra
Copy link

leodutra commented Mar 24, 2018 via email

@aznh
Copy link

aznh commented Apr 2, 2018

+1

@larsbs
Copy link

larsbs commented Apr 3, 2018

Please, stop writing +1 comments, since Github added support for reacting to a comment, those are not needed anymore and just pollute the notifications of those subscribed to see real changes on issues.

If you want to show your support to a feature, simply +1 the initial comment. That would also help maintainers to see if there's enough interest in the feature. It's easier to just have a look at the first comment and see 52 +1 than having to count them manually through the comments.

@jourdanrodrigues
Copy link

@larsbs Hope the maintainers get annoyed enough to work on it. +1 👍

@leodutra
Copy link

leodutra commented Apr 5, 2018 via email

@larsbs
Copy link

larsbs commented Apr 6, 2018

https://hueniverse.com/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time-d933471d59de

@jourdanrodrigues
Copy link

@larsbs You're right. Sorry about that, then.

@techsin
Copy link

techsin commented Jul 15, 2018

what do you think of this approach

https://medium.com/riipen-engineering/squashing-migrations-with-sequelize-5478336247e5

create schema dump file
delete all migrations
whenever someone new has to run all migrations they simply load schema file through psql.
and npm script would do it fast.

personally i want a solution where i can tell from where to where, consolidate all files.

@flexxnn
Copy link

flexxnn commented Dec 24, 2018

Hi, sequelize-auto-migrations was updated today (changelist here: flexxnn/sequelize-auto-migrations#31)

@alirezamohammadi
Copy link

+1

codetriage-readme-bot pushed a commit to codetriage-readme-bot/cli that referenced this issue Jun 5, 2019
Only display "to get the next exercise" message if the server responds with the correct iteration value.
@jnelken
Copy link

jnelken commented Feb 7, 2020

For those interested, keep an eye on this project. It seems to attack the issue correctly. However it's currently a WIP
https://www.npmjs.com/package/@transcend-io/wildebeest

@Gcaufy
Copy link

Gcaufy commented Feb 28, 2020

Also looking for an Auto-migration solution in Node.

Djiango Migration solution is the best solution so far. wish sequelize can support it.

@duaneking
Copy link

I support a code-first migration feature; its the thing that is really missing here as the forced use of a CLI tool that does multiple things at once - for both model generation and migration creation - is a clear violation of DRY.

@kamilglod
Copy link

In Python world we have

  1. django with built-in migration tool that can auto-generate migration script based on the models and built state from all previous migrations.
  2. alembic for sqlalchemy that does the same job but using db connection to get current state instead of previous migrations

I know that it's not trivial to implement it from scratch but it would definitely improve usability of this library. And in most cases you still should review generated script before applying it to any environment as there is always some risk of error.

It would be good to see some code examples of adding migrations manually as well (like simple table creation, adding foreign keys, dropping columns etc).

@tribiec
Copy link

tribiec commented Nov 10, 2023

I'm very surprised to see the response from the mantainers on this issue, about to stop using Sequelize because this.

@aneeq-ur-rehman4
Copy link

+1

1 similar comment
@zikyfranky
Copy link

+1

@uchar
Copy link

uchar commented Jun 28, 2024

Some of this js librarys are so weird , how can ORM not having this feature in 2024?

@techsin
Copy link

techsin commented Jun 29, 2024

Use primsa or drizzle i think a decade is enough forget this library

@oxedom
Copy link

oxedom commented Feb 10, 2025

I’m a bit surprised that I’m expected to maintain two separate files to represent a model. It feels like a trivial task to be able to generate a migration file directly from the class representation of a model, especially since the ORM is already interacting with the database using SQL behind the scenes. At the very least, it would be great to have the option to export the commands that are exucuted from themethod sequelize.sync() into a some sort of output (.sql file or return it as a string)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests