1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! 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:
//!
//! ```toml
//! [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.
//!
//! ```rust,ignore
//! 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:
//!
//! ```rust,ignore
//! 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
//! - [`M0004`] - Replaces `idx_event_type` with a composite cursor-scan index
//!
//! # 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 |
use ;
pub use InitMigration;
pub use M0002;
pub use M0003;
pub use M0004;
/// Creates a new [`Migrator`] instance with all Evento migrations registered.
///
/// The migrator is generic over the database type and works with SQLite, MySQL, and PostgreSQL
/// when the corresponding feature is enabled.
///
/// # Example
///
/// ```rust,ignore
/// use sqlx_migrator::{Migrate, Plan};
///
/// // For SQLite
/// let migrator = evento_sql_migrator::new::<sqlx::Sqlite>()?;
///
/// // For PostgreSQL
/// let migrator = evento_sql_migrator::new::<sqlx::Postgres>()?;
///
/// // For MySQL
/// let migrator = evento_sql_migrator::new::<sqlx::MySql>()?;
///
/// // Run migrations
/// migrator.run(&mut *conn, &Plan::apply_all()).await?;
/// ```
///
/// # Errors
///
/// Returns an error if migration registration fails.