Skip to main content

agentmux_srv_core/migrations/
m0052_drone_access.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The table behind who may fire a drone
5//! (`docs/specs/SPEC_DRONE_REAL_WORLD_JOBS_2026_11_11.md` ยง6.5).
6//!
7//! `db_drone_access(drone_id, agent, kind, at, by)`: `owner` is the agent that created the drone, `granted`
8//! an agent a person (or the owner) gave it to. An agent fires a drone it owns or was given at once; any other
9//! drone goes through the sensitive-operation countdown. A drone that was there before this migration has no
10//! row, so no agent owns it and every agent's fire of it takes the countdown. There is **no foreign key** to
11//! `db_drone_definitions`, for the reason `m0049` has none (a definition and its runs can live in different stores).
12//!
13//! The table belongs beside the definitions, in the shared store, and the per-channel store is created too for the
14//! fallback `id_store` of an install whose shared backfill has not run. Both are `IF NOT EXISTS`, so a second run
15//! changes nothing.
16
17use rusqlite::Connection;
18
19use super::{Migration, MigrationContext, MigrationError, MigrationScope};
20
21pub struct M0052DroneAccess;
22
23/// The table and its primary key; the same DDL is in the store schemas for a fresh store.
24const CREATE: &str = "CREATE TABLE IF NOT EXISTS db_drone_access (
25    drone_id TEXT NOT NULL,
26    agent    TEXT NOT NULL,
27    kind     TEXT NOT NULL,
28    set_at   INTEGER NOT NULL,
29    set_by   TEXT NOT NULL,
30    PRIMARY KEY (drone_id, agent, kind)
31);";
32
33fn create(conn: &Connection) -> Result<(), String> {
34    conn.execute_batch(CREATE).map_err(|e| format!("db_drone_access: {e}"))
35}
36
37impl Migration for M0052DroneAccess {
38    fn id(&self) -> &'static str {
39        "0052_drone_access"
40    }
41
42    fn scope(&self) -> MigrationScope {
43        MigrationScope::Channel
44    }
45
46    fn description(&self) -> &'static str {
47        "Create db_drone_access (who owns a drone and which agents were given it, no definition FK)"
48    }
49
50    fn up(&self, ctx: &MigrationContext) -> Result<(), MigrationError> {
51        if ctx.shared_store_path.exists() {
52            let conn = Connection::open(&ctx.shared_store_path)
53                .map_err(|e| MigrationError(format!("drone_access: open shared store: {e}")))?;
54            create(&conn).map_err(MigrationError)?;
55        }
56        if ctx.channel_store_path.exists() {
57            let conn = Connection::open(&ctx.channel_store_path)
58                .map_err(|e| MigrationError(format!("drone_access: open channel store: {e}")))?;
59            create(&conn).map_err(MigrationError)?;
60        }
61        Ok(())
62    }
63}
64
65#[cfg(test)]
66#[path = "m0052_drone_access_tests.rs"]
67mod tests;