Skip to content

open_or_create_database: open the database when it exists, create it when it does not, in both packages - #480

Merged
tae898 merged 1 commit into
mainfrom
feat/474-open-or-create
Nov 24, 2026
Merged

tae898 merged 1 commit into
mainfrom
feat/474-open-or-create

Conversation

@tae898

@tae898 tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds arcadedb.open_or_create_database(path, jvm_kwargs=None, read_only=False) and DatabaseFactory.open_or_create(read_only=False), in both packages: one call that opens the database when it exists and creates it when it does not, so a script that keeps its data in one database runs the same way the first time and every time after. create_database and open_database are unchanged. The asyncio facade (arcadedb.aio) has no create or open calls, it wraps an open Database, so it gets none.

What it does outside the plain first and later run (each case run on both wheels, documented in the docstring and on the "Databases and queries" page, pinned by a test):

  • read_only=True never creates. A path with no database raises ArcadeDBError with the same message as open_database.
  • The path is a directory that holds no database: a database is created inside it and its other files stay. The path is a file: ArcadeDBError ("Not a directory"), nothing changed.
  • Another process holds the database open: the engine's "locked by another process" ArcadeDBError, no waiting. The same process cannot hold one path twice ("already in use").
  • Two processes on the same new path at the same moment: one creates it, the other's create fails, it sees the database and opens it. It never fails with "already exists" or "does not exist", only with the lock error while the first process still has it open.

Related issues

Closes #474

Docs

how-to/databases-and-queries.adoc (new "Open or create" section, the if database_exists snippet replaced by the call, the snippet run twice on both builds), one sentence each in quickstart.adoc and README.md. The docs build runs locally without a warning.

Milestone

26.12.1: small and additive, and I checked both builds locally. It stays there if both builds are green in CI here, otherwise it moves to 27.1.1.

Release note draft

Added arcadedb.open_or_create_database(path): opens the database when it exists and creates it when it does not, in arcadedb and arcadedb-jvm. It replaces open_database(path) if database_exists(path) else create_database(path) and is safe when two processes start on the same new path.

Tests

tests/test_open_or_create.py, 13 tests, run by both CI builds: first and second run, the existing calls keep their errors, read_only, a file path, a directory with files, the same process twice, a holder process that locks the database, and two processes racing on a new path (3 rounds). Local run on the main wheels of 2026-11-24 with this branch's Python files laid over them (laptop): 13 passed on each build, and 30 racing rounds on each build all ended as one process OK and the other OK or the lock error.

Checklist

  • A regression test fails on the old code and passes on the new
  • The test suite that the change can affect passes
  • uvx pre-commit run --files <changed files> passes

…when it does not, in both packages

A script that keeps its data in one database could not run twice: create_database raises "already exists" on the second run and
open_database raises "does not exist" on the first. The code that works in both runs, open_database(path) if
database_exists(path) else create_database(path), is what an agent has to find first, and it races when two processes start together.

arcadedb.open_or_create_database(path, jvm_kwargs=None, read_only=False) and DatabaseFactory.open_or_create(read_only=False) do it in one
call. The routine is shared by DatabaseFactory (JVM build) and NativeDatabaseFactory (native build) in _open_or_create.py. A create that
fails because another process created the database first falls back to opening it. read_only=True never creates. The existing calls are
unchanged. The asyncio facade has no create or open calls (it wraps an open Database), so it gets none.

Behavior checked on both wheels before writing the docs: a path that is a file raises "Not a directory" and changes nothing; a
directory that holds other files gets a database inside it and the files stay; a database held by another process raises the engine's
"locked by another process" error and the call does not wait; the same process cannot hold a path twice ("already in use").

Tests (tests/test_open_or_create.py, run by both CI builds): first and second run, the existing calls keep their errors, read_only,
file path, directory with files, same process twice, locked by a holder process, and two processes racing on a new path (3 rounds;
30 of 30 rounds on each build in a local stress run ended with OK for one process and OK or the lock error for the other).
@tae898 tae898 added this to the 26.12.1 milestone Nov 24, 2026
@tae898 tae898 added the enhancement New feature or request label Nov 24, 2026
@tae898 tae898 self-assigned this Nov 24, 2026
@tae898
tae898 merged commit cb7d4da into main Nov 24, 2026
25 checks passed
@tae898
tae898 deleted the feat/474-open-or-create branch November 24, 2026 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No open-or-create call: create_database fails on the second run and open_database on the first

1 participant