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 tracking;
40pub use tracking::schema_hash;
41
42pub mod plugin_schemas;
43
44pub mod config;
45pub mod diff;
46pub mod generate;
47
48/// All known plugin names.
49pub const ALL_PLUGINS: &[&str] = &[
50    "email-password",
51    "passkey",
52    "mfa",
53    "oauth",
54    "bearer",
55    "api-key",
56    "magic-link",
57    "oauth2-server",
58    "account-lockout",
59    "webhooks",
60    "oidc",
61];
62
63/// Get the schema tables for a plugin by name.
64///
65/// Returns `None` if the plugin name is not recognized.
66pub fn plugin_schema_by_name(name: &str) -> Option<Vec<TableDef>> {
67    match name {
68        "email-password" => Some(plugin_schemas::email_password_schema()),
69        "passkey" => Some(plugin_schemas::passkey_schema()),
70        "mfa" => Some(plugin_schemas::mfa_schema()),
71        "oauth" => Some(plugin_schemas::oauth_schema()),
72        "bearer" => Some(plugin_schemas::bearer_schema()),
73        "api-key" => Some(plugin_schemas::api_key_schema()),
74        "magic-link" => Some(plugin_schemas::magic_link_schema()),
75        "oauth2-server" => Some(plugin_schemas::oauth2_server_schema()),
76        "account-lockout" => Some(plugin_schemas::account_lockout_schema()),
77        "webhooks" => Some(plugin_schemas::webhooks_schema()),
78        "oidc" => Some(plugin_schemas::oidc_schema()),
79        _ => None,
80    }
81}
82
83/// Check if a plugin name is valid (even if it has no database tables).
84/// Plugins like `admin`, `status`, `telemetry`, and `openapi` are code-only.
85pub fn is_known_plugin(name: &str) -> bool {
86    matches!(
87        name,
88        "email-password"
89            | "passkey"
90            | "mfa"
91            | "oauth"
92            | "bearer"
93            | "api-key"
94            | "magic-link"
95            | "admin"
96            | "status"
97            | "oauth2-server"
98            | "account-lockout"
99            | "webhooks"
100            | "oidc"
101            | "telemetry"
102            | "openapi"
103    )
104}
105
106/// Collect a schema from a list of plugin names plus core tables.
107///
108/// The `table_prefix` replaces the default `yauth_` prefix on all table names
109/// and FK references.
110pub fn collect_schema_for_plugins(
111    plugins: &[String],
112    table_prefix: &str,
113) -> Result<YAuthSchema, SchemaError> {
114    let mut table_lists = vec![core_schema()];
115    for plugin in plugins {
116        match plugin_schema_by_name(plugin) {
117            Some(tables) => table_lists.push(tables),
118            None if is_known_plugin(plugin) => {
119                // Plugin exists but has no tables (e.g., admin, status, telemetry, openapi)
120            }
121            None => {
122                return Err(SchemaError::UnknownPlugin(plugin.clone()));
123            }
124        }
125    }
126
127    // Apply table prefix if not the default
128    if table_prefix != "yauth_" {
129        for list in &mut table_lists {
130            for table in list.iter_mut() {
131                table.apply_prefix("yauth_", table_prefix);
132            }
133        }
134    }
135
136    collect_schema(table_lists)
137}
138
139/// Generate DDL for a schema in the given dialect.
140pub fn generate_ddl(schema: &YAuthSchema, dialect: Dialect) -> String {
141    match dialect {
142        Dialect::Postgres => generate_postgres_ddl(schema),
143        Dialect::Sqlite => generate_sqlite_ddl(schema),
144        Dialect::Mysql => generate_mysql_ddl(schema),
145    }
146}
147
148#[cfg(test)]
149mod tests;