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

前端 未结 8 798
醉酒成梦
醉酒成梦 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 07:44

    I'm afraid the magic solution you're looking for does not exist. This file is normally managed in version control, then for any conflicts on the version line just choose the later of the two dates. As long as you're also running all of the associated migrations nothing should get out of sync this way. If two developers have caused modifications to a similar area of schema.rb and you get conflicts in addition to the version then you are faced with a normal merge conflict resolution, but in my opinion these are normally easy to understand and resolve. I hope this helps some!

    0 讨论(0)
  • 2020-12-01 07:48

    Instead of using .gitignore, use separate branches: Develop which omits schema.rb and Test and Deploy which include schema.rb. Only make code changes in the Develop branches and never merge from Test into Develop. Keep schema.rb in a separate branch:

    Developer A             
        Develop      --------             
        Local Schema          \           Your Repo
        Test                    --------->    Dev A
                                --------->    Dev B
    Developer B               /               Master
        Develop      --------                 Schema
        Local Schema                          Test
        Test                                  Deploy
    

    In Git, branches are pointers to collections of file contents, so they can include or exclude particular files as well as track file versions. This makes them flexible tools for building your particular workflow.

    0 讨论(0)
  • 2020-12-01 07:55

    One other thing you can do is use:

    git update-index --assume-unchanged /path/schema.rb
    

    This will keep the file in the repository but won't track changes. you can switch the tracking anytime by using:

    git update-index --no-assume-unchanged /path/schema.rb
    
    0 讨论(0)
  • 2020-12-01 07:56

    You could define a merge strategy. I've found this solution, but dont remember the source

    [merge "railsschema"]
    name = newer Rails schema version
    driver = "ruby -e '\n\
        system %(git), %(merge-file), %(--marker-size=%L), %(%A), %(%O), %(%B)\n\
        b = File.read(%(%A))\n\
        b.sub!(/^<+ .*\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n=+\\nActiveRecord::Schema\\.define.:version => (\\d+). do\\n>+ .*/) do\n\
          %(ActiveRecord::Schema.define(:version => #{[$1, $2].max}) do)\n\
        end\n\
        File.open(%(%A), %(w)) {|f| f.write(b)}\n\
        exit 1 if b.include?(%(<)*%L)'"
    

    put this "somewhere" and

    git-config --global core.attributesfile "somewhere"
    
    0 讨论(0)
  • 2020-12-01 07:57

    Would it be sufficient to do a rake db:dump in a pre-commit git hook?

    The following won't necessarily fix (1) or (2), but it might take care of the merging issue, and then maybe (1) and (2) go away.

    0 讨论(0)
  • 2020-12-01 07:58
    1. Commit schema.rb file.
    2. Run git pull (or continue with what you're doing)

    Every time you migrate the database, the schema.rb file updates and appears in git status. When working on something and occasionally doing git pull, this can be annoying because you have to commit schema.rb file before pulling to resolve conflict. This means that every time you migrate the database, you need to commit schema.rb file.

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