Skip to content

Split connection creation from connection caching in Schema and QueryBuilder#5

Merged
eaguad1337 merged 2 commits into
3.xfrom
circulon/connection-caching
Jun 7, 2026
Merged

Split connection creation from connection caching in Schema and QueryBuilder#5
eaguad1337 merged 2 commits into
3.xfrom
circulon/connection-caching

Conversation

@eaguad1337

Copy link
Copy Markdown
Contributor

Migrated from MasoniteFramework/orm#959 and MasoniteFramework/orm#961 (original author: @circulon) as part of the move to the masonitedev organization. Ported, evaluated, and hardened for 3.x.

Closes #2
Closes #3

What this does

Gives new_connection() / get_connection() consistent semantics in both Schema and QueryBuilder:

  • new_connection() is now a pure factory — it always creates and returns a fresh connection without storing it. This allows independent connections to be used alongside each other (e.g. running SET statements or DDL on a separate connection during migrations).
  • get_connection() owns the cached connection and is what all internal call sites now use.
  • Schema previously recreated its connection before every DDL call — a single migration with several schema.xxx calls opened a new backend connection each time, which can exhaust connection limits during long migration runs. It now reuses one connection.
  • Adds Schema.query_builder() returning a QueryBuilder bound to the same connection settings.

Fixes added on top of the original PRs

The original branches had three latent issues, addressed here in a follow-up commit:

  1. MSSQLPostProcessor.process_insert_get_id() still called builder.new_connection(). Under the new semantics that creates an independent connection, and SELECT @@Identity is connection-scoped — the last insert id would always come back NULL on MSSQL. It now uses builder.get_connection() (matching what SQLitePostProcessor already did).
  2. Schema.query_builder() referenced self.dry, which doesn't exist on Schema (the attribute is _dry) — calling it raised AttributeError.
  3. Replaced a leftover """Create""" docstring on Schema.get_connection().

Tests

  • New tests/schema/test_schema.py and tests/query/test_querybuilder.py covering: connection caching (same object returned), new_connection() independence (does not replace the cached connection), and Schema.query_builder().
  • Full suite: 1034 passed, 1 pre-existing failure (test_alter_drop_on_table_schema_table requires a live Postgres server — fails identically on 3.x).

circulon and others added 2 commits June 7, 2026 00:17
…QueryBuilder

Ported from MasoniteFramework/orm#959 and MasoniteFramework/orm#961.

- new_connection() is now a pure factory: it always creates and returns a
  fresh connection without storing it, allowing independent connections to
  be used alongside each other.
- get_connection() now owns the cached-connection lifecycle and is used by
  all internal call sites.
- Schema previously recreated its connection before every DDL call, which
  could exhaust backend connection limits during migrations.
- Adds Schema.query_builder() to get a QueryBuilder bound to the same
  connection settings, useful for arbitrary queries (DDL, SET) during
  migrations.
…ic change

- MSSQLPostProcessor read the last insert id via new_connection(); with
  new_connection() now returning an independent connection, @@IDENTITY
  (connection-scoped) would always be NULL. Use the builder's cached
  connection instead.
- Schema.query_builder() referenced self.dry which does not exist on
  Schema (the attribute is _dry).
- Clarify Schema.get_connection() docstring.
@eaguad1337 eaguad1337 merged commit a72ca99 into 3.x Jun 7, 2026
12 of 14 checks passed
@eaguad1337 eaguad1337 deleted the circulon/connection-caching branch June 7, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QueryBuilder should use get_connection() not new_connection() internally Schema creates a new connection object for every call

2 participants