Skip to content

Open a database with configuration, and so read-only - #39

Closed
calvinchengx wants to merge 10 commits into
fpt:mainfrom
calvinchengx:duckdb-config
Closed

calvinchengx wants to merge 10 commits into
fpt:mainfrom
calvinchengx:duckdb-config

Conversation

@calvinchengx

Copy link
Copy Markdown
Contributor

duckdb_open takes no configuration, so there is currently no way to open a database read-only through this driver. A process whose only job is to read should not be able to write by accident, and asking the engine to enforce that is worth more than intending it.

Options arrive as a DSN query string and go through duckdb_open_ext:

sql.Open("duckdb", "warehouse.duckdb?access_mode=READ_ONLY&threads=2")

Three details worth a look:

  • A path with no ? takes the original duckdb_open path unchanged, so nothing existing changes behaviour.
  • The last ? separates, not the first. A DuckDB path may legitimately contain one, and taking the first would make such a file unopenable through database/sql with no way to say otherwise.
  • An unrecognised option fails the open with DuckDB's own message rather than being silently dropped. duckdb_open_ext reports through a caller-owned char*, freed with duckdb_free.

NewDuckDBWithSettings(path, settings) is the direct form; NewDuckDB(path) is unchanged and delegates to it.

Tests cover the DSN split (including the awkward paths), and there is one that writes to a database, reopens it read-only, reads successfully, and checks the write is refused — so the flag is proven to reach DuckDB rather than merely be passed to it.

duckdb_open takes no configuration, so there was no way to ask for a
read-only database. A process whose only job is to read should not be
able to write by accident, and asking the engine to enforce that is
worth more than intending it.

Options arrive as a DSN query string and go through duckdb_open_ext.
The last ? separates, so a path containing one is still openable, and a
path with no ? takes the original code path unchanged.
@calvinchengx

Copy link
Copy Markdown
Contributor Author

Already on main: this branch has 0 commits main does not have and an empty diff against it. Closing as landed, not as rejected.

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.

1 participant