Repository navigation
open_or_create_database: open the database when it exists, create it when it does not, in both packages - #480
Merged
Merged
Conversation
…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).
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.
What does this PR do?
Adds
arcadedb.open_or_create_database(path, jvm_kwargs=None, read_only=False)andDatabaseFactory.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_databaseandopen_databaseare unchanged. The asyncio facade (arcadedb.aio) has no create or open calls, it wraps an openDatabase, 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=Truenever creates. A path with no database raisesArcadeDBErrorwith the same message asopen_database.ArcadeDBError("Not a directory"), nothing changed.ArcadeDBError, no waiting. The same process cannot hold one path twice ("already in use").Related issues
Closes #474
Docs
how-to/databases-and-queries.adoc(new "Open or create" section, theif database_existssnippet replaced by the call, the snippet run twice on both builds), one sentence each inquickstart.adocandREADME.md. Thedocsbuild 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, inarcadedbandarcadedb-jvm. It replacesopen_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 themainwheels 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
uvx pre-commit run --files <changed files>passes