What is the preferred way to manage schema.rb in git?

前端 未结 8 799
醉酒成梦
醉酒成梦 2020-12-01 07:05

I don\'t want to add schema.rb to .gitignore, because I want to be able to load a new database schema from that file. However, keeping it checked

相关标签:
8条回答
  • 2020-12-01 08:02

    What has worked really well for me is to delete and .gitignore schema.rb and then have it regenerated for each developer when they rake db:migrate.

    You can still achieve what you wanted without migrating from 0 and risking broken migrations from years ago by simply doing a "roll-up" of the migrations periodically. You can do this by:

    1. Run all outstanding migrations with rake db:migrate
    2. Taking the contents of your schema.rb in the ActiveRecord::Schema.define block
    3. Paste it into your initial_schema migration inside def up (overwriting what's already there)
    4. Delete all other migrations

    Now your initial_schema migration is your starting point for new systems and you don't have to worry about conflicts in schema.rb that may not be resolved correctly. It's not magical, but it works.

    0 讨论(0)
  • 2020-12-01 08:02

    I built a gem to solve this problem.

    It sorts columns, index names and foreign keys, removes excess whitespace and runs Rubocop for some formatting to unify the output of your schema.rb file.

    https://github.com/jakeonrails/fix-db-schema-conflicts

    After you add it to your Gemfile you just run rake db:migrate or rake db:schema:dump like normal.

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