Skip to content

Conversation

@nacoool
Copy link

@nacoool nacoool commented Dec 16, 2025

No description provided.

Copy link
Contributor

@adamruzicka adamruzicka left a comment

Choose a reason for hiding this comment

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

Please use two spaces per indentation level.

What would happen in the down direction if the current value was already outside the integer range?

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

In addition to @adamruzicka's questions I'm looking at other places where a simlar change happend. When I look at db/migrate/20170110113824_change_id_value_range.rb then it changes 3 tables. Shouldn't we migrate all 3 to bigint? And in db/migrate/20200217110708_alter_session_sequence_to_cycle.rb it does the same for sessions so isn't that also affected?

What would happen in the down direction if the current value was already outside the integer range?

FYI: it's also possible to make the migration non-reversible, which is probably fine because I don't think we really reverse migrations.

@nacoool
Copy link
Author

nacoool commented Dec 18, 2025

In addition to @adamruzicka's questions I'm looking at other places where a simlar change happend. When I look at db/migrate/20170110113824_change_id_value_range.rb then it changes 3 tables. Shouldn't we migrate all 3 to bigint? And in db/migrate/20200217110708_alter_session_sequence_to_cycle.rb it does the same for sessions so isn't that also affected?

Do we also want to make similar change for reports and fact_values tables as well?

@ekohl
Copy link
Member

ekohl commented Dec 18, 2025

Do we also want to make similar change for reports and fact_values tables as well?

It makes sense to me. @adamruzicka?

In https://community.theforeman.org/t/pg-error-nextval-reached-maximum-value-of-sequence-logs-id-seq-2147483647/45095 there's some related discussion about setting the start of the sequence. Have you had a look at converting a table with existing entries?

@adamruzicka
Copy link
Contributor

Do we also want to make similar change for reports and fact_values tables as well?

It makes sense to me. @adamruzicka?

Yup, those can get pretty big

@nacoool nacoool force-pushed the fixes-38949 branch 2 times, most recently from 879591b to 2546a5a Compare December 19, 2025 10:14
@ekohl
Copy link
Member

ekohl commented Dec 22, 2025

@nacoool have you done any investigation about the start of the sequence? Is that needed?

@nacoool
Copy link
Author

nacoool commented Dec 23, 2025

@ekohl Yes, I looked into it. The start value is only relevant when a sequence is created or explicitly restarted. For existing sequences, PostgreSQL continues from the current last_value, so the start value itself isn’t needed here. What does matter is ensuring the current value is valid after converting to bigint, which is why resetting it to MAX(id) is the safe approach. I’ll update the migration accordingly.

@nacoool
Copy link
Author

nacoool commented Dec 24, 2025

@ekohl
I have retested on local and after converting to bigint, It picks next value from current max value.
I think we should not change it.
Screenshot From 2025-12-24 13-51-17

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Thanks for checking that. A note for next time: please copy paste text instead of making a screenshot when possible. That makes it easier to read.

Comment on lines 19 to 20
def change_sequence(name, as:)
connection.execute("ALTER SEQUENCE #{connection.quote_table_name(name)} AS #{as}")
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}")

Copy link
Author

Choose a reason for hiding this comment

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

@ekohl
Table name can be different than sequence. then we can use below to fetch sequence

# pg_get_serial_sequence queries the database for the real sequence name
sequence_name = conn.select_value(
  "SELECT pg_get_serial_sequence(#{conn.quote('logs')}, 'id')"
)

# Remove schema prefix if present (e.g., "public.logs_id_seq" -> "logs_id_seq")
sequence_name = sequence_name.split('.').last if sequence_name&.include?('.')

doc: pg_get_serial_sequence
https://www.postgresql.org/docs/16/functions-info.html#FUNCTIONS-INFO-CATALOG-TABLE-PG-GET-SERIAL-SEQUENCE

default_sequence_name will add _id_seq to table name.

[3] pry(main)> conn.default_sequence_name(:logs)
=> "public.logs_id_seq"
[4] pry(main)> conn.default_sequence_name(:logs_id_seq)
=> "logs_id_seq_id_seq"

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to refactor the migration to be based on table names instead of sequence names because all other migrations are also based on the table names. Under the hood default_sequence_name uses pg_get_serial_sequence (see https://github.com/rails/rails/blob/f4321f78fe567fd29dd0a0fb5cb17af68efd36bb/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L246-L257) so that should already be safe.

Copy link
Author

Choose a reason for hiding this comment

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

Thank You for clarification. I have made changes as suggested.

ekohl
ekohl previously approved these changes Jan 5, 2026
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

Code wise 👍 but I think it's good to mention somewhere why this is needed.

@adamruzicka would you mind reviewing since there were some changes I suggested.

@@ -0,0 +1,17 @@
class ChangeIdSequencesToBigint < ActiveRecord::Migration[7.0]

Copy link
Member

Choose a reason for hiding this comment

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

I'd leave out this empty line

Suggested change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants