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