MySQL & MariaDB Support
Relevant source files
The following files were used as context for generating this wiki page:
SeaSchema provides a robust framework for managing database schemas specifically tailored for MySQL and MariaDB. It maps database structures to Rust type definitions, allowing developers to perform schema discovery, parsing, and SQL generation (writing) with high precision. The library treats MySQL and MariaDB as closely related but distinct systems, providing specific logic to handle their versioning and feature differences, such as MariaDB's SYSTEM VERSIONED tables.
Sources: README.md:9-12, CHANGELOG.md:129-130, src/mysql/def/system.rs:18-26
Architecture and Module Structure
The MySQL support is organized into specialized modules that handle different stages of the schema management lifecycle. These modules are conditionally compiled based on feature flags such as def, discovery, parser, query, writer, and probe.
The diagram above illustrates the data flow from a live database through discovery and parsing into internal definitions, which can then be written back as SQL.
Sources: src/mysql/mod.rs:4-28, README.md:23-28
Key Components
| Component | Description |
|---|---|
def | Contains structures for TableDef, ColumnInfo, IndexInfo, and ForeignKeyInfo. |
query | Executes specific SQL against INFORMATION_SCHEMA to retrieve raw metadata. |
parser | Converts raw query results into structured Rust types defined in def. |
discovery | Orchestrates the process of connecting to a database and building a complete Schema. |
writer | Translates TableDef and other structures into sea-query statements or raw SQL. |
Sources: README.md:23-28, src/mysql/def/mod.rs:3-13
System Identification and Versioning
SeaSchema distinguishes between MySQL and MariaDB by parsing the system version string. This is critical for handling vendor-specific features like MariaDB's unique table versioning or specific index types.
SystemInfo Structure
The SystemInfo struct stores the parsed version and identifies the vendor. The version is stored as a u32 using a calculated formula: major * 10000 + minor * 100 + sub_version.
pub struct SystemInfo {
pub version: u32,
pub system: String, // e.g., "MariaDB" or "0ubuntu0.*"
pub suffix: Vec<String>,
}
Sources: src/mysql/def/system.rs:5-13
Parsing Logic
The parser splits the version string (e.g., 10.2.31-MariaDB) to extract the numeric version and the system identifier.
This flow shows how a raw version string is transformed into a standardized u32 for comparison logic.
Sources: src/mysql/parser/system.rs:15-28, src/mysql/def/system.rs:30-36
Schema Discovery and Definition
Schema discovery involves mapping physical database tables to the TableDef structure. This includes capturing metadata such as storage engines (e.g., InnoDB), character sets (e.g., utf8mb4), and specific column attributes.
Table Definition Metadata
The library captures extensive details about MySQL tables:
- Columns: Type (e.g.,
SmallInt,Timestamp), nullability, default values, and extra attributes (e.g.,auto_increment,on_update_current_timestamp). - Indexes: Type (
BTree,Vector), uniqueness, and functional status. - Foreign Keys: Referenced tables/columns and referential actions (
Cascade,Restrict).
Sources: README.md:37-149, CHANGELOG.md:30-31, tests/live/mysql/src/main.rs:136-152
Data Type Mapping
SeaSchema handles a wide array of MySQL-specific types and attributes:
- Numeric Attributes: Unsigned flags and zerofill.
- Time Attributes: Fractional seconds for timestamps and
CURRENT_TIMESTAMPdefaults. - PlanetScale Support: Includes
IndexType::Vectorsupport when theplanetscalefeature is enabled.
Sources: README.md:58-63, CHANGELOG.md:29-30, tests/live/mysql/src/main.rs:293-300
Live Testing and SQL Generation
The tests/live/mysql/src/main.rs file demonstrates how the library performs end-to-end schema synchronization. It creates tables using sea-query, discovers them back into TableDef objects, and then verifies that the writer can regenerate the original SQL.
Sequence of a live discovery test verifying the accuracy of the parser and writer.
Sources: tests/live/mysql/src/main.rs:15-70
Conclusion
MySQL and MariaDB support in SeaSchema is built to provide high-fidelity mapping between Rust code and database schemas. By leveraging specialized modules for parsing system versions and information schema metadata, it enables automated migration and discovery workflows that are aware of the subtle differences between MySQL versions and MariaDB forks.