SeaQuery Integration
Relevant source files
The following files were used as context for generating this wiki page:
SeaSchema leverages the SeaQuery ecosystem to provide a bridge between raw database metadata and programmatic SQL statement generation. This integration allows developers to discover existing database structures (tables, columns, indexes, and foreign keys) and convert them into SeaQuery DDL statements for migrations or schema synchronization.
The integration is primarily handled by the writer module, which transforms internal schema definitions into SeaQuery types such as TableCreateStatement and ForeignKeyCreateStatement. This enables a round-trip workflow: discovering a schema from a live database and then reproducing that schema using SeaQuery builders.
Sources: README.md:15-22, src/lib.rs:20, Cargo.toml:39-41
Core Architecture and Data Flow
The integration operates by mapping database-specific definitions to SeaQuery's abstract syntax tree (AST). This process involves three primary stages: Discovery, Definition Mapping, and Writing.
Schema Transformation Flow
The following diagram illustrates how database metadata is retrieved and eventually converted into SeaQuery statements:
The diagram shows the transition from raw database state to SeaQuery objects.
Sources: README.md:18-22, tests/live/mysql/src/main.rs:65-75
Key Integration Components
| Component | Role in SeaQuery Integration |
|---|---|
sea_query::Table | Used to build TableCreateStatement from discovered TableDef. |
sea_query::ColumnDef | Maps internal ColumnInfo to SeaQuery column definitions. |
sea_query::Index | Maps internal IndexInfo to SeaQuery index statements. |
sea_query::ForeignKey | Maps internal ForeignKeyInfo to SeaQuery constraint statements. |
writer module | The logic layer that calls .write() on internal types to produce SeaQuery objects. |
Sources: tests/live/mysql/src/main.rs:4-8, sea-schema-sync/tests/live/sqlite/src/main.rs:6-10
Implementation Details
Conversion to SeaQuery Statements
The integration allows internal structures to be converted into SeaQuery builders. For instance, a TableDef (discovered from MySQL, Postgres, or SQLite) provides a .write() method that returns a TableCreateStatement.
// Example: Converting a discovered table back to a SeaQuery statement
let table: &TableDef = map.get(&tbl_name).unwrap();
let sql = table.write().to_string(MysqlQueryBuilder);
Sources: tests/live/mysql/src/main.rs:73-74, sea-schema-sync/tests/live/sqlite/src/main.rs:166-167
Supported Database Backends
SeaSchema links its features to specific SeaQuery backend implementations via Cargo.toml.
- MySQL: Enabled via
sea-query/backend-mysql. - Postgres: Enabled via
sea-query/backend-postgres, including support forpostgres-vector. - SQLite: Enabled via
sea-query/backend-sqlite.
Sources: Cargo.toml:44-50, CHANGELOG.md:31-35
Advanced Mapping Features
The integration handles complex SQL features by mapping them to SeaQuery expressions and attributes:
- Default Values: Maps internal
ColumnDefaulttosea_query::Expr(e.g.,CURRENT_TIMESTAMP). - Foreign Key Actions: Maps database actions (CASCADE, SET NULL, RESTRICT) to
sea_query::ForeignKeyAction. - Generated Columns: Recent updates include support for Postgres
is_identityand MySQL/MariaDB customized parsing for column defaults.
UML representation of the relationship between SeaSchema definitions and SeaQuery output.
Sources: README.md:38-124, CHANGELOG.md:83-88
Discovery and Writer Interaction
The SchemaDiscovery struct utilizes SeaQuery's Iden trait to build metadata queries dynamically. For example, in MySQL discovery, SchemaQueryBuilder uses Alias and DynIden from SeaQuery to construct queries against INFORMATION_SCHEMA.
pub struct SchemaDiscovery {
pub query: SchemaQueryBuilder,
pub schema: DynIden,
exec: Option<Executor>,
}
Sources: sea-schema-sync/src/mysql/discovery/mod.rs:23-27
Statement Comparison in Tests
The project uses SeaQuery to validate its own discovery logic. Tests create tables using SeaQuery, discover them back into SeaSchema definitions, and then use the writer to regenerate the SeaQuery statement to ensure the SQL matches the original input.
Sources: tests/live/mysql/src/main.rs:65-80, sea-schema-sync/tests/live/sqlite/src/main.rs:163-180
Conclusion
SeaQuery Integration is the backbone of SeaSchema's ability to translate live database state into reusable code. By mapping internal schema definitions to the SeaQuery AST, the library provides a unified interface for schema management across MySQL, PostgreSQL, and SQLite, supporting modern features like vector types and complex constraints.