Clear schema cache on DDL changes
Problem
Some database specs use _test_table
as a default table name.
Depending on the spec the table schema is different and can lead to flaky specs when these specs are run and the schema cache is not cleared. See #450198 (closed)
Proposed solution
- Clear schema cache whenever DDL has changed in specs.
- Remove
connection.schema_cache.clear!
from specs.
Implementation guide
@ahegyi wrote in #450198 (comment 1878526312):
I think we can detect DDL changes in specs using PgQuery (
lib/gitlab/database/query_analyzers/
). In case we detect such cases, we could clear the schema cache in an after block.
We have a way to detect DDL changes here: lib/gitlab/database/query_analyzers/restrict_allowed_schemas.rb
- Implement a new query analyzer that runs in RSpec context.
- Detect DDL changes, flip a thread-local boolean variable for the yielded DB
connection
to true. Example:Thread.current[:ddl_changes][connection.name] = true
- In an
after
RSpec block clear the schema cache for that connection.
Edited by Adam Hegyi