Skip to main content

agentmux_srv_core/migrations/
m0049_drone_versions.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The table behind graph versions
5//! (`docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md` ยง3.3, "Graph versions").
6//!
7//! Saving a drone records `db_drone_versions(drone_id, version, graph, hash, saved_at, saved_by)`,
8//! keyed by `(drone_id, version)`; a job pins `latest` or a number. There is **no foreign key**
9//! to `db_drone_definitions`: a drone's definition and its runs can live in different stores
10//! (`m0038`), so a version of a drone that is only in another store must still save.
11//!
12//! The table belongs beside the definitions, in the shared store, and the per-channel store is
13//! created too for the fallback `id_store` of an install whose shared backfill has not run
14//! (`pick_id_store`). Both are `IF NOT EXISTS`, so a second run changes nothing.
15
16use rusqlite::Connection;
17
18use super::{Migration, MigrationContext, MigrationError, MigrationScope};
19
20pub struct M0049DroneVersions;
21
22/// The table and its primary key; the same DDL is in the store schemas for a fresh store.
23const CREATE: &str = "\
24CREATE TABLE IF NOT EXISTS db_drone_versions (
25    drone_id TEXT NOT NULL,
26    version  INTEGER NOT NULL,
27    graph    TEXT NOT NULL,
28    hash     TEXT NOT NULL,
29    saved_at INTEGER NOT NULL,
30    saved_by TEXT NOT NULL,
31    PRIMARY KEY (drone_id, version)
32);";
33
34fn create(conn: &Connection) -> Result<(), String> {
35    conn.execute_batch(CREATE).map_err(|e| format!("db_drone_versions: {e}"))
36}
37
38impl Migration for M0049DroneVersions {
39    fn id(&self) -> &'static str {
40        "0049_drone_versions"
41    }
42
43    fn scope(&self) -> MigrationScope {
44        MigrationScope::Channel
45    }
46
47    fn description(&self) -> &'static str {
48        "Create db_drone_versions (a numbered graph per drone, no definition FK)"
49    }
50
51    fn up(&self, ctx: &MigrationContext) -> Result<(), MigrationError> {
52        if ctx.shared_store_path.exists() {
53            let conn = Connection::open(&ctx.shared_store_path)
54                .map_err(|e| MigrationError(format!("drone_versions: open shared store: {e}")))?;
55            create(&conn).map_err(MigrationError)?;
56        }
57        if ctx.channel_store_path.exists() {
58            let conn = Connection::open(&ctx.channel_store_path)
59                .map_err(|e| MigrationError(format!("drone_versions: open channel store: {e}")))?;
60            create(&conn).map_err(MigrationError)?;
61        }
62        Ok(())
63    }
64}
65
66#[cfg(test)]
67#[path = "m0049_drone_versions_tests.rs"]
68mod tests;