Repository navigation
Support common application usage: forked workers, shared connections, DATABASE_URL - #64
Merged
Merged
Conversation
Forked workers (Puma/Sidekiq), connections shared with another library, DATABASE_URL, transactions opened by another library, schema-per-tenant search_path, read replicas, memoized/subclassed/wrapped query objects and Marshal/JSON of rows. Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
Contributor
Author
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
pg: queries pass their own type maps and read rows with string field names, so a connection shared with another library keeps type_map_for_results / _for_queries and field_name_type. QuerySql.connect takes a URL. pg / mysql2: a connection inherited through fork is detached from the parent's socket before connection_pool's after_fork closes it; a direct pg connection reconnects in the child, a direct mysql2 client raises. mysql2: rows are read with cast: true, cast_booleans: false whatever the client's query options. Document the MySQL outer transaction limitation. Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
connection_pool's after_fork closes every pooled connection in a forked child, also ones the application closed before; PG::Connection#finish raises for those, which broke the fork in the child. Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
…r fork on macOS) Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
connection_pool's after_fork only closes the idle pooled connections; one another thread had checked out at fork time, or a direct connection the child never used, was still the parent's socket in the child, and the child's exit (PQfinish / COM_QUIT on finalization) ended the parent's session. A Process._fork hook points all of them at /dev/null in the child. Co-Authored-By: Doron Eli Rachman <doron050@gmail.com>
doron050
approved these changes
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
E2E tests (and, in follow-up commits on this PR, fixes) for ways Ruby applications commonly use the generated code that were not covered yet.
New scenarios (PostgreSQL / MySQL / SQLite where applicable):
raw_connection, Sequel): PG type maps /field_name_type, mysql2query_optionsandMysql2::Client.default_query_options, SQLiteresults_as_hashQuerySql.connect(DATABASE_URL)on PostgreSQL#transactioninside a transaction opened by another librarysearch_pathswitching with prepared statements.../**,SimpleDelegatorMarshal(Rails.cache) and JSONFailing on
main(a44f282):test_a_forked_child_leaves_the_parent_connections_usable(child's connection_pool after_fork closes the inherited connections and terminates the parent's sessions)test_a_connection_shared_with_another_library_keeps_its_settings(generated code overwrites the connection's type maps;field_name_type = :symbolraisesUndefinedEncoder)test_connect_accepts_a_connection_url(TypeError)test_a_forked_child_leaves_the_parent_connections_usable(same as pg)test_a_client_with_other_query_options_still_returns_typed_rows(cast: falsereturns Strings,cast_booleans: truereturns true/false for Integer columns)Fixes (generated code,
lib/sqlc_gen_ruby/drivers/{pg,mysql2}.rb):exec_prepared/exec_paramswithclient.query_types, results getclient.result_typesandfield_name_type = :string), so the connection'stype_map_for_results/_for_queries/field_name_typeare never changed.QuerySql.connectalso takes a URL string.ConnectionStateremembers the pid that opened the connection; when connection_pool'safter_forkcloses it in a child, its socket is first pointed at/dev/null, so the parent's session survives. A pg connection used directly in a child reconnects; a direct mysql2 client raisesMysql2::Error::ConnectionError.Process._forkhook releases, in the child, every inherited connection the generated code has used (ObjectSpace.each_object(ConnectionState)), also ones another thread had checked out at fork time and ones the child never uses, so the child's exit does not end the parent's sessions. Closing an already closed pg connection is a no-op (connection_pool'safter_forkcloses those too).as: :array, cast: true, cast_booleans: falsewhatever the client's / default query options.transactioninside a rawBEGINcommits it; SQLite children must reopen the database.docs/Coverage.mdlists every new test.Checklist
examples/is regenerated (rake sqlc_generate_process) and committedrake test, andrake end2end_*when the generated code changes)rake rubocop) and Steep-clean (rake steep)bug,enhancement,documentation, orbreaking) so the release notes are grouped correctlyRequested by: @doron050