Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5.1.0 how to upgrade

Rails 5.1.0 introduces the bigint primary keys and foreign keys.

All new tables will have bigint pk and creating a reference migration to an old table will not work since the old pk is a normal int.

Using change_column _, :id,:bigint just errors with a foreign key is pointing towards it, not to mention all the manual labour of finding all the tables and which has which key that needs to be modified.

How do I migrate my production database all my tables to use bigint pk and fk's?

Ofcourse since it's production rails db:drop rails db:setup is not an option.

like image 654
Iaan Krynauw Avatar asked Nov 07 '22 22:11

Iaan Krynauw


1 Answers

I facing the same problem. Temporary remove fk's should work. The goal is to change all primary keys from int to bigint.

class ChangeForeignKeysToBigInt < ActiveRecord::Migration[5.1]
  def change

    remove_foreign_key "event_users", "events"

    change_column :event_users, :event_id, :bigint
    change_column :events, :id, :bigint

    add_foreign_key "event_users", "events"

  end
end

you have to do that with all your tables and foreign key columns maybe handle indices the same way, i dont tested that

like image 111
grohmio Avatar answered Nov 15 '22 05:11

grohmio