appstruct-schema 0.2.0

Database schema contracts and deterministic SQL snapshots for AppStruct
Documentation
use appstruct_ir::{AppIr, OnDeleteIr};

use super::{ColumnSchema, DatabaseType, ForeignKeySchema, TableSchema};

mod capture;
mod saved_views;

pub(super) fn tables() -> Vec<TableSchema> {
    vec![
        accounts(),
        sessions(),
        password_resets(),
        email_verifications(),
        oauth_accounts(),
        api_tokens(),
        login_attempts(),
        saved_views::table_schema(),
        capture::table_schema(),
    ]
}

fn accounts() -> TableSchema {
    table(
        "accounts",
        vec![
            column(
                "accounts.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                true,
            ),
            column(
                "accounts.password_hash",
                "password_hash",
                DatabaseType::Text,
                false,
                false,
            ),
            column("accounts.roles", "roles", DatabaseType::Json, false, false),
            nullable_datetime("accounts.email_verified_at", "email_verified_at"),
            column(
                "accounts.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn sessions() -> TableSchema {
    table(
        "sessions",
        vec![
            column(
                "sessions.token_hash",
                "token_hash",
                DatabaseType::Text,
                false,
                true,
            ),
            column(
                "sessions.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                false,
            ),
            column(
                "sessions.csrf_hash",
                "csrf_hash",
                DatabaseType::Text,
                false,
                false,
            ),
            column(
                "sessions.expires_at",
                "expires_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
            column(
                "sessions.revoked_at",
                "revoked_at",
                DatabaseType::Datetime,
                true,
                false,
            ),
            column(
                "sessions.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn password_resets() -> TableSchema {
    table(
        "password_resets",
        vec![
            column(
                "password_resets.token_hash",
                "token_hash",
                DatabaseType::Text,
                false,
                true,
            ),
            column(
                "password_resets.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                false,
            ),
            column(
                "password_resets.expires_at",
                "expires_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
            column(
                "password_resets.used_at",
                "used_at",
                DatabaseType::Datetime,
                true,
                false,
            ),
            column(
                "password_resets.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn email_verifications() -> TableSchema {
    table(
        "email_verifications",
        vec![
            column(
                "email_verifications.token_hash",
                "token_hash",
                DatabaseType::Text,
                false,
                true,
            ),
            column(
                "email_verifications.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                false,
            ),
            column(
                "email_verifications.expires_at",
                "expires_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
            nullable_datetime("email_verifications.used_at", "used_at"),
            column(
                "email_verifications.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn oauth_accounts() -> TableSchema {
    table(
        "oauth_accounts",
        vec![
            column(
                "oauth_accounts.provider",
                "provider",
                DatabaseType::Text,
                false,
                false,
            ),
            column(
                "oauth_accounts.subject",
                "subject",
                DatabaseType::Text,
                false,
                true,
            ),
            column(
                "oauth_accounts.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                false,
            ),
            column(
                "oauth_accounts.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn api_tokens() -> TableSchema {
    table(
        "api_tokens",
        vec![
            column("api_tokens.id", "id", DatabaseType::Uuid, false, true),
            column(
                "api_tokens.user_id",
                "user_id",
                DatabaseType::Uuid,
                false,
                false,
            ),
            unique_column("api_tokens.token_hash", "token_hash", DatabaseType::Text),
            column("api_tokens.name", "name", DatabaseType::Text, false, false),
            nullable_datetime("api_tokens.last_used_at", "last_used_at"),
            nullable_datetime("api_tokens.expires_at", "expires_at"),
            nullable_datetime("api_tokens.revoked_at", "revoked_at"),
            column(
                "api_tokens.created_at",
                "created_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
        ],
    )
}

fn login_attempts() -> TableSchema {
    table(
        "login_attempts",
        vec![
            column("login_attempts.key", "key", DatabaseType::Text, false, true),
            column(
                "login_attempts.window_started_at",
                "window_started_at",
                DatabaseType::Datetime,
                false,
                false,
            ),
            column(
                "login_attempts.attempts",
                "attempts",
                DatabaseType::Integer,
                false,
                false,
            ),
        ],
    )
}

fn nullable_datetime(id: &str, name: &str) -> ColumnSchema {
    ColumnSchema {
        id: format!("appstruct::auth::{id}"),
        name: name.to_owned(),
        data_type: DatabaseType::Datetime,
        nullable: true,
        primary_key: false,
        unique: false,
        default: None,
        generated: None,
    }
}

fn table(name: &str, columns: Vec<ColumnSchema>) -> TableSchema {
    TableSchema {
        id: format!("appstruct::auth::{name}"),
        name: format!("_appstruct_auth_{name}"),
        columns,
    }
}

fn column(
    id: &str,
    name: &str,
    data_type: DatabaseType,
    nullable: bool,
    primary_key: bool,
) -> ColumnSchema {
    ColumnSchema {
        id: format!("appstruct::auth::{id}"),
        name: name.to_owned(),
        data_type,
        nullable,
        primary_key,
        unique: false,
        default: None,
        generated: None,
    }
}

fn unique_column(id: &str, name: &str, data_type: DatabaseType) -> ColumnSchema {
    let mut column = column(id, name, data_type, false, false);
    column.unique = true;
    column
}

pub(super) fn unique_constraints() -> Vec<super::UniqueConstraintSchema> {
    saved_views::unique_constraints()
}

pub(super) fn foreign_keys(ir: &AppIr) -> Vec<ForeignKeySchema> {
    let user = ir
        .entities
        .iter()
        .find(|entity| Some(&entity.id) == ir.auth.user_entity.as_ref())
        .expect("compiler resolved auth user entity");
    let user_key = user
        .fields
        .iter()
        .find(|field| field.primary_key)
        .expect("compiler validated auth user key");

    let mut foreign_keys = vec![
        foreign_key(
            "account_user",
            "accounts",
            "user_id",
            &user.table_name,
            &user_key.column_name,
        ),
        foreign_key(
            "session_user",
            "sessions",
            "user_id",
            "_appstruct_auth_accounts",
            "user_id",
        ),
        foreign_key(
            "reset_user",
            "password_resets",
            "user_id",
            "_appstruct_auth_accounts",
            "user_id",
        ),
        foreign_key(
            "email_verification_user",
            "email_verifications",
            "user_id",
            "_appstruct_auth_accounts",
            "user_id",
        ),
        foreign_key(
            "oauth_account_user",
            "oauth_accounts",
            "user_id",
            "_appstruct_auth_accounts",
            "user_id",
        ),
        foreign_key(
            "api_token_user",
            "api_tokens",
            "user_id",
            "_appstruct_auth_accounts",
            "user_id",
        ),
    ];
    foreign_keys.extend(saved_views::foreign_keys(ir));
    foreign_keys
}

fn foreign_key(
    id: &str,
    source: &str,
    source_column: &str,
    target_table: &str,
    target_column: &str,
) -> ForeignKeySchema {
    ForeignKeySchema {
        id: format!("appstruct::auth::{id}"),
        source_table: format!("_appstruct_auth_{source}"),
        source_columns: vec![source_column.to_owned()],
        target_table: target_table.to_owned(),
        target_columns: vec![target_column.to_owned()],
        unique: false,
        on_delete: OnDeleteIr::Cascade,
    }
}