[LRUG] Rails schema line order changing
Carlos I Peña
carlos2 at compendium.com.ar
Tue Mar 2 13:11:49 PST 2021
My experience is that sooner or later an unwanted change will make its
way to the schema.rb and the schema.rb won't represent the state of the
production database anymore and will drift apart from it more and more as time
goes by.
A solution I came up with was to monitor the schema.rb in the relase
process. In production, wether we use Heroku, Capistrano or
other, at some point we run the migrations in the production database,
the trick is comparing the schema.rb before and after running the
migration. If there's no change that means the versioned schema is
alright, if there's a diff it means the production database and the
versioned schema.rb don't match, in that case a slack message is
sent to the #dev channel and someone will deal with it..
For comparing the old schema.rb against the new schema.rb, I'm just
using the old Unix `cmp` program. For rails to actually update the
schema.rb in production for this comparison to happen, the
config/environments/production.rb config should include:
```
config.active_record.dump_schema_after_migration = true
```
I hope it helps.
Cheers,
CIP
On Mon, Mar 01, 2021 at 08:15:30AM +0000, Ed James wrote:
> The only reliable “solution” I’ve come across is to ensure your team
> treat the “schema.rb” file the same way they treat their own own when
> raising a pull request i.e. if I add a migration the schema file should
> only reflect my change - if it doesn’t that is a signal to me that I
> need to rebuild my database:
>
> rails db:drop db:create db:schema:load db:seed
>
> Once your colleagues get used to this (and it’s pretty easy really)
> you’ll find you have very little churn on your schema file.
>
> Good luck!
> __________________________________________________________________
>
> [rubbish.png] Ed James
> [1]I will respect your spam
>
> On 1 Mar 2021, at 08:09, Stuart Harrison <[2]pezholio at gmail.com> wrote:
>
> Morning LRUG
> I wanted to share an annoyance I've had on multiple projects, and
> wondered if anyone had any fixes for it?
> If there are multiple devs on a project committing a bunch of
> migrations and running `db:migrate`, it seems that, over time, the
> order of columns in the schema.rb file jumps around every time the
> command is run.
> I can get around this by only committing the relevant line(s) when I
> run a migration, but this does get annoying.
> I'm sure there is a solution, but it's disappeared out of my head, and
> seems to be an ungoogleable problem, so was wondering if anyone had
> come across a solution?
> Cheers
> Stu
> _______________________________________________
> Chat mailing list
> [3]Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription:
> http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
> References
>
> 1. mailto:ed.james.spam at gmail.com
> 2. mailto:pezholio at gmail.com
> 3. mailto:Chat at lists.lrug.org
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
More information about the Chat
mailing list