ctb_storage/migrations/
m2026_07_02_1_add_user_subscription.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 user subscription and billing columns.
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_07_02_1_add_user_subscription";
28pub const DESCRIPTION: &str =
29    "Add subscription_expiry and token_quota columns to users table";
30pub const UP_SQL: Option<&str> = None;
31
32pub async fn run_rust_migration(conn: &Connection) -> Result<()> {
33    // Check if subscription_expiry already exists
34    let has_subscription = crate::migrations::check_column_exists(
35        conn,
36        "users",
37        "subscription_expiry",
38    )
39    .await?;
40    if !has_subscription {
41        conn.execute(
42            "ALTER TABLE users ADD COLUMN subscription_expiry INTEGER",
43            (),
44        )
45        .await?;
46    }
47
48    // Check if token_quota already exists
49    let has_quota =
50        crate::migrations::check_column_exists(conn, "users", "token_quota")
51            .await?;
52    if !has_quota {
53        conn.execute("ALTER TABLE users ADD COLUMN token_quota INTEGER", ())
54            .await?;
55    }
56
57    Ok(())
58}