ctb_storage/migrations/m2026_08_06_1_add_node_timestamp.rs
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Database migration adding timestamp columns to graph nodes.
21
22use crate::migrations::DbSchemaType;
23use crate::utilities::Result;
24use turso::Connection;
25
26pub const DB_TYPE: DbSchemaType = DbSchemaType::Nodes;
27pub const NAME: &str = "2026_08_06_1_add_node_timestamp";
28pub const DESCRIPTION: &str =
29 "Add high-resolution timestamp column to nodes table";
30pub const UP_SQL: Option<&str> =
31 Some("ALTER TABLE nodes ADD COLUMN timestamp BLOB");
32
33pub async fn run_rust_migration(conn: &Connection) -> Result<()> {
34 let now = std::time::SystemTime::now()
35 .duration_since(std::time::UNIX_EPOCH)
36 // Reason for fallback: system clock time prior to UNIX epoch defaults duration to 0 duration
37 .unwrap_or_default()
38 .as_micros();
39 let now_blob = now.to_be_bytes().to_vec();
40 conn.execute(
41 "UPDATE nodes SET timestamp = ?1 WHERE timestamp IS NULL",
42 (now_blob,),
43 )
44 .await?;
45 Ok(())
46}