runique 3.0.3

A Django-inspired web framework for Rust with ORM, templates, and comprehensive security middleware
Documentation
//! Shared data types between migration utilities: parsed schemas, columns, FKs, indexes, diffs.

/// A table schema as reconstructed by the AST parsers (from a `model!{}` DSL
/// source or a generated SeaORM snapshot). Feeds the diff engine that decides
/// what a migration needs to change.
#[derive(Debug, Clone)]
pub struct ParsedSchema {
    pub table_name: String,
    pub primary_key: Option<ParsedColumn>,
    pub columns: Vec<ParsedColumn>,
    pub foreign_keys: Vec<ParsedFk>,
    pub indexes: Vec<ParsedIndex>,
}

/// A single parsed column, as extracted from either the `model!{}` DSL or a
/// generated SeaORM snapshot. Compared field-by-field between two schema
/// versions to build a [`Changes`] set.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ParsedColumn {
    pub name: String,
    pub col_type: String,
    pub nullable: bool,
    pub unique: bool,
    pub ignored: bool,
    /// Column with DEFAULT CURRENT_TIMESTAMP: declared `auto_now`/`auto_now_update`,
    /// or read back from a snapshot.
    pub has_default_now: bool,
    /// Literal default value (`[default: 0]`, `[default: true]`, `[default: "x"]`) rendered as
    /// the Rust expression placed inside `.default(...)`. `None` when no literal default.
    /// Distinct from `has_default_now` (timestamp default), which remains its own flag.
    pub default_value: Option<String>,
    /// Enum name for string enum columns (used in the snapshot).
    pub enum_name: Option<String>,
    /// Current DB values for string enum columns (e.g., ["Fix", "Feature", "Added"]).
    pub enum_string_values: Vec<String>,
    /// Explicit rename directive (`[renamed_from: "old_name"]`). Transient: it lives in the
    /// source model only and is NEVER written to snapshots (the snapshot holds the new name).
    /// Consumed by the diff to emit `RENAME COLUMN` instead of DROP + ADD (no data loss).
    pub renamed_from: Option<String>,
    /// Declared column length: `VARCHAR(n)` for text, `BINARY(n)`/`VARBINARY(n)`
    /// for bytes. `None` leaves the engine's default length.
    pub max_length: Option<u32>,
}

/// A parsed foreign key constraint.
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedFk {
    pub from_column: String,
    pub to_table: String,
    pub to_column: String,
    pub on_delete: String,
    pub on_update: String,
}

/// A parsed table index.
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedIndex {
    pub name: String,
    pub columns: Vec<String>,
    pub unique: bool,
}

/// The set of DDL operations needed to bring a table from one [`ParsedSchema`]
/// version to the next — the output of the diff step, consumed by the
/// migration file generators to render `up`/`down` bodies.
#[derive(Debug, Clone)]
pub struct Changes {
    pub table_name: String,
    pub added_columns: Vec<ParsedColumn>,
    pub dropped_columns: Vec<ParsedColumn>, // <- CHANGED
    pub modified_columns: Vec<(ParsedColumn, ParsedColumn)>,
    /// Column renames via `[renamed_from: "old"]`: (old_name, new_name). Emitted as
    /// `ALTER TABLE … RENAME COLUMN` (PG/MySQL/MariaDB/SQLite all support it).
    pub renamed_columns: Vec<(String, String)>,
    pub added_fks: Vec<ParsedFk>,
    pub dropped_fks: Vec<ParsedFk>,
    pub added_indexes: Vec<ParsedIndex>,
    pub dropped_indexes: Vec<ParsedIndex>,
    pub is_new_table: bool,
    /// String enum value renames: (column_name, enum_name, old_value, new_value).
    pub enum_renames: Vec<(String, String, String, String)>,
    /// Added enum values: (column_name, value).
    pub enum_value_adds: Vec<(String, String, String)>,
    /// Dropped enum values: (column_name, value).
    pub enum_value_drops: Vec<(String, String, String)>,
}

impl Changes {
    /// True if this change set has no effect — a fresh migration would have
    /// nothing to write.
    pub fn is_empty(&self) -> bool {
        !self.is_new_table
            && self.added_columns.is_empty()
            && self.dropped_columns.is_empty()
            && self.modified_columns.is_empty()
            && self.renamed_columns.is_empty()
            && self.added_fks.is_empty()
            && self.dropped_fks.is_empty()
            && self.added_indexes.is_empty()
            && self.dropped_indexes.is_empty()
            && self.enum_renames.is_empty()
            && self.enum_value_adds.is_empty()
            && self.enum_value_drops.is_empty()
    }
}