Skip to main content

Crate evento_sql_migrator

Crate evento_sql_migrator 

Source
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 support
  • mysql - Enables MySQL database support
  • postgres - 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 - Adds timestamp_subsec column for sub-second precision timestamps
  • M0003 - 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:

ColumnTypeDescription
idVARCHAR(26)Event ID (ULID format)
nameVARCHAR(50)Event type name
aggregator_typeVARCHAR(50)Aggregate root type
aggregator_idVARCHAR(26)Aggregate root instance ID
versionINTEGEREvent sequence number
dataBLOBSerialized event data
metadataBLOBSerialized event metadata
routing_keyVARCHAR(50)Optional routing key
timestampBIGINTEvent timestamp (seconds)
timestamp_subsecBIGINTSub-second precision

§Subscriber Table

Tracks event subscription progress:

ColumnTypeDescription
keyVARCHAR(50)Subscriber identifier (primary key)
worker_idVARCHAR(26)Associated worker ID
cursorTEXTCurrent event stream position
lagINTEGERSubscription lag counter
enabledBOOLEANWhether subscription is active
created_atTIMESTAMPCreation timestamp
updated_atTIMESTAMPLast update timestamp

Structs§

InitMigration
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.

Functions§

new
Creates a new Migrator instance with all Evento migrations registered.