What's a good way to clean up my migrations in Rails?

后端 未结 4 1523
梦如初夏
梦如初夏 2021-02-04 09:30

So I\'ve been working on this web app for a year now and I would like to compile to schema into ONE migration, that way my text editor loads faster, git working directory isn\'t

相关标签:
4条回答
  • 2021-02-04 09:47

    One way to go is to take a blank database and run all the migrations. Now you've got all the template data which you can save to a yaml. The yaml plus the schema should be enough to bring the DB back without running any of your previously existing migrations.

    However, other answers should mention an existing tool or gem for doing this.

    0 讨论(0)
  • 2021-02-04 09:52

    Given that none of the answers mention it, this is the gem that does the job: https://github.com/jalkoby/squasher

    It basically reruns the migrations from scratch until the date you specify, and then loads the resulting db/schema.rb into an initial migration that replaces the old ones. It can also cleanup the schema_migrations table so you don't get those

    up     <timestamp>  ********** NO FILE **********
    

    entries when running rake db:migrate:status.

    0 讨论(0)
  • 2021-02-04 09:53

    Remove the migration files once you've migrated your servers. If you ever want to start with a fresh deployment, run rake db:schema:load or rake db:setup. You shouldn't be re-running all your migrations as explained here.

    0 讨论(0)
  • 2021-02-04 09:57

    You don't need to keep your migrations around forever, you are free to delete them as soon as you're sure you don't need them anymore. Just go into your db/migrate/ directory and delete the migrations that are older than, say, a couple months.

    As long as all the migrations that you want to delete have been applied everywhere (i.e. development and production) then you don't need them anymore (unless you want to go backwards). Really, migrations aren't meant to be permanent files, they're just around to get you from A to B and then they're just baggage.

    0 讨论(0)
提交回复
热议问题