Skip to content

fix(cli): let a second database on the same server join a runtime's integrated storage - #2623

Merged
aparajon merged 3 commits into
mainfrom
armand/second-database-joins-integrated-storage
Dec 4, 2026
Merged

aparajon merged 3 commits into
mainfrom
armand/second-database-joins-integrated-storage

Conversation

@aparajon

@aparajon aparajon commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

Why

Most developers run one local database server with a database per application. After init --integrated sets up the first application, a second one on the same server cannot be added, and none of the errors names a way through:

  • Integrated storage reaches the server's schemabot database through each application's own connection variable. The second application names the same state database through a different variable, so registration refuses it as "runtime storage differs; registration cannot replace durable state storage".
  • Giving it a runtime of its own fails because the schemabot database already exists, with advice to "choose standalone", a wizard choice that names no flag.
  • If the shell has not set the first application's connection variable, the second registration fails as "DSN must name a database", which names the first application, not the variable.

What

  • A second application database on the server that holds a runtime's integrated storage joins that runtime. Only an exact match of server address and database joins. The runtime keeps its existing storage reference.
  • A registration whose state would live elsewhere says where the runtime keeps its state, and names the flags that add the database to that runtime or give it a runtime and profile of its own.
  • When the server's schemabot database already holds another local runtime's state, init names that runtime and the --runtime flag that adds this database to it.
  • A connection read from an unset environment variable is refused naming the variable.
  • docs/cli.md and docs/init.md describe joining.

Before and after

prisma_app was set up with init --integrated --dsn env:PRISMA_DSN. drizzle_app is on the same server, at the same host and port.

Before                                  After

┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ init --integrated -d drizzle_app │    │ init --integrated -d drizzle_app │
│ --dsn env:DRIZZLE_DSN            │    │ --dsn env:DRIZZLE_DSN            │
└────────────────┬─────────────────┘    └────────────────┬─────────────────┘
                 │ register on                           │ register on
                 │ runtime "local"                       │ runtime "local"
                 ▼                                       ▼
┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ storage env:DRIZZLE_DSN ≠        │    │ both reach 127.0.0.1:5432/       │
│ storage env:PRISMA_DSN           │    │ schemabot: same state database   │
└────────────────┬─────────────────┘    └────────────────┬─────────────────┘
                 │                                       │
                 ▼                                       ▼
┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ "runtime storage differs;        │ ✗  │ drizzle_app registered; storage  │
│ registration cannot replace      │    │ stays env:PRISMA_DSN; both       │
│ durable state storage"           │    │ databases plan                   │
└──────────────────────────────────┘    └──────────────────────────────────┘
State database on another server: before and after

Before:

Error: runtime storage differs; registration cannot replace durable state storage

After:

Error: runtime "local" keeps its state in a schemabot database on another server, and a runtime keeps one state database. If drizzle_app is on that server, connect with the host and port env:PRISMA_DSN uses, and it joins the runtime. Otherwise give it a runtime of its own: add --runtime drizzle_app --profile drizzle_app
A new runtime on a server whose schemabot database another runtime uses: before and after

Before:

Error: the schemabot database already exists, possibly from an earlier setup attempt; if it is yours, choose standalone and connect to it to continue. Existing databases are never adopted automatically: database error 42P04

After:

Error: the schemabot database on this server already holds the state of runtime "local", and a runtime keeps one state database. Pass --runtime local to add this database to that runtime: database error 42P04
An unset connection variable: before and after

Before:

Error: database "prisma_app" environment "development": DSN must name a database

After:

Error: database "prisma_app" environment "development": environment variable PRISMA_DSN is not set

How

Registration still compares storage configuration first. Only when the configurations differ, and both are integrated storage, does it resolve both connections and compare the server address and database. A host alias or a tunnel may reach the same server under another name. That case is refused with the remedy rather than joined, since joining the wrong server would split an application's history across two state databases.

Risk

The change affects only local runtimes set up by init. Joining needs an exact address match, so the worst outcome of a mismatch is a refusal with a remedy.

  • AZ-7: upholds. A join keeps the runtime's existing storage reference, its other databases and its settings. Every other storage difference is still refused. The Enforced: line now names pkg/localsetup/integrated_join.go, where the join decision lives.

Upgrade notes

None.

🤖 Generated with Claude Code

…ntegrated storage

- A second application database on the server that already holds a
  runtime's integrated storage joins that runtime. Its own connection
  reference names the same schemabot database, so registration now
  accepts it when the server address and database match exactly. The
  runtime keeps the storage reference it was set up with.
- A registration whose state database is elsewhere is refused with where
  the runtime keeps its state and the flags for each way forward: add the
  database to that runtime, or give it a runtime and profile of its own.
- When integrated setup finds a schemabot database that another local
  runtime already keeps its state in, the refusal names that runtime and
  the --runtime flag that adds this database to it.
- A connection read from an unset environment variable is refused naming
  the variable, rather than as a connection that names no database.
- docs/cli.md and docs/init.md describe joining, and AZ-7's enforcement
  line names the new file.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 21:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Connection validation can accept off-server registrations through differing fallback endpoints and reject valid resolver-provided connections, compromising isolation and blocking integrated joins.

2 open issues
What changed in this PR

Allows another application database on the same server to join a local SchemaBot runtime’s integrated storage while retaining its existing storage reference. This extends the registration guard associated with AZ-7.

Changes:

  • Compare resolved storage addresses when integrated connection references differ.
  • Add actionable storage-conflict and unset-variable errors.
  • Cover joining with unit and integration tests, and document the workflow.

For two application databases using different connection variables on the same server:

Before: second init -> different references -> registration refused
After:  second init -> same storage address -> joins existing runtime
Unset connection variable: before
Error: database "prisma_app" environment "development": DSN must name a database
Unset connection variable: after
Error: database "prisma_app" environment "development": environment variable PRISMA_DSN is not set

Upgrade notes: None.

File Description
pkg/​localsetup/​register.go Allows matching integrated storage to join.
pkg/​localsetup/​register_test.go Covers joining, refusals, and runtime lookup.
pkg/​localsetup/​integrated_storage.go Names the runtime using existing state.
pkg/​localsetup/​integrated_storage_test.go Checks storage-creation guidance.
pkg/​localsetup/​integrated_join.go Adds address comparison and runtime discovery.
pkg/​localsetup/​check_connection_test.go Updates the error-helper call.
pkg/​cmd/​commands/​init.go Adds storage-conflict remedies.
pkg/​cmd/​commands/​init_test.go Checks actionable error wording.
pkg/​api/​storage_isolation.go Detects unset connection variables.
pkg/​api/​storage_isolation_test.go Covers missing-variable diagnostics.
integration/​localruntime/​init_second_database_test.go Exercises joining on MySQL and PostgreSQL.
docs/​invariants.md Updates AZ-7’s enforcement references.
docs/​init.md Explains integrated runtime joining.
docs/​cli.md Documents connection and runtime requirements.

Review effort: Balanced

Comment thread pkg/localsetup/integrated_join.go Resolved
Comment thread pkg/api/storage_isolation.go Resolved
…oining storage

- Two PostgreSQL integrated storages join only when every endpoint their
  connections may reach matches, fallback hosts included. A connection
  that shares the first host but falls back to another server is a
  different state database and is refused.
- Connection references are resolved through the registered secret
  resolvers, the way the runtime resolves them, before an unset env:
  variable is reported. A resolver registered for the env prefix keeps
  its precedence over the process environment.
@aparajon
aparajon marked this pull request as ready for review December 4, 2026 22:06
@JashLal

JashLal commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

🤖 I reviewed the full diff at d9801ec14685. The join preserves the runtime's original storage reference, and the MySQL and PostgreSQL integration cases cover setup and planning for both registrations.

Blocker — At pkg/localsetup/integrated_join.go:223, sorting fallback hosts makes two connections with the same primary and reversed fallbacks compare equal. If the primary is unavailable, pgx tries each connection's fallbacks in the supplied order, so each can pick a different server. At minimum, compare the ordered endpoints after deduplicating TLS retries and add a regression case for reversed fallbacks. To guarantee a same-server join across distinct endpoints, the guard would need to verify server identity or decline the join.

ValidateStorageIsolation and stateDatabaseAddress each resolve the storage secret twice before inspecting it. Reusing a single resolved value for the empty-value check and database-name rewrite would avoid extra remote resolver calls and ensure the value checked is the value parsed.

This was a local static review. I couldn't retrieve a Builderbot result for this head, and I did not run tests.

…al servers

- A PostgreSQL storage connection that lists more than one host reaches
  whichever answers first, so two such connections cannot be shown to
  share a state database, whatever order or hosts they list. A
  registration whose integrated storage names more than one server no
  longer joins; it is refused as storage that differs. Fallbacks that
  differ only in TLS settings name one server and still join.
- The storage connection is resolved once for both the unset-variable
  check and the database-name rewrite, so the value checked is the value
  parsed and a remote resolver is asked once. ResolveStorageDSN returns
  both.
@aparajon

aparajon commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 Thanks, both points are fixed in 454c53a.

  • Several servers (blocker): I took the stricter of your two options. pgx connects to the first host that answers, so even two connections that list the same hosts in the same order can reach different servers if availability changes between connections. A PostgreSQL storage connection that names more than one server now never joins, whatever order or hosts it lists, and the registration is refused as storage that differs. Fallbacks that differ only in TLS settings (for example sslmode=prefer) name one server and still join. TestRegisterNeverJoinsAPostgresConnectionNamingSeveralServers covers a different fallback, reversed fallbacks, identical fallbacks, and the TLS retry on one server. docs/init.md says a connection that lists more than one host never joins.
  • One resolution: ServerConfig.ResolveStorageDSN resolves the storage connection once and returns both the resolved value and the DSN with storage.database applied. ValidateStorageIsolation and stateDatabaseAddress use it, so the value checked for an unset variable is the value parsed, and a remote resolver is asked once. StorageDSN is built on it.

— Claude Code (Claude Opus 5.5)

@aparajon
aparajon added this pull request to the merge queue Dec 4, 2026
Merged via the queue into main with commit 6254e24 Dec 4, 2026
49 checks passed
@aparajon
aparajon deleted the armand/second-database-joins-integrated-storage branch December 4, 2026 22:58
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.

3 participants