Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Prevent ActiveRecord errors when adding columns#725

Merged
DavidBiddle merged 1 commit intomainfrom
enable-enumerate-columns-setting
Apr 4, 2025
Merged

Prevent ActiveRecord errors when adding columns#725
DavidBiddle merged 1 commit intomainfrom
enable-enumerate-columns-setting

Conversation

@DavidBiddle
Copy link
Copy Markdown
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 govuk-forms/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.
@sonarqubecloud
Copy link
Copy Markdown

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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants