Skip to content

Prevent ActiveRecord errors when adding columns #725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025

Conversation

DavidBiddle
Copy link
Contributor

@DavidBiddle DavidBiddle commented Apr 4, 2025

What problem does this pull request solve?

Trello card: N/A

Rails uses prepared statements to query the database. By default these use wildcard select statements.

This commonly causes issues when adding new columns - the new column will cause postgres to throw an error cached plan must not change result type`.

In Rails 7, a new config setting, enumerate_columns_in_select_statements, was added. This replaces the wildcard select statements with explicit calls to each column. See the Rails PR for details.

This means that the prepared statements will be regenerated every time a column is added, so postgres won't throw the error.

This PR turns that config setting on for our ActiveRecord models.

We've already done this in forms-admin (see alphagov/forms-admin#1877).

Things to consider when reviewing

  • Ensure that you consider the wider context.
  • Does it work when run on your machine?
  • Is it clear what the code is doing?
  • Do the commit messages explain why the changes were made?
  • Are there all the unit tests needed?
  • Do the end to end tests need updating before these changes will pass?
  • Has all relevant documentation been updated?

Rails uses [prepared
statements](https://www.postgresql.org/docs/current/sql-prepare.html) to
query the database. By default these use wildcard select statements.

This commonly causes issues when adding new columns - the new column
will cause postgres to throw an error `cached plan must not change
result type`.

In Rails 7, a new config setting,
`enumerate_columns_in_select_statements`, was added. This replaces the
wildcard select statements with explicit calls to each column. See [the
Rails PR](rails/rails#41718) for details

This means that the prepared statements will be regenerated every time a
column is added, so postgres won't throw the error.

This PR turns that config setting on for our ActiveRecord models.
Copy link

sonarqubecloud bot commented Apr 4, 2025

@DavidBiddle DavidBiddle marked this pull request as ready for review April 4, 2025 09:46
@DavidBiddle DavidBiddle merged commit 27aa74b into main Apr 4, 2025
7 checks passed
@DavidBiddle DavidBiddle deleted the enable-enumerate-columns-setting branch April 4, 2025 09:50
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.

2 participants