node-pg-migrate icon indicating copy to clipboard operation
node-pg-migrate copied to clipboard

Generate migrations from existing schema

Open theoephraim opened this issue 7 years ago • 7 comments

When starting to use this module with an existing project and schema, it could be useful to generate the existing schema into migration files. This way you would have the entire DB structure expressed in your migration files.

For me in particular, when I run my test suite, I am generating the db structure from sequelize's "sync" method, meaning that my test db could end up having a slightly different schema than my real production schema. I would much rather just run the migrations on my test db to get it into the right structure, but that would require having the entire schema expressed within migrations.

This would likely require some other changes too though, being able to mark certain migrations (or even operations) as not relevant to a "load up my schema from blank state" type of migration run.

theoephraim avatar Jun 18 '18 19:06 theoephraim

I have this in my backlog for a long time, but it is quite a big feature and I still can't find a time to do it :(

dolezel avatar Jun 20 '18 06:06 dolezel

yeah its a big one for sure... maybe a simpler way to think about it is starting with a base .sql dump file and a helper command to create it?

theoephraim avatar Jun 22 '18 16:06 theoephraim

pg_dump --clean --if-exists --no-owner --no-acl $DATABASE_URL > 0000000000000-init.sql :)

dolezel avatar Jun 26 '18 11:06 dolezel

inspiration:

  • https://github.com/graphile/postgraphile
  • https://github.com/djrobstep/migra

my consideration is that everything I have is either in sql files or in the database. using this tool would require rewriting everything.

disarticulate avatar Jun 10 '19 23:06 disarticulate

Would squashing existing migrations be the same as this?

nurmuhammadsirat avatar Sep 23 '19 02:09 nurmuhammadsirat

No it is for cases you already have same schema in DB and you would like to start using migrations

dolezel avatar Sep 23 '19 06:09 dolezel

@nurmuhammadsirat you could also occasionally clear out all your migrations are replace with a current snapshot of the database

theoephraim avatar Sep 23 '19 15:09 theoephraim