I recently created a migration to update some data, and after I ran it, the migration changed a bunch of my schema.rb
column definitions from :bigint
to :integer
I've noticed this a few times in the past. Sometimes a migration will change them from :bigint
to :integer
and vice versa other times.
Why is this happening and how can I prevent it? I want to keep them as :bigint
I'm running Ruby on Rails 7.0.2.3 and PostgreSQL 14.2
Thanks
CodePudding user response:
Not sure about the reason, but as far as I remember, this problem arose back in RoR version 3 or 4. You need to use a format like this
tbl.column :really_big_int, :bigint
CodePudding user response:
I was able to figure it out ....
Issue: the database type was different than what the schema had, so when the migration ran, rails realized the types in the actual db were different so it updated the schema for me.
Why we're the types different though?
I had pulled a database dump down from Heroku in order to replicate a problem locally that I could not replicate (it only occurred on Heroku with that data set). Upon doing so, this problem started.
The issue was that Heroku was running Postgres 13.6 and the column types were integer
in that db for whatever reason.
Locally I'm running Postgres 14.2 and the types locally were bigint
.
Why was the schema.rb file being updated if the migration didn't change anything?
This happens because the db:schema:dump
is run after the migrations automatically. This happens in the database.rake
file in Rails.
Source: https://github.com/rails/rails/blob/main/activerecord/lib/active_record/railties/databases.rake#L105
In other words, the migrations get run, then Active Record automatically dumps the schema.
The new schema dump was different than the original schema, due to different data types. Therefore the schema.rb
file was updated (and also any model files if you're using the annotate
gem).
How was I able to tell that?
Log into the DB with psql
and check.
- Local:
psql -h localhost
- Heroku:
heroku pg:psql -a your-app-name
Then inspect the table:
\d table_name
And you'll get something like this:
Table "public.account_invitations"
Column | Type | Collation | Nullable | Default
--------------- -------------------------------- ----------- ---------- -------------------------------------------------
id | bigint | | not null | nextval('account_invitations_id_seq'::regclass)
account_id | integer | | not null |
invited_by_id | integer | | |
token | character varying | | not null |
In there you can see the column types. Locally they were bigint
with Postgres 14.2 but on Heroku they were integer
with Postgres 13.6.
When I pulled a dump down, I restored the local db from that dump, which changed the database vs what was in the schema.
Fix: Restored my original version of my db, ran the migrations, no changes to the schema were made.
How to test this theory ...
Go into your database console and change a column from integer
to bigint
or vice versa. Then run a migration. You'll notice your schema file gets updated, even though your migration probably had nothing to do with those columns whatsoever.