cli icon indicating copy to clipboard operation
cli copied to clipboard

Auto-Create Migrations

Open jgr3go opened this issue 9 years ago • 51 comments

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.

jgr3go avatar Jan 29 '16 20:01 jgr3go

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.

Americas avatar Mar 09 '16 16:03 Americas

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.

benirose avatar Jul 01 '16 22:07 benirose

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.

bisubus avatar Jul 19 '16 15:07 bisubus

@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.

itzmattu avatar Sep 30 '16 07:09 itzmattu

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.

benirose avatar Sep 30 '16 15:09 benirose

+1 to auto-migration generator feature.

andyhennie avatar Nov 06 '16 09:11 andyhennie

+1

iDVB avatar Feb 16 '17 15:02 iDVB

+1

iagomelanias avatar Feb 18 '17 08:02 iagomelanias

So @Americas what about this highly requested feature ?

ilan-schemoul avatar Apr 12 '17 10:04 ilan-schemoul

+1

antoniovassell avatar Apr 24 '17 00:04 antoniovassell

+1. This would be an awesome feature

adaml881 avatar Apr 24 '17 13:04 adaml881

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

gtg092x avatar Apr 27 '17 23:04 gtg092x

+1

fcpauldiaz avatar May 11 '17 19:05 fcpauldiaz

+1

asdfjackal avatar May 31 '17 13:05 asdfjackal

+1

iknowmac avatar Jun 07 '17 16:06 iknowmac

+1

brud avatar Jun 08 '17 13:06 brud

+10086

littlee avatar Jun 13 '17 04:06 littlee

+1

sergmetelin avatar Jun 26 '17 05:06 sergmetelin

+1

tonymayoral avatar Jul 02 '17 02:07 tonymayoral

Are there any other alternate ORMs supporting auto migrations?

moiz-frost avatar Jul 03 '17 22:07 moiz-frost

+1000000000000000

zuhair-naqvi avatar Jul 11 '17 04:07 zuhair-naqvi

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

flexxnn avatar Jul 18 '17 09:07 flexxnn

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

adaml881 avatar Jul 18 '17 09:07 adaml881

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

SirWaithaka avatar Sep 09 '17 09:09 SirWaithaka

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

aryeharmon avatar Sep 28 '17 14:09 aryeharmon

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

leodutra avatar Sep 29 '17 22:09 leodutra

+1. Help @flexxnn's solution.

jourdanrodrigues avatar Oct 13 '17 16:10 jourdanrodrigues

+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.

johnny-dash avatar Oct 16 '17 03:10 johnny-dash

+1k

Aknilam avatar Nov 25 '17 10:11 Aknilam

+1

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

pythonscraping avatar Feb 21 '18 23:02 pythonscraping