Skip to main content

yauth_migration/
lib.rs

1//! Declarative schema system and migration file generator for yauth.
2//!
3//! This crate has **zero ORM dependencies** — it is a pure code generator.
4//! It provides:
5//!
6//! - Schema types (`TableDef`, `ColumnDef`, `ColumnType`, etc.)
7//! - Core + plugin schema definitions
8//! - Schema collector with topological sort by FK dependencies
9//! - Dialect-specific DDL generators (Postgres, SQLite, MySQL)
10//! - Schema diff engine for incremental migrations
11//! - Migration file generators (diesel up.sql/down.sql, sqlx numbered .sql)
12//! - `yauth.toml` config file support
13//! - Schema hash for tracking
14
15mod types;
16pub use types::*;
17
18mod core;
19pub use core::core_schema;
20
21mod collector;
22pub use collector::{SchemaError, YAuthSchema, collect_schema};
23
24mod postgres;
25pub use postgres::{generate_postgres_ddl, generate_postgres_drop, generate_postgres_drops};
26
27mod sqlite;
28pub use sqlite::{generate_sqlite_ddl, generate_sqlite_drop, generate_sqlite_drops};
29
30mod mysql;
31pub use mysql::{generate_mysql_ddl, generate_mysql_drop, generate_mysql_drops};
32
33mod diesel_schema;
34pub use diesel_schema::generate_diesel_schema;
35
36mod seaorm_entities;
37pub use seaorm_entities::generate_seaorm_entities;
38
39mod toasty_models;
40pub use toasty_models::generate_toasty_models;
41
42mod tracking;
43pub use tracking::schema_hash;
44
45pub mod plugin_schemas;
46
47pub mod config;
48pub mod diff;
49pub mod generate;
50
51/// All known plugin names.
52pub const ALL_PLUGINS: &[&str] = &[
53    "email-password",
54    "passkey",
55    "mfa",
56    "oauth",
57    "bearer",
58    "api-key",
59    "magic-link",
60    "oauth2-server",
61    "account-lockout",
62    "webhooks",
63    "oidc",
64];
65
66/// Get the schema tables for a plugin by name.
67///
68/// Returns `None` if the plugin name is not recognized.
69pub fn plugin_schema_by_name(name: &str) -> Option<Vec<TableDef>> {
70    match name {
71        "email-password" => Some(plugin_schemas::email_password_schema()),
72        "passkey" => Some(plugin_schemas::passkey_schema()),
73        "mfa" => Some(plugin_schemas::mfa_schema()),
74        "oauth" => Some(plugin_schemas::oauth_schema()),
75        "bearer" => Some(plugin_schemas::bearer_schema()),
76        "api-key" => Some(plugin_schemas::api_key_schema()),
77        "magic-link" => Some(plugin_schemas::magic_link_schema()),
78        "oauth2-server" => Some(plugin_schemas::oauth2_server_schema()),
79        "account-lockout" => Some(plugin_schemas::account_lockout_schema()),
80        "webhooks" => Some(plugin_schemas::webhooks_schema()),
81        "oidc" => Some(plugin_schemas::oidc_schema()),
82        _ => None,
83    }
84}
85
86/// Check if a plugin name is valid (even if it has no database tables).
87/// Plugins like `admin`, `status`, `telemetry`, and `openapi` are code-only.
88pub fn is_known_plugin(name: &str) -> bool {
89    matches!(
90        name,
91        "email-password"
92            | "passkey"
93            | "mfa"
94            | "oauth"
95            | "bearer"
96            | "api-key"
97            | "magic-link"
98            | "admin"
99            | "status"
100            | "oauth2-server"
101            | "account-lockout"
102            | "webhooks"
103            | "oidc"
104            | "telemetry"
105            | "openapi"
106    )
107}
108
109/// Collect a schema from a list of plugin names plus core tables.
110///
111/// The `table_prefix` replaces the default `yauth_` prefix on all table names
112/// and FK references.
113pub fn collect_schema_for_plugins(
114    plugins: &[String],
115    table_prefix: &str,
116) -> Result<YAuthSchema, SchemaError> {
117    let mut table_lists = vec![core_schema()];
118    for plugin in plugins {
119        match plugin_schema_by_name(plugin) {
120            Some(tables) => table_lists.push(tables),
121            None if is_known_plugin(plugin) => {
122                // Plugin exists but has no tables (e.g., admin, status, telemetry, openapi)
123            }
124            None => {
125                return Err(SchemaError::UnknownPlugin(plugin.clone()));
126            }
127        }
128    }
129
130    // Apply table prefix if not the default
131    if table_prefix != "yauth_" {
132        for list in &mut table_lists {
133            for table in list.iter_mut() {
134                table.apply_prefix("yauth_", table_prefix);
135            }
136        }
137    }
138
139    collect_schema(table_lists)
140}
141
142/// Generate DDL for a schema in the given dialect.
143pub fn generate_ddl(schema: &YAuthSchema, dialect: Dialect) -> String {
144    match dialect {
145        Dialect::Postgres => generate_postgres_ddl(schema),
146        Dialect::Sqlite => generate_sqlite_ddl(schema),
147        Dialect::Mysql => generate_mysql_ddl(schema),
148    }
149}
150
151#[cfg(test)]
152mod tests;