feat(storage,app): PostgreSQL backend (D3) + project manager window (D4)
D3: oakdb+pg:// fully wired (shared sea-orm entities, BIGSERIAL DDL, connect-probe instead of pool retry on dead servers); Storage/Backend=pg + Storage/PgUrl config; 13 OAK_TEST_PG_URL-gated PG tests (verified against a Docker postgres:16), always-on clean-error tests otherwise. D4: DaVinci-style project manager — list with derived stats, create/ rename/duplicate/delete (confirm)/import/export (native dialogs, ove/otio/fcpxml), shown at startup and from the file menu; facade oakengine_library_* exports (list/create/delete/rename/duplicate/ import/export + project_load_library that binds write-through); save/save-as menu becomes 'export project file', open splits into from-library/from-file; status bar shows library write state; storage activates on app start and flushes on exit; spawn_modal reentrancy fixed (window-callback path) with a doc note. Also: the P1 audio test's environment probe was lost in the ffi purge; restored on cpal (the output device is cpal now).
This commit is contained in:
@@ -454,10 +454,43 @@ mod tests {
|
||||
fn output_callback_consumes_pushed_samples() {
|
||||
// Skip when the audio system cannot actually run a stream: open a
|
||||
// silent stream and require at least one callback within 2 s. A
|
||||
// device existing is not enough — headless sessions report
|
||||
// is_active=true while delivering zero callbacks.
|
||||
// device existing is not enough — headless sessions report the
|
||||
// stream running while delivering zero callbacks.
|
||||
use std::sync::atomic::AtomicI64 as A;
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
static PROBE: A = A::new(0);
|
||||
PROBE.store(0, Ordering::Relaxed);
|
||||
let can_play = (|| {
|
||||
let host = cpal::default_host();
|
||||
let device = host.default_output_device()?;
|
||||
let config = device.default_output_config().ok()?;
|
||||
let cb = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
|
||||
PROBE.fetch_add(data.len() as i64, Ordering::Relaxed);
|
||||
for s in data.iter_mut() {
|
||||
*s = 0.0;
|
||||
}
|
||||
};
|
||||
let stream = device
|
||||
.build_output_stream(
|
||||
config.config(),
|
||||
cb,
|
||||
|_| {},
|
||||
None,
|
||||
)
|
||||
.ok()?;
|
||||
stream.play().ok()?;
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
|
||||
while std::time::Instant::now() < deadline && PROBE.load(Ordering::Relaxed) == 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
let got = PROBE.load(Ordering::Relaxed) > 0;
|
||||
drop(stream);
|
||||
Some(got)
|
||||
})();
|
||||
if can_play != Some(true) {
|
||||
eprintln!("audio session cannot deliver callbacks; skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
DESTROYED.store(false, Ordering::SeqCst);
|
||||
let manager = MANAGER.get_or_init(|| Mutex::new(ManagerInner::default()));
|
||||
|
||||
@@ -74,6 +74,7 @@ pub mod handle;
|
||||
pub mod ipc;
|
||||
#[cfg(not(test))]
|
||||
pub mod linkage;
|
||||
pub mod library;
|
||||
pub mod node;
|
||||
pub mod plugin;
|
||||
pub mod pods;
|
||||
|
||||
@@ -0,0 +1,368 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! The project-library manager C ABI (plan M13 §4): list / create / open /
|
||||
//! rename / duplicate / delete / import / export over the oakstorage
|
||||
//! database backend the write-through binds to ([`crate::storage`]).
|
||||
//!
|
||||
//! These exports are additive (D4): the app talks to the engine dylib only
|
||||
//! through the frozen `oakengine_*` surface, so the manager's data source
|
||||
//! crosses the boundary here instead of linking oakstorage directly (which
|
||||
//! would give the app a second copy of the handle/serializer types).
|
||||
//!
|
||||
//! All operations address the configured default library (the same
|
||||
//! `Storage/Backend` + `Storage/SqlitePath` configuration the write-through
|
||||
//! uses); with storage disabled every call fails with `OAKENGINE_E_STATE`
|
||||
//! except [`oakengine_library_list`], which reports an empty library
|
||||
//! (`"[]"`) so the manager window can still open.
|
||||
|
||||
use std::ffi::{c_char, c_int};
|
||||
|
||||
use oakstorage::backend::StorageBackend;
|
||||
use oakstorage::uri::StorageUri;
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::handle::{guard, guard_int, read_cstr, write_string, OakEngineProject};
|
||||
use crate::stubs::node as n;
|
||||
|
||||
/// One library row as the project manager shows it: the project metadata
|
||||
/// plus the stats derived from the head state (plan §4).
|
||||
#[derive(serde::Serialize)]
|
||||
struct LibraryRow {
|
||||
/// Library row uuid (the open/duplicate/export selector).
|
||||
uuid: String,
|
||||
/// Display name.
|
||||
name: String,
|
||||
/// Row creation time (unix seconds, UTC).
|
||||
created_at: i64,
|
||||
/// Last-write time (unix seconds, UTC; the manager sort key).
|
||||
modified_at: i64,
|
||||
/// Longest sequence duration, milliseconds.
|
||||
duration_ms: i64,
|
||||
/// Total tracks across all sequences.
|
||||
track_count: i32,
|
||||
/// Total clip blocks.
|
||||
clip_count: i32,
|
||||
/// Total footage nodes.
|
||||
footage_count: i32,
|
||||
}
|
||||
|
||||
/// Map an oakstorage error onto the facade error space (the context string
|
||||
/// is log-only per the error contract).
|
||||
fn map_err(e: oakstorage::error::Error) -> Error {
|
||||
use oakstorage::error::Error as E;
|
||||
match e {
|
||||
E::Invalid => Error::Invalid,
|
||||
E::State => Error::State,
|
||||
E::NotFound => Error::NotFound,
|
||||
E::NoMem => Error::NoMem,
|
||||
other => Error::Failed(other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The configured default library as a parsed URI; [`Error::State`] when
|
||||
/// the write-through backend is disabled or the path does not resolve.
|
||||
fn library() -> Result<StorageUri> {
|
||||
if !crate::storage::storage_enabled() {
|
||||
return Err(Error::State);
|
||||
}
|
||||
let uri = crate::storage::library_uri().ok_or(Error::State)?;
|
||||
StorageUri::parse(&uri).map_err(map_err)
|
||||
}
|
||||
|
||||
/// The library URI selecting one row (`…?project=<uuid>`).
|
||||
fn project_uri(uuid: &str) -> Result<StorageUri> {
|
||||
let uri = library()?;
|
||||
StorageUri::parse(&format!("{}?project={uuid}", uri.to_uri_string())).map_err(map_err)
|
||||
}
|
||||
|
||||
/// Load one library row as an owned project handle (refcount 1).
|
||||
fn load_handle(uuid: &str) -> Result<crate::handle::CHandle> {
|
||||
let uri = project_uri(uuid)?;
|
||||
let result = crate::storage::backend().load(&uri).map_err(map_err)?;
|
||||
if result.project.is_null() {
|
||||
return Err(Error::Failed(format!(
|
||||
"library load of {uuid} returned no project (info code {})",
|
||||
result.version_info
|
||||
)));
|
||||
}
|
||||
Ok(result.project)
|
||||
}
|
||||
|
||||
/// Release an owned handle (refcount 1).
|
||||
fn release(h: crate::handle::CHandle) {
|
||||
if let Some(release) = h.release {
|
||||
unsafe { release(h.ctx) };
|
||||
}
|
||||
}
|
||||
|
||||
/// `oakengine_library_list` — the library rows as a JSON array (buf/size
|
||||
/// convention), most recently modified first. Each row carries the manager
|
||||
/// stats derived from the head state; a row whose stats fail to replay
|
||||
/// degrades to zeros instead of failing the whole list. With storage
|
||||
/// disabled the result is the empty array (`"[]"`), not an error.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_list(buf: *mut c_char, buf_size: c_int) -> c_int {
|
||||
guard_int(|| unsafe {
|
||||
if !crate::storage::storage_enabled() {
|
||||
return Ok(write_string("[]", buf, buf_size));
|
||||
}
|
||||
let uri = library()?;
|
||||
let infos = crate::storage::backend()
|
||||
.list_projects(&uri)
|
||||
.map_err(map_err)?;
|
||||
let mut rows = Vec::with_capacity(infos.len());
|
||||
for info in infos {
|
||||
let stats = crate::storage::backend()
|
||||
.project_stats(&uri, &info.uuid)
|
||||
.unwrap_or_default();
|
||||
rows.push(LibraryRow {
|
||||
uuid: info.uuid,
|
||||
name: info.name,
|
||||
created_at: info.created_at.and_utc().timestamp(),
|
||||
modified_at: info.modified_at.and_utc().timestamp(),
|
||||
duration_ms: stats.duration_ms,
|
||||
track_count: stats.track_count,
|
||||
clip_count: stats.clip_count,
|
||||
footage_count: stats.footage_count,
|
||||
});
|
||||
}
|
||||
let json = serde_json::to_string(&rows)
|
||||
.map_err(|e| Error::Failed(format!("library list encode: {e}")))?;
|
||||
Ok(write_string(&json, buf, buf_size))
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_create` — create a blank project named `name` as a
|
||||
/// new library row and report its uuid (buf/size convention on
|
||||
/// `out_uuid`; the return value is the uuid length, negative on error).
|
||||
/// The row lands immediately (one `kind='import'` command), so the
|
||||
/// manager list shows it before the first edit.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_create(
|
||||
name: *const c_char,
|
||||
out_uuid: *mut c_char,
|
||||
out_size: c_int,
|
||||
) -> c_int {
|
||||
guard_int(|| unsafe {
|
||||
if name.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let name = read_cstr(name);
|
||||
if name.trim().is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let uri = library()?;
|
||||
|
||||
let mut h = n::oaknode_project_init();
|
||||
if h.is_null() {
|
||||
return Err(Error::NoMem);
|
||||
}
|
||||
let outcome = (|| -> Result<String> {
|
||||
Error::from_module(n::oaknode_project_initialize(h))?;
|
||||
let uuid = {
|
||||
let arc = crate::handle::domain::project_of(&h).ok_or(Error::Invalid)?;
|
||||
let mut guard = arc.lock().unwrap_or_else(|e| e.into_inner());
|
||||
guard.settings.insert("projectname".to_string(), name);
|
||||
guard.uuid.clone()
|
||||
};
|
||||
crate::storage::backend().save(h, &uri, 0).map_err(map_err)?;
|
||||
Ok(uuid)
|
||||
})();
|
||||
n::oaknode_project_free(&mut h);
|
||||
let uuid = outcome?;
|
||||
Ok(write_string(&uuid, out_uuid, out_size))
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_delete` — delete the library row `uuid` (cascades
|
||||
/// settings / snapshots / journal; `OAKENGINE_E_NOT_FOUND` when absent).
|
||||
/// The manager confirms with the user before calling.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_delete(uuid: *const c_char) -> c_int {
|
||||
guard(|| unsafe {
|
||||
if uuid.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let uuid = read_cstr(uuid);
|
||||
if uuid.is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
crate::storage::backend()
|
||||
.delete_project(&library()?, &uuid)
|
||||
.map_err(map_err)
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_rename` — rename the library row `uuid` (the
|
||||
/// manager's list name; the in-project `projectname` setting is
|
||||
/// untouched).
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_rename(
|
||||
uuid: *const c_char,
|
||||
name: *const c_char,
|
||||
) -> c_int {
|
||||
guard(|| unsafe {
|
||||
if uuid.is_null() || name.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let (uuid, name) = (read_cstr(uuid), read_cstr(name));
|
||||
if uuid.is_empty() || name.trim().is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
crate::storage::backend()
|
||||
.rename_project(&library()?, &uuid, name.trim())
|
||||
.map_err(map_err)
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_duplicate` — copy the library row `uuid` (settings,
|
||||
/// snapshots and the full journal history included) under a fresh uuid,
|
||||
/// reporting the new row's uuid (buf/size convention on `out_uuid`; the
|
||||
/// return value is the uuid length, negative on error).
|
||||
/// `name` is the copy's display name; NULL/empty defaults to
|
||||
/// `<name> (copy)`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_duplicate(
|
||||
uuid: *const c_char,
|
||||
name: *const c_char,
|
||||
out_uuid: *mut c_char,
|
||||
out_size: c_int,
|
||||
) -> c_int {
|
||||
guard_int(|| unsafe {
|
||||
if uuid.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let uuid = read_cstr(uuid);
|
||||
if uuid.is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let name = read_cstr(name);
|
||||
let name = match name.trim() {
|
||||
"" => None,
|
||||
trimmed => Some(trimmed),
|
||||
};
|
||||
let info = crate::storage::backend()
|
||||
.duplicate_project(&library()?, &uuid, name)
|
||||
.map_err(map_err)?;
|
||||
Ok(write_string(&info.uuid, out_uuid, out_size))
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_import` — import a `.ove` / `.otio` / `.fcpxml`
|
||||
/// project file as a new library row (the file backend parses it, a fresh
|
||||
/// uuid is assigned, and the first save journals the whole project as one
|
||||
/// `kind='import'` command). Reports the new row's uuid (buf/size
|
||||
/// convention on `out_uuid`; the return value is the uuid length,
|
||||
/// negative on error).
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_import(
|
||||
path: *const c_char,
|
||||
out_uuid: *mut c_char,
|
||||
out_size: c_int,
|
||||
) -> c_int {
|
||||
guard_int(|| unsafe {
|
||||
if path.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let path = read_cstr(path);
|
||||
if path.is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let file_uri = StorageUri::parse(&path).map_err(map_err)?;
|
||||
let uuid = crate::storage::backend()
|
||||
.import_from_file(&library()?, &file_uri)
|
||||
.map_err(map_err)?;
|
||||
Ok(write_string(&uuid, out_uuid, out_size))
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_library_export` — export the library row `uuid` to the file
|
||||
/// `path`; the format is dispatched by extension through the oakstorage
|
||||
/// registry (`.ove` / `.ovexml` → ove-xml, `.otio` / `.fcpxml` → the
|
||||
/// interchange backend). Nothing is written back to the library.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_library_export(
|
||||
uuid: *const c_char,
|
||||
path: *const c_char,
|
||||
) -> c_int {
|
||||
guard(|| unsafe {
|
||||
if uuid.is_null() || path.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let (uuid, path) = (read_cstr(uuid), read_cstr(path));
|
||||
if uuid.is_empty() || path.is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let file_uri = StorageUri::parse(&path).map_err(map_err)?;
|
||||
if file_uri.scheme != "file" {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let handle = load_handle(&uuid)?;
|
||||
let backend = oakstorage::registry::Registry::global()
|
||||
.resolve(&file_uri)
|
||||
.map_err(map_err)?;
|
||||
let result = backend.save(handle, &file_uri, 0).map_err(map_err);
|
||||
release(handle);
|
||||
result
|
||||
})
|
||||
}
|
||||
|
||||
/// `oakengine_project_load_library` — load the library row `uuid` into a
|
||||
/// fresh project shell (same contract as `oakengine_project_load`: the
|
||||
/// shell must carry no content). On success the undo stack is cleared, the
|
||||
/// modified flag is reset, and the project is bound to the library session
|
||||
/// (the write-through continues the row's journal from its head seq).
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn oakengine_project_load_library(
|
||||
self_: *mut OakEngineProject,
|
||||
uuid: *const c_char,
|
||||
err: *mut c_char,
|
||||
err_size: c_int,
|
||||
) -> c_int {
|
||||
guard(|| unsafe {
|
||||
if self_.is_null() || uuid.is_null() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let h = crate::handle::unbox(self_)?;
|
||||
if !n::oaknode_project_root(h).is_null() {
|
||||
return Err(Error::State);
|
||||
}
|
||||
let uuid = read_cstr(uuid);
|
||||
if uuid.is_empty() {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
let loaded = match load_handle(&uuid) {
|
||||
Ok(handle) => handle,
|
||||
Err(e) => {
|
||||
write_string(&e.to_string(), err, err_size);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
// Swap the loaded content into the caller's shell box, releasing
|
||||
// the empty shell handle the box was created with.
|
||||
let mut old = (*self_).handle;
|
||||
(*self_).handle = loaded;
|
||||
n::oaknode_project_free(&mut old);
|
||||
crate::undo::oakengine_undo_clear();
|
||||
Error::from_module(n::oaknode_project_set_modified(loaded, 0))?;
|
||||
crate::storage::bind_project(loaded);
|
||||
if !err.is_null() && err_size > 0 {
|
||||
*err = 0;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
@@ -41,9 +41,9 @@
|
||||
//! The library is selected from the `Storage` config group (all defaults
|
||||
//! are config-driven, plan §5):
|
||||
//!
|
||||
//! - `Storage/Backend` — `"sqlite"` (the documented default value) or
|
||||
//! `"database"` enable the write-through; any other value (e.g.
|
||||
//! `"off"`) disables it. When the key is absent, no library is
|
||||
//! - `Storage/Backend` — `"sqlite"` (the documented default value),
|
||||
//! `"database"` or `"pg"` enable the write-through; any other value
|
||||
//! (e.g. `"off"`) disables it. When the key is absent, no library is
|
||||
//! configured: projects stay unbound and the undo path runs without
|
||||
//! touching a database (graceful degradation — this is what keeps
|
||||
//! headless consumers and the test suite from writing to the user's
|
||||
@@ -52,6 +52,12 @@
|
||||
//! storage is enabled) `<system data directory>/library.db` (the same
|
||||
//! location `FileFunctions::get_configuration_location` derives,
|
||||
//! honoring `OAK_CONFIG_DIR` and portable mode).
|
||||
//! - `Storage/PgUrl` — the PostgreSQL connection string (plan D3), used
|
||||
//! when `Storage/Backend` is `"pg"`: `user:pass@host:5432/dbname`
|
||||
//! (libpq URL form; an optional `postgres://`/`postgresql://` scheme is
|
||||
//! accepted and stripped). The resolved library URI is
|
||||
//! `oakdb+pg://<PgUrl>`. When `Backend = "pg"` but `PgUrl` is absent
|
||||
//! or empty, no library is configured (same graceful degradation).
|
||||
//!
|
||||
//! ## Snapshot thread and exit flush
|
||||
//!
|
||||
@@ -101,9 +107,9 @@ struct Binding {
|
||||
}
|
||||
|
||||
/// The process-wide oakstorage backend (shared by the UI thread's
|
||||
/// write-throughs and the snapshot thread; the backend serializes its own
|
||||
/// operations).
|
||||
fn backend() -> &'static DatabaseBackend {
|
||||
/// write-throughs, the snapshot thread and the library manager exports in
|
||||
/// [`crate::library`]; the backend serializes its own operations).
|
||||
pub(crate) fn backend() -> &'static DatabaseBackend {
|
||||
static BACKEND: OnceLock<DatabaseBackend> = OnceLock::new();
|
||||
BACKEND.get_or_init(DatabaseBackend::new)
|
||||
}
|
||||
@@ -291,18 +297,18 @@ fn record_error(key: usize, message: &str) {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Whether the write-through backend is enabled (config-driven, plan §5):
|
||||
/// `Storage/Backend` set to `"sqlite"` or `"database"` enables it; any
|
||||
/// other explicit value (e.g. `"off"`) disables it. When NO `Storage`
|
||||
/// configuration is present the backend is NOT enabled — "no library
|
||||
/// configured" degrades gracefully to plain unbound projects, which keeps
|
||||
/// headless consumers (the CLI) and the test suite from ever writing to
|
||||
/// the user's default library. The documented default *values* are
|
||||
/// `Backend = "sqlite"` and `SqlitePath = <system data dir>/library.db`
|
||||
/// `Storage/Backend` set to `"sqlite"`, `"database"` or `"pg"` enables
|
||||
/// it; any other explicit value (e.g. `"off"`) disables it. When NO
|
||||
/// `Storage` configuration is present the backend is NOT enabled — "no
|
||||
/// library configured" degrades gracefully to plain unbound projects,
|
||||
/// which keeps headless consumers (the CLI) and the test suite from ever
|
||||
/// writing to the user's default library. The documented default *values*
|
||||
/// are `Backend = "sqlite"` and `SqlitePath = <system data dir>/library.db`
|
||||
/// (used once storage is enabled without an explicit path).
|
||||
pub(crate) fn storage_enabled() -> bool {
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
match store.get(Some("Storage"), "Backend") {
|
||||
Ok(b) => b == "sqlite" || b == "database",
|
||||
Ok(b) => b == "sqlite" || b == "database" || b == "pg",
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
@@ -316,6 +322,29 @@ fn configured_sqlite_path() -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The configured PostgreSQL connection string (`Storage/PgUrl`; empty
|
||||
/// value = not configured).
|
||||
fn configured_pg_url() -> Option<String> {
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
match store.get(Some("Storage"), "PgUrl") {
|
||||
Ok(u) if !u.trim().is_empty() => Some(u.trim().to_string()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The `oakdb+pg://…` uri of the configured PostgreSQL library (None
|
||||
/// when `Storage/PgUrl` is absent). A `postgres://`/`postgresql://`
|
||||
/// scheme on the config value is stripped — the oakdb uri body is the
|
||||
/// bare connection string (`user:pass@host:5432/dbname`).
|
||||
fn pg_library_uri() -> Option<String> {
|
||||
let url = configured_pg_url()?;
|
||||
let body = url
|
||||
.strip_prefix("postgres://")
|
||||
.or_else(|| url.strip_prefix("postgresql://"))
|
||||
.unwrap_or(&url);
|
||||
Some(format!("oakdb+pg://{body}"))
|
||||
}
|
||||
|
||||
/// The default library file: `<system data directory>/library.db`, where
|
||||
/// the data directory is the standard per-user location
|
||||
/// (`FileFunctions::get_configuration_location`: macOS Application
|
||||
@@ -327,9 +356,14 @@ pub(crate) fn default_library_path() -> String {
|
||||
format!("{}/library.db", dir)
|
||||
}
|
||||
|
||||
/// The `oakdb+sqlite://…` uri of the configured library (None when the
|
||||
/// path cannot be made absolute).
|
||||
fn library_uri() -> Option<String> {
|
||||
/// The `oakdb+…` uri of the configured library (None when the path
|
||||
/// cannot be made absolute, or the PG url is missing). Shared with the
|
||||
/// library manager exports in [`crate::library`].
|
||||
pub(crate) fn library_uri() -> Option<String> {
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
if store.get(Some("Storage"), "Backend").ok().as_deref() == Some("pg") {
|
||||
return pg_library_uri();
|
||||
}
|
||||
let path = match configured_sqlite_path() {
|
||||
Some(p) => p,
|
||||
None => default_library_path(),
|
||||
@@ -580,4 +614,47 @@ mod tests {
|
||||
assert!(!storage_enabled());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn library_uri_resolves_pg_config() {
|
||||
use oakcommon::configstore::ConfigStore;
|
||||
let store = ConfigStore::instance();
|
||||
let _g = crate::tests::common::STORAGE_CONFIG_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
|
||||
// Backend = "pg" yields an oakdb+pg uri from Storage/PgUrl; a
|
||||
// postgres:// scheme on the config value is stripped.
|
||||
store.set(Some("Storage"), "Backend", "pg");
|
||||
store.set(
|
||||
Some("Storage"),
|
||||
"PgUrl",
|
||||
"postgres://user:pass@host:5432/oak",
|
||||
);
|
||||
assert!(storage_enabled());
|
||||
assert_eq!(
|
||||
library_uri().as_deref(),
|
||||
Some("oakdb+pg://user:pass@host:5432/oak")
|
||||
);
|
||||
|
||||
// postgresql:// is accepted too.
|
||||
store.set(
|
||||
Some("Storage"),
|
||||
"PgUrl",
|
||||
"postgresql://u@h/db?sslmode=disable",
|
||||
);
|
||||
assert_eq!(
|
||||
library_uri().as_deref(),
|
||||
Some("oakdb+pg://u@h/db?sslmode=disable")
|
||||
);
|
||||
|
||||
// Backend = "pg" with no PgUrl = no library (graceful
|
||||
// degradation, same as an absent sqlite path).
|
||||
store.set(Some("Storage"), "PgUrl", "");
|
||||
assert_eq!(library_uri(), None);
|
||||
|
||||
// Leave the store in a safe state.
|
||||
store.set(Some("Storage"), "Backend", "off");
|
||||
assert!(!storage_enabled());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,426 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! D4 integration tests: the project-library manager C ABI
|
||||
//! (`src/library.rs`, plan M13 §4).
|
||||
//!
|
||||
//! End-to-end against real SQLite library files in temp directories,
|
||||
//! driving the facade exactly like the app's project manager: create lands
|
||||
//! a row immediately, list reports it with the derived stats, open
|
||||
//! (`oakengine_project_load_library`) binds the loaded project to the
|
||||
//! library session (the next undoable edit write-throughs onto the row's
|
||||
//! journal), rename / duplicate / delete / import / export round-trip, and
|
||||
//! the disabled-backend configuration degrades to an empty list + error
|
||||
//! codes.
|
||||
//!
|
||||
//! Every test holds the shared undo-stack lock (the facade's stack is
|
||||
//! process-wide, same as the it_undo / it_storage families) and the
|
||||
//! storage-config lock, so the suite never races on either singleton.
|
||||
|
||||
use std::ffi::CString;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use super::common;
|
||||
use super::it_undo::GLOBAL_STACK_LOCK;
|
||||
|
||||
use crate::error::{OAKENGINE_OK, OAKENGINE_E_INVALID, OAKENGINE_E_NOT_FOUND, OAKENGINE_E_STATE};
|
||||
use crate::handle::OakEngineProject;
|
||||
|
||||
/// The math node type id (a factory type the tests add as an undoable
|
||||
/// edit; footage is not factory-creatable, so the write-through is
|
||||
/// verified on the journal rows directly).
|
||||
const MATH: &str = "org.olivevideoeditor.Olive.math";
|
||||
|
||||
/// The journal row count of a library row (direct sea-orm read, the same
|
||||
/// pattern as the it_storage tests).
|
||||
fn journal_rows(db: &Path, uuid: &str) -> usize {
|
||||
use sea_orm::entity::prelude::*;
|
||||
use oakstorage::backends::database::entities::{journal, project};
|
||||
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
runtime.block_on(async {
|
||||
let conn = sea_orm::Database::connect(format!("sqlite://{}?mode=ro", db.display()))
|
||||
.await
|
||||
.unwrap();
|
||||
let model = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(uuid))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.expect("the row exists");
|
||||
journal::Entity::find()
|
||||
.filter(journal::Column::ProjectId.eq(model.id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.len()
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Serialize a library test: hold the process-global undo-stack lock AND
|
||||
/// the storage-config lock for the whole body, then point the library at a
|
||||
/// temp SQLite file.
|
||||
fn with_library<R>(db: &Path, f: impl FnOnce() -> R) -> R {
|
||||
let _stack = GLOBAL_STACK_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let _config = common::STORAGE_CONFIG_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
store.set(Some("Storage"), "Backend", "sqlite");
|
||||
store.set(Some("Storage"), "SqlitePath", &db.to_string_lossy());
|
||||
f()
|
||||
}
|
||||
|
||||
/// A fresh, unique temp directory for one test.
|
||||
fn temp_dir(tag: &str) -> PathBuf {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("oakengine_library_{}_{}", std::process::id(), tag));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// Two-stage string read over a facade `(buf, size)` getter.
|
||||
fn read_string(f: impl Fn(*mut std::ffi::c_char, i32) -> i32) -> String {
|
||||
let needed = f(std::ptr::null_mut(), 0);
|
||||
if needed <= 0 {
|
||||
return String::new();
|
||||
}
|
||||
let mut buf = vec![0 as std::ffi::c_char; needed as usize + 1];
|
||||
f(buf.as_mut_ptr(), needed + 1);
|
||||
let len = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
|
||||
String::from_utf8_lossy(unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, len) })
|
||||
.into_owned()
|
||||
}
|
||||
|
||||
/// The library list JSON.
|
||||
fn list_json() -> String {
|
||||
read_string(|buf, size| unsafe { crate::library::oakengine_library_list(buf, size) })
|
||||
}
|
||||
|
||||
/// Create a library row; returns its uuid. The create export has a side
|
||||
/// effect, so it is called ONCE with a stack buffer (never two-stage).
|
||||
fn create(name: &str) -> String {
|
||||
let name = CString::new(name).unwrap();
|
||||
let mut buf = [0 as std::ffi::c_char; 256];
|
||||
let rc = unsafe { crate::library::oakengine_library_create(name.as_ptr(), buf.as_mut_ptr(), 256) };
|
||||
assert!(rc > 0, "create {name:?} rc={rc}");
|
||||
let len = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
|
||||
String::from_utf8_lossy(unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, len) })
|
||||
.into_owned()
|
||||
}
|
||||
|
||||
/// Duplicate a library row (single call with a stack buffer, see
|
||||
/// [`create`]).
|
||||
fn duplicate(uuid: &str) -> String {
|
||||
let uuid = CString::new(uuid).unwrap();
|
||||
let mut buf = [0 as std::ffi::c_char; 256];
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_duplicate(
|
||||
uuid.as_ptr(),
|
||||
std::ptr::null(),
|
||||
buf.as_mut_ptr(),
|
||||
256,
|
||||
)
|
||||
};
|
||||
assert!(rc > 0, "duplicate rc={rc}");
|
||||
let len = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
|
||||
String::from_utf8_lossy(unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, len) })
|
||||
.into_owned()
|
||||
}
|
||||
|
||||
/// The uuids in the library list JSON (order preserved).
|
||||
fn list_uuids(json: &str) -> Vec<String> {
|
||||
serde_json::from_str::<serde_json::Value>(json)
|
||||
.expect("list is JSON")
|
||||
.as_array()
|
||||
.expect("list is an array")
|
||||
.iter()
|
||||
.map(|row| {
|
||||
row.get("uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.expect("row uuid")
|
||||
.to_string()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One row of the library list JSON by uuid.
|
||||
fn list_row<'a>(json: &'a str, uuid: &str) -> Option<serde_json::Value> {
|
||||
serde_json::from_str::<serde_json::Value>(json)
|
||||
.expect("list is JSON")
|
||||
.as_array()
|
||||
.expect("list is an array")
|
||||
.iter()
|
||||
.find(|row| row.get("uuid").and_then(|v| v.as_str()) == Some(uuid))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Open a library row into a fresh facade project shell.
|
||||
fn open(uuid: &str) -> *mut OakEngineProject {
|
||||
let project = unsafe { crate::node::oakengine_project_create() };
|
||||
assert!(!project.is_null());
|
||||
let uuid_c = CString::new(uuid).unwrap();
|
||||
let mut err = [0 as std::ffi::c_char; 4096];
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_project_load_library(
|
||||
project,
|
||||
uuid_c.as_ptr(),
|
||||
err.as_mut_ptr(),
|
||||
err.len() as i32,
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_OK, "open {uuid}");
|
||||
project
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Create lands a row immediately; list reports it with the metadata and
|
||||
/// the (zero) stats.
|
||||
#[test]
|
||||
fn create_then_list_shows_the_row() {
|
||||
let dir = temp_dir("create");
|
||||
with_library(&dir.join("lib.db"), || {
|
||||
let uuid = create("Demo Reel");
|
||||
assert!(!uuid.is_empty(), "create reports the new uuid");
|
||||
|
||||
let json = list_json();
|
||||
let row = list_row(&json, &uuid).expect("the created row is listed");
|
||||
assert_eq!(row.get("name").and_then(|v| v.as_str()), Some("Demo Reel"));
|
||||
assert!(row.get("modified_at").and_then(|v| v.as_i64()).unwrap() > 0);
|
||||
assert_eq!(row.get("track_count").and_then(|v| v.as_i64()), Some(0));
|
||||
assert_eq!(row.get("footage_count").and_then(|v| v.as_i64()), Some(0));
|
||||
|
||||
// A second create adds a second row.
|
||||
let other = create("Second");
|
||||
assert_ne!(uuid, other);
|
||||
assert_eq!(list_uuids(&list_json()).len(), 2);
|
||||
});
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Opening a library row binds the project to the library session: the
|
||||
/// next undoable edit write-throughs onto the row's journal.
|
||||
#[test]
|
||||
fn open_binds_and_write_through_advances_the_row() {
|
||||
let dir = temp_dir("open");
|
||||
let db = dir.join("lib.db");
|
||||
with_library(&db, || {
|
||||
let uuid = create("Editable");
|
||||
let before = journal_rows(&db, &uuid);
|
||||
let project = open(&uuid);
|
||||
assert_eq!(
|
||||
unsafe { crate::storage::oakengine_storage_is_bound(project) },
|
||||
1,
|
||||
"the library-opened project is bound"
|
||||
);
|
||||
|
||||
// An undoable edit write-throughs.
|
||||
let node = unsafe {
|
||||
crate::node::oakengine_project_add_node(project, CString::new(MATH).unwrap().as_ptr())
|
||||
};
|
||||
assert!(!node.is_null());
|
||||
unsafe { crate::node::oakengine_node_free(node) };
|
||||
|
||||
let after = journal_rows(&db, &uuid);
|
||||
assert!(
|
||||
after > before,
|
||||
"the edit journaled new rows ({before} -> {after})"
|
||||
);
|
||||
|
||||
// The row name comes from the projectname setting (the facade's
|
||||
// project name is filename-derived, so a library project displays
|
||||
// "(untitled)"; the app overrides it with the row name).
|
||||
let row = list_row(&list_json(), &uuid).expect("row after the edit");
|
||||
assert_eq!(row.get("name").and_then(|v| v.as_str()), Some("Editable"));
|
||||
|
||||
unsafe { crate::node::oakengine_project_free(project) };
|
||||
});
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Rename and duplicate keep the list coherent; delete removes the row and
|
||||
/// opening it afterwards fails.
|
||||
#[test]
|
||||
fn rename_duplicate_delete() {
|
||||
let dir = temp_dir("rdd");
|
||||
with_library(&dir.join("lib.db"), || {
|
||||
let uuid = create("Original");
|
||||
|
||||
// Rename.
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_rename(
|
||||
CString::new(uuid.clone()).unwrap().as_ptr(),
|
||||
CString::new("Renamed").unwrap().as_ptr(),
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_OK);
|
||||
let row = list_row(&list_json(), &uuid).expect("renamed row");
|
||||
assert_eq!(row.get("name").and_then(|v| v.as_str()), Some("Renamed"));
|
||||
|
||||
// Duplicate (default "<name> (copy)" name).
|
||||
let copy = duplicate(&uuid);
|
||||
assert_ne!(copy, uuid);
|
||||
let row = list_row(&list_json(), ©).expect("the copy is listed");
|
||||
assert_eq!(
|
||||
row.get("name").and_then(|v| v.as_str()),
|
||||
Some("Renamed (copy)")
|
||||
);
|
||||
|
||||
// The copy opens (its journal history came along).
|
||||
let project = open(©);
|
||||
unsafe { crate::node::oakengine_project_free(project) };
|
||||
|
||||
// Delete the copy; opening it afterwards fails.
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_delete(CString::new(copy.clone()).unwrap().as_ptr())
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_OK);
|
||||
assert!(!list_uuids(&list_json()).contains(©));
|
||||
let shell = unsafe { crate::node::oakengine_project_create() };
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_project_load_library(
|
||||
shell,
|
||||
CString::new(copy).unwrap().as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_E_NOT_FOUND, "a deleted row does not open");
|
||||
unsafe { crate::node::oakengine_project_free(shell) };
|
||||
|
||||
// Unknown uuids are E_NOT_FOUND; empty arguments E_INVALID.
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_delete(CString::new("{no-such}").unwrap().as_ptr())
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_E_NOT_FOUND);
|
||||
let rc = unsafe { crate::library::oakengine_library_delete(c"".as_ptr()) };
|
||||
assert_eq!(rc, OAKENGINE_E_INVALID);
|
||||
});
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Export writes the row's head state to a file (dispatched by extension),
|
||||
/// and import brings a file back as a new library row that opens.
|
||||
#[test]
|
||||
fn export_then_import_round_trip() {
|
||||
let dir = temp_dir("xport");
|
||||
with_library(&dir.join("lib.db"), || {
|
||||
// A row with one node, so the exported file has content.
|
||||
let uuid = create("Exchange");
|
||||
let project = open(&uuid);
|
||||
let node = unsafe {
|
||||
crate::node::oakengine_project_add_node(project, CString::new(MATH).unwrap().as_ptr())
|
||||
};
|
||||
assert!(!node.is_null());
|
||||
unsafe { crate::node::oakengine_node_free(node) };
|
||||
unsafe { crate::node::oakengine_project_free(project) };
|
||||
|
||||
// Export as .ove and as .otio.
|
||||
let ove = dir.join("out.ove");
|
||||
let otio = dir.join("out.otio");
|
||||
for path in [&ove, &otio] {
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_export(
|
||||
CString::new(uuid.clone()).unwrap().as_ptr(),
|
||||
CString::new(path.to_string_lossy().into_owned()).unwrap().as_ptr(),
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_OK, "export {}", path.display());
|
||||
assert!(path.exists(), "{} exists", path.display());
|
||||
}
|
||||
let xml = std::fs::read_to_string(&ove).unwrap();
|
||||
assert!(xml.contains("<project"), "ove payload:\n{xml}");
|
||||
|
||||
// Import the .ove back as a new row and open it (single call with a
|
||||
// stack buffer — the import export has a side effect).
|
||||
let path_c = CString::new(ove.to_string_lossy().into_owned()).unwrap();
|
||||
let mut buf = [0 as std::ffi::c_char; 256];
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_import(path_c.as_ptr(), buf.as_mut_ptr(), 256)
|
||||
};
|
||||
assert!(rc > 0, "import rc={rc}");
|
||||
let len = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
|
||||
let imported =
|
||||
String::from_utf8_lossy(unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, len) })
|
||||
.into_owned();
|
||||
assert!(!imported.is_empty(), "import reports the new uuid");
|
||||
assert_ne!(imported, uuid, "import assigns a fresh uuid");
|
||||
let project = open(&imported);
|
||||
// The projectname setting round-trips into the imported row's name.
|
||||
let row = list_row(&list_json(), &imported).expect("the imported row is listed");
|
||||
assert_eq!(row.get("name").and_then(|v| v.as_str()), Some("Exchange"));
|
||||
unsafe { crate::node::oakengine_project_free(project) };
|
||||
|
||||
// Importing a missing file fails.
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_import(
|
||||
CString::new(dir.join("nope.ove").to_string_lossy().into_owned())
|
||||
.unwrap()
|
||||
.as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
assert!(rc < 0, "a missing file does not import");
|
||||
});
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// With the backend disabled the list is empty (not an error) and every
|
||||
/// mutating call fails with E_STATE.
|
||||
#[test]
|
||||
fn disabled_backend_degrades_gracefully() {
|
||||
let _stack = GLOBAL_STACK_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let _off = common::storage_off_guard();
|
||||
|
||||
assert_eq!(list_json(), "[]", "no library configured reads as empty");
|
||||
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_library_create(
|
||||
c"Nope".as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_E_STATE);
|
||||
let rc = unsafe { crate::library::oakengine_library_delete(c"{x}".as_ptr()) };
|
||||
assert_eq!(rc, OAKENGINE_E_STATE);
|
||||
let rc = unsafe { crate::library::oakengine_library_export(c"{x}".as_ptr(), c"/tmp/x.ove".as_ptr()) };
|
||||
assert_eq!(rc, OAKENGINE_E_STATE);
|
||||
|
||||
let shell = unsafe { crate::node::oakengine_project_create() };
|
||||
let rc = unsafe {
|
||||
crate::library::oakengine_project_load_library(
|
||||
shell,
|
||||
c"{x}".as_ptr(),
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
)
|
||||
};
|
||||
assert_eq!(rc, OAKENGINE_E_STATE);
|
||||
unsafe { crate::node::oakengine_project_free(shell) };
|
||||
}
|
||||
@@ -41,6 +41,7 @@ mod it_audio;
|
||||
mod it_codec;
|
||||
mod it_common;
|
||||
mod it_export;
|
||||
mod it_library;
|
||||
mod it_plugin;
|
||||
mod it_storage;
|
||||
mod it_task;
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
//!
|
||||
//! One module per entity: the `DeriveEntityModel` macro generates
|
||||
//! `Entity`/`Column`/`Model`/`ActiveModel` in the defining module, so
|
||||
//! each table gets its own scope. The same definitions serve SQLite
|
||||
//! today and PostgreSQL (D3) — the column types (`i64`, `String`,
|
||||
//! `Option<String>`, `NaiveDateTime`) map to BIGINT/TEXT/TIMESTAMP on
|
||||
//! both.
|
||||
//! each table gets its own scope. The same definitions serve SQLite and
|
||||
//! PostgreSQL — the column types (`i64`, `String`, `Option<String>`,
|
||||
//! `NaiveDateTime`) map to BIGINT/TEXT/TIMESTAMP on both, so the shared
|
||||
//! save/replay logic never branches on the dialect.
|
||||
|
||||
pub mod journal;
|
||||
pub mod project;
|
||||
|
||||
@@ -60,22 +60,53 @@ const SQLITE_DDL: &[&str] = &[
|
||||
)",
|
||||
];
|
||||
|
||||
/// Apply the schema (idempotent; safe to run on every connection open).
|
||||
///
|
||||
/// PostgreSQL (D3) gets its own `BIGSERIAL` DDL — same shape, currently
|
||||
/// unreachable because the backend rejects `oakdb+pg://` before any
|
||||
/// connection is opened.
|
||||
/// PostgreSQL DDL for the four tables — the same shape with the PG
|
||||
/// dialect differences (plan D3: BIGSERIAL for the surrogate key,
|
||||
/// BIGINT for the FKs; `TEXT` everywhere, matching the SQLite TEXT
|
||||
/// decision — no BYTEA). `CREATE TABLE IF NOT EXISTS` makes it
|
||||
/// idempotent, exactly like the SQLite side.
|
||||
const POSTGRES_DDL: &[&str] = &[
|
||||
"CREATE TABLE IF NOT EXISTS projects (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
uuid TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
schema_ver INTEGER NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
modified_at TIMESTAMP NOT NULL,
|
||||
command_seq BIGINT NOT NULL DEFAULT 0
|
||||
)",
|
||||
"CREATE TABLE IF NOT EXISTS settings (
|
||||
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||
key TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
PRIMARY KEY (project_id, key)
|
||||
)",
|
||||
"CREATE TABLE IF NOT EXISTS snapshots (
|
||||
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||
command_seq BIGINT NOT NULL,
|
||||
payload TEXT NOT NULL,
|
||||
written_at TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (project_id, command_seq)
|
||||
)",
|
||||
"CREATE TABLE IF NOT EXISTS journal (
|
||||
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||
seq BIGINT NOT NULL,
|
||||
node_identity BIGINT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
old_xml TEXT,
|
||||
new_xml TEXT,
|
||||
at TIMESTAMP NOT NULL,
|
||||
PRIMARY KEY (project_id, seq, node_identity)
|
||||
)",
|
||||
];
|
||||
|
||||
/// Apply the schema for the connection's dialect (idempotent; safe to
|
||||
/// run on every connection open).
|
||||
pub async fn migrate(db: &impl ConnectionTrait) -> crate::error::Result<()> {
|
||||
let backend = db.get_database_backend();
|
||||
let statements: &[&str] = match backend {
|
||||
DatabaseBackend::Sqlite => SQLITE_DDL,
|
||||
// D3: `BIGSERIAL PRIMARY KEY` for projects.id, `BIGINT` for the
|
||||
// FKs — the rest of the column set is identical.
|
||||
DatabaseBackend::Postgres => {
|
||||
return Err(crate::error::Error::Failed(
|
||||
"oakdb+pg is a D3 milestone; the PostgreSQL schema is not wired yet".to_string(),
|
||||
));
|
||||
}
|
||||
DatabaseBackend::Postgres => POSTGRES_DDL,
|
||||
other => {
|
||||
return Err(crate::error::Error::Failed(format!(
|
||||
"unsupported database backend {other:?}"
|
||||
|
||||
@@ -14,16 +14,20 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! The database backend (plan M13 §1/§2), SQLite first.
|
||||
//! The database backend (plan M13 §1/§2): SQLite and PostgreSQL share
|
||||
//! one code path — the same sea-orm entities, diff/save/replay logic and
|
||||
//! migrations, with the dialect differences (BIGSERIAL vs AUTOINCREMENT,
|
||||
//! `IF NOT EXISTS` DDL) converged in the connection and migration layer.
|
||||
//!
|
||||
//! URI forms:
|
||||
//! - `oakdb+sqlite:///absolute/path.db[?project=<uuid>]` — local file
|
||||
//! database; the optional `project` query selects the library row to
|
||||
//! load (default: the most recently modified project).
|
||||
//! - `oakdb+pg://user:pass@host:5432/db[?project=<uuid>]` — PostgreSQL
|
||||
//! (D3). The target is parsed into [`DbTarget::Pg`] today, but every
|
||||
//! operation rejects it with [`crate::error::Error::NoBackend`] until
|
||||
//! the D3 client lands.
|
||||
//! - `oakdb+pg://user:pass@host:5432/db[?project=<uuid>]` — PostgreSQL;
|
||||
//! the body is the libpq connection string without a scheme. A `?…`
|
||||
//! part is the `?project=` selector only when it carries a `project=`
|
||||
//! key — a bare query (e.g. `?sslmode=disable`) belongs to the
|
||||
//! connection string and is passed through untouched.
|
||||
//!
|
||||
//! The async sea-orm API is driven by a private current-thread tokio
|
||||
//! runtime so the public surface stays synchronous (plan §6: "后端内嵌
|
||||
@@ -104,9 +108,11 @@ pub(crate) enum DbTarget {
|
||||
/// `?project=` uuid; `None` = most recently modified row.
|
||||
project: Option<String>,
|
||||
},
|
||||
/// PostgreSQL (parsed for D3; rejected until then).
|
||||
/// PostgreSQL server.
|
||||
Pg {
|
||||
/// Connection string (`user:pass@host:port/db`).
|
||||
/// Connection string body (`user:pass@host:port/db`, without a
|
||||
/// scheme; may carry its own `?param=…` query when it does not
|
||||
/// contain a `project=` key).
|
||||
conn: String,
|
||||
/// `?project=` uuid; `None` = most recently modified row.
|
||||
project: Option<String>,
|
||||
@@ -114,20 +120,6 @@ pub(crate) enum DbTarget {
|
||||
}
|
||||
|
||||
impl DbTarget {
|
||||
/// True for the PostgreSQL variant (all ops return E_NO_BACKEND
|
||||
/// until D3).
|
||||
fn is_pg(&self) -> bool {
|
||||
matches!(self, DbTarget::Pg { .. })
|
||||
}
|
||||
|
||||
/// The SQLite path (`None` for PG).
|
||||
fn sqlite_path(&self) -> Option<&str> {
|
||||
match self {
|
||||
DbTarget::Sqlite { path, .. } => Some(path),
|
||||
DbTarget::Pg { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// The `?project=` selector.
|
||||
fn project(&self) -> Option<&str> {
|
||||
match self {
|
||||
@@ -138,12 +130,23 @@ impl DbTarget {
|
||||
}
|
||||
}
|
||||
|
||||
/// The connection-cache key of a parsed target (the database location —
|
||||
/// an absolute SQLite path or a PostgreSQL connection string body).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
enum DbKey {
|
||||
/// SQLite file database at an absolute path.
|
||||
Sqlite(String),
|
||||
/// PostgreSQL server (connection string body, see [`DbTarget::Pg`]).
|
||||
Pg(String),
|
||||
}
|
||||
|
||||
/// Parse a storage URI into a [`DbTarget`].
|
||||
///
|
||||
/// `oakdb+sqlite://` requires an absolute path (E_INVALID otherwise);
|
||||
/// the body may carry `?project=<uuid>` (no percent-decoding — uuid
|
||||
/// values contain only hex/braces/hyphens). Unknown `oakdb` variants
|
||||
/// are E_NO_BACKEND.
|
||||
/// values contain only hex/braces/hyphens). `oakdb+pg://` requires a
|
||||
/// non-empty, parseable connection string (E_INVALID otherwise). Unknown
|
||||
/// `oakdb` variants are E_NO_BACKEND.
|
||||
pub(crate) fn parse_target(uri: &StorageUri) -> Result<DbTarget> {
|
||||
match uri.scheme.as_str() {
|
||||
"oakdb+sqlite" => {
|
||||
@@ -157,16 +160,37 @@ pub(crate) fn parse_target(uri: &StorageUri) -> Result<DbTarget> {
|
||||
})
|
||||
}
|
||||
"oakdb+pg" => {
|
||||
let (conn, query) = split_query(&uri.body);
|
||||
Ok(DbTarget::Pg {
|
||||
conn: conn.to_string(),
|
||||
project: query_param(query, "project"),
|
||||
})
|
||||
let (base, query) = split_query(&uri.body);
|
||||
// A `?…` part is the `?project=` selector only when it carries
|
||||
// a `project=` key; otherwise (e.g. `?sslmode=disable`) it
|
||||
// belongs to the connection string and the whole body passes
|
||||
// through.
|
||||
let project = match query {
|
||||
Some(q) if query_param(Some(q), "project").is_some() => {
|
||||
query_param(Some(q), "project")
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let conn = match &project {
|
||||
Some(_) => base.to_string(),
|
||||
None => uri.body.clone(),
|
||||
};
|
||||
if conn.is_empty() || !valid_pg_conn(&conn) {
|
||||
return Err(Error::Invalid);
|
||||
}
|
||||
Ok(DbTarget::Pg { conn, project })
|
||||
}
|
||||
_ => Err(Error::NoBackend),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `conn` parses as a PostgreSQL connection string (the libpq
|
||||
/// URL form, `user:pass@host:port/db[?params]`, with a scheme prepended).
|
||||
fn valid_pg_conn(conn: &str) -> bool {
|
||||
use std::str::FromStr;
|
||||
sea_orm::sqlx::postgres::PgConnectOptions::from_str(&format!("postgres://{conn}")).is_ok()
|
||||
}
|
||||
|
||||
/// Split `body` at the first `?` (the query part, if any).
|
||||
fn split_query(body: &str) -> (&str, Option<&str>) {
|
||||
match body.split_once('?') {
|
||||
@@ -310,8 +334,8 @@ pub struct DatabaseBackend {
|
||||
runtime: OnceLock<tokio::runtime::Runtime>,
|
||||
/// Serializes all database operations of this backend.
|
||||
op_lock: Mutex<()>,
|
||||
/// Open connections by database path.
|
||||
connections: Mutex<HashMap<String, DatabaseConnection>>,
|
||||
/// Open connections by database location (path or PG conn string).
|
||||
connections: Mutex<HashMap<DbKey, DatabaseConnection>>,
|
||||
}
|
||||
|
||||
impl DatabaseBackend {
|
||||
@@ -332,10 +356,11 @@ impl DatabaseBackend {
|
||||
.expect("tokio current_thread runtime")
|
||||
}
|
||||
|
||||
/// Run `f` against the database at `key` (an absolute SQLite path).
|
||||
/// The op lock is held for the whole call; the connection is opened
|
||||
/// and migrated on first use, then cached.
|
||||
fn run<T, F, Fut>(&self, key: &str, f: F) -> Result<T>
|
||||
/// Run `f` against the database at `key` (an absolute SQLite path or a
|
||||
/// PostgreSQL connection string). The op lock is held for the whole
|
||||
/// call; the connection is opened and migrated on first use, then
|
||||
/// cached.
|
||||
fn run<T, F, Fut>(&self, key: DbKey, f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(DatabaseConnection) -> Fut,
|
||||
Fut: Future<Output = Result<T>>,
|
||||
@@ -343,33 +368,36 @@ impl DatabaseBackend {
|
||||
let _guard = self.op_lock.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let runtime = self.runtime.get_or_init(Self::build_runtime);
|
||||
runtime.block_on(async {
|
||||
let conn = self.connect_cached(key).await?;
|
||||
let conn = self.connect_cached(&key).await?;
|
||||
f(conn).await
|
||||
})
|
||||
}
|
||||
|
||||
/// Open (and migrate) the SQLite database at `path`, caching the
|
||||
/// connection.
|
||||
/// Open (and migrate) the database at `key`, caching the connection.
|
||||
///
|
||||
/// Two writers racing on a fresh file surface SQLite's `database is
|
||||
/// Two writers racing on a fresh SQLite file surface `database is
|
||||
/// locked` (SQLITE_BUSY) — most often when a fresh connection's WAL
|
||||
/// mode switch collides with the other's, which `busy_timeout` cannot
|
||||
/// wait out — so connect+ migrate are retried a few times (plan §6:
|
||||
/// single-writer is the contract; a clean error beats a silent
|
||||
/// failure).
|
||||
async fn connect_cached(&self, path: &str) -> Result<DatabaseConnection> {
|
||||
/// wait out — so the SQLite connect + migrate are retried a few times
|
||||
/// (plan §6: single-writer is the contract; a clean error beats a
|
||||
/// silent failure). PostgreSQL connections are never retried (their
|
||||
/// errors are not retryable by [`retryable`]).
|
||||
async fn connect_cached(&self, key: &DbKey) -> Result<DatabaseConnection> {
|
||||
if let Some(conn) = self
|
||||
.connections
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.get(path)
|
||||
.get(key)
|
||||
{
|
||||
return Ok(conn.clone());
|
||||
}
|
||||
let mut attempt = 0;
|
||||
let conn = loop {
|
||||
let result = async {
|
||||
let conn = connect_sqlite(path).await?;
|
||||
let conn = match key {
|
||||
DbKey::Sqlite(path) => connect_sqlite(path).await?,
|
||||
DbKey::Pg(conn) => connect_pg(conn).await?,
|
||||
};
|
||||
migration::migrate(&conn).await?;
|
||||
Ok::<_, Error>(conn)
|
||||
}
|
||||
@@ -386,7 +414,7 @@ impl DatabaseBackend {
|
||||
self.connections
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.insert(path.to_string(), conn.clone());
|
||||
.insert(key.clone(), conn.clone());
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
@@ -396,8 +424,8 @@ impl DatabaseBackend {
|
||||
/// node graph).
|
||||
pub fn list_projects(&self, uri: &StorageUri) -> Result<Vec<ProjectInfo>> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
self.run(path, |conn| async move {
|
||||
let key = db_key_of(&target);
|
||||
self.run(key, |conn| async move {
|
||||
let models = project::Entity::find()
|
||||
.order_by_desc(project::Column::ModifiedAt)
|
||||
.all(&conn)
|
||||
@@ -422,9 +450,9 @@ impl DatabaseBackend {
|
||||
/// the project's data is gone — the manager confirms before calling).
|
||||
pub fn delete_project(&self, uri: &StorageUri, uuid: &str) -> Result<()> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let tx = conn.begin().await.map_err(db_err)?;
|
||||
let res = project::Entity::delete_many()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
@@ -449,10 +477,10 @@ impl DatabaseBackend {
|
||||
new_name: Option<&str>,
|
||||
) -> Result<ProjectInfo> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
let new_name = new_name.map(str::to_string);
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let tx = conn.begin().await.map_err(db_err)?;
|
||||
let src = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
@@ -556,10 +584,10 @@ impl DatabaseBackend {
|
||||
/// in-app project name (`settings["projectname"]`) is untouched.
|
||||
pub fn rename_project(&self, uri: &StorageUri, uuid: &str, new_name: &str) -> Result<()> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
let new_name = new_name.to_string();
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let now = chrono::Utc::now().naive_utc();
|
||||
let tx = conn.begin().await.map_err(db_err)?;
|
||||
let res = project::Entity::update_many()
|
||||
@@ -633,9 +661,9 @@ impl DatabaseBackend {
|
||||
/// the head seq.
|
||||
pub fn snapshot(&self, uri: &StorageUri, uuid: &str) -> Result<()> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let model = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -657,9 +685,9 @@ impl DatabaseBackend {
|
||||
/// the empty project. E_INVALID when `seq` is out of range.
|
||||
pub fn load_at(&self, uri: &StorageUri, uuid: &str, seq: i64) -> Result<CHandle> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let model = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -695,9 +723,9 @@ impl DatabaseBackend {
|
||||
/// handle (the head state).
|
||||
fn load_handle_by_uuid(&self, uri: &StorageUri, uuid: &str) -> Result<CHandle> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let uuid = uuid.to_string();
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let model = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -736,9 +764,9 @@ impl StorageBackend for DatabaseBackend {
|
||||
|
||||
fn load(&self, uri: &StorageUri) -> Result<LoadResult> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
let project = target.project().map(str::to_string);
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
let model = pick_project(&conn, project.as_deref()).await?;
|
||||
let xml = assemble_at(&conn, model.id, &model.uuid, model.command_seq).await?;
|
||||
let handle =
|
||||
@@ -749,7 +777,7 @@ impl StorageBackend for DatabaseBackend {
|
||||
|
||||
fn save(&self, project: CHandle, uri: &StorageUri, _options: u32) -> Result<()> {
|
||||
let target = parse_target(uri)?;
|
||||
let path = sqlite_path_of(&target)?;
|
||||
let key = db_key_of(&target);
|
||||
// Serialize under the project lock (the same per-node writer the
|
||||
// `.ove` backend uses — one serialization truth).
|
||||
let arc = unsafe { crate::nodeutil::project_arc(&project)? };
|
||||
@@ -760,21 +788,19 @@ impl StorageBackend for DatabaseBackend {
|
||||
let (nodes, settings_xml, settings_map) = serialize_project_state(&guard)?;
|
||||
(uuid, name, nodes, settings_xml, settings_map)
|
||||
};
|
||||
self.run(path, move |conn| async move {
|
||||
self.run(key, move |conn| async move {
|
||||
save_tx(&conn, &uuid, &name, &nodes, &settings_xml, &settings_map).await
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The SQLite path of a target, rejecting PG with E_NO_BACKEND (D3).
|
||||
fn sqlite_path_of(target: &DbTarget) -> Result<&str> {
|
||||
if target.is_pg() {
|
||||
// D3: `oakdb+pg://` is parsed (see parse_target) but every
|
||||
// operation refuses it until the PostgreSQL client is wired.
|
||||
return Err(Error::NoBackend);
|
||||
/// The connection-cache key of a target (SQLite path or PG conn string).
|
||||
fn db_key_of(target: &DbTarget) -> DbKey {
|
||||
match target {
|
||||
DbTarget::Sqlite { path, .. } => DbKey::Sqlite(path.clone()),
|
||||
DbTarget::Pg { conn, .. } => DbKey::Pg(conn.clone()),
|
||||
}
|
||||
Ok(target.sqlite_path().expect("pg rejected above"))
|
||||
}
|
||||
|
||||
/// Display name for a project on first entry: the `projectname` setting
|
||||
@@ -804,6 +830,34 @@ async fn connect_sqlite(path: &str) -> Result<DatabaseConnection> {
|
||||
Ok(DatabaseConnection::from(pool))
|
||||
}
|
||||
|
||||
/// Open the PostgreSQL database at `conn` (a connection string body,
|
||||
/// e.g. `user:pass@host:5432/db`; a scheme is prepended for sqlx).
|
||||
///
|
||||
/// The server is first probed with a single, non-retrying connection
|
||||
/// attempt: the pool retries refused connections with backoff for the
|
||||
/// whole acquire timeout, which would hold a dead server for seconds.
|
||||
/// A single `connect()` surfaces the refusal immediately, so a dead /
|
||||
/// unreachable host fails fast with a clean E_IO.
|
||||
async fn connect_pg(conn: &str) -> Result<DatabaseConnection> {
|
||||
use std::str::FromStr;
|
||||
use sea_orm::sqlx::postgres::{PgConnectOptions, PgPoolOptions};
|
||||
use sea_orm::sqlx::ConnectOptions;
|
||||
let url = format!("postgres://{conn}");
|
||||
let options = PgConnectOptions::from_str(&url)
|
||||
.map_err(|e| Error::Io(format!("invalid postgres connection string: {e}")))?;
|
||||
options
|
||||
.connect()
|
||||
.await
|
||||
.map_err(|e| Error::Io(format!("cannot connect to postgres database: {e}")))?;
|
||||
let pool = PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.acquire_timeout(Duration::from_secs(10))
|
||||
.connect_with(options)
|
||||
.await
|
||||
.map_err(|e| Error::Io(format!("cannot connect to postgres database: {e}")))?;
|
||||
Ok(DatabaseConnection::from(pool))
|
||||
}
|
||||
|
||||
/// Whether an error is worth retrying (SQLite BUSY — plan §6 note in
|
||||
/// [`DatabaseBackend::run`]).
|
||||
fn retryable(e: &Error) -> bool {
|
||||
@@ -1450,7 +1504,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_target_pg_kept_for_d3() {
|
||||
fn parse_target_pg_conn_string() {
|
||||
let t = parse_target(&uri("oakdb+pg://user:pass@host:5432/db")).unwrap();
|
||||
assert_eq!(
|
||||
t,
|
||||
@@ -1459,6 +1513,47 @@ mod tests {
|
||||
project: None
|
||||
}
|
||||
);
|
||||
// A `?project=` query is the row selector, not a conn param.
|
||||
let t = parse_target(&uri("oakdb+pg://user@host/db?project={abc-123}")).unwrap();
|
||||
assert_eq!(
|
||||
t,
|
||||
DbTarget::Pg {
|
||||
conn: "user@host/db".to_string(),
|
||||
project: Some("{abc-123}".to_string())
|
||||
}
|
||||
);
|
||||
// A query without a `project=` key belongs to the conn string
|
||||
// (e.g. `?sslmode=disable` passes through untouched).
|
||||
let t = parse_target(&uri("oakdb+pg://user@host/db?sslmode=disable")).unwrap();
|
||||
assert_eq!(
|
||||
t,
|
||||
DbTarget::Pg {
|
||||
conn: "user@host/db?sslmode=disable".to_string(),
|
||||
project: None
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_target_pg_invalid_rejected() {
|
||||
// Empty / unparseable connection strings are E_INVALID (clean, no
|
||||
// network touched), matching the sqlite relative-path rule.
|
||||
assert!(matches!(
|
||||
parse_target(&uri("oakdb+pg://")),
|
||||
Err(Error::Invalid)
|
||||
));
|
||||
assert!(matches!(
|
||||
parse_target(&uri("oakdb+pg://?project={x}")),
|
||||
Err(Error::Invalid)
|
||||
));
|
||||
assert!(matches!(
|
||||
parse_target(&uri("oakdb+pg://user@")),
|
||||
Err(Error::Invalid)
|
||||
));
|
||||
assert!(matches!(
|
||||
parse_target(&uri("oakdb+pg://not a url")),
|
||||
Err(Error::Invalid)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,525 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Shared fixtures and helpers for the database-backend integration
|
||||
//! tests (plan M13 D1/D3).
|
||||
//!
|
||||
//! Everything here is dialect-agnostic: the test bodies run against a
|
||||
//! `oakdb+sqlite://…` or `oakdb+pg://…` uri string, and the helpers that
|
||||
//! inspect rows behind the backend's back open a raw sea-orm connection
|
||||
//! ([`inspect_sqlite`] / [`inspect_pg`]). `tests/database_test.rs` runs
|
||||
//! the SQLite suite; `tests/database_pg_test.rs` runs the same behaviors
|
||||
//! against a real PostgreSQL server gated on `OAK_TEST_PG_URL`.
|
||||
|
||||
// The module is compiled into two test binaries, each of which uses only
|
||||
// the helpers for its own dialect, so the other dialect's helpers look
|
||||
// dead to one binary while the other uses them.
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use oakcore_rs::Rational;
|
||||
use oaknode::block::ClipBlockBehavior;
|
||||
use oaknode::footage::FootageBehavior;
|
||||
use oaknode::id::NodeId;
|
||||
use oaknode::keyframe::{Interpolation, Keyframe};
|
||||
use oaknode::node::NodeCore;
|
||||
use oaknode::project::Project;
|
||||
use oaknode::sequence::SequenceBehavior;
|
||||
use oaknode::track::{TrackBehavior, TrackListBehavior, TrackType};
|
||||
use oaknode::value::NodeValue;
|
||||
use oakstorage::backend::StorageBackend;
|
||||
use oakstorage::backends::database::entities::settings;
|
||||
use oakstorage::backends::database::DatabaseBackend;
|
||||
use oakstorage::error::OAKSTORAGE_OK;
|
||||
use oakstorage::handle::CHandle;
|
||||
use oakstorage::nodeutil::{make_project_owned, project_arc};
|
||||
use oakstorage::uri::StorageUri;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// URIs and paths
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// A fresh, unique temp directory for one test.
|
||||
pub(crate) fn temp_dir(tag: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("oakstorage_db_{}_{}", std::process::id(), tag));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// `oakdb+sqlite:///…` URI for a database file.
|
||||
pub(crate) fn db_uri(path: &Path) -> String {
|
||||
format!("oakdb+sqlite://{}", path.display())
|
||||
}
|
||||
|
||||
/// `oakdb+sqlite:///…?project=<uuid>` URI selecting one library row.
|
||||
pub(crate) fn project_uri(db: &str, uuid: &str) -> String {
|
||||
format!("{db}?project={uuid}")
|
||||
}
|
||||
|
||||
/// `file://…` URI for a plain file.
|
||||
pub(crate) fn file_uri(path: &Path) -> String {
|
||||
format!("file://{}", path.display())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Save / load through the backend
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Release an owned handle (refcount 1).
|
||||
pub(crate) fn release(h: CHandle) {
|
||||
if let Some(release) = h.release {
|
||||
unsafe { release(h.ctx) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Save an `Arc<Mutex<Project>>` through the database backend.
|
||||
pub(crate) fn save_project(
|
||||
backend: &DatabaseBackend,
|
||||
project: &Arc<Mutex<Project>>,
|
||||
uri: &str,
|
||||
) -> oakstorage::error::Result<()> {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let handle = make_project_owned(project.clone());
|
||||
let result = backend.save(handle, &parsed, 0);
|
||||
release(handle);
|
||||
result
|
||||
}
|
||||
|
||||
/// Load a project through a *new* database backend session (a fresh
|
||||
/// connection pool = a fresh session), returning `(uuid, loaded)`.
|
||||
pub(crate) fn load_project(backend: &DatabaseBackend, uri: &str) -> (String, Arc<Mutex<Project>>) {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let result = backend.load(&parsed).unwrap();
|
||||
assert_eq!(result.version_info, OAKSTORAGE_OK);
|
||||
let handle = result.project;
|
||||
let loaded = unsafe { project_arc(&handle) }.unwrap();
|
||||
let uuid = loaded.lock().unwrap().uuid.clone();
|
||||
release(handle);
|
||||
(uuid, loaded)
|
||||
}
|
||||
|
||||
/// Load the state at `seq` (undo to any point).
|
||||
pub(crate) fn load_at(backend: &DatabaseBackend, uri: &str, uuid: &str, seq: i64) -> Arc<Mutex<Project>> {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let handle = backend.load_at(&parsed, uuid, seq).unwrap();
|
||||
let loaded = unsafe { project_arc(&handle) }.unwrap();
|
||||
release(handle);
|
||||
loaded
|
||||
}
|
||||
|
||||
pub(crate) fn r_to_f(r: Rational) -> f64 {
|
||||
r.numerator() as f64 / r.denominator() as f64
|
||||
}
|
||||
|
||||
pub(crate) fn assert_close(a: f64, b: f64) {
|
||||
assert!((a - b).abs() < 1e-6, "expected {a} close to {b}");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Row inspection (raw sea-orm connections behind the backend's back)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Open a raw sea-orm SQLite connection to `path` and drive one future
|
||||
/// against it on a private current-thread runtime.
|
||||
pub(crate) fn inspect_sqlite<R, Fut>(path: &Path, f: impl FnOnce(sea_orm::DatabaseConnection) -> Fut) -> R
|
||||
where
|
||||
Fut: std::future::Future<Output = R>,
|
||||
{
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
rt.block_on(async {
|
||||
let options = sea_orm::sqlx::sqlite::SqliteConnectOptions::new()
|
||||
.filename(path)
|
||||
.create_if_missing(true)
|
||||
.journal_mode(sea_orm::sqlx::sqlite::SqliteJournalMode::Wal)
|
||||
.busy_timeout(Duration::from_secs(5))
|
||||
.foreign_keys(true);
|
||||
let pool = sea_orm::sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.unwrap();
|
||||
f(sea_orm::DatabaseConnection::from(pool)).await
|
||||
})
|
||||
}
|
||||
|
||||
/// Open a raw sea-orm PostgreSQL connection to `url` and drive one
|
||||
/// future against it on a private current-thread runtime.
|
||||
pub(crate) fn inspect_pg<R, Fut>(url: &str, f: impl FnOnce(sea_orm::DatabaseConnection) -> Fut) -> R
|
||||
where
|
||||
Fut: std::future::Future<Output = R>,
|
||||
{
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
rt.block_on(async {
|
||||
let pool = sea_orm::sqlx::postgres::PgPoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect(url)
|
||||
.await
|
||||
.unwrap();
|
||||
f(sea_orm::DatabaseConnection::from(pool)).await
|
||||
})
|
||||
}
|
||||
|
||||
/// The settings mirror rows of a project (SQLite).
|
||||
pub(crate) fn settings_rows(path: &Path, project_id: i64) -> Vec<(String, String)> {
|
||||
inspect_sqlite(path, |conn| async move {
|
||||
settings::Entity::find()
|
||||
.filter(settings::Column::ProjectId.eq(project_id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|s| (s.key, s.value))
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// The settings mirror rows of a project (PostgreSQL).
|
||||
pub(crate) fn settings_rows_pg(url: &str, project_id: i64) -> Vec<(String, String)> {
|
||||
inspect_pg(url, |conn| async move {
|
||||
settings::Entity::find()
|
||||
.filter(settings::Column::ProjectId.eq(project_id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|s| (s.key, s.value))
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// The project uuid.
|
||||
pub(crate) fn uuid_of(project: &Arc<Mutex<Project>>) -> String {
|
||||
project.lock().unwrap().uuid.clone()
|
||||
}
|
||||
|
||||
/// Read a config value for the duration of a test (the config store is
|
||||
/// process-global, so restore it afterwards).
|
||||
pub(crate) fn with_config(group: &str, key: &str, value: i32, f: impl FnOnce()) {
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
let before = store.get_int(Some(group), key, 0);
|
||||
store.set_int(Some(group), key, value);
|
||||
f();
|
||||
store.set_int(Some(group), key, before);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Full-feature fixture (footage / sequence / track / clip / effect /
|
||||
// keyframes) — the union of the ove round-trip and timeline fixtures.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub(crate) const MATH: &str = "org.olivevideoeditor.Olive.math";
|
||||
|
||||
/// Build the round-trip fixture: root folder + two math nodes with
|
||||
/// values/keyframes/label/color/link/connection + a sequence "My Seq"
|
||||
/// with one video track carrying two clips (footage /a/b.mp4, /a/c.mp4).
|
||||
pub(crate) fn build_full_project() -> Arc<Mutex<Project>> {
|
||||
let project = Project::new();
|
||||
let mut p = project.lock().unwrap();
|
||||
p.initialize().unwrap();
|
||||
|
||||
let (core, behavior) = (oaknode::factory::Factory::global().find(MATH).unwrap().create)();
|
||||
let a = p.graph.add_node(core, behavior);
|
||||
{
|
||||
let e = p.graph.get_mut(a).unwrap();
|
||||
e.core.label = "Math A".to_string();
|
||||
e.core.override_color = 2;
|
||||
e.core.set_standard_value("param_a_in", -1, NodeValue::Float(2.5));
|
||||
e.core
|
||||
.keyframe_track_mut("param_a_in", -1)
|
||||
.set_key(Keyframe {
|
||||
time: Rational::new(0, 1),
|
||||
value: NodeValue::Float(1.0),
|
||||
interpolation: Interpolation::Linear,
|
||||
bezier_in: (0.0, 0.0),
|
||||
bezier_out: (0.0, 0.0),
|
||||
});
|
||||
e.core
|
||||
.keyframe_track_mut("param_a_in", -1)
|
||||
.set_key(Keyframe {
|
||||
time: Rational::new(1, 1),
|
||||
value: NodeValue::Float(3.0),
|
||||
interpolation: Interpolation::Bezier,
|
||||
bezier_in: (0.1, 0.2),
|
||||
bezier_out: (0.3, 0.4),
|
||||
});
|
||||
}
|
||||
let (core, behavior) = (oaknode::factory::Factory::global().find(MATH).unwrap().create)();
|
||||
let b = p.graph.add_node(core, behavior);
|
||||
p.graph
|
||||
.get_mut(b)
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(4.0));
|
||||
p.graph.connect(a, b, "param_b_in", -1).unwrap();
|
||||
p.graph.link(a, b);
|
||||
|
||||
// Timeline: sequence + video track + two clips with footage.
|
||||
let (seq_id, lists) = oakstorage::nodeutil::create_sequence(&mut p.graph);
|
||||
p.graph.get_mut(seq_id).unwrap().core.label = "My Seq".to_string();
|
||||
|
||||
let mut tb = TrackBehavior::new(TrackType::Video);
|
||||
tb.track_list = Some(lists[0]);
|
||||
let track_id = p.graph.add_node(NodeCore::new(), Box::new(tb));
|
||||
|
||||
let foot1 = p
|
||||
.graph
|
||||
.add_node(NodeCore::new(), Box::new(FootageBehavior::new("/a/b.mp4")));
|
||||
let clip1 = {
|
||||
let (core, mut behavior) = oaknode::block::clip_create();
|
||||
let clip = behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
clip.core.range = oakcore_rs::TimeRange::new(Rational::new(0, 1), Rational::new(100, 25));
|
||||
clip.core.media_in = Rational::new(0, 1);
|
||||
clip.core.track = Some(track_id);
|
||||
clip.footage = Some(foot1);
|
||||
p.graph.add_node(core, behavior)
|
||||
};
|
||||
|
||||
let foot2 = p
|
||||
.graph
|
||||
.add_node(NodeCore::new(), Box::new(FootageBehavior::new("/a/c.mp4")));
|
||||
let clip2 = {
|
||||
let (core, mut behavior) = oaknode::block::clip_create();
|
||||
let clip = behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
clip.core.range =
|
||||
oakcore_rs::TimeRange::new(Rational::new(100, 25), Rational::new(150, 25));
|
||||
clip.core.media_in = Rational::new(10, 25);
|
||||
clip.core.track = Some(track_id);
|
||||
clip.footage = Some(foot2);
|
||||
p.graph.add_node(core, behavior)
|
||||
};
|
||||
|
||||
if let Some(entry) = p.graph.get_mut(track_id) {
|
||||
entry
|
||||
.behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<TrackBehavior>())
|
||||
.unwrap()
|
||||
.blocks = vec![clip1, clip2];
|
||||
}
|
||||
if let Some(entry) = p.graph.get_mut(lists[0]) {
|
||||
entry
|
||||
.behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<TrackListBehavior>())
|
||||
.unwrap()
|
||||
.tracks = vec![track_id];
|
||||
}
|
||||
|
||||
p.settings
|
||||
.insert("projectname".to_string(), "full-fixture".to_string());
|
||||
drop(p);
|
||||
project
|
||||
}
|
||||
|
||||
/// Field-by-field comparison of the full fixture after a round-trip
|
||||
/// (uuid included).
|
||||
pub(crate) fn assert_full_fields(orig: &Project, loaded: &Project) {
|
||||
assert_eq!(loaded.uuid, orig.uuid, "uuid");
|
||||
assert_full_state(orig, loaded);
|
||||
}
|
||||
|
||||
/// Field-by-field comparison that ignores the uuid (a duplicated row
|
||||
/// carries a fresh uuid by design).
|
||||
pub(crate) fn assert_full_state(orig: &Project, loaded: &Project) {
|
||||
assert_eq!(loaded.settings, orig.settings, "settings");
|
||||
|
||||
// Node set: count, type order (slot order preserved by the writer).
|
||||
let o_ids = orig.graph.node_ids();
|
||||
let l_ids = loaded.graph.node_ids();
|
||||
assert_eq!(l_ids.len(), o_ids.len(), "node count");
|
||||
let o_types: Vec<&str> = o_ids
|
||||
.iter()
|
||||
.map(|id| orig.graph.get(*id).unwrap().behavior.type_id())
|
||||
.collect();
|
||||
let l_types: Vec<&str> = l_ids
|
||||
.iter()
|
||||
.map(|id| loaded.graph.get(*id).unwrap().behavior.type_id())
|
||||
.collect();
|
||||
assert_eq!(l_types, o_types, "node types");
|
||||
|
||||
// Math A: label, color, value, keyframes.
|
||||
let (a_o, a_l) = (o_ids[1], l_ids[1]);
|
||||
assert_eq!(
|
||||
loaded.graph.get(a_l).unwrap().core.label,
|
||||
orig.graph.get(a_o).unwrap().core.label,
|
||||
"label"
|
||||
);
|
||||
assert_eq!(
|
||||
loaded.graph.get(a_l).unwrap().core.override_color,
|
||||
orig.graph.get(a_o).unwrap().core.override_color,
|
||||
"color"
|
||||
);
|
||||
assert_eq!(
|
||||
loaded
|
||||
.graph
|
||||
.get(a_l)
|
||||
.unwrap()
|
||||
.core
|
||||
.standard_value("param_a_in", -1),
|
||||
orig.graph
|
||||
.get(a_o)
|
||||
.unwrap()
|
||||
.core
|
||||
.standard_value("param_a_in", -1),
|
||||
"value a"
|
||||
);
|
||||
let keys_o = orig
|
||||
.graph
|
||||
.get(a_o)
|
||||
.unwrap()
|
||||
.core
|
||||
.keyframe_track("param_a_in", -1)
|
||||
.unwrap()
|
||||
.keys()
|
||||
.to_vec();
|
||||
let keys_l = loaded
|
||||
.graph
|
||||
.get(a_l)
|
||||
.unwrap()
|
||||
.core
|
||||
.keyframe_track("param_a_in", -1)
|
||||
.unwrap()
|
||||
.keys()
|
||||
.to_vec();
|
||||
assert_eq!(keys_l.len(), keys_o.len(), "keyframe count");
|
||||
for (ko, kl) in keys_o.iter().zip(&keys_l) {
|
||||
assert_eq!(kl.time, ko.time, "key time");
|
||||
assert_eq!(kl.value.to_double(), ko.value.to_double(), "key value");
|
||||
assert_eq!(kl.interpolation, ko.interpolation, "key interpolation");
|
||||
assert_eq!(kl.bezier_in, ko.bezier_in, "key bezier in");
|
||||
assert_eq!(kl.bezier_out, ko.bezier_out, "key bezier out");
|
||||
}
|
||||
|
||||
// Connection a -> b.param_b_in and the link.
|
||||
assert_eq!(
|
||||
loaded.graph.connected_output(l_ids[2], "param_b_in", -1),
|
||||
Some(a_l),
|
||||
"connection"
|
||||
);
|
||||
assert!(loaded.graph.are_linked(a_l, l_ids[2]), "link");
|
||||
|
||||
// Timeline: sequence, track lists, track, clips, footage.
|
||||
let mut seq: Option<(NodeId, Vec<NodeId>)> = None;
|
||||
for id in loaded.graph.node_ids() {
|
||||
let entry = loaded.graph.get(id).unwrap();
|
||||
if entry.behavior.type_id() == "org.olivevideoeditor.Olive.sequence" {
|
||||
let s = entry
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<SequenceBehavior>())
|
||||
.unwrap();
|
||||
seq = Some((id, s.track_lists.clone()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (_seq_id, lists) = seq.expect("sequence present");
|
||||
assert_eq!(lists.len(), 3, "video/audio/subtitle lists");
|
||||
let list = loaded
|
||||
.graph
|
||||
.get(lists[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<TrackListBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(list.kind, TrackType::Video);
|
||||
assert_eq!(list.array_base, 0);
|
||||
assert_eq!(list.tracks.len(), 1);
|
||||
let track = loaded
|
||||
.graph
|
||||
.get(list.tracks[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<TrackBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(track.kind, TrackType::Video);
|
||||
assert_eq!(track.blocks.len(), 2);
|
||||
let c1 = loaded
|
||||
.graph
|
||||
.get(track.blocks[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
assert_close(r_to_f(c1.core.length()), 4.0);
|
||||
assert_close(r_to_f(c1.core.media_in), 0.0);
|
||||
let f1 = loaded
|
||||
.graph
|
||||
.get(c1.footage.unwrap())
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<FootageBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(f1.filename, "/a/b.mp4");
|
||||
let c2 = loaded
|
||||
.graph
|
||||
.get(track.blocks[1])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
assert_close(r_to_f(c2.core.length()), 2.0);
|
||||
assert_close(r_to_f(c2.core.media_in), 0.4);
|
||||
let f2 = loaded
|
||||
.graph
|
||||
.get(c2.footage.unwrap())
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<FootageBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(f2.filename, "/a/c.mp4");
|
||||
}
|
||||
|
||||
/// Build a small project with a `projectname` setting (fast saves).
|
||||
pub(crate) fn build_named_project(name: &str) -> Arc<Mutex<Project>> {
|
||||
let project = Project::new();
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
p.initialize().unwrap();
|
||||
p.settings.insert("projectname".to_string(), name.to_string());
|
||||
}
|
||||
project
|
||||
}
|
||||
|
||||
/// Save a fresh named project, returning its uuid.
|
||||
pub(crate) fn save_named_project(backend: &DatabaseBackend, uri: &str, name: &str) -> String {
|
||||
let project = build_named_project(name);
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(backend, &project, uri).unwrap();
|
||||
uuid
|
||||
}
|
||||
@@ -0,0 +1,996 @@
|
||||
// Oak Video Editor - Non-Linear Video Editor
|
||||
// Copyright (C) 2026 Oak Team
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! PostgreSQL variants of the database-backend integration tests (plan
|
||||
//! M13 D3): the same behaviors `database_test.rs` runs against SQLite,
|
||||
//! driven by the shared fixtures in `tests/common/mod.rs`, against a
|
||||
//! real PostgreSQL server.
|
||||
//!
|
||||
//! Gating: when `OAK_TEST_PG_URL` (a libpq URL such as
|
||||
//! `postgres://user:pass@host:5432/db`) is set, every test connects to
|
||||
//! that server and runs for real; when it is absent the tests skip with
|
||||
//! a note on stderr, so `cargo test -p oakstorage` is green without a
|
||||
//! PostgreSQL server (CI included). The URL should point at a dedicated
|
||||
//! test database: each test resets the four schema tables (DROP + the
|
||||
//! migration recreates them on connect), and the tests run serialized
|
||||
//! against the shared database.
|
||||
|
||||
mod common;
|
||||
|
||||
use std::sync::Mutex;
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{ConnectionTrait, QueryOrder};
|
||||
use oaknode::id::NodeId;
|
||||
use oaknode::value::NodeValue;
|
||||
use oakstorage::backend::StorageBackend;
|
||||
use oakstorage::backends::database::entities::{journal, project, snapshot};
|
||||
use oakstorage::backends::database::{
|
||||
derive_stats, DatabaseBackend, KIND_IMPORT, KIND_REDO, ProjectStats, SNAPSHOT_KEEP,
|
||||
};
|
||||
use oakstorage::error::{
|
||||
OAKSTORAGE_E_FORMAT, OAKSTORAGE_E_INVALID, OAKSTORAGE_E_NOT_FOUND, OAKSTORAGE_OK,
|
||||
};
|
||||
use oakstorage::nodeutil::project_arc;
|
||||
use oakstorage::registry::Registry;
|
||||
use oakstorage::uri::StorageUri;
|
||||
|
||||
use common::*;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Gating and isolation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// The `OAK_TEST_PG_URL` connection string (None when not configured).
|
||||
fn pg_url() -> Option<String> {
|
||||
std::env::var("OAK_TEST_PG_URL")
|
||||
.ok()
|
||||
.filter(|s| !s.trim().is_empty())
|
||||
}
|
||||
|
||||
/// Strip the `postgres://` / `postgresql://` scheme from a connection
|
||||
/// URL to get the `oakdb+pg://` uri body.
|
||||
fn pg_body(url: &str) -> &str {
|
||||
url.strip_prefix("postgres://")
|
||||
.or_else(|| url.strip_prefix("postgresql://"))
|
||||
.unwrap_or(url)
|
||||
}
|
||||
|
||||
/// Reset the four schema tables (the migration recreates them on the
|
||||
/// next connection). Serialized with the other PG tests.
|
||||
fn reset_tables(url: &str) {
|
||||
inspect_pg(url, |conn| async move {
|
||||
for table in ["journal", "snapshots", "settings", "projects"] {
|
||||
conn.execute_raw(sea_orm::Statement::from_string(
|
||||
sea_orm::DatabaseBackend::Postgres,
|
||||
format!("DROP TABLE IF EXISTS {table} CASCADE"),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Run `f` with `(pg_url, oakdb+pg uri)` for an isolated test session.
|
||||
/// Skips (with a note) when `OAK_TEST_PG_URL` is absent; otherwise the
|
||||
/// tables are reset and the test runs against the shared database (the
|
||||
/// tests share one server and one schema, so they run one at a time).
|
||||
fn with_pg_uri(tag: &str, f: impl FnOnce(&str, &str)) {
|
||||
let Some(url) = pg_url() else {
|
||||
eprintln!("OAK_TEST_PG_URL is not set — skipping the PostgreSQL variant of '{tag}'");
|
||||
return;
|
||||
};
|
||||
static PG_LOCK: Mutex<()> = Mutex::new(());
|
||||
let _g = PG_LOCK.lock().unwrap_or_else(|e| e.into_inner());
|
||||
reset_tables(&url);
|
||||
let uri = format!("oakdb+pg://{}", pg_body(&url));
|
||||
f(&url, &uri);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Round-trip through a fresh session
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn pg_roundtrip_field_by_field() {
|
||||
with_pg_uri("roundtrip_field_by_field", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
// A new backend instance = a new session; the library row is
|
||||
// selected explicitly by uuid.
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, loaded) = load_project(&session, &project_uri(uri, &uuid));
|
||||
assert_eq!(loaded_uuid, uuid);
|
||||
{
|
||||
let o = project.lock().unwrap();
|
||||
let l = loaded.lock().unwrap();
|
||||
assert_full_fields(&o, &l);
|
||||
}
|
||||
|
||||
// The default (no ?project=) pick returns the same single row.
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, _) = load_project(&session, uri);
|
||||
assert_eq!(loaded_uuid, uuid);
|
||||
|
||||
// The settings mirror carries the current keys.
|
||||
let uuid_c = uuid.clone();
|
||||
let row = inspect_pg(url, move |conn| async move {
|
||||
project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.id
|
||||
});
|
||||
let mirrored = settings_rows_pg(url, row);
|
||||
assert!(
|
||||
mirrored.contains(&("projectname".to_string(), "full-fixture".to_string())),
|
||||
"{mirrored:?}"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Journal semantics: import, diff, no-op save
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// First save is one `kind='import'` command carrying every node plus
|
||||
/// the settings pseudo-node (plan §2); the project row advances to seq 1.
|
||||
#[test]
|
||||
fn pg_first_save_is_an_import_command() {
|
||||
with_pg_uri("first_save_is_an_import_command", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
let node_count = project.lock().unwrap().graph.node_count();
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
let uuid_c = uuid.clone();
|
||||
let rows = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let rows = journal::Entity::find()
|
||||
.filter(journal::Column::ProjectId.eq(proj.id))
|
||||
.order_by_asc(journal::Column::Seq)
|
||||
.order_by_asc(journal::Column::NodeIdentity)
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
(proj.id, proj.command_seq, rows)
|
||||
});
|
||||
let (pid, command_seq, rows) = rows;
|
||||
assert_eq!(command_seq, 1);
|
||||
assert_eq!(
|
||||
rows.len(),
|
||||
node_count + 1,
|
||||
"every node + the settings pseudo-node"
|
||||
);
|
||||
assert!(rows.iter().all(|r| r.seq == 1 && r.kind == KIND_IMPORT), "one import command");
|
||||
assert!(rows.iter().all(|r| r.old_xml.is_none()), "import has no before images");
|
||||
assert!(rows.iter().all(|r| r.new_xml.is_some()), "import has after images");
|
||||
// The settings pseudo-node (identity 0) is present.
|
||||
assert!(rows.iter().any(|r| r.node_identity == 0), "settings row present");
|
||||
assert_eq!(
|
||||
rows.iter().filter(|r| r.node_identity == 0).count(),
|
||||
1,
|
||||
"exactly one settings row"
|
||||
);
|
||||
// The real-node rows use the +1 offset (settings 0 reserved).
|
||||
assert!(rows.iter().all(|r| r.node_identity >= 1 || r.node_identity == 0));
|
||||
// The node fragments round-trip: the settings row holds the settings
|
||||
// element.
|
||||
let settings_row = rows.iter().find(|r| r.node_identity == 0).unwrap();
|
||||
assert!(settings_row.new_xml.as_deref().unwrap().starts_with("<settings>"));
|
||||
let _ = pid;
|
||||
});
|
||||
}
|
||||
|
||||
/// A later save is a diff: only the changed nodes land as `kind='redo'`
|
||||
/// rows with both before and after images (plan §0).
|
||||
#[test]
|
||||
fn pg_later_saves_are_diffs() {
|
||||
with_pg_uri("later_saves_are_diffs", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
// Change one math node's value and one setting key.
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = p
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
let a = math_ids[0];
|
||||
p.graph
|
||||
.get_mut(a)
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(9.5));
|
||||
p.settings.insert("projectname".to_string(), "diffed".to_string());
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
let uuid_c = uuid.clone();
|
||||
let rows = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let rows = journal::Entity::find()
|
||||
.filter(journal::Column::ProjectId.eq(proj.id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
(proj.id, proj.command_seq, rows)
|
||||
});
|
||||
let (pid, command_seq, rows) = rows;
|
||||
assert_eq!(command_seq, 2);
|
||||
let seq2: Vec<&journal::Model> = rows.iter().filter(|r| r.seq == 2).collect();
|
||||
assert_eq!(seq2.len(), 2, "one changed node + the settings row, nothing else");
|
||||
assert!(seq2.iter().all(|r| r.kind == KIND_REDO));
|
||||
assert!(
|
||||
seq2.iter().all(|r| r.old_xml.is_some() && r.new_xml.is_some()),
|
||||
"diff carries both images"
|
||||
);
|
||||
let settings_row = seq2.iter().find(|r| r.node_identity == 0).unwrap();
|
||||
assert!(settings_row.new_xml.as_deref().unwrap().contains(">diffed<"));
|
||||
let _ = pid;
|
||||
|
||||
// Head state after the diff reflects both changes.
|
||||
let session = DatabaseBackend::new();
|
||||
let (_, loaded) = load_project(&session, &project_uri(uri, &uuid));
|
||||
{
|
||||
let l = loaded.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = l
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| l.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
l.graph.get(math_ids[0]).unwrap().core.standard_value("param_a_in", -1),
|
||||
NodeValue::Float(9.5)
|
||||
);
|
||||
assert_eq!(
|
||||
l.settings.get("projectname").cloned(),
|
||||
Some("diffed".to_string())
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A save that changes nothing bumps no command seq and writes no rows.
|
||||
#[test]
|
||||
fn pg_no_op_save_is_a_touch_only() {
|
||||
with_pg_uri("no_op_save_is_a_touch_only", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
let uuid_c = uuid.clone();
|
||||
let (head, count) = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let n = journal::Entity::find()
|
||||
.filter(journal::Column::ProjectId.eq(proj.id))
|
||||
.count(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
(proj.command_seq, n)
|
||||
});
|
||||
assert_eq!(head, 1, "no-op save keeps the head seq");
|
||||
// The import wrote 12 nodes + 1 settings row; the no-op added none.
|
||||
assert_eq!(count, 13, "only the import rows remain");
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Snapshot + journal replay and undo to any point
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// With `Storage/SnapshotIntervalSec` ≤ 0 every dirty save writes a
|
||||
/// snapshot; the newest snapshot is the replay base and the journal rows
|
||||
/// after it are applied on top. Deleting every snapshot still recovers
|
||||
/// the same state from an empty base plus the full journal (plan §0).
|
||||
#[test]
|
||||
fn pg_snapshot_and_journal_replay() {
|
||||
with_pg_uri("snapshot_and_journal_replay", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
with_config("Storage", "SnapshotIntervalSec", 0, || {
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
// Second command: one value change.
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = p
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
p.graph
|
||||
.get_mut(math_ids[0])
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(7.25));
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
// Both saves produced snapshots (head seq 2).
|
||||
let uuid_c = uuid.clone();
|
||||
let snaps = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
snapshot::Entity::find()
|
||||
.filter(snapshot::Column::ProjectId.eq(proj.id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
});
|
||||
assert_eq!(snaps.len(), 2, "one snapshot per dirty save");
|
||||
|
||||
// Load (head): snapshot seq 2 is the base, nothing after it.
|
||||
let session = DatabaseBackend::new();
|
||||
let (_, loaded) = load_project(&session, &project_uri(uri, &uuid));
|
||||
{
|
||||
let o = project.lock().unwrap();
|
||||
let l = loaded.lock().unwrap();
|
||||
assert_full_fields(&o, &l);
|
||||
}
|
||||
|
||||
// Destroy the snapshots: replay degrades to empty base + full
|
||||
// journal and still reconstructs the head.
|
||||
let uuid_c = uuid.clone();
|
||||
inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
snapshot::Entity::delete_many()
|
||||
.filter(snapshot::Column::ProjectId.eq(proj.id))
|
||||
.exec(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
let session = DatabaseBackend::new();
|
||||
let (_, loaded) = load_project(&session, &project_uri(uri, &uuid));
|
||||
{
|
||||
let o = project.lock().unwrap();
|
||||
let l = loaded.lock().unwrap();
|
||||
assert_full_fields(&o, &l);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// The journal is the persistent undo history: `load_at(seq)` replays to
|
||||
/// any point (plan §0 "撤销到任意点").
|
||||
#[test]
|
||||
fn pg_undo_to_any_point() {
|
||||
with_pg_uri("undo_to_any_point", |_url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
let math_a = {
|
||||
let p = project.lock().unwrap();
|
||||
p.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.find(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.unwrap()
|
||||
};
|
||||
// Command 1: value 2.5 (fixture default).
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
// Command 2: value 6.0.
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
p.graph
|
||||
.get_mut(math_a)
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(6.0));
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
// Command 3: add a node.
|
||||
let (new_id, value) = {
|
||||
let mut p = project.lock().unwrap();
|
||||
let (core, behavior) = (oaknode::factory::Factory::global().find(MATH).unwrap().create)();
|
||||
let id = p.graph.add_node(core, behavior);
|
||||
p.graph
|
||||
.get_mut(id)
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(11.0));
|
||||
let v = p
|
||||
.graph
|
||||
.get(id)
|
||||
.unwrap()
|
||||
.core
|
||||
.standard_value("param_a_in", -1);
|
||||
(id, v)
|
||||
};
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
// Head (seq 3): the extra node exists.
|
||||
let session = DatabaseBackend::new();
|
||||
let (_, head) = load_project(&session, &project_uri(uri, &uuid));
|
||||
{
|
||||
let h = head.lock().unwrap();
|
||||
assert!(h.graph.is_valid(new_id), "node added in command 3 is live");
|
||||
assert_eq!(
|
||||
h.graph.get(new_id).unwrap().core.standard_value("param_a_in", -1),
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
// Undo to seq 2: the node is gone, the value is 6.0.
|
||||
let session = DatabaseBackend::new();
|
||||
let at2 = load_at(&session, uri, &uuid, 2);
|
||||
{
|
||||
let l = at2.lock().unwrap();
|
||||
assert!(!l.graph.is_valid(new_id), "command 3 rolled back");
|
||||
let id = l
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.find(|id| l.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
l.graph.get(id).unwrap().core.standard_value("param_a_in", -1),
|
||||
NodeValue::Float(6.0)
|
||||
);
|
||||
}
|
||||
|
||||
// Undo to seq 1: the value is back to the fixture default.
|
||||
let session = DatabaseBackend::new();
|
||||
let at1 = load_at(&session, uri, &uuid, 1);
|
||||
{
|
||||
let l = at1.lock().unwrap();
|
||||
assert_eq!(l.graph.node_count(), 12, "fixture node count");
|
||||
let id = l
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.find(|id| l.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
l.graph.get(id).unwrap().core.standard_value("param_a_in", -1),
|
||||
NodeValue::Float(2.5)
|
||||
);
|
||||
}
|
||||
|
||||
// Undo to seq 0: an empty project.
|
||||
let session = DatabaseBackend::new();
|
||||
let at0 = load_at(&session, uri, &uuid, 0);
|
||||
{
|
||||
let l = at0.lock().unwrap();
|
||||
assert_eq!(l.graph.node_count(), 0, "empty project at seq 0");
|
||||
}
|
||||
|
||||
// Out of range -> E_INVALID.
|
||||
let session = DatabaseBackend::new();
|
||||
assert_eq!(
|
||||
session
|
||||
.load_at(&StorageUri::parse(uri).unwrap(), &uuid, 99)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_INVALID
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Snapshot pruning keeps the newest [`SNAPSHOT_KEEP`] copies.
|
||||
#[test]
|
||||
fn pg_snapshot_pruning_keeps_three() {
|
||||
with_pg_uri("snapshot_pruning_keeps_three", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
with_config("Storage", "SnapshotIntervalSec", 0, || {
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
// 6 commands, each dirty-snapshotted.
|
||||
for i in 0..6i64 {
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let id = p.graph.node_ids()[0];
|
||||
p.graph.get_mut(id).unwrap().core.label = format!("step {i}");
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
}
|
||||
let uuid_c = uuid.clone();
|
||||
let seqs = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
snapshot::Entity::find()
|
||||
.filter(snapshot::Column::ProjectId.eq(proj.id))
|
||||
.order_by_desc(snapshot::Column::CommandSeq)
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|s| s.command_seq)
|
||||
.collect::<Vec<i64>>()
|
||||
});
|
||||
assert_eq!(seqs.len() as u64, SNAPSHOT_KEEP, "only the newest {SNAPSHOT_KEEP}");
|
||||
assert_eq!(seqs[0], 6, "the newest survives");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Journal retention (`Storage/JournalRetentionDays`): rows older than
|
||||
/// the window and covered by the newest snapshot are dropped; the head
|
||||
/// stays reconstructible (snapshot + remaining rows).
|
||||
#[test]
|
||||
fn pg_journal_retention_truncation() {
|
||||
with_pg_uri("journal_retention_truncation", |url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
// Command 1 (import) + command 2 (a value change).
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = p
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
p.graph
|
||||
.get_mut(math_ids[0])
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(3.25));
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
// Snapshot at the head so rows ≤ 2 are covered.
|
||||
backend
|
||||
.snapshot(&StorageUri::parse(uri).unwrap(), &uuid)
|
||||
.unwrap();
|
||||
|
||||
// Backdate every journal row two days, then save a third command
|
||||
// with a 1-day retention window.
|
||||
inspect_pg(url, |conn| async move {
|
||||
let old = chrono::Utc::now().naive_utc() - chrono::Duration::days(2);
|
||||
journal::Entity::update_many()
|
||||
.col_expr(journal::Column::At, sea_orm::sea_query::Expr::value(old))
|
||||
.exec(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
with_config("Storage", "JournalRetentionDays", 1, || {
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = p
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
p.graph
|
||||
.get_mut(math_ids[0])
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(4.5));
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
});
|
||||
|
||||
// Only the third command's rows survive.
|
||||
let uuid_c = uuid.clone();
|
||||
let (pid, rows) = inspect_pg(url, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_c))
|
||||
.one(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let rows = journal::Entity::find()
|
||||
.filter(journal::Column::ProjectId.eq(proj.id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap();
|
||||
(proj.id, rows)
|
||||
});
|
||||
let seqs: Vec<i64> = rows.iter().map(|r| r.seq).collect();
|
||||
assert!(!seqs.contains(&1) && !seqs.contains(&2), "old commands pruned: {seqs:?}");
|
||||
assert_eq!(seqs, vec![3], "only the fresh command remains");
|
||||
let _ = pid;
|
||||
|
||||
// The head state is still correct (snapshot at seq 2 + command 3).
|
||||
let session = DatabaseBackend::new();
|
||||
let (_, loaded) = load_project(&session, &project_uri(uri, &uuid));
|
||||
{
|
||||
let l = loaded.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = l
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| l.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
l.graph.get(math_ids[0]).unwrap().core.standard_value("param_a_in", -1),
|
||||
NodeValue::Float(4.5)
|
||||
);
|
||||
assert_eq!(l.graph.node_count(), 12);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Project-manager API surface
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn pg_list_delete_duplicate_rename() {
|
||||
with_pg_uri("list_delete_duplicate_rename", |_url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
// Empty library lists nothing.
|
||||
assert!(backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap().is_empty());
|
||||
|
||||
let alpha = save_named_project(&backend, uri, "Alpha");
|
||||
let beta = save_named_project(&backend, uri, "Beta");
|
||||
|
||||
let list = backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap();
|
||||
assert_eq!(list.len(), 2, "two rows");
|
||||
// Most recently modified first.
|
||||
assert_eq!(list[0].name, "Beta");
|
||||
assert_eq!(list[1].name, "Alpha");
|
||||
assert_eq!(list[0].command_seq, 1);
|
||||
assert_eq!(list[0].schema_ver, oaknode::serializer::CURRENT_VERSION.0 as i32);
|
||||
|
||||
// Rename (library metadata).
|
||||
backend
|
||||
.rename_project(&StorageUri::parse(uri).unwrap(), &alpha, "Alpha Renamed")
|
||||
.unwrap();
|
||||
let list = backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap();
|
||||
assert!(list.iter().any(|p| p.name == "Alpha Renamed"), "{list:?}");
|
||||
assert_eq!(
|
||||
backend
|
||||
.rename_project(&StorageUri::parse(uri).unwrap(), "{missing}", "X")
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
|
||||
// Duplicate: fresh uuid, default "(copy)" name, history copied.
|
||||
let copy = backend
|
||||
.duplicate_project(&StorageUri::parse(uri).unwrap(), &beta, None)
|
||||
.unwrap();
|
||||
assert_ne!(copy.uuid, beta, "fresh uuid");
|
||||
assert_eq!(copy.name, "Beta (copy)");
|
||||
assert_eq!(copy.command_seq, 1);
|
||||
let list = backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap();
|
||||
assert_eq!(list.len(), 3);
|
||||
let renamed_copy = backend
|
||||
.duplicate_project(&StorageUri::parse(uri).unwrap(), &beta, Some("Beta Clone"))
|
||||
.unwrap();
|
||||
assert_eq!(renamed_copy.name, "Beta Clone");
|
||||
assert_eq!(
|
||||
backend
|
||||
.duplicate_project(&StorageUri::parse(uri).unwrap(), "{missing}", None)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
|
||||
// Delete.
|
||||
for uuid in [&alpha, &beta, ©.uuid, &renamed_copy.uuid] {
|
||||
backend
|
||||
.delete_project(&StorageUri::parse(uri).unwrap(), uuid)
|
||||
.unwrap();
|
||||
}
|
||||
assert!(backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap().is_empty());
|
||||
assert_eq!(
|
||||
backend
|
||||
.delete_project(&StorageUri::parse(uri).unwrap(), "{missing}")
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Duplicating a full-feature project copies the whole history: the copy
|
||||
/// loads identically under its own uuid and keeps the undo history.
|
||||
#[test]
|
||||
fn pg_duplicate_preserves_history() {
|
||||
with_pg_uri("duplicate_preserves_history", |_url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = p
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| p.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
p.graph
|
||||
.get_mut(math_ids[0])
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(6.0));
|
||||
}
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
let copy = backend
|
||||
.duplicate_project(&StorageUri::parse(uri).unwrap(), &uuid, None)
|
||||
.unwrap();
|
||||
assert_eq!(copy.command_seq, 2);
|
||||
|
||||
// The copy loads through a fresh session, field-for-field (uuid is
|
||||
// fresh by design).
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, loaded) = load_project(&session, &project_uri(uri, ©.uuid));
|
||||
assert_eq!(loaded_uuid, copy.uuid);
|
||||
{
|
||||
let o = project.lock().unwrap();
|
||||
let l = loaded.lock().unwrap();
|
||||
assert_full_state(&o, &l);
|
||||
}
|
||||
|
||||
// Undo history travels with the copy: load_at(1) on the copy gives
|
||||
// the pre-change state.
|
||||
let session = DatabaseBackend::new();
|
||||
let at1 = load_at(&session, uri, ©.uuid, 1);
|
||||
{
|
||||
let l = at1.lock().unwrap();
|
||||
let math_ids: Vec<NodeId> = l
|
||||
.graph
|
||||
.node_ids()
|
||||
.into_iter()
|
||||
.filter(|id| l.graph.get(*id).unwrap().behavior.type_id() == MATH)
|
||||
.collect();
|
||||
assert_eq!(
|
||||
l.graph.get(math_ids[0]).unwrap().core.standard_value("param_a_in", -1),
|
||||
NodeValue::Float(2.5)
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Manager stats are derived from the node graph, not stored (plan §4).
|
||||
#[test]
|
||||
fn pg_project_stats_derived_from_graph() {
|
||||
with_pg_uri("project_stats_derived_from_graph", |_url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
let stats = backend
|
||||
.project_stats(&StorageUri::parse(uri).unwrap(), &uuid)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
stats,
|
||||
ProjectStats {
|
||||
duration_ms: 6000,
|
||||
track_count: 1,
|
||||
clip_count: 2,
|
||||
footage_count: 2,
|
||||
}
|
||||
);
|
||||
// Same numbers derive_stats yields directly on the live project.
|
||||
let guard = project.lock().unwrap();
|
||||
assert_eq!(derive_stats(&guard), stats);
|
||||
assert_eq!(
|
||||
backend
|
||||
.project_stats(&StorageUri::parse(uri).unwrap(), "{missing}")
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pg_export_and_import_round_trip() {
|
||||
with_pg_uri("export_and_import_round_trip", |_url, uri| {
|
||||
let dir = temp_dir("pg_xi");
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let project = build_full_project();
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(&backend, &project, uri).unwrap();
|
||||
|
||||
// Export: .ove written from an in-memory assembly; the file backend
|
||||
// re-imports it byte-for-byte.
|
||||
let out = dir.join("exported.ove");
|
||||
let file = file_uri(&out);
|
||||
backend
|
||||
.export_to_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
&uuid,
|
||||
&StorageUri::parse(&file).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(out.exists(), ".ove written");
|
||||
let text = std::fs::read_to_string(&out).unwrap();
|
||||
assert!(text.starts_with("<project version=\"1\">"), "{text}");
|
||||
let file_backend = Registry::global()
|
||||
.resolve(&StorageUri::parse(&file).unwrap())
|
||||
.unwrap();
|
||||
let result = file_backend
|
||||
.load(&StorageUri::parse(&file).unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(result.version_info, OAKSTORAGE_OK);
|
||||
let loaded = unsafe { project_arc(&result.project) }.unwrap();
|
||||
release(result.project);
|
||||
{
|
||||
let o = project.lock().unwrap();
|
||||
let l = loaded.lock().unwrap();
|
||||
assert_full_fields(&o, &l);
|
||||
}
|
||||
|
||||
// Import: the .ove lands as a new library row under a fresh uuid;
|
||||
// importing it again yields a distinct row.
|
||||
let imported = backend
|
||||
.import_from_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
&StorageUri::parse(&file).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_ne!(imported, uuid, "import gets a fresh uuid");
|
||||
let imported2 = backend
|
||||
.import_from_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
&StorageUri::parse(&file).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_ne!(imported2, imported, "repeat imports are new rows");
|
||||
let list = backend.list_projects(&StorageUri::parse(uri).unwrap()).unwrap();
|
||||
assert_eq!(list.len(), 3, "original + two imports");
|
||||
let session = DatabaseBackend::new();
|
||||
let (imported_uuid, imported_proj) = load_project(&session, &project_uri(uri, &imported));
|
||||
assert_eq!(imported_uuid, imported);
|
||||
{
|
||||
let l = imported_proj.lock().unwrap();
|
||||
assert_eq!(l.graph.node_count(), 12);
|
||||
assert_eq!(
|
||||
l.settings.get("projectname").cloned(),
|
||||
Some("full-fixture".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
// Error paths: unknown project on export; non-file target; corrupt
|
||||
// file on import.
|
||||
assert_eq!(
|
||||
backend
|
||||
.export_to_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
"{missing}",
|
||||
&StorageUri::parse(&file).unwrap(),
|
||||
)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
assert_eq!(
|
||||
backend
|
||||
.export_to_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
&uuid,
|
||||
&StorageUri::parse("oakdb+sqlite:///tmp/x.db").unwrap(),
|
||||
)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_INVALID
|
||||
);
|
||||
let corrupt = dir.join("corrupt.ove");
|
||||
std::fs::write(&corrupt, "<project><nodes><node></project>").unwrap();
|
||||
assert_eq!(
|
||||
backend
|
||||
.import_from_file(
|
||||
&StorageUri::parse(uri).unwrap(),
|
||||
&StorageUri::parse(&file_uri(&corrupt)).unwrap(),
|
||||
)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_FORMAT
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Project selection (`?project=` vs default)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[test]
|
||||
fn pg_project_selection_via_query() {
|
||||
with_pg_uri("project_selection_via_query", |_url, uri| {
|
||||
let backend = DatabaseBackend::new();
|
||||
|
||||
let a = save_named_project(&backend, uri, "A");
|
||||
let b = save_named_project(&backend, uri, "B");
|
||||
|
||||
// Explicit uuid picks the right row regardless of recency.
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, _) = load_project(&session, &project_uri(uri, &a));
|
||||
assert_eq!(loaded_uuid, a);
|
||||
|
||||
// Default pick = most recently modified (B was written last).
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, loaded) = load_project(&session, uri);
|
||||
assert_eq!(loaded_uuid, b);
|
||||
assert_eq!(
|
||||
loaded.lock().unwrap().settings.get("projectname").cloned(),
|
||||
Some("B".to_string())
|
||||
);
|
||||
|
||||
// Unknown uuid -> E_NOT_FOUND; empty library -> E_NOT_FOUND.
|
||||
let session = DatabaseBackend::new();
|
||||
assert_eq!(
|
||||
session
|
||||
.load(&StorageUri::parse(&project_uri(uri, "{missing}")).unwrap())
|
||||
.err()
|
||||
.unwrap()
|
||||
.code(),
|
||||
OAKSTORAGE_E_NOT_FOUND
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -22,459 +22,33 @@
|
||||
//! journal (import + diff), snapshot + journal replay, undo to any
|
||||
//! point, snapshot pruning and journal retention, the project-manager
|
||||
//! API surface (list/delete/duplicate/rename/import/export) and the
|
||||
//! error/URI matrices. Nothing touches a real library.
|
||||
//! error/URI matrices. Nothing touches a real library. The same
|
||||
//! behaviors run against real PostgreSQL in `database_pg_test.rs`
|
||||
//! (gated on `OAK_TEST_PG_URL`); both suites share the fixtures and
|
||||
//! helpers in `tests/common/mod.rs`.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
mod common;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::QueryOrder;
|
||||
use oakcore_rs::Rational;
|
||||
use oaknode::block::ClipBlockBehavior;
|
||||
use oaknode::footage::FootageBehavior;
|
||||
use oaknode::id::NodeId;
|
||||
use oaknode::keyframe::{Interpolation, Keyframe};
|
||||
use oaknode::node::NodeCore;
|
||||
use oaknode::project::Project;
|
||||
use oaknode::sequence::SequenceBehavior;
|
||||
use oaknode::track::{TrackBehavior, TrackListBehavior, TrackType};
|
||||
use oaknode::value::NodeValue;
|
||||
use oakstorage::backend::StorageBackend;
|
||||
use oakstorage::backends::database::entities::{journal, project, settings, snapshot};
|
||||
use oakstorage::backends::database::{derive_stats, DatabaseBackend, KIND_IMPORT, KIND_REDO, ProjectStats, SNAPSHOT_KEEP};
|
||||
use oakstorage::backends::database::entities::{journal, project, snapshot};
|
||||
use oakstorage::backends::database::{
|
||||
derive_stats, DatabaseBackend, KIND_IMPORT, KIND_REDO, ProjectStats, SNAPSHOT_KEEP,
|
||||
};
|
||||
use oakstorage::error::{
|
||||
OAKSTORAGE_E_FORMAT, OAKSTORAGE_E_INVALID, OAKSTORAGE_E_IO, OAKSTORAGE_E_NO_BACKEND,
|
||||
OAKSTORAGE_E_NOT_FOUND, OAKSTORAGE_OK,
|
||||
};
|
||||
use oakstorage::handle::CHandle;
|
||||
use oakstorage::nodeutil::{make_project_owned, project_arc};
|
||||
use oakstorage::nodeutil::project_arc;
|
||||
use oakstorage::registry::Registry;
|
||||
use oakstorage::uri::StorageUri;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// A fresh, unique temp directory for one test.
|
||||
fn temp_dir(tag: &str) -> PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("oakstorage_db_{}_{}", std::process::id(), tag));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
/// `oakdb+sqlite:///…` URI for a database file.
|
||||
fn db_uri(path: &Path) -> String {
|
||||
format!("oakdb+sqlite://{}", path.display())
|
||||
}
|
||||
|
||||
/// `oakdb+sqlite:///…?project=<uuid>` URI selecting one library row.
|
||||
fn project_uri(db: &str, uuid: &str) -> String {
|
||||
format!("{db}?project={uuid}")
|
||||
}
|
||||
|
||||
/// `file://…` URI for a plain file.
|
||||
fn file_uri(path: &Path) -> String {
|
||||
format!("file://{}", path.display())
|
||||
}
|
||||
|
||||
/// Release an owned handle (refcount 1).
|
||||
fn release(h: CHandle) {
|
||||
if let Some(release) = h.release {
|
||||
unsafe { release(h.ctx) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Save an `Arc<Mutex<Project>>` through the database backend.
|
||||
fn save_project(
|
||||
backend: &DatabaseBackend,
|
||||
project: &Arc<Mutex<Project>>,
|
||||
uri: &str,
|
||||
) -> oakstorage::error::Result<()> {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let handle = make_project_owned(project.clone());
|
||||
let result = backend.save(handle, &parsed, 0);
|
||||
release(handle);
|
||||
result
|
||||
}
|
||||
|
||||
/// Load a project through a *new* database backend session (a fresh
|
||||
/// connection pool = a fresh session), returning `(uuid, loaded)`.
|
||||
fn load_project(backend: &DatabaseBackend, uri: &str) -> (String, Arc<Mutex<Project>>) {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let result = backend.load(&parsed).unwrap();
|
||||
assert_eq!(result.version_info, OAKSTORAGE_OK);
|
||||
let handle = result.project;
|
||||
let loaded = unsafe { project_arc(&handle) }.unwrap();
|
||||
let uuid = loaded.lock().unwrap().uuid.clone();
|
||||
release(handle);
|
||||
(uuid, loaded)
|
||||
}
|
||||
|
||||
/// Load the state at `seq` (undo to any point).
|
||||
fn load_at(backend: &DatabaseBackend, uri: &str, uuid: &str, seq: i64) -> Arc<Mutex<Project>> {
|
||||
let parsed = StorageUri::parse(uri).unwrap();
|
||||
let handle = backend.load_at(&parsed, uuid, seq).unwrap();
|
||||
let loaded = unsafe { project_arc(&handle) }.unwrap();
|
||||
release(handle);
|
||||
loaded
|
||||
}
|
||||
|
||||
fn r_to_f(r: Rational) -> f64 {
|
||||
r.numerator() as f64 / r.denominator() as f64
|
||||
}
|
||||
|
||||
fn assert_close(a: f64, b: f64) {
|
||||
assert!((a - b).abs() < 1e-6, "expected {a} close to {b}");
|
||||
}
|
||||
|
||||
/// Open a raw sea-orm connection to the database file (for inspecting
|
||||
/// and injecting rows behind the backend's back) and drive one future
|
||||
/// against it on a private current-thread runtime. The connection is
|
||||
/// handed to `f` by value (a clone of the pool).
|
||||
fn inspect_db<R, Fut>(path: &Path, f: impl FnOnce(sea_orm::DatabaseConnection) -> Fut) -> R
|
||||
where
|
||||
Fut: std::future::Future<Output = R>,
|
||||
{
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.unwrap();
|
||||
rt.block_on(async {
|
||||
let options = sea_orm::sqlx::sqlite::SqliteConnectOptions::new()
|
||||
.filename(path)
|
||||
.create_if_missing(true)
|
||||
.journal_mode(sea_orm::sqlx::sqlite::SqliteJournalMode::Wal)
|
||||
.busy_timeout(Duration::from_secs(5))
|
||||
.foreign_keys(true);
|
||||
let pool = sea_orm::sqlx::sqlite::SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.unwrap();
|
||||
f(sea_orm::DatabaseConnection::from(pool)).await
|
||||
})
|
||||
}
|
||||
|
||||
/// The settings mirror rows of a project.
|
||||
fn settings_rows(path: &Path, project_id: i64) -> Vec<(String, String)> {
|
||||
inspect_db(path, |conn| async move {
|
||||
settings::Entity::find()
|
||||
.filter(settings::Column::ProjectId.eq(project_id))
|
||||
.all(&conn)
|
||||
.await
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|s| (s.key, s.value))
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// The project uuid.
|
||||
fn uuid_of(project: &Arc<Mutex<Project>>) -> String {
|
||||
project.lock().unwrap().uuid.clone()
|
||||
}
|
||||
|
||||
/// Read a config value for the duration of a test (the config store is
|
||||
/// process-global, so restore it afterwards).
|
||||
fn with_config(group: &str, key: &str, value: i32, f: impl FnOnce()) {
|
||||
let store = oakcommon::configstore::ConfigStore::instance();
|
||||
let before = store.get_int(Some(group), key, 0);
|
||||
store.set_int(Some(group), key, value);
|
||||
f();
|
||||
store.set_int(Some(group), key, before);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Full-feature fixture (footage / sequence / track / clip / effect /
|
||||
// keyframes) — the union of the ove round-trip and timeline fixtures.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const MATH: &str = "org.olivevideoeditor.Olive.math";
|
||||
|
||||
/// Build the round-trip fixture: root folder + two math nodes with
|
||||
/// values/keyframes/label/color/link/connection + a sequence "My Seq"
|
||||
/// with one video track carrying two clips (footage /a/b.mp4, /a/c.mp4).
|
||||
fn build_full_project() -> Arc<Mutex<Project>> {
|
||||
let project = Project::new();
|
||||
let mut p = project.lock().unwrap();
|
||||
p.initialize().unwrap();
|
||||
|
||||
let (core, behavior) = (oaknode::factory::Factory::global().find(MATH).unwrap().create)();
|
||||
let a = p.graph.add_node(core, behavior);
|
||||
{
|
||||
let e = p.graph.get_mut(a).unwrap();
|
||||
e.core.label = "Math A".to_string();
|
||||
e.core.override_color = 2;
|
||||
e.core.set_standard_value("param_a_in", -1, NodeValue::Float(2.5));
|
||||
e.core
|
||||
.keyframe_track_mut("param_a_in", -1)
|
||||
.set_key(Keyframe {
|
||||
time: Rational::new(0, 1),
|
||||
value: NodeValue::Float(1.0),
|
||||
interpolation: Interpolation::Linear,
|
||||
bezier_in: (0.0, 0.0),
|
||||
bezier_out: (0.0, 0.0),
|
||||
});
|
||||
e.core
|
||||
.keyframe_track_mut("param_a_in", -1)
|
||||
.set_key(Keyframe {
|
||||
time: Rational::new(1, 1),
|
||||
value: NodeValue::Float(3.0),
|
||||
interpolation: Interpolation::Bezier,
|
||||
bezier_in: (0.1, 0.2),
|
||||
bezier_out: (0.3, 0.4),
|
||||
});
|
||||
}
|
||||
let (core, behavior) = (oaknode::factory::Factory::global().find(MATH).unwrap().create)();
|
||||
let b = p.graph.add_node(core, behavior);
|
||||
p.graph
|
||||
.get_mut(b)
|
||||
.unwrap()
|
||||
.core
|
||||
.set_standard_value("param_a_in", -1, NodeValue::Float(4.0));
|
||||
p.graph.connect(a, b, "param_b_in", -1).unwrap();
|
||||
p.graph.link(a, b);
|
||||
|
||||
// Timeline: sequence + video track + two clips with footage.
|
||||
let (seq_id, lists) = oakstorage::nodeutil::create_sequence(&mut p.graph);
|
||||
p.graph.get_mut(seq_id).unwrap().core.label = "My Seq".to_string();
|
||||
|
||||
let mut tb = TrackBehavior::new(TrackType::Video);
|
||||
tb.track_list = Some(lists[0]);
|
||||
let track_id = p.graph.add_node(NodeCore::new(), Box::new(tb));
|
||||
|
||||
let foot1 = p
|
||||
.graph
|
||||
.add_node(NodeCore::new(), Box::new(FootageBehavior::new("/a/b.mp4")));
|
||||
let clip1 = {
|
||||
let (core, mut behavior) = oaknode::block::clip_create();
|
||||
let clip = behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
clip.core.range = oakcore_rs::TimeRange::new(Rational::new(0, 1), Rational::new(100, 25));
|
||||
clip.core.media_in = Rational::new(0, 1);
|
||||
clip.core.track = Some(track_id);
|
||||
clip.footage = Some(foot1);
|
||||
p.graph.add_node(core, behavior)
|
||||
};
|
||||
|
||||
let foot2 = p
|
||||
.graph
|
||||
.add_node(NodeCore::new(), Box::new(FootageBehavior::new("/a/c.mp4")));
|
||||
let clip2 = {
|
||||
let (core, mut behavior) = oaknode::block::clip_create();
|
||||
let clip = behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
clip.core.range =
|
||||
oakcore_rs::TimeRange::new(Rational::new(100, 25), Rational::new(150, 25));
|
||||
clip.core.media_in = Rational::new(10, 25);
|
||||
clip.core.track = Some(track_id);
|
||||
clip.footage = Some(foot2);
|
||||
p.graph.add_node(core, behavior)
|
||||
};
|
||||
|
||||
if let Some(entry) = p.graph.get_mut(track_id) {
|
||||
entry
|
||||
.behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<TrackBehavior>())
|
||||
.unwrap()
|
||||
.blocks = vec![clip1, clip2];
|
||||
}
|
||||
if let Some(entry) = p.graph.get_mut(lists[0]) {
|
||||
entry
|
||||
.behavior
|
||||
.as_any_mut()
|
||||
.and_then(|a| a.downcast_mut::<TrackListBehavior>())
|
||||
.unwrap()
|
||||
.tracks = vec![track_id];
|
||||
}
|
||||
|
||||
p.settings
|
||||
.insert("projectname".to_string(), "full-fixture".to_string());
|
||||
drop(p);
|
||||
project
|
||||
}
|
||||
|
||||
/// Field-by-field comparison of the full fixture after a round-trip
|
||||
/// (uuid included).
|
||||
fn assert_full_fields(orig: &Project, loaded: &Project) {
|
||||
assert_eq!(loaded.uuid, orig.uuid, "uuid");
|
||||
assert_full_state(orig, loaded);
|
||||
}
|
||||
|
||||
/// Field-by-field comparison that ignores the uuid (a duplicated row
|
||||
/// carries a fresh uuid by design).
|
||||
fn assert_full_state(orig: &Project, loaded: &Project) {
|
||||
assert_eq!(loaded.settings, orig.settings, "settings");
|
||||
|
||||
// Node set: count, type order (slot order preserved by the writer).
|
||||
let o_ids = orig.graph.node_ids();
|
||||
let l_ids = loaded.graph.node_ids();
|
||||
assert_eq!(l_ids.len(), o_ids.len(), "node count");
|
||||
let o_types: Vec<&str> = o_ids
|
||||
.iter()
|
||||
.map(|id| orig.graph.get(*id).unwrap().behavior.type_id())
|
||||
.collect();
|
||||
let l_types: Vec<&str> = l_ids
|
||||
.iter()
|
||||
.map(|id| loaded.graph.get(*id).unwrap().behavior.type_id())
|
||||
.collect();
|
||||
assert_eq!(l_types, o_types, "node types");
|
||||
|
||||
// Math A: label, color, value, keyframes.
|
||||
let (a_o, a_l) = (o_ids[1], l_ids[1]);
|
||||
assert_eq!(
|
||||
loaded.graph.get(a_l).unwrap().core.label,
|
||||
orig.graph.get(a_o).unwrap().core.label,
|
||||
"label"
|
||||
);
|
||||
assert_eq!(
|
||||
loaded.graph.get(a_l).unwrap().core.override_color,
|
||||
orig.graph.get(a_o).unwrap().core.override_color,
|
||||
"color"
|
||||
);
|
||||
assert_eq!(
|
||||
loaded
|
||||
.graph
|
||||
.get(a_l)
|
||||
.unwrap()
|
||||
.core
|
||||
.standard_value("param_a_in", -1),
|
||||
orig.graph
|
||||
.get(a_o)
|
||||
.unwrap()
|
||||
.core
|
||||
.standard_value("param_a_in", -1),
|
||||
"value a"
|
||||
);
|
||||
let keys_o = orig
|
||||
.graph
|
||||
.get(a_o)
|
||||
.unwrap()
|
||||
.core
|
||||
.keyframe_track("param_a_in", -1)
|
||||
.unwrap()
|
||||
.keys()
|
||||
.to_vec();
|
||||
let keys_l = loaded
|
||||
.graph
|
||||
.get(a_l)
|
||||
.unwrap()
|
||||
.core
|
||||
.keyframe_track("param_a_in", -1)
|
||||
.unwrap()
|
||||
.keys()
|
||||
.to_vec();
|
||||
assert_eq!(keys_l.len(), keys_o.len(), "keyframe count");
|
||||
for (ko, kl) in keys_o.iter().zip(&keys_l) {
|
||||
assert_eq!(kl.time, ko.time, "key time");
|
||||
assert_eq!(kl.value.to_double(), ko.value.to_double(), "key value");
|
||||
assert_eq!(kl.interpolation, ko.interpolation, "key interpolation");
|
||||
assert_eq!(kl.bezier_in, ko.bezier_in, "key bezier in");
|
||||
assert_eq!(kl.bezier_out, ko.bezier_out, "key bezier out");
|
||||
}
|
||||
|
||||
// Connection a -> b.param_b_in and the link.
|
||||
assert_eq!(
|
||||
loaded.graph.connected_output(l_ids[2], "param_b_in", -1),
|
||||
Some(a_l),
|
||||
"connection"
|
||||
);
|
||||
assert!(loaded.graph.are_linked(a_l, l_ids[2]), "link");
|
||||
|
||||
// Timeline: sequence, track lists, track, clips, footage.
|
||||
let mut seq: Option<(NodeId, Vec<NodeId>)> = None;
|
||||
for id in loaded.graph.node_ids() {
|
||||
let entry = loaded.graph.get(id).unwrap();
|
||||
if entry.behavior.type_id() == "org.olivevideoeditor.Olive.sequence" {
|
||||
let s = entry
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<SequenceBehavior>())
|
||||
.unwrap();
|
||||
seq = Some((id, s.track_lists.clone()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
let (_seq_id, lists) = seq.expect("sequence present");
|
||||
assert_eq!(lists.len(), 3, "video/audio/subtitle lists");
|
||||
let list = loaded
|
||||
.graph
|
||||
.get(lists[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<TrackListBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(list.kind, TrackType::Video);
|
||||
assert_eq!(list.array_base, 0);
|
||||
assert_eq!(list.tracks.len(), 1);
|
||||
let track = loaded
|
||||
.graph
|
||||
.get(list.tracks[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<TrackBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(track.kind, TrackType::Video);
|
||||
assert_eq!(track.blocks.len(), 2);
|
||||
let c1 = loaded
|
||||
.graph
|
||||
.get(track.blocks[0])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
assert_close(r_to_f(c1.core.length()), 4.0);
|
||||
assert_close(r_to_f(c1.core.media_in), 0.0);
|
||||
let f1 = loaded
|
||||
.graph
|
||||
.get(c1.footage.unwrap())
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<FootageBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(f1.filename, "/a/b.mp4");
|
||||
let c2 = loaded
|
||||
.graph
|
||||
.get(track.blocks[1])
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<ClipBlockBehavior>())
|
||||
.unwrap();
|
||||
assert_close(r_to_f(c2.core.length()), 2.0);
|
||||
assert_close(r_to_f(c2.core.media_in), 0.4);
|
||||
let f2 = loaded
|
||||
.graph
|
||||
.get(c2.footage.unwrap())
|
||||
.unwrap()
|
||||
.behavior
|
||||
.as_any()
|
||||
.and_then(|a| a.downcast_ref::<FootageBehavior>())
|
||||
.unwrap();
|
||||
assert_eq!(f2.filename, "/a/c.mp4");
|
||||
}
|
||||
|
||||
/// Build a small project with a `projectname` setting (fast saves).
|
||||
fn build_named_project(name: &str) -> Arc<Mutex<Project>> {
|
||||
let project = Project::new();
|
||||
{
|
||||
let mut p = project.lock().unwrap();
|
||||
p.initialize().unwrap();
|
||||
p.settings.insert("projectname".to_string(), name.to_string());
|
||||
}
|
||||
project
|
||||
}
|
||||
use common::*;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Round-trip through a fresh session
|
||||
@@ -508,7 +82,7 @@ fn roundtrip_field_by_field() {
|
||||
assert_eq!(loaded_uuid, uuid);
|
||||
|
||||
// The settings mirror carries the current keys.
|
||||
let row = inspect_db(&db, |conn| async move {
|
||||
let row = inspect_sqlite(&db, |conn| async move {
|
||||
project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -566,7 +140,7 @@ fn first_save_is_an_import_command() {
|
||||
let node_count = project.lock().unwrap().graph.node_count();
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
|
||||
let rows = inspect_db(&db, |conn| async move {
|
||||
let rows = inspect_sqlite(&db, |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -637,7 +211,7 @@ fn later_saves_are_diffs() {
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
|
||||
let uuid_q = uuid.clone();
|
||||
let rows = inspect_db(&db, move |conn| async move {
|
||||
let rows = inspect_sqlite(&db, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_q))
|
||||
.one(&conn)
|
||||
@@ -693,7 +267,7 @@ fn no_op_save_is_a_touch_only() {
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
|
||||
let (head, count) = inspect_db(&db, |conn| async move {
|
||||
let (head, count) = inspect_sqlite(&db, |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -751,7 +325,7 @@ fn snapshot_and_journal_replay() {
|
||||
|
||||
// Both saves produced snapshots (head seq 2).
|
||||
let uuid_q = uuid.clone();
|
||||
let snaps = inspect_db(&db, move |conn| async move {
|
||||
let snaps = inspect_sqlite(&db, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_q))
|
||||
.one(&conn)
|
||||
@@ -778,7 +352,7 @@ fn snapshot_and_journal_replay() {
|
||||
// Destroy the snapshots: replay degrades to empty base + full
|
||||
// journal and still reconstructs the head.
|
||||
let uuid_q = uuid.clone();
|
||||
inspect_db(&db, move |conn| async move {
|
||||
inspect_sqlite(&db, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_q))
|
||||
.one(&conn)
|
||||
@@ -944,7 +518,7 @@ fn snapshot_pruning_keeps_three() {
|
||||
}
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
}
|
||||
let seqs = inspect_db(&db, |conn| async move {
|
||||
let seqs = inspect_sqlite(&db, |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid))
|
||||
.one(&conn)
|
||||
@@ -1000,7 +574,7 @@ fn journal_retention_truncation() {
|
||||
|
||||
// Backdate every journal row two days, then save a third command
|
||||
// with a 1-day retention window.
|
||||
inspect_db(&db, |conn| async move {
|
||||
inspect_sqlite(&db, |conn| async move {
|
||||
let old = chrono::Utc::now().naive_utc() - chrono::Duration::days(2);
|
||||
journal::Entity::update_many()
|
||||
.col_expr(journal::Column::At, sea_orm::sea_query::Expr::value(old))
|
||||
@@ -1028,7 +602,7 @@ fn journal_retention_truncation() {
|
||||
|
||||
// Only the third command's rows survive.
|
||||
let uuid_q = uuid.clone();
|
||||
let (pid, rows) = inspect_db(&db, move |conn| async move {
|
||||
let (pid, rows) = inspect_sqlite(&db, move |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&uuid_q))
|
||||
.one(&conn)
|
||||
@@ -1070,13 +644,6 @@ fn journal_retention_truncation() {
|
||||
// Project-manager API surface
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn save_named_project(backend: &DatabaseBackend, uri: &str, name: &str) -> String {
|
||||
let project = build_named_project(name);
|
||||
let uuid = uuid_of(&project);
|
||||
save_project(backend, &project, uri).unwrap();
|
||||
uuid
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_delete_duplicate_rename() {
|
||||
let dir = temp_dir("mgr");
|
||||
@@ -1404,8 +971,13 @@ fn project_selection_via_query() {
|
||||
// Error paths, locking, URI matrix
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// PG targets are now live: malformed connection strings fail cleanly at
|
||||
/// parse time (E_INVALID, no network touched) and an unreachable server
|
||||
/// fails cleanly at connect (E_IO, no panic). Both run without a real
|
||||
/// PostgreSQL server — the connection-refused case uses port 1 on
|
||||
/// loopback, which nothing listens on.
|
||||
#[test]
|
||||
fn pg_target_rejected_until_d3() {
|
||||
fn pg_invalid_and_unreachable_targets_error_cleanly() {
|
||||
let dir = temp_dir("pg");
|
||||
let db = dir.join("lib.db");
|
||||
let uri = db_uri(&db);
|
||||
@@ -1414,27 +986,45 @@ fn pg_target_rejected_until_d3() {
|
||||
save_project(&backend, &project, &uri).unwrap();
|
||||
let uuid = uuid_of(&project);
|
||||
|
||||
let pg = "oakdb+pg://user:pass@host:5432/db";
|
||||
// Invalid connection strings -> E_INVALID at parse (no connection).
|
||||
for bad in ["oakdb+pg://", "oakdb+pg://?project={x}", "oakdb+pg://user@"] {
|
||||
let err = backend
|
||||
.list_projects(&StorageUri::parse(bad).unwrap())
|
||||
.err()
|
||||
.unwrap();
|
||||
assert_eq!(err.code(), OAKSTORAGE_E_INVALID, "{bad}");
|
||||
}
|
||||
|
||||
// Unreachable server (port 1 on loopback) -> E_IO at connect, and the
|
||||
// error is clean (no panic) across every operation. The connect probe
|
||||
// fails on the refused TCP connection without waiting out the pool's
|
||||
// acquire timeout.
|
||||
let dead = "oakdb+pg://user@127.0.0.1:1/db";
|
||||
for call in [
|
||||
backend.load(&StorageUri::parse(pg).unwrap()).map(|_| ()),
|
||||
backend.save(CHandle::null(), &StorageUri::parse(pg).unwrap(), 0),
|
||||
backend.list_projects(&StorageUri::parse(pg).unwrap()).map(|_| ()),
|
||||
backend.delete_project(&StorageUri::parse(pg).unwrap(), &uuid),
|
||||
backend.rename_project(&StorageUri::parse(pg).unwrap(), &uuid, "X"),
|
||||
backend.load(&StorageUri::parse(dead).unwrap()).map(|_| ()),
|
||||
save_project(&backend, &project, dead),
|
||||
backend.list_projects(&StorageUri::parse(dead).unwrap()).map(|_| ()),
|
||||
backend.delete_project(&StorageUri::parse(dead).unwrap(), &uuid),
|
||||
backend.rename_project(&StorageUri::parse(dead).unwrap(), &uuid, "X"),
|
||||
backend
|
||||
.duplicate_project(&StorageUri::parse(pg).unwrap(), &uuid, None)
|
||||
.duplicate_project(&StorageUri::parse(dead).unwrap(), &uuid, None)
|
||||
.map(|_| ()),
|
||||
backend
|
||||
.export_to_file(
|
||||
&StorageUri::parse(pg).unwrap(),
|
||||
&StorageUri::parse(dead).unwrap(),
|
||||
&uuid,
|
||||
&StorageUri::parse("file:///tmp/x.ove").unwrap(),
|
||||
),
|
||||
backend.snapshot(&StorageUri::parse(pg).unwrap(), &uuid),
|
||||
backend.load_at(&StorageUri::parse(pg).unwrap(), &uuid, 1).map(|_| ()),
|
||||
backend.snapshot(&StorageUri::parse(dead).unwrap(), &uuid),
|
||||
backend.load_at(&StorageUri::parse(dead).unwrap(), &uuid, 1).map(|_| ()),
|
||||
] {
|
||||
assert_eq!(call.err().unwrap().code(), OAKSTORAGE_E_NO_BACKEND, "pg target");
|
||||
assert_eq!(call.err().unwrap().code(), OAKSTORAGE_E_IO, "dead pg target");
|
||||
}
|
||||
|
||||
// The sqlite library next to it is untouched by all of the above.
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, _) = load_project(&session, &project_uri(&uri, &uuid));
|
||||
assert_eq!(loaded_uuid, uuid);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1606,7 +1196,7 @@ fn concurrent_writers_are_serialized() {
|
||||
let session = DatabaseBackend::new();
|
||||
let (loaded_uuid, _) = load_project(&session, &project_uri(&uri, &same));
|
||||
assert_eq!(loaded_uuid, same, "library consistent after races");
|
||||
let (head, seqs) = inspect_db(&db, |conn| async move {
|
||||
let (head, seqs) = inspect_sqlite(&db, |conn| async move {
|
||||
let proj = project::Entity::find()
|
||||
.filter(project::Column::Uuid.eq(&same))
|
||||
.one(&conn)
|
||||
|
||||
Reference in New Issue
Block a user