db_migrate/
lib.rs

1pub mod commands;
2pub mod config;
3pub mod migration;
4pub mod schema;
5pub mod utils;
6
7use serde::{Deserialize, Serialize};
8use chrono::{DateTime, Utc};
9
10/// Represents a migration record in the database
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct MigrationRecord {
13    pub version: String,
14    pub applied_at: DateTime<Utc>,
15    pub checksum: String,
16    pub description: String,
17}
18
19/// Represents a migration file on disk
20#[derive(Debug, Clone)]
21pub struct MigrationFile {
22    pub version: String,
23    pub description: String,
24    pub file_path: std::path::PathBuf,
25    pub content: String,
26    pub checksum: String,
27}
28
29/// Represents the result of a command execution
30#[derive(Debug, Serialize)]
31pub struct CommandOutput {
32    pub success: bool,
33    pub message: String,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub data: Option<serde_json::Value>,
36}
37
38impl CommandOutput {
39    pub fn success(message: impl Into<String>) -> Self {
40        Self {
41            success: true,
42            message: message.into(),
43            data: None,
44        }
45    }
46
47    pub fn success_with_data(message: impl Into<String>, data: serde_json::Value) -> Self {
48        Self {
49            success: true,
50            message: message.into(),
51            data: Some(data),
52        }
53    }
54
55    pub fn error(message: impl Into<String>) -> Self {
56        Self {
57            success: false,
58            message: message.into(),
59            data: None,
60        }
61    }
62}
63
64impl std::fmt::Display for CommandOutput {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{}", self.message)
67    }
68}
69
70/// Custom error types for the migration tool
71#[derive(thiserror::Error, Debug)]
72pub enum MigrationError {
73    #[error("Database connection error: {0}")]
74    DatabaseError(#[from] scylla::transport::errors::NewSessionError),
75
76    #[error("Query execution error: {0}")]
77    QueryError(#[from] scylla::transport::errors::QueryError),
78
79    #[error("Migration file error: {0}")]
80    FileError(#[from] std::io::Error),
81
82    #[error("Configuration error: {0}")]
83    ConfigError(String),
84
85    #[error("Migration integrity error: {0}")]
86    IntegrityError(String),
87
88    #[error("Migration not found: {0}")]
89    MigrationNotFound(String),
90
91    #[error("Checksum mismatch for migration {version}: expected {expected}, got {actual}")]
92    ChecksumMismatch {
93        version: String,
94        expected: String,
95        actual: String,
96    },
97
98    #[error("Cannot rollback migration {version}: {reason}")]
99    RollbackError { version: String, reason: String },
100
101    #[error("Migration {version} is already applied")]
102    AlreadyApplied { version: String },
103
104    #[error("Invalid migration format: {0}")]
105    InvalidFormat(String),
106}