Skip to content

Commit 82eff28

Browse files
authored
Implement UntenantedConnectionPool#size (#224)
so the mock object can be inspected for that config param. This allows, e.g. Solid Queue to boot up on a tenanted database.
2 parents 123f66e + 118c41b commit 82eff28

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## next / unreleased
44

5-
### Changed
5+
### Breaking change: Rake tasks
66

77
Some rake task changes to rename tasks for the database name (like Rails does it):
88

@@ -24,6 +24,11 @@ Some additional changes:
2424
- `ActiveRecord::Tenanted.base_configs` is a new utility method that returns all the tenanted base configs for the current environment.
2525

2626

27+
### Added
28+
29+
- `UntenantedConnectionPool#size` returns the database configuration's `max_connections` value, so that code (like Solid Queue) can inspect config params without a tenant context.
30+
31+
2732
## v0.5.0 / 2025-10-12
2833

2934
### Fixed

GUIDE.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
## Contents
1212

13-
<!-- regenerate TOC with `rake format:toc` -->
13+
<!-- regenerate TOC with `rake toc` -->
1414

1515
<!-- toc -->
1616

@@ -19,6 +19,7 @@
1919
* [1.2 High-level implementation](#12-high-level-implementation)
2020
* [1.3 Concepts](#13-concepts)
2121
* [1.4 Prior Art](#14-prior-art)
22+
* [1.5 Shared state (WIP)](#15-shared-state-wip)
2223
- [2. Application Configuration](#2-application-configuration)
2324
* [2.1 The Default Configuration](#21-the-default-configuration)
2425
* [2.2 Configuring the Database](#22-configuring-the-database)
@@ -127,6 +128,17 @@ In December 2020, [Rails 6.1 was released](https://guides.rubyonrails.org/6_1_re
127128
In early 2025, Julik Tarkhanov published a [tenanting implementation named "Shardine"](https://blog.julik.nl/2025/04/a-can-of-shardines) that uses the Rails sharding API. However, it also provided very limited integration with the rest of the Rails framework.
128129

129130

131+
### 1.5 Shared state (WIP)
132+
133+
- when we talk about "integration with the rest of rails" we're talking about Rails' assumptions about shared state
134+
- some of these assumptions are now busted
135+
- database ids are no longer unique
136+
- global ids are no longer global
137+
- cache is no longer global
138+
- cable channels are no longer global
139+
- jobs are no longer global
140+
141+
130142
## 2. Application Configuration
131143

132144
This gem offers an "omakase" configuration that specifies:
@@ -384,19 +396,10 @@ TODO:
384396

385397
Documentation outline:
386398

387-
- configuring either tenant-by-subdomain or a tenant-by-root-path-element
388-
- fragment cache disambiguation
389-
- global id disambiguation
390-
- invalid characters in a tenant name
399+
- invalid characters in a tenant name (which is database-dependent)
391400
- and how the application may want to do additional validation (e.g. ICANN subdomain restrictions)
392401
- `#tenant` is a readonly attribute on all tenanted model instances
393402
- `.current_tenant` returns the execution context for the model connection class
394-
- talk a bit about busted assumptions about shared state
395-
- database ids are no longer unique
396-
- global ids are no longer global
397-
- cache is no longer global
398-
- cable channels are no longer global
399-
- jobs are no longer global
400403
- and what we do in this gem to help manage that "current tenant" state
401404
- logging
402405
- SQL query logs

lib/active_record/tenanted/untenanted_connection_pool.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def schema_cache
2828
ActiveRecord::ConnectionAdapters::BoundSchemaReflection.new(schema_reflection, self)
2929
end
3030

31+
def size
32+
db_config.max_connections
33+
end
34+
3135
def lease_connection(...)
3236
raise Tenanted::NoTenantError, "Cannot connect to a tenanted database while untenanted (#{@model})."
3337
end

test/unit/untenanted_connection_pool_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
assert_equal("Cannot connect to a tenanted database while untenanted (User).", e.message)
2020
end
2121
end
22+
23+
test "size returns max_connections from db_config" do
24+
def config.max_connections; 42; end
25+
assert_equal 42, subject.size
26+
end
2227
end
2328
end

0 commit comments

Comments
 (0)