fix(sqlite): resolve TOML database paths instead of rewriting them - #412
Merged
Merged
Conversation
buildDSNFromSource encoded SQLite DSNs as `sqlite:///${database}`
unconditionally. SQLiteDSNParser reads the path positionally, so that
encoding only round-trips for Windows drive letters and `:memory:`:
/var/lib/x.db -> sqlite:////var/lib/x.db -> var/lib/x.db (absolute -> relative)
data/x.db -> sqlite:///data/x.db -> /data/x.db (relative -> absolute)
The relative case fails loudly with ERR_SQLITE_ERROR 14. The absolute case
fails silently: the server starts and serves queries against a different
file than the config names.
Resolve `database` at load time against the directory holding the config
file rather than process.cwd(), so a config selects the same database
regardless of where the server was launched from. Normalise separators to
forward slashes so the parser's `C:/` drive-letter branch matches, and
encode the DSN as `sqlite://` plus a leading-slash path so the value
survives the round-trip on both platforms.
This also repairs `~` expansion on Windows, which produced
`sqlite:///C:\Users\...` and decoded back to `/C:\Users\...`.
The existing DSN assertion encoded the old behaviour, so it is updated;
the new round-trip tests assert the path the connector actually opens
rather than the intermediate DSN string, which is what let this through.
Fixes bytebase#411
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes SQLite TOML database path handling by resolving paths at config-load time and encoding SQLite DSNs so SQLiteDSNParser round-trips the intended filesystem path (preventing silent absolute→relative rewrites and relative→absolute rewrites).
Changes:
- Added
resolveSqliteDatabasePath()to expand~, normalize separators, and resolve relative SQLitedatabasepaths against the TOML file’s directory. - Added
buildSqliteDSN()and updatedbuildDSNFromSource()to emit SQLite DSNs that preserve POSIX absolute paths and Windows drive-letter paths throughSQLiteDSNParser. - Expanded test coverage to assert actual parser round-trips (and updated existing expectations accordingly).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/config/toml-loader.ts | Adds SQLite path resolution + DSN encoding changes and wires them into TOML source processing / DSN building. |
| src/config/tests/toml-loader.test.ts | Adds/updates tests for config-dir-relative resolution, :memory:, ~ normalization expectations, and parser round-trip coverage. |
Suppressed comments (1)
src/config/toml-loader.ts:783
- The SQLite DSN
~expansion inprocessSourceConfigsis still incorrect:substring(11)strips the~soexpandHomeDir()won’t expand, andsqlite:///${...}can also reintroduce the 4-slash form that SQLiteDSNParser decodes as a relative path. Rebuild the DSN using the expanded path andbuildSqliteDSN()to preserve POSIX/Windows absolute paths.
processed.database = resolveSqliteDatabasePath(processed.database, configPath);
}
// Expand ~ in DSN for SQLite
if (processed.dsn && processed.dsn.startsWith("sqlite:///~")) {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Member
|
Thanks for the fix. |
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.
Fixes #411.
Problem
buildDSNFromSource()encoded SQLite DSNs by prefixing three slashes unconditionally:SQLiteDSNParserreads the path positionally, so this only round-trips for Windows drive letters and:memory:. Verified against 1.2.1 on Node v24.19.0:databaseC:/x/y.dbsqlite:///C:/x/y.dbC:/x/y.db/var/lib/y.dbsqlite:////var/lib/y.dbvar/lib/y.dbdata/y.dbsqlite:///data/y.db/data/y.db:memory:sqlite:///:memory::memory:The relative case fails loudly (
ERR_SQLITE_ERROR14). The absolute case fails silently — with a database at<cwd>/sub/a.dbanddatabase = "/sub/a.db", the server starts and answers queries against<cwd>/sub/a.db. A config naming an absolute path can therefore attach to a different file than it names.Windows
~expansion was broken the same way:expandHomeDirreturnsC:\Users\..., which yieldssqlite:///C:\Users\...; the drive-letter branch requiresC:/, so it fell through and decoded to/C:\Users\....Change
resolveSqliteDatabasePath()— resolvesdatabaseat load time. Relative paths resolve against the config file's directory rather thanprocess.cwd(), so a config selects the same database regardless of where the server was launched from. Separators are normalised to forward slashes.:memory:passes through.buildSqliteDSN()— encodes assqlite://+ a leading-slash path, which the parser decodes back unchanged for POSIX paths and de-prefixes correctly forC:/.I picked "resolve against the config file" over "reject relative paths with an error" — happy to switch if you'd rather not have implicit resolution.
Tests
SQLiteDSNParseractually returns (POSIX absolute, Windows drive letter, path with spaces,:memory:). This is the coverage that was missing — the previous test asserted the DSN string alone, so an encoding that rewrote absolute paths into relative ones passed review.loadTomlConfigtests: relative resolves against the config directory (config placed in a subdirectory so cwd and config dir differ), absolute is left alone,:memory:untouched.should build SQLite DSN from database pathassertion encoded the old four-slash output and is updated.~-expansion assertions now compare against forward slashes; they were platform-dependent and, on Windows, asserted a value that could not be opened.Worth noting: every SQLite source in
src/__fixtures__/toml/*.tomlusesdatabase = ":memory:", one of the two forms that round-tripped correctly — which is why CI stayed green.Verification
src/config/__tests__/toml-loader.test.ts: 156/156 pass (154 before, plus new coverage).mainand are environment-dependent —manager.test.ts > should resolve SSH config from ~/.ssh/configandsqlite.integration.test.ts > should open file-based database in readonly mode. Confirmed by stashing the change and re-running.tsc --noEmitreports 19 errors in the two touched files both before and after — delta zero. (tsc --noEmitis not clean onmain; the build runs through tsup.)