Skip to content
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

Add ability to ignore tables in the schema cache #43075

Merged

Conversation

eileencodes
Copy link
Member

@eileencodes eileencodes commented Aug 23, 2021

In cases where an application uses pt-osc or lhm they may have
temporary tables being used for migrations. Those tables shouldn't be
included by the schema cache because it makes the cache bigger and those
tables shouldn't ever be queried by the application. This feature allows
applications to configure a list of or regex of tables to ignore. The
behavior for ignored tables for each method in the schema cache
(columns, columns_hash, primary_keys, and indexes) matches the
behavior of a non-existent table.

To use in an application configure config.active_record.schema_cache_ignored_tables
to an array of tables or regex's. Those tables will not be included in
the schema cache dump.

cc/ @rafaelfranca @casperisfine @tenderlove - I think this will work for your schema cache. Note that in our app I found we actually weren't ensuring that primary_keys, columns, columns_hash and indexes were behaving the same as a non-existent table which meant that if these tables were ever queried then they'd populate the cache in-memory. Since these tables are not supposed to be queried this technically shouldn't change behavior when your app opts into this.

Copy link
Contributor

@casperisfine casperisfine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Works for us

eileencodes added a commit to eileencodes/rails that referenced this pull request Aug 26, 2021
In rails#43075 I added the ability to skip adding a configured set of tables
to the schema cache. After pushing that PR I found the behavior across
methods and database adapters to be inconsistent when a table didn't exist.
Since ignored tables should behave the same as missing tables, I wanted
to make the behavior consistent.

Previously the behavior was:

*mysql*
columns & columns_hash: Raised db error for non-existent table
primary_keys: returned `nil` for non-existent table
indexes: Raised db error for non-existent table

*sqlite3*
columns & columns_hash: Raised db error for non-existent table
primary_keys: returned `nil` for non-existent table
indexes: returned `[]` for non-existent table

*postgresql*
columns & columns_hash: Raised db error for non-existent table
primary_keys: returned `nil` for non-existent table
indexes: returned `[]` for non-existent table

Current behavior:

*mysql, sqlite3, and postgresql*
columns & columns_hash: Raise db error for non-existent table
primary_keys: returns `nil` for non-existent table
indexes: returns `[]` for non-existent table

The only change is to mysql, which used to raise for indexes, now it
returns and empty array like all the other adapters.
In cases where an application uses pt-osc or lhm they may have
temporary tables being used for migrations. Those tables shouldn't be
included by the schema cache because it makes the cache bigger and those
tables shouldn't ever be queried by the application. This feature allows
applications to configure a list of or regex of tables to ignore. The
behavior for ignored tables for each method in the schema cache
(`columns`, `columns_hash`, `primary_keys`, and `indexes`) matches the
behavior of a non-existent table. `columns` and `columns_hash` will
raise a database not found error, `primary_keys` will return `nil`. and
`indexes` will return an empty array. See rails#43105 which make behavior
across adapters consistent.

To use in an application configure `config.active_record.schema_cache_ignored_tables`
to an array of tables or regex's. Those tables will not be included in
the schema cache dump.
@eileencodes eileencodes force-pushed the add-ignore-tables-to-schema-cache branch from 730cf95 to e3b4a46 Compare August 26, 2021 19:06
@eileencodes eileencodes merged commit b5fcf15 into rails:main Aug 26, 2021
@eileencodes eileencodes deleted the add-ignore-tables-to-schema-cache branch August 26, 2021 20:48
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.

None yet

2 participants