Skip to content

Commit

Permalink
Change int primary keys to bigint (#1088)
Browse files Browse the repository at this point in the history
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
Mr0grog authored Feb 9, 2023
1 parent 81f440b commit d12fa29
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
32 changes: 32 additions & 0 deletions db/migrate/20230209200733_change_int_to_bigint_for_primary_keys.rb
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
16 changes: 8 additions & 8 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_01_24_184531) do
ActiveRecord::Schema[7.0].define(version: 2023_02_09_200733) do
# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "pgcrypto"
Expand All @@ -19,7 +19,7 @@

create_table "annotations", primary_key: "uuid", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "change_uuid", null: false
t.integer "author_id", null: false
t.bigint "author_id", null: false
t.jsonb "annotation", null: false
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
Expand All @@ -39,8 +39,8 @@
t.index ["uuid_to"], name: "index_changes_on_uuid_to"
end

create_table "imports", id: :serial, force: :cascade do |t|
t.integer "user_id"
create_table "imports", force: :cascade do |t|
t.bigint "user_id"
t.integer "status", default: 0, null: false
t.string "file"
t.jsonb "processing_errors"
Expand All @@ -53,9 +53,9 @@
t.index ["user_id"], name: "index_imports_on_user_id"
end

create_table "invitations", id: :serial, force: :cascade do |t|
t.integer "issuer_id"
t.integer "redeemer_id"
create_table "invitations", force: :cascade do |t|
t.bigint "issuer_id"
t.bigint "redeemer_id"
t.string "code"
t.string "email"
t.datetime "expires_on", precision: nil
Expand Down Expand Up @@ -132,7 +132,7 @@
t.index ["name"], name: "index_tags_on_name", unique: true
end

create_table "users", id: :serial, force: :cascade do |t|
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
Expand Down

0 comments on commit d12fa29

Please sign in to comment.