Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions db/migrate/20251216122223_change_id_sequences_to_bigint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class ChangeIdSequencesToBigint < ActiveRecord::Migration[7.0]
# The 20170110113824_change_id_value_range migration changed logs.id, reports.id,
# and fact_values.id columns to bigint, but ActiveRecord's change_column doesn't
# update the underlying sequence type. This fixes the sequences to allow values
# beyond 2^31-1.

def up
change_sequence :logs_id_seq, as: :bigint
change_sequence :reports_id_seq, as: :bigint
change_sequence :fact_values_id_seq, as: :bigint
end

def down
raise ActiveRecord::IrreversibleMigration
end

private

def change_sequence(name, as:)
connection.execute("ALTER SEQUENCE #{connection.quote_table_name(name)} AS #{as}")
Comment on lines +19 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's a guarantee that the table name is the same as the sequence. In the ActiveRecord code I see you can at least use connection.default_sequence_name(table_name, primary_key) with the latter argument being optional. https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-default_sequence_name doesn't have a good description, but for PostgreSQL it'll do the right thing: return the sequence name for a column.

So I'd suggest:

Suggested change
def change_sequence(name, as:)
connection.execute("ALTER SEQUENCE #{connection.quote_table_name(name)} AS #{as}")
def change_sequence(table_name, as:)
connection.execute("ALTER SEQUENCE #{connection.default_sequence_name(table_name)} AS #{as}")

end
end
Loading