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

Delay Adapter#check_version to #configure_connection #49378

Merged
merged 1 commit into from
Sep 29, 2023

Conversation

casperisfine
Copy link
Contributor

Ideally we should be able to checkout an Adapter instance without triggering a connection to the server.

For some adapters like Trilogy that is the case today, but only if you have a schema cache loaded, otherwise check_version will trigger a query.

I think this check can be delayed to when we actually use the connection.

@casperisfine casperisfine marked this pull request as draft September 25, 2023 16:24
@casperisfine
Copy link
Contributor Author

There's some intricate build failures to investigate. I'll have another look tomorrow.

Copy link
Member

@composerinteralia composerinteralia left a comment

Choose a reason for hiding this comment

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

👍🏻 Making this change was on my list of things to do. GitHub has a lingering Trilogy adapter patch that does basically the same thing, and it'll be nice for us to get that cleaned up.

@casperisfine casperisfine force-pushed the ar-delay-version-check branch 5 times, most recently from 3e566f6 to 50e337f Compare September 27, 2023 10:56
@rails-bot rails-bot bot added the railties label Sep 27, 2023
@casperisfine
Copy link
Contributor Author

I only have one failure left in Action Cable, but I'm having trouble reproducing it locally :/

@casperisfine
Copy link
Contributor Author

I haven't had the time to figure out the Action Cable issue today, but I don't expect it to need much more change.

@matthewd any opinions here?

@casperisfine
Copy link
Contributor Author

I only have one failure left in Action Cable, but I'm having trouble reproducing it locally :/

Ok, so actually it seems they are currently broken on main. All the PostgresqlAdapter tests are skipped because we fail to connect to postgresql.

The PG server is started but I think we fail to create the database or something.

@@ -233,7 +233,7 @@ def with_connection

# Returns true if a connection has already been opened.
def connected?
synchronize { @connections.any? }
synchronize { @connections.any?(&:connected?) }
Copy link
Member

Choose a reason for hiding this comment

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

Gently hesitant here on whether callers are most likely to expect this to mean "are there physical sockets established" or "does this pool have conns in it"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, the doc says "connection has been opened".

before my change this was true as soon as there one one adapter instance in the pool, now that the connection is properly lazy, it's no longer true. I had to change this to fix a good number of tests.

warn "Ignoring #{filename} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{cache.schema_version}."
connection_pool.schema_reflection.clear!
Copy link
Member

Choose a reason for hiding this comment

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

This sounds reasonable but only semi-related... assuming it is separate (the instigating change?), might be better split out, it only because of how confusing it is that this is also about a different version check? 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to #49415

@casperisfine
Copy link
Contributor Author

I rebased on top of #49415, this solves the circular issue in PostgresAdapter.

casperisfine pushed a commit to Shopify/rails that referenced this pull request Sep 28, 2023
Ref: rails#49378

As discussed with Matthew Draper, we have a bit of a chicken and egg
problem with the schema cache and the database version.

The database version is stored in the cache to avoid a query,
but the schema cache need to query the schema version in the database
to be revalidated.

So `check_version` depends on `schema_cache`, which depends on
`Migrator.current_version`, which depends on `configure_connection`
which depends on `check_version`.

But ultimately, we think storing the server version in the cache
is incorrect, because upgrading a DB server is orthogonal from
regenerating the schema cache.

So not persisting the version in cache is better. Instead we store
it in the pool config, so that we only check it once per process
and per database.
@casperisfine casperisfine force-pushed the ar-delay-version-check branch 2 times, most recently from c3c3854 to 8f2f25d Compare September 28, 2023 15:17
Ideally we should be able to checkout an Adapter instance without
triggering a connection to the server.

For some adapters like Trilogy that is the case today, but only if
you have a schema cache loaded, otherwise `check_version` will trigger
a query.

I think this check can be delayed to when we actually use the connection.
@byroot byroot merged commit 0d0e6bd into rails:main Sep 29, 2023
4 checks passed
tagliala added a commit to ifad/chronomodel that referenced this pull request Oct 5, 2023
tagliala added a commit to ifad/chronomodel that referenced this pull request Oct 5, 2023
tagliala added a commit to ifad/chronomodel that referenced this pull request Oct 5, 2023
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

5 participants