Skip to main content

soma_schema/
error.rs

1use thiserror::Error;
2
3#[non_exhaustive]
4#[derive(Debug, Error)]
5pub enum Error {
6    #[error("I/O error: {0}")]
7    Io(#[from] std::io::Error),
8
9    #[error("migration-order.yaml not found at the migrations root — run `soma-schema init <dir>` to scaffold one")]
10    ManifestMissing,
11
12    #[error("failed to parse migration-order.yaml: {0}")]
13    ManifestParse(String),
14
15    #[error("unsupported manifest_version {found}; this tool only supports version 1")]
16    UnsupportedManifestVersion { found: u32 },
17
18    #[error(
19        "invalid migration filename (must be a bare .sql name, no path separators, no '..'): {0}"
20    )]
21    InvalidFileName(String),
22
23    #[error("duplicate entry in manifest: version {version}, file {file}")]
24    DuplicateEntry { version: u32, file: String },
25
26    #[error("on-disk migration not listed in manifest: {path} — add it to migration-order.yaml, or delete the file")]
27    OrphanMigration { path: String },
28
29    #[error("manifest entry not found on disk: version {version}, file {file} — create the file, or remove its migration-order.yaml entry")]
30    MissingFile { version: u32, file: String },
31
32    #[error("checksum drift for version {version}, file {file}: applied checksum differs from file on disk — applied migrations are immutable; write a NEW migration instead of editing this one")]
33    ChecksumDrift { version: u32, file: String },
34
35    #[error("no DOWN section in version {version}, file {file}")]
36    MissingDown { version: u32, file: String },
37
38    #[error("identifier contains invalid characters (only [A-Za-z0-9_] allowed): {0}")]
39    InvalidIdentifier(String),
40
41    #[error("setup file {file} failed: {source}")]
42    SetupFailed {
43        file: String,
44        #[source]
45        source: sqlx::Error,
46    },
47
48    #[error("database error: {0}")]
49    Sqlx(#[from] sqlx::Error),
50
51    /// The pool must have at least 2 connections: one is reserved for the advisory lock.
52    #[error(
53        "pool too small: max_connections must be >= 2 (one is reserved for the advisory lock)"
54    )]
55    PoolTooSmall,
56
57    /// An applied migration (present in the tracking table) is no longer on disk or in
58    /// the manifest. Silently skipping it would bypass the integrity guard — surface it
59    /// as an explicit error so operators notice the tampered or deleted migration file.
60    #[error("migration version {version}, file {file} is recorded as applied but is missing from the manifest and disk — the file was deleted after being applied; restore it, or remove its tracking row")]
61    AppliedButMissing { version: u32, file: String },
62
63    #[error("explorer build failed: {0}")]
64    Explorer(String),
65}
66
67pub type Result<T> = std::result::Result<T, Error>;