Schema Writer
Relevant source files
The following files were used as context for generating this wiki page:
The Schema Writer is a core module within the SeaSchema library responsible for exporting database Schema definitions into SeaQuery objects and SQL statements. It acts as the final stage of the schema management pipeline, converting discovered or manually defined schema structures back into executable DDL (Data Definition Language).
This module supports multiple database backends, including MySQL, PostgreSQL, and SQLite, by providing backend-specific implementations that map internal type definitions to their respective SQL dialects. The writer allows developers to programmatically generate TableCreateStatement objects, which can then be converted to raw SQL strings using SeaQuery's query builders.
Sources: README.md:21-27, src/mysql/writer/mod.rs:13-17, src/postgres/writer/mod.rs:11-15
Architecture and Data Flow
The Schema Writer operates by iterating over the tables defined within a Schema struct and invoking a backend-specific write() method on each table. This process transforms abstract schema definitions into structured TableCreateStatement objects.
The diagram shows the transformation of a high-level Schema object into individual Table definitions, which are then processed by writer methods to produce SeaQuery statements and final SQL.
Core Implementation
The implementation is provided as an extension to the Schema struct for each supported database. The write function typically returns a Vec<TableCreateStatement>, representing the DDL required to recreate the entire schema.
impl Schema {
pub fn write(&self) -> Vec<TableCreateStatement> {
self.tables.iter().map(|table| table.write()).collect()
}
}
Sources: src/mysql/writer/mod.rs:13-17, src/postgres/writer/mod.rs:11-15
Backend Specifics
The writer is organized into sub-modules for each database dialect, handling the nuances of columns, foreign keys, indexes, and data types.
MySQL Writer
The MySQL implementation maps MySQL-specific definitions (like storage engines and character sets) into SeaQuery statements. It utilizes internal modules to handle different aspects of the table definition:
column: Logic for writing column definitions and attributes.foreign_key: Logic for writing relational constraints.index: Logic for writing database indexes.types: Mapping of MySQL data types.
Sources: src/mysql/writer/mod.rs:3-9
PostgreSQL Writer
The PostgreSQL implementation handles advanced Postgres features such as custom enumerations and various constraint types. The TableDef for Postgres includes a wider array of constraints compared to other backends:
| Constraint Category | Description |
|---|---|
check_constraints | Domain integrity checks. |
not_null_constraints | Nullability requirements. |
unique_constraints | Uniqueness requirements. |
primary_key_constraints | Table primary keys. |
reference_constraints | Foreign key relationships. |
exclusion_constraints | Specialized Postgres exclusion logic. |
Sources: sea-schema-sync/src/postgres/def/schema.rs:19-25, src/postgres/writer/mod.rs:1-6
Integration with SeaQuery
The Writer's primary output is the TableCreateStatement from the sea_query crate. This allows the generated schema to be further manipulated or rendered using different QueryBuilder implementations (e.g., MysqlQueryBuilder, PostgresQueryBuilder, or SqliteQueryBuilder).
This sequence diagram illustrates the workflow from calling write() to obtaining a final SQL string for a specific database dialect.
Sources: tests/live/mysql/src/main.rs:64-70, sea-schema-sync/tests/live/sqlite/src/main.rs:172-176
Data Structures
The writer relies on backend-specific Schema and TableDef structures to generate the correct SQL.
| Structure | Backend | Key Fields |
|---|---|---|
Schema | MySQL | schema, system, tables |
TableDef | MySQL | info, columns, indexes, foreign_keys |
Schema | Postgres | schema, tables, enums |
TableDef | Postgres | info, columns, check_constraints, reference_constraints, etc. |
Sources: src/mysql/def/schema.rs:7-19, sea-schema-sync/src/postgres/def/schema.rs:7-27
Summary
The Schema Writer provides a standardized way to convert internal database representations back into SQL through the use of SeaQuery. By abstracting the complexities of different database dialects into backend-specific modules, it ensures that generated schemas are technically accurate and compatible with MySQL, PostgreSQL, and SQLite environments.