-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change int primary keys to bigint (#1088)
Integer primary keys can cause problems over time on big tables, and in general it's just better practice to use bigints instead. Some of our larger tables use UUIDs instead of ints, so this only upgrades the tables that used ints. (Practically speaking, none of these tables are really *likely* to overflow their primary key sequence, but better safe than sorry here, especially with technical maintainers offboarding.) Fixes #1067.
- Loading branch information
Showing
2 changed files
with
40 additions
and
8 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
db/migrate/20230209200733_change_int_to_bigint_for_primary_keys.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class ChangeIntToBigintForPrimaryKeys < ActiveRecord::Migration[7.0] | ||
def change_serial_primary_key_type(table, key, key_type) | ||
change_column table, key, key_type | ||
execute "ALTER SEQUENCE #{table}_#{key}_seq AS #{key_type};" | ||
end | ||
|
||
def up | ||
# Foreign Keys | ||
change_column :annotations, :author_id, :bigint | ||
change_column :imports, :user_id, :bigint | ||
change_column :invitations, :issuer_id, :bigint | ||
change_column :invitations, :redeemer_id, :bigint | ||
|
||
# Primary Keys | ||
change_serial_primary_key_type :imports, :id, :bigint | ||
change_serial_primary_key_type :invitations, :id, :bigint | ||
change_serial_primary_key_type :users, :id, :bigint | ||
end | ||
|
||
def down | ||
# Primary Keys | ||
change_serial_primary_key_type :imports, :id, :int | ||
change_serial_primary_key_type :invitations, :id, :int | ||
change_serial_primary_key_type :users, :id, :int | ||
|
||
# Foreign Keys | ||
change_column :annotations, :author_id, :int | ||
change_column :imports, :user_id, :int | ||
change_column :invitations, :issuer_id, :int | ||
change_column :invitations, :redeemer_id, :int | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters