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

@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}")

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants