agentmux_srv_core/migrations/
m0052_drone_access.rs1use rusqlite::Connection;
18
19use super::{Migration, MigrationContext, MigrationError, MigrationScope};
20
21pub struct M0052DroneAccess;
22
23const 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;