gaman_core/migration_engine/
catalog.rs1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4use super::adapters::{BoxFuture, ExecutorError, MigrationStore, StoreError, TrackingError};
5use super::engine::graph_from;
6use super::execution_diagnostic::StatementDiagnostic;
7use crate::clarifier::Clarification;
8use crate::graphs::{GraphError, MigrationGraph};
9use crate::migrations::Migration;
10use crate::offline_planner::OfflineError;
11use crate::sql_plan::SqlPlanError;
12#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct MigrationArtifact {
15 pub id: String,
17 pub content: String,
19}
20
21#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
23pub struct MigrationMovement {
24 pub applied: usize,
26 pub reverted: usize,
28}
29
30#[derive(Debug, Error)]
32pub enum EngineError {
33 #[error(transparent)]
35 Store(#[from] StoreError),
36 #[error(transparent)]
38 Tracking(#[from] TrackingError),
39 #[error(transparent)]
41 Executor(#[from] ExecutorError),
42 #[error("migration '{migration}' {direction} statement {statement_ordinal} failed: {source}")]
44 MigrationExecution {
45 migration: String,
47 direction: &'static str,
49 statement_ordinal: usize,
51 statement: Box<StatementDiagnostic>,
53 #[source]
55 source: Box<ExecutorError>,
56 },
57 #[error(transparent)]
59 Graph(#[from] GraphError),
60 #[error(transparent)]
62 Offline(#[from] OfflineError),
63 #[error(transparent)]
65 SqlPlan(#[from] SqlPlanError),
66 #[error("migration generation needs clarification input")]
68 NeedsInput(Vec<Clarification>),
69 #[error("migration engine configuration error: {0}")]
71 Config(String),
72}
73
74pub struct MigrationCatalog {
76 migrations: Vec<Migration>,
77 graph: MigrationGraph,
78 ordered: Vec<String>,
79}
80
81pub(crate) struct CatalogMigrationStore<'a, M> {
83 pub(super) catalog: &'a MigrationCatalog,
84 pub(super) writer: &'a M,
85}
86
87impl<M> MigrationStore for CatalogMigrationStore<'_, M>
88where
89 M: MigrationStore,
90{
91 fn load_all<'a>(&'a self) -> BoxFuture<'a, Result<Vec<Migration>, StoreError>> {
92 Box::pin(async move { Ok(self.catalog.migrations.clone()) })
93 }
94
95 fn save<'a>(&'a self, migration: &'a Migration) -> BoxFuture<'a, Result<(), StoreError>> {
96 self.writer.save(migration)
97 }
98}
99
100impl MigrationCatalog {
101 pub fn new(migrations: Vec<Migration>) -> Result<Self, EngineError> {
103 let (graph, ordered) = graph_from(&migrations)?;
104 Ok(Self {
105 migrations,
106 graph,
107 ordered,
108 })
109 }
110
111 pub fn migrations(&self) -> &[Migration] {
113 &self.migrations
114 }
115
116 pub fn ordered_ids(&self) -> &[String] {
118 &self.ordered
119 }
120
121 pub fn resolve_id(&self, input: &str) -> Result<String, EngineError> {
123 Ok(self.graph.resolve_id(input)?)
124 }
125}