use sqlx::PgExecutor;
#[cfg(feature = "event-store")]
use crate::error::PostgresEventStoreError;
#[cfg(feature = "event-store")]
pub const MIGRATION_1: &str = include_str!("../migrations/0001_event_log.sql");
#[cfg(feature = "event-store")]
pub const SCHEMA_VERSION: u32 = 1;
#[cfg(feature = "event-store")]
pub async fn apply<'e, E>(executor: E) -> Result<(), PostgresEventStoreError>
where
E: PgExecutor<'e>,
{
sqlx::raw_sql(MIGRATION_1).execute(executor).await?;
Ok(())
}
#[cfg(feature = "projection-store")]
pub const MIGRATION_2: &str = include_str!("../migrations/0002_projection_checkpoint.sql");
#[cfg(feature = "projection-store")]
pub const PROJECTION_SCHEMA_VERSION: u32 = 1;
#[cfg(feature = "projection-store")]
pub async fn apply_projection<'e, E>(
executor: E,
) -> Result<(), crate::error::PostgresProjectionStoreError>
where
E: PgExecutor<'e>,
{
sqlx::raw_sql(MIGRATION_2).execute(executor).await?;
Ok(())
}
#[cfg(test)]
mod tests {
#[cfg(feature = "event-store")]
use super::{MIGRATION_1, SCHEMA_VERSION};
#[cfg(feature = "event-store")]
fn statements() -> String {
MIGRATION_1
.lines()
.map(|line| line.split_once("--").map_or(line, |(code, _)| code))
.collect::<Vec<_>>()
.join(
"
",
)
}
#[cfg(feature = "event-store")]
#[test]
fn the_comment_stripper_removes_prose_and_keeps_sql() {
let stripped = statements();
assert!(
!stripped.contains("ADR-0024"),
"prose survived the strip, so the guards below are scanning comments"
);
assert!(
stripped.contains("CREATE TABLE IF NOT EXISTS event"),
"the strip ate SQL, so the guards below are scanning nothing"
);
}
#[cfg(feature = "event-store")]
#[test]
fn migration_1_carries_the_visibility_mechanism_column() {
let statements = statements();
assert!(
statements.contains("xact_id"),
"the frontier mechanism's column is gone, but `head` and every read still filter on it"
);
assert!(
statements.contains("event_xact_idx"),
"the frontier predicate has lost its index, which turns every read into a sequential scan and charges the mechanism for the absence of an index rather than for what it costs"
);
}
#[cfg(feature = "event-store")]
#[test]
fn position_is_not_a_serial_column() {
let statements = statements();
for forbidden in ["bigserial", "serial", "AS IDENTITY"] {
assert!(
!statements.contains(forbidden),
"migration 1 contains `{forbidden}`: a sequence default on `position` \
is exactly the construction ES-10 forbids"
);
}
}
#[cfg(feature = "event-store")]
#[test]
fn the_schema_version_is_one_until_first_publish() {
assert_eq!(SCHEMA_VERSION, 1);
}
#[cfg(feature = "projection-store")]
#[test]
fn migration_2_stores_a_constrained_authority() {
let sql = super::MIGRATION_2;
assert!(
sql.contains(
"authority text NOT NULL CHECK (authority IN ('live', 'rebuilding'))"
),
"the authority column has lost its CHECK, so a row can record a state `Checkpoint` cannot represent and `checkpoint` will fail on read instead of the write failing where it happened"
);
}
#[cfg(feature = "projection-store")]
#[test]
fn no_migration_file_creates_the_probe_table() {
for sql in [
#[cfg(feature = "event-store")]
MIGRATION_1,
super::MIGRATION_2,
] {
assert!(
!sql.contains("projection_probe"),
"a migration file creates the conformance probe table, so applying this crate's schema would put the suite's read model in a consumer's database"
);
}
}
}