Expand description
SQL database migrations for the Evento event sourcing library.
This crate provides database schema migrations required for storing events, snapshots, and subscriber state in SQL databases. It supports SQLite, MySQL, and PostgreSQL through feature flags.
§Features
sqlite- Enables SQLite database supportmysql- Enables MySQL database supportpostgres- Enables PostgreSQL database support
All features are enabled by default. You can selectively enable only the databases you need:
[dependencies]
evento-sql-migrator = { version = "1.8", default-features = false, features = ["postgres"] }§Usage
The main entry point is the new function, which creates a Migrator
instance configured with all Evento migrations.
ⓘ
use sqlx_migrator::{Migrate, Plan};
// Acquire a database connection
let mut conn = pool.acquire().await?;
// Create the migrator for your database type
let migrator = evento_sql_migrator::new::<sqlx::Sqlite>()?;
// Run all pending migrations
migrator.run(&mut *conn, &Plan::apply_all()).await?;When using the main evento crate, the migrator is re-exported:
ⓘ
let migrator = evento::sql_migrator::new::<sqlx::Sqlite>()?;§Migrations
The crate includes the following migrations:
InitMigration- Creates the initial database schema (event, snapshot, subscriber tables)M0002- Addstimestamp_subseccolumn for sub-second precision timestampsM0003- Drops the snapshot table and extends the event name column length
§Database Schema
After running all migrations, the database will contain:
§Event Table
Stores all domain events:
| Column | Type | Description |
|---|---|---|
id | VARCHAR(26) | Event ID (ULID format) |
name | VARCHAR(50) | Event type name |
aggregator_type | VARCHAR(50) | Aggregate root type |
aggregator_id | VARCHAR(26) | Aggregate root instance ID |
version | INTEGER | Event sequence number |
data | BLOB | Serialized event data |
metadata | BLOB | Serialized event metadata |
routing_key | VARCHAR(50) | Optional routing key |
timestamp | BIGINT | Event timestamp (seconds) |
timestamp_subsec | BIGINT | Sub-second precision |
§Subscriber Table
Tracks event subscription progress:
| Column | Type | Description |
|---|---|---|
key | VARCHAR(50) | Subscriber identifier (primary key) |
worker_id | VARCHAR(26) | Associated worker ID |
cursor | TEXT | Current event stream position |
lag | INTEGER | Subscription lag counter |
enabled | BOOLEAN | Whether subscription is active |
created_at | TIMESTAMP | Creation timestamp |
updated_at | TIMESTAMP | Last update timestamp |
Structs§
- Init
Migration - Initial migration that creates the core database schema for Evento.
- M0002
- Migration that adds sub-second precision to event timestamps.
- M0003
- Migration that drops the snapshot table and extends the event name column.