rat_quickdb 0.5.2

强大的跨数据库ODM库,支持自动索引创建、统一接口和现代异步架构
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! 表版本管理
//!
//! 提供表模式版本控制和迁移功能

use super::schema::TableSchema;
use crate::error::{QuickDbError, QuickDbResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 模式版本信息
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SchemaVersion {
    /// 表名
    pub table_name: String,
    /// 版本号
    pub version: u32,
    /// 模式定义
    pub schema: TableSchema,
    /// 创建时间
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// 迁移脚本
    pub migration_script: Option<MigrationScript>,
    /// 版本描述
    pub description: Option<String>,
    /// 是否为当前版本
    pub is_current: bool,
}

/// 迁移脚本
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MigrationScript {
    /// 脚本ID
    pub id: String,
    /// 源版本
    pub from_version: u32,
    /// 目标版本
    pub to_version: u32,
    /// 升级脚本
    pub up_script: String,
    /// 降级脚本
    pub down_script: Option<String>,
    /// 脚本类型
    pub script_type: MigrationScriptType,
    /// 创建时间
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// 执行时间
    pub executed_at: Option<chrono::DateTime<chrono::Utc>>,
    /// 执行状态
    pub status: MigrationStatus,
}

/// 迁移脚本类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MigrationScriptType {
    /// DDL脚本(结构变更)
    Ddl,
    /// DML脚本(数据变更)
    Dml,
    /// 混合脚本
    Mixed,
    /// 自定义脚本
    Custom,
}

/// 迁移状态
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum MigrationStatus {
    /// 待执行
    Pending,
    /// 执行中
    Running,
    /// 执行成功
    Success,
    /// 执行失败
    Failed,
    /// 已回滚
    Rollback,
}

/// 版本管理器
#[derive(Debug)]
pub struct VersionManager {
    /// 版本存储
    versions: HashMap<String, Vec<SchemaVersion>>,
    /// 迁移历史
    migration_history: Vec<MigrationRecord>,
}

/// 迁移记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MigrationRecord {
    /// 记录ID
    pub id: String,
    /// 表名
    pub table_name: String,
    /// 迁移脚本ID
    pub migration_id: String,
    /// 源版本
    pub from_version: u32,
    /// 目标版本
    pub to_version: u32,
    /// 执行开始时间
    pub started_at: chrono::DateTime<chrono::Utc>,
    /// 执行结束时间
    pub completed_at: Option<chrono::DateTime<chrono::Utc>>,
    /// 执行状态
    pub status: MigrationStatus,
    /// 错误信息
    pub error_message: Option<String>,
    /// 执行耗时(毫秒)
    pub duration_ms: Option<u64>,
}

impl VersionManager {
    /// 创建新的版本管理器
    pub fn new() -> Self {
        Self {
            versions: HashMap::new(),
            migration_history: Vec::new(),
        }
    }

    /// 注册表的新版本
    pub fn register_version(
        &mut self,
        table_name: String,
        schema: TableSchema,
        description: Option<String>,
    ) -> QuickDbResult<u32> {
        let versions = self
            .versions
            .entry(table_name.clone())
            .or_insert_with(Vec::new);

        // 计算新版本号
        let new_version = versions.iter().map(|v| v.version).max().unwrap_or(0) + 1;

        // 将之前的版本标记为非当前版本
        for version in versions.iter_mut() {
            version.is_current = false;
        }

        // 创建新版本
        let schema_version = SchemaVersion {
            table_name: table_name.clone(),
            version: new_version,
            schema,
            created_at: chrono::Utc::now(),
            migration_script: None,
            description,
            is_current: true,
        };

        versions.push(schema_version);

        Ok(new_version)
    }

    /// 获取表的当前版本
    pub fn get_current_version(&self, table_name: &str) -> Option<&SchemaVersion> {
        self.versions.get(table_name)?.iter().find(|v| v.is_current)
    }

    /// 获取表的指定版本
    pub fn get_version(&self, table_name: &str, version: u32) -> Option<&SchemaVersion> {
        self.versions
            .get(table_name)?
            .iter()
            .find(|v| v.version == version)
    }

    /// 获取表的所有版本
    pub fn get_all_versions(&self, table_name: &str) -> Option<&Vec<SchemaVersion>> {
        self.versions.get(table_name)
    }

    /// 获取表的版本历史
    pub fn get_version_history(&self, table_name: &str) -> Vec<&SchemaVersion> {
        if let Some(versions) = self.versions.get(table_name) {
            let mut sorted_versions: Vec<&SchemaVersion> = versions.iter().collect();
            sorted_versions.sort_by(|a, b| a.version.cmp(&b.version));
            sorted_versions
        } else {
            Vec::new()
        }
    }

    /// 创建迁移脚本
    pub fn create_migration(
        &mut self,
        table_name: &str,
        from_version: u32,
        to_version: u32,
        up_script: String,
        down_script: Option<String>,
        script_type: MigrationScriptType,
    ) -> QuickDbResult<String> {
        // 验证版本存在
        if self.get_version(table_name, from_version).is_none() {
            return Err(QuickDbError::ValidationError {
                field: "from_version".to_string(),
                message: format!("源版本 {} 不存在", from_version),
            });
        }

        if self.get_version(table_name, to_version).is_none() {
            return Err(QuickDbError::ValidationError {
                field: "to_version".to_string(),
                message: format!("目标版本 {} 不存在", to_version),
            });
        }

        let migration_id = format!(
            "{}_{}_to_{}_{}_{}",
            table_name,
            from_version,
            to_version,
            chrono::Utc::now().timestamp(),
            uuid::Uuid::new_v4().to_string()[..8].to_string()
        );

        let migration = MigrationScript {
            id: migration_id.clone(),
            from_version,
            to_version,
            up_script,
            down_script,
            script_type,
            created_at: chrono::Utc::now(),
            executed_at: None,
            status: MigrationStatus::Pending,
        };

        // 将迁移脚本添加到对应版本
        if let Some(versions) = self.versions.get_mut(table_name) {
            if let Some(version) = versions.iter_mut().find(|v| v.version == to_version) {
                version.migration_script = Some(migration);
            }
        }

        Ok(migration_id)
    }

    /// 执行迁移
    pub async fn execute_migration(
        &mut self,
        table_name: &str,
        migration_id: &str,
    ) -> QuickDbResult<()> {
        // 先获取迁移信息,避免借用冲突
        let (from_version, to_version) = {
            let migration = self
                .find_migration_mut(table_name, migration_id)
                .ok_or_else(|| QuickDbError::ValidationError {
                    field: "migration_id".to_string(),
                    message: format!("迁移脚本 {} 不存在", migration_id),
                })?;

            // 检查迁移状态
            if migration.status != MigrationStatus::Pending {
                return Err(QuickDbError::ValidationError {
                    field: "migration_status".to_string(),
                    message: format!(
                        "迁移脚本 {} 状态不正确: {:?}",
                        migration_id, migration.status
                    ),
                });
            }

            let start_time = chrono::Utc::now();
            migration.status = MigrationStatus::Running;

            (migration.from_version, migration.to_version)
        };

        let start_time = chrono::Utc::now();

        // 创建迁移记录
        let record = MigrationRecord {
            id: uuid::Uuid::new_v4().to_string(),
            table_name: table_name.to_string(),
            migration_id: migration_id.to_string(),
            from_version,
            to_version,
            started_at: start_time,
            completed_at: None,
            status: MigrationStatus::Running,
            error_message: None,
            duration_ms: None,
        };

        self.migration_history.push(record);

        // TODO: 实际执行迁移脚本
        // 这里需要与数据库适配器集成

        let end_time = chrono::Utc::now();
        let duration = end_time.signed_duration_since(start_time);

        // 更新迁移状态
        if let Some(migration) = self.find_migration_mut(table_name, migration_id) {
            migration.status = MigrationStatus::Success;
            migration.executed_at = Some(end_time);
        }

        // 更新迁移记录
        if let Some(record) = self.migration_history.last_mut() {
            record.completed_at = Some(end_time);
            record.status = MigrationStatus::Success;
            record.duration_ms = Some(duration.num_milliseconds() as u64);
        }

        Ok(())
    }

    /// 回滚迁移
    pub async fn rollback_migration(
        &mut self,
        table_name: &str,
        migration_id: &str,
    ) -> QuickDbResult<()> {
        // 先获取迁移信息,避免借用冲突
        let (from_version, to_version, down_script) = {
            let migration = self
                .find_migration_mut(table_name, migration_id)
                .ok_or_else(|| QuickDbError::ValidationError {
                    field: "migration_id".to_string(),
                    message: format!("迁移脚本 {} 不存在", migration_id),
                })?;

            // 检查是否有回滚脚本
            let down_script =
                migration
                    .down_script
                    .clone()
                    .ok_or_else(|| QuickDbError::ValidationError {
                        field: "down_script".to_string(),
                        message: format!("迁移脚本 {} 没有回滚脚本", migration_id),
                    })?;

            // 检查迁移状态
            if migration.status != MigrationStatus::Success {
                return Err(QuickDbError::ValidationError {
                    field: "migration_status".to_string(),
                    message: format!(
                        "迁移脚本 {} 状态不正确,无法回滚: {:?}",
                        migration_id, migration.status
                    ),
                });
            }

            (migration.to_version, migration.from_version, down_script)
        };

        let start_time = chrono::Utc::now();

        // 创建回滚记录
        let record = MigrationRecord {
            id: uuid::Uuid::new_v4().to_string(),
            table_name: table_name.to_string(),
            migration_id: format!("{}_rollback", migration_id),
            from_version,
            to_version,
            started_at: start_time,
            completed_at: None,
            status: MigrationStatus::Running,
            error_message: None,
            duration_ms: None,
        };

        self.migration_history.push(record);

        // TODO: 实际执行回滚脚本
        // 这里需要与数据库适配器集成

        let end_time = chrono::Utc::now();
        let duration = end_time.signed_duration_since(start_time);

        // 更新迁移状态
        if let Some(migration) = self.find_migration_mut(table_name, migration_id) {
            migration.status = MigrationStatus::Rollback;
        }

        // 更新迁移记录
        if let Some(record) = self.migration_history.last_mut() {
            record.completed_at = Some(end_time);
            record.status = MigrationStatus::Success;
            record.duration_ms = Some(duration.num_milliseconds() as u64);
        }

        Ok(())
    }

    /// 获取迁移历史
    pub fn get_migration_history(&self, table_name: Option<&str>) -> Vec<&MigrationRecord> {
        if let Some(table) = table_name {
            self.migration_history
                .iter()
                .filter(|record| record.table_name == table)
                .collect()
        } else {
            self.migration_history.iter().collect()
        }
    }

    /// 检查是否需要迁移
    pub fn needs_migration(&self, table_name: &str, current_db_version: u32) -> bool {
        if let Some(current_version) = self.get_current_version(table_name) {
            current_version.version > current_db_version
        } else {
            false
        }
    }

    /// 获取迁移路径
    pub fn get_migration_path(
        &self,
        table_name: &str,
        from_version: u32,
        to_version: u32,
    ) -> QuickDbResult<Vec<&MigrationScript>> {
        if from_version == to_version {
            return Ok(Vec::new());
        }

        let versions =
            self.versions
                .get(table_name)
                .ok_or_else(|| QuickDbError::ValidationError {
                    field: "table_name".to_string(),
                    message: format!("{} 不存在", table_name),
                })?;

        let mut path = Vec::new();

        if from_version < to_version {
            // 升级路径
            for version in (from_version + 1)..=to_version {
                if let Some(schema_version) = versions.iter().find(|v| v.version == version) {
                    if let Some(migration) = &schema_version.migration_script {
                        path.push(migration);
                    } else {
                        return Err(QuickDbError::ValidationError {
                            field: "migration_script".to_string(),
                            message: format!("版本 {} 缺少迁移脚本", version),
                        });
                    }
                } else {
                    return Err(QuickDbError::ValidationError {
                        field: "version".to_string(),
                        message: format!("版本 {} 不存在", version),
                    });
                }
            }
        } else {
            // 降级路径
            for version in (to_version + 1..=from_version).rev() {
                if let Some(schema_version) = versions.iter().find(|v| v.version == version) {
                    if let Some(migration) = &schema_version.migration_script {
                        if migration.down_script.is_some() {
                            path.push(migration);
                        } else {
                            return Err(QuickDbError::ValidationError {
                                field: "down_script".to_string(),
                                message: format!("版本 {} 缺少回滚脚本", version),
                            });
                        }
                    } else {
                        return Err(QuickDbError::ValidationError {
                            field: "migration_script".to_string(),
                            message: format!("版本 {} 缺少迁移脚本", version),
                        });
                    }
                } else {
                    return Err(QuickDbError::ValidationError {
                        field: "version".to_string(),
                        message: format!("版本 {} 不存在", version),
                    });
                }
            }
        }

        Ok(path)
    }

    /// 查找迁移脚本(可变引用)
    fn find_migration_mut(
        &mut self,
        table_name: &str,
        migration_id: &str,
    ) -> Option<&mut MigrationScript> {
        self.versions.get_mut(table_name)?.iter_mut().find_map(|v| {
            if let Some(migration) = &mut v.migration_script {
                if migration.id == migration_id {
                    Some(migration)
                } else {
                    None
                }
            } else {
                None
            }
        })
    }

    /// 清理历史记录
    pub fn cleanup_history(&mut self, keep_days: u32) {
        let cutoff_date = chrono::Utc::now() - chrono::Duration::days(keep_days as i64);

        self.migration_history
            .retain(|record| record.started_at > cutoff_date);
    }

    /// 导出版本信息
    pub fn export_versions(&self) -> QuickDbResult<String> {
        serde_json::to_string_pretty(&self.versions).map_err(|e| QuickDbError::SerializationError {
            message: e.to_string(),
        })
    }

    /// 导入版本信息
    pub fn import_versions(&mut self, data: &str) -> QuickDbResult<()> {
        let versions: HashMap<String, Vec<SchemaVersion>> =
            serde_json::from_str(data).map_err(|e| QuickDbError::SerializationError {
                message: e.to_string(),
            })?;

        self.versions = versions;
        Ok(())
    }
}

impl Default for VersionManager {
    fn default() -> Self {
        Self::new()
    }
}