ctb_storage/migrations/
m2026_06_30_1_create_users.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 creating the initial users and tenancy tables.
21
22use crate::migrations::DbSchemaType;
23use crate::utilities::Result;
24use turso::Connection;
25
26pub const DB_TYPE: DbSchemaType = DbSchemaType::Users;
27pub const NAME: &str = "2026_06_30_1_create_users";
28pub const DESCRIPTION: &str = "Create initial users database tables";
29pub const UP_SQL: Option<&str> = None;
30
31pub async fn run_rust_migration(conn: &Connection) -> Result<()> {
32    let sqls = [
33        "CREATE TABLE IF NOT EXISTS users_ids (key TEXT PRIMARY KEY, value INTEGER)",
34        "CREATE TABLE IF NOT EXISTS users_ids_rev (key INTEGER PRIMARY KEY, value TEXT)",
35        "CREATE TABLE IF NOT EXISTS users_uuids (key INTEGER PRIMARY KEY, value BLOB)",
36        "CREATE TABLE IF NOT EXISTS users_auth (key INTEGER PRIMARY KEY, value TEXT)",
37        "CREATE TABLE IF NOT EXISTS users_display_names (key INTEGER PRIMARY KEY, value BLOB)",
38        "CREATE TABLE IF NOT EXISTS users_pictures (key INTEGER PRIMARY KEY, value BLOB)",
39        "CREATE TABLE IF NOT EXISTS users_key_encryption_key_params (key INTEGER PRIMARY KEY, value BLOB)",
40        "CREATE TABLE IF NOT EXISTS users_wrapped_dek (key INTEGER PRIMARY KEY, value BLOB)",
41        "CREATE TABLE IF NOT EXISTS users_pubkeys (key INTEGER PRIMARY KEY, value BLOB)",
42        "CREATE TABLE IF NOT EXISTS users_metadata (key TEXT PRIMARY KEY, value INTEGER)",
43        "CREATE TABLE IF NOT EXISTS sync_local_id_ranges (key INTEGER PRIMARY KEY, value TEXT)",
44    ];
45    for sql in sqls {
46        conn.execute(sql, ()).await?;
47    }
48    Ok(())
49}