Use a MAX() aggregation when fetching current count during reconciliation #254
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #253
On PostgreSQL, if the table where the counts are stored doesn't have a primary_key specified (in our case, because it's actually a view), reconciliation fails with an error:
This is because, according to the Postgres docs:
When there's no primary key index present, even though Rails knows what the primary key is, no functional dependency gets detected at the DB level.
This
MAX
agg should effectively be a no-op in every case, but it prevents PG from blowing up. An alternate fix to this same problem would be adding"#{relation_class_table_name}.#{column_name}"
to the GROUP BY fields.Note: the specs added in this commit will pass on SQLite even without these fixes — they work as expected when running the specs on a PostgreSQL DB. Running the tests on PG also reveals that the existing string ID specs (which also don't have a primary_key index) fail in the same way).