Database Seeding
Relevant source files
The following files were used as context for generating this wiki page:
Database seeding in SeaORM Pro is a critical process for initializing development, demo, and production environments with essential data. This system encompasses both automated migration-based seeding for core application state (like users and roles) and task-based bulk ingestion for demonstration datasets (like the AdventureWorks subset).
The seeding architecture is designed to support various database backends including SQLite, MySQL, and PostgreSQL, ensuring that developers can quickly bootstrap a functional admin panel with pre-configured administrative accounts and sample business records.
Sources: README.md:1-25, src/tasks/seed.rs:1-10
Seeding Mechanisms
The project employs two primary methods for seeding data: programmatic migrations and file-based task execution.
Migration-Based Seeding
Administrative users and initial RBAC (Role-Based Access Control) settings are typically handled via SeaORM migrations. These ensure that every new instance of the database has the required credentials to access the admin panel.
This diagram illustrates the flow of inserting default system users during the migration phase.
Sources: migration/src/m20250101_000002_seed_users.rs:12-70, README.md:95-105
Task-Based Seeding
Bulk data ingestion is managed through the seed_data task. This task parses external TSV/CSV files, transcodes them according to specific encodings (UTF-16LE or Windows-1252), and performs chunked inserts into the database.
| Component | Description |
|---|---|
SeedData Struct | Implements the Task trait for the seed_data command. |
seed_table Function | Generic asynchronous function for reading and inserting CSV data. |
encoding_rs | Used to handle various file encodings (e.g., UTF-16LE for product models). |
| Chunking | Data is inserted in batches of 100 records to optimize performance. |
Sources: src/tasks/seed.rs:25-50, src/tasks/seed.rs:90-125
Execution and Configuration
Command Line Interface
The seeding process is triggered via the cargo run task interface. Users can choose to simply migrate or perform a full reset.
- Standard Seed:
cargo run task seed_data - Refresh/Reset Seed:
cargo run task seed_data refresh:true(Drops existing tables before seeding)
Sources: src/tasks/seed.rs:10-18, README.md:33-40
Technical Workflow
The seed_data task follows a strict sequence to maintain referential integrity, loading base tables (Customers, Addresses) before dependent transaction tables (Sales Orders).
The sequence above shows how the task interacts with the file system and database to populate tables.
Sources: src/tasks/seed.rs:40-130, README.md:42-63
Data Models and Entities
The seeding system targets several core entities. The following table highlights key entities seeded during the demonstration setup:
| Entity | Source File | Encoding |
|---|---|---|
| Customer | Customer.tsv | WINDOWS_1252 |
| Product | Product.tsv | WINDOWS_1252 |
| ProductModel | ProductModel.tsv | UTF_16LE |
| ProductDescription | ProductDescription.tsv | UTF_16LE |
| SalesOrderHeader | SalesOrderHeader.tsv | WINDOWS_1252 |
Sources: src/tasks/seed.rs:65-85
Scaffolding and Cleanup
The project includes a scaffold.sh script that allows developers to remove demo seeding logic once they are ready to build their own entities. This script specifically removes the AdventureWorks entities while preserving the user model and migration-based user seeding.
# Example from scaffold.sh
mv src/models/user.rs src/models/user.bak
rm src/models/*.rs || true
mv src/models/user.bak src/models/user.rs
Sources: build_tools/scaffold.sh:1-15
Summary
Database seeding in SeaORM Pro provides a robust foundation for both immediate demo availability and long-term project development. By combining Loco-rs tasks for bulk TSV processing and SeaORM migrations for identity management, the system ensures consistent data states across different environments. Developers can utilize the refresh:true flag to rapidly iterate on schema changes while maintaining a populated database for testing.