elif_orm/migrations/
definitions.rs

1//! Migration Definitions - Core types and structures for migrations
2//!
3//! Defines the fundamental types used throughout the migration system including
4//! Migration, MigrationRecord, and MigrationConfig structures.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::path::PathBuf;
9
10/// Represents a database migration
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Migration {
13    /// Unique identifier for the migration (typically timestamp)
14    pub id: String,
15    /// Human-readable name for the migration
16    pub name: String,
17    /// SQL statements to apply the migration
18    pub up_sql: String,
19    /// SQL statements to rollback the migration
20    pub down_sql: String,
21    /// When the migration was created
22    pub created_at: DateTime<Utc>,
23}
24
25/// Migration status in the database
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct MigrationRecord {
28    /// Migration ID
29    pub id: String,
30    /// When the migration was applied
31    pub applied_at: DateTime<Utc>,
32    /// Batch number (for grouping migrations)
33    pub batch: i32,
34}
35
36/// Configuration for the migration system
37#[derive(Debug, Clone)]
38pub struct MigrationConfig {
39    /// Directory where migration files are stored
40    pub migrations_dir: PathBuf,
41    /// Table name for tracking migrations
42    pub migrations_table: String,
43}
44
45impl Default for MigrationConfig {
46    fn default() -> Self {
47        Self {
48            migrations_dir: PathBuf::from("migrations"),
49            migrations_table: "elif_migrations".to_string(),
50        }
51    }
52}
53
54/// Result of running migrations
55#[derive(Debug)]
56pub struct MigrationRunResult {
57    /// Number of migrations that were applied
58    pub applied_count: usize,
59    /// IDs of migrations that were applied
60    pub applied_migrations: Vec<String>,
61    /// Number of migrations that were skipped (already applied)
62    pub skipped_count: usize,
63    /// Total execution time in milliseconds
64    pub execution_time_ms: u128,
65}
66
67/// Result of rolling back migrations
68#[derive(Debug)]
69pub struct RollbackResult {
70    /// Number of migrations that were rolled back
71    pub rolled_back_count: usize,
72    /// IDs of migrations that were rolled back
73    pub rolled_back_migrations: Vec<String>,
74    /// Total execution time in milliseconds
75    pub execution_time_ms: u128,
76}
77
78/// Migration direction for execution
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub enum MigrationDirection {
81    /// Apply the migration (run UP statements)
82    Up,
83    /// Rollback the migration (run DOWN statements)
84    Down,
85}
86
87/// Migration status in the system
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub enum MigrationStatus {
90    /// Migration is pending (not yet applied)
91    Pending,
92    /// Migration has been applied
93    Applied {
94        /// When it was applied
95        applied_at: DateTime<Utc>,
96        /// Batch number
97        batch: i32,
98    },
99}