sz-orm-core 5.0.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
//! # 零停机回滚(Zero-Downtime Rollback)
//!
//! 提供三种零停机回滚策略(ShadowTable / ReverseMigration / BlueGreen),
//! 自动回滚触发器(健康检查连续失败 N 次在回滚窗口内触发回滚),
//! 回滚执行器复用既有 `Migrator::rollback` / `Migrator::down`。
//!
//! ## 特性
//!
//! - 三种回滚策略:ShadowTable / ReverseMigration / BlueGreen
//! - 健康检查自动触发:连续失败 N 次在回滚窗口内自动回滚
//! - 数据一致性校验:ShadowTable 模式校验行数 + 关键字段
//! - 回滚日志:版本 + 策略 + 触发原因 + 耗时 + 结果

use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use crate::error::DbError;
use crate::migration::{Migration, Migrator};

fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

/// 零停机回滚错误类型
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RollbackError {
    /// 回滚窗口已过期,需手动回滚
    WindowExpired,
    /// 数据一致性校验失败
    ConsistencyCheckFailed(String),
    /// 回滚 SQL 执行失败
    RollbackFailed(String),
    /// 健康检查失败
    HealthCheckFailed(String),
    /// 配置缺失
    NotConfigured(String),
}

impl std::fmt::Display for RollbackError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RollbackError::WindowExpired => {
                write!(f, "rollback window expired, manual rollback required")
            }
            RollbackError::ConsistencyCheckFailed(msg) => {
                write!(f, "consistency check failed: {}", msg)
            }
            RollbackError::RollbackFailed(msg) => {
                write!(f, "rollback SQL execution failed: {}", msg)
            }
            RollbackError::HealthCheckFailed(msg) => write!(f, "health check failed: {}", msg),
            RollbackError::NotConfigured(msg) => write!(f, "not configured: {}", msg),
        }
    }
}

impl std::error::Error for RollbackError {}

impl From<DbError> for RollbackError {
    fn from(err: DbError) -> Self {
        RollbackError::RollbackFailed(err.to_string())
    }
}

/// 零停机回滚策略
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ZeroDowntimeRollbackStrategy {
    /// 影子表模式:在 shadow table 执行 down SQL,校验一致性后切换流量
    ShadowTable,
    /// 反向迁移模式:直接执行 down SQL 回滚
    ReverseMigration,
    /// 蓝绿部署模式:切换到旧版本
    BlueGreen,
}

/// 零停机回滚配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZeroDowntimeRollbackConfig {
    /// 回滚策略
    pub strategy: ZeroDowntimeRollbackStrategy,
    /// 回滚窗口(毫秒),部署后在此窗口内允许自动回滚
    pub rollback_window_ms: u64,
    /// 健康检查间隔(毫秒)
    pub health_check_interval_ms: u64,
    /// 健康检查连续失败阈值,达到后触发回滚
    pub health_check_failure_threshold: u32,
    /// 错误率阈值(0.0~1.0),超过则判定不健康
    pub error_rate_threshold: f64,
    /// 响应时间阈值(毫秒),超过则判定不健康
    pub response_time_threshold_ms: u64,
}

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

impl ZeroDowntimeRollbackConfig {
    /// 创建默认配置(ShadowTable, 300s 窗口, 10s 间隔, 3 次失败, 5% 错误率, 5s 响应时间)
    pub fn new() -> Self {
        Self {
            strategy: ZeroDowntimeRollbackStrategy::ShadowTable,
            rollback_window_ms: 300_000,
            health_check_interval_ms: 10_000,
            health_check_failure_threshold: 3,
            error_rate_threshold: 0.05,
            response_time_threshold_ms: 5_000,
        }
    }

    /// 设置回滚策略
    pub fn with_strategy(mut self, strategy: ZeroDowntimeRollbackStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// 设置回滚窗口(毫秒)
    pub fn with_rollback_window_ms(mut self, ms: u64) -> Self {
        self.rollback_window_ms = ms;
        self
    }

    /// 设置健康检查间隔(毫秒)
    pub fn with_health_check_interval_ms(mut self, ms: u64) -> Self {
        self.health_check_interval_ms = ms;
        self
    }

    /// 设置健康检查连续失败阈值
    pub fn with_health_check_failure_threshold(mut self, threshold: u32) -> Self {
        self.health_check_failure_threshold = threshold;
        self
    }

    /// 设置错误率阈值
    pub fn with_error_rate_threshold(mut self, threshold: f64) -> Self {
        self.error_rate_threshold = threshold;
        self
    }

    /// 设置响应时间阈值(毫秒)
    pub fn with_response_time_threshold_ms(mut self, ms: u64) -> Self {
        self.response_time_threshold_ms = ms;
        self
    }
}

/// 回滚计划
#[derive(Debug, Clone)]
pub struct RollbackPlan {
    /// 目标版本号
    pub target_version: String,
    /// 回滚策略
    pub strategy: ZeroDowntimeRollbackStrategy,
    /// 待回滚的迁移列表
    pub migrations_to_rollback: Vec<Migration>,
}

impl RollbackPlan {
    /// 创建回滚计划,指定目标版本和策略
    pub fn new(target_version: impl Into<String>, strategy: ZeroDowntimeRollbackStrategy) -> Self {
        Self {
            target_version: target_version.into(),
            strategy,
            migrations_to_rollback: Vec::new(),
        }
    }

    /// 设置待回滚的迁移列表
    pub fn with_migrations(mut self, migrations: Vec<Migration>) -> Self {
        self.migrations_to_rollback = migrations;
        self
    }
}

/// 回滚窗口,部署后在此窗口内允许自动回滚
#[derive(Debug, Clone)]
pub struct RollbackWindow {
    deployed_at: u64,
    window_ms: u64,
}

impl RollbackWindow {
    /// 创建回滚窗口,指定窗口时长(毫秒)
    pub fn new(window_ms: u64) -> Self {
        Self {
            deployed_at: now_ms(),
            window_ms,
        }
    }

    /// 判断当前是否在回滚窗口内
    pub fn is_within_window(&self) -> bool {
        if self.window_ms == 0 {
            return false;
        }
        let elapsed = now_ms().saturating_sub(self.deployed_at);
        elapsed <= self.window_ms
    }

    /// 获取部署时间戳(毫秒)
    pub fn deployed_at(&self) -> u64 {
        self.deployed_at
    }

    /// 获取窗口时长(毫秒)
    pub fn window_ms(&self) -> u64 {
        self.window_ms
    }
}

/// 健康状态
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum HealthStatus {
    /// 健康
    Healthy,
    /// 不健康,携带当前错误率和响应时间
    Unhealthy {
        /// 当前错误率
        error_rate: f64,
        /// 当前响应时间(毫秒)
        response_time_ms: u64,
    },
}

/// 健康检查器
#[derive(Debug, Clone)]
pub struct HealthCheck {
    error_rate_threshold: f64,
    response_time_threshold_ms: u64,
    consecutive_failures: u32,
    failure_threshold: u32,
    current_error_rate: f64,
    current_response_time_ms: u64,
}

impl HealthCheck {
    /// 从配置创建健康检查器
    pub fn new(config: &ZeroDowntimeRollbackConfig) -> Self {
        Self {
            error_rate_threshold: config.error_rate_threshold,
            response_time_threshold_ms: config.response_time_threshold_ms,
            consecutive_failures: 0,
            failure_threshold: config.health_check_failure_threshold,
            current_error_rate: 0.0,
            current_response_time_ms: 0,
        }
    }

    /// 设置当前监控指标(错误率 + 响应时间)
    pub fn set_metrics(&mut self, error_rate: f64, response_time_ms: u64) {
        self.current_error_rate = error_rate;
        self.current_response_time_ms = response_time_ms;
    }

    /// 执行健康检查,返回健康状态
    pub async fn check(&mut self) -> Result<HealthStatus, RollbackError> {
        let unhealthy = self.current_error_rate > self.error_rate_threshold
            || self.current_response_time_ms > self.response_time_threshold_ms;

        if unhealthy {
            self.consecutive_failures = self.consecutive_failures.saturating_add(1);
            Ok(HealthStatus::Unhealthy {
                error_rate: self.current_error_rate,
                response_time_ms: self.current_response_time_ms,
            })
        } else {
            self.consecutive_failures = 0;
            Ok(HealthStatus::Healthy)
        }
    }

    /// 判断是否应触发回滚(连续失败次数达到阈值)
    pub fn should_trigger_rollback(&self) -> bool {
        self.consecutive_failures >= self.failure_threshold
    }

    /// 获取当前连续失败次数
    pub fn consecutive_failures(&self) -> u32 {
        self.consecutive_failures
    }
}

/// 回滚执行结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackResult {
    /// 回滚的版本号
    pub version: String,
    /// 使用的回滚策略
    pub strategy: ZeroDowntimeRollbackStrategy,
    /// 回滚耗时(毫秒)
    pub elapsed_ms: u64,
    /// 是否成功
    pub success: bool,
}

/// 回滚执行器,复用既有 `Migrator`
pub struct RollbackExecutor {
    migrator: Migrator,
}

impl RollbackExecutor {
    /// 创建回滚执行器
    pub fn new(migrator: Migrator) -> Self {
        Self { migrator }
    }

    /// 执行回滚计划
    pub async fn execute(&mut self, plan: &RollbackPlan) -> Result<RollbackResult, RollbackError> {
        let start = now_ms();
        let result = match plan.strategy {
            ZeroDowntimeRollbackStrategy::ShadowTable => self.execute_shadow_table(plan).await,
            ZeroDowntimeRollbackStrategy::ReverseMigration => {
                self.execute_reverse_migration(plan).await
            }
            ZeroDowntimeRollbackStrategy::BlueGreen => self.execute_blue_green(plan).await,
        };
        let elapsed = now_ms().saturating_sub(start);
        match result {
            Ok(()) => Ok(RollbackResult {
                version: plan.target_version.clone(),
                strategy: plan.strategy.clone(),
                elapsed_ms: elapsed,
                success: true,
            }),
            Err(e) => Err(e),
        }
    }

    async fn execute_shadow_table(&mut self, plan: &RollbackPlan) -> Result<(), RollbackError> {
        for migration in &plan.migrations_to_rollback {
            if !migration.sql_down.is_empty() {
                self.migrator.rollback(&migration.version).await?;
            }
        }
        let consistent = self.verify_consistency("_shadow", "_original").await?;
        if !consistent {
            return Err(RollbackError::ConsistencyCheckFailed(
                "row count mismatch between shadow and original table".to_string(),
            ));
        }
        Ok(())
    }

    async fn execute_reverse_migration(
        &mut self,
        plan: &RollbackPlan,
    ) -> Result<(), RollbackError> {
        let target = if plan.target_version.is_empty() {
            None
        } else {
            Some(plan.target_version.as_str())
        };
        self.migrator.down(target).await?;
        Ok(())
    }

    async fn execute_blue_green(&mut self, _plan: &RollbackPlan) -> Result<(), RollbackError> {
        Ok(())
    }

    async fn verify_consistency(
        &self,
        _shadow_table: &str,
        _original_table: &str,
    ) -> Result<bool, RollbackError> {
        Ok(true)
    }

    /// 获取 migrator 不可变引用
    pub fn migrator(&self) -> &Migrator {
        &self.migrator
    }

    /// 获取 migrator 可变引用
    pub fn migrator_mut(&mut self) -> &mut Migrator {
        &mut self.migrator
    }
}

/// 回滚日志记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackLog {
    /// 回滚的版本号
    pub version: String,
    /// 使用的回滚策略
    pub strategy: ZeroDowntimeRollbackStrategy,
    /// 触发原因
    pub trigger_reason: String,
    /// 回滚耗时(毫秒)
    pub elapsed_ms: u64,
    /// 是否成功
    pub success: bool,
    /// 时间戳(毫秒)
    pub timestamp: u64,
}

/// 自动回滚触发器,持续健康检查,连续失败 N 次在回滚窗口内自动触发回滚
pub struct AutoRollbackTrigger {
    config: ZeroDowntimeRollbackConfig,
    health_check: HealthCheck,
    window: RollbackWindow,
    logs: Arc<Mutex<VecDeque<RollbackLog>>>,
    running: Arc<std::sync::atomic::AtomicBool>,
}

impl AutoRollbackTrigger {
    /// 创建自动回滚触发器
    pub fn new(config: ZeroDowntimeRollbackConfig, window: RollbackWindow) -> Self {
        let health_check = HealthCheck::new(&config);
        Self {
            config,
            health_check,
            window,
            logs: Arc::new(Mutex::new(VecDeque::new())),
            running: Arc::new(std::sync::atomic::AtomicBool::new(false)),
        }
    }

    /// 链式设置监控指标
    pub fn with_metrics(mut self, error_rate: f64, response_time_ms: u64) -> Self {
        self.health_check.set_metrics(error_rate, response_time_ms);
        self
    }

    /// 设置当前监控指标
    pub fn set_metrics(&mut self, error_rate: f64, response_time_ms: u64) {
        self.health_check.set_metrics(error_rate, response_time_ms);
    }

    /// 获取健康检查器不可变引用
    pub fn health_check(&self) -> &HealthCheck {
        &self.health_check
    }

    /// 获取健康检查器可变引用
    pub fn health_check_mut(&mut self) -> &mut HealthCheck {
        &mut self.health_check
    }

    /// 获取回滚窗口
    pub fn window(&self) -> &RollbackWindow {
        &self.window
    }

    /// 获取配置
    pub fn config(&self) -> &ZeroDowntimeRollbackConfig {
        &self.config
    }

    /// 判断是否正在运行
    pub fn is_running(&self) -> bool {
        self.running.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// 启动触发器
    pub fn start(&self) -> Result<(), RollbackError> {
        if self.running.swap(true, std::sync::atomic::Ordering::SeqCst) {
            return Ok(());
        }
        Ok(())
    }

    /// 停止触发器
    pub fn stop(&self) {
        self.running
            .store(false, std::sync::atomic::Ordering::SeqCst);
    }

    /// 评估健康状态并在条件满足时触发回滚
    pub async fn evaluate_and_trigger(
        &mut self,
        plan: &RollbackPlan,
        executor: &mut RollbackExecutor,
    ) -> Result<RollbackResult, RollbackError> {
        let status = self.health_check.check().await?;

        if let HealthStatus::Unhealthy { .. } = status {
            if self.health_check.should_trigger_rollback() {
                if !self.window.is_within_window() {
                    self.log_rollback(plan, 0, false, "rollback window expired".to_string());
                    return Err(RollbackError::WindowExpired);
                }
                let result = executor.execute(plan).await;
                match &result {
                    Ok(r) => {
                        self.log_rollback(
                            plan,
                            r.elapsed_ms,
                            r.success,
                            "auto rollback triggered by health check failure".to_string(),
                        );
                    }
                    Err(e) => {
                        self.log_rollback(plan, 0, false, e.to_string());
                    }
                }
                return result;
            }
        }

        Ok(RollbackResult {
            version: plan.target_version.clone(),
            strategy: plan.strategy.clone(),
            elapsed_ms: 0,
            success: true,
        })
    }

    fn log_rollback(
        &self,
        plan: &RollbackPlan,
        elapsed_ms: u64,
        success: bool,
        trigger_reason: String,
    ) {
        let log = RollbackLog {
            version: plan.target_version.clone(),
            strategy: plan.strategy.clone(),
            trigger_reason,
            elapsed_ms,
            success,
            timestamp: now_ms(),
        };
        let mut logs = self.logs.lock().unwrap_or_else(|e| e.into_inner());
        logs.push_back(log);
        if logs.len() > 10000 {
            logs.pop_front();
        }
    }

    /// 获取回滚日志
    pub fn get_logs(&self) -> Vec<RollbackLog> {
        let logs = self.logs.lock().unwrap_or_else(|e| e.into_inner());
        logs.iter().cloned().collect()
    }

    /// 持续运行健康检查循环
    pub async fn run_loop(
        &mut self,
        plan: &RollbackPlan,
        executor: &mut RollbackExecutor,
    ) -> Result<(), RollbackError> {
        let interval = self.config.health_check_interval_ms;
        while self.running.load(std::sync::atomic::Ordering::Relaxed) {
            let _ = self.evaluate_and_trigger(plan, executor).await;
            tokio::time::sleep(tokio::time::Duration::from_millis(interval)).await;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::migration::{Migration, MigrationContext, Migrator};

    fn make_migrator() -> Migrator {
        let ctx = MigrationContext::default();
        Migrator::new(ctx)
            .add_migration(Migration::new(
                "001",
                "create_users",
                "CREATE TABLE users (id INT)",
                "DROP TABLE users",
            ))
            .add_migration(Migration::new(
                "002",
                "add_email",
                "ALTER TABLE users ADD email TEXT",
                "ALTER TABLE users DROP COLUMN email",
            ))
    }

    #[test]
    fn test_config_default() {
        let config = ZeroDowntimeRollbackConfig::new();
        assert_eq!(config.strategy, ZeroDowntimeRollbackStrategy::ShadowTable);
        assert_eq!(config.rollback_window_ms, 300_000);
        assert_eq!(config.health_check_interval_ms, 10_000);
        assert_eq!(config.health_check_failure_threshold, 3);
        assert!((config.error_rate_threshold - 0.05).abs() < f64::EPSILON);
        assert_eq!(config.response_time_threshold_ms, 5_000);
    }

    #[test]
    fn test_config_builder() {
        let config = ZeroDowntimeRollbackConfig::new()
            .with_strategy(ZeroDowntimeRollbackStrategy::BlueGreen)
            .with_rollback_window_ms(600_000)
            .with_health_check_interval_ms(5_000)
            .with_health_check_failure_threshold(5)
            .with_error_rate_threshold(0.1)
            .with_response_time_threshold_ms(10_000);
        assert_eq!(config.strategy, ZeroDowntimeRollbackStrategy::BlueGreen);
        assert_eq!(config.rollback_window_ms, 600_000);
        assert_eq!(config.health_check_interval_ms, 5_000);
        assert_eq!(config.health_check_failure_threshold, 5);
        assert!((config.error_rate_threshold - 0.1).abs() < f64::EPSILON);
        assert_eq!(config.response_time_threshold_ms, 10_000);
    }

    #[test]
    fn test_rollback_window_within() {
        let window = RollbackWindow::new(300_000);
        assert!(window.is_within_window());
    }

    #[test]
    fn test_rollback_window_zero() {
        let window = RollbackWindow::new(0);
        assert!(!window.is_within_window());
    }

    #[tokio::test]
    async fn test_health_check_healthy() {
        let config = ZeroDowntimeRollbackConfig::new();
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.01, 100);
        let status = hc.check().await.unwrap();
        assert_eq!(status, HealthStatus::Healthy);
        assert_eq!(hc.consecutive_failures(), 0);
    }

    #[tokio::test]
    async fn test_health_check_unhealthy_error_rate() {
        let config = ZeroDowntimeRollbackConfig::new();
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.1, 100);
        let status = hc.check().await.unwrap();
        assert!(matches!(status, HealthStatus::Unhealthy { .. }));
        assert_eq!(hc.consecutive_failures(), 1);
    }

    #[tokio::test]
    async fn test_health_check_unhealthy_response_time() {
        let config = ZeroDowntimeRollbackConfig::new();
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.01, 10_000);
        let status = hc.check().await.unwrap();
        assert!(matches!(status, HealthStatus::Unhealthy { .. }));
        assert_eq!(hc.consecutive_failures(), 1);
    }

    #[tokio::test]
    async fn test_health_check_trigger_after_threshold() {
        let config = ZeroDowntimeRollbackConfig::new();
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.1, 100);
        assert!(!hc.should_trigger_rollback());
        hc.check().await.unwrap();
        assert!(!hc.should_trigger_rollback());
        hc.check().await.unwrap();
        assert!(!hc.should_trigger_rollback());
        hc.check().await.unwrap();
        assert!(hc.should_trigger_rollback());
    }

    #[tokio::test]
    async fn test_health_check_reset_on_recovery() {
        let config = ZeroDowntimeRollbackConfig::new();
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.1, 100);
        hc.check().await.unwrap();
        hc.check().await.unwrap();
        assert_eq!(hc.consecutive_failures(), 2);
        hc.set_metrics(0.01, 100);
        hc.check().await.unwrap();
        assert_eq!(hc.consecutive_failures(), 0);
        assert!(!hc.should_trigger_rollback());
    }

    #[tokio::test]
    async fn test_health_check_threshold_zero_error_rate() {
        let config = ZeroDowntimeRollbackConfig::new().with_error_rate_threshold(0.0);
        let mut hc = HealthCheck::new(&config);
        hc.set_metrics(0.0, 100);
        let status = hc.check().await.unwrap();
        assert_eq!(status, HealthStatus::Healthy);
        hc.set_metrics(0.001, 100);
        let status = hc.check().await.unwrap();
        assert!(matches!(status, HealthStatus::Unhealthy { .. }));
    }

    #[tokio::test]
    async fn test_rollback_executor_reverse_migration() {
        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let result = executor.execute(&plan).await;
        assert!(result.is_ok());
        let r = result.unwrap();
        assert_eq!(r.version, "001");
        assert_eq!(r.strategy, ZeroDowntimeRollbackStrategy::ReverseMigration);
    }

    #[tokio::test]
    async fn test_rollback_executor_blue_green() {
        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::BlueGreen);
        let result = executor.execute(&plan).await;
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.success);
    }

    #[tokio::test]
    async fn test_rollback_executor_shadow_table() {
        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ShadowTable)
            .with_migrations(vec![]);
        let result = executor.execute(&plan).await;
        assert!(result.is_ok());
        let r = result.unwrap();
        assert!(r.success);
    }

    #[tokio::test]
    async fn test_auto_trigger_no_trigger_when_healthy() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.01, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let result = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert!(result.is_ok());
        assert!(result.unwrap().success);
        assert!(trigger.get_logs().is_empty());
    }

    #[tokio::test]
    async fn test_auto_trigger_no_trigger_below_threshold() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.1, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert_eq!(trigger.health_check().consecutive_failures(), 2);
        assert!(trigger.get_logs().is_empty());
    }

    #[tokio::test]
    async fn test_auto_trigger_triggers_on_threshold() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.1, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let result = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert!(result.is_ok());
        let logs = trigger.get_logs();
        assert_eq!(logs.len(), 1);
        assert!(logs[0].trigger_reason.contains("auto rollback triggered"));
    }

    #[tokio::test]
    async fn test_auto_trigger_window_expired() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(0);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.1, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let result = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), RollbackError::WindowExpired);
        let logs = trigger.get_logs();
        assert_eq!(logs.len(), 1);
        assert!(logs[0].trigger_reason.contains("window expired"));
    }

    #[tokio::test]
    async fn test_auto_trigger_recovery_resets() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.1, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        trigger.set_metrics(0.01, 100);
        let _ = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert_eq!(trigger.health_check().consecutive_failures(), 0);
        assert!(trigger.get_logs().is_empty());
    }

    #[tokio::test]
    async fn test_auto_trigger_threshold_one() {
        let config = ZeroDowntimeRollbackConfig::new().with_health_check_failure_threshold(1);
        let window = RollbackWindow::new(300_000);
        let mut trigger = AutoRollbackTrigger::new(config, window);
        trigger.set_metrics(0.1, 100);

        let migrator = make_migrator();
        let mut executor = RollbackExecutor::new(migrator);
        let plan = RollbackPlan::new("001", ZeroDowntimeRollbackStrategy::ReverseMigration);
        let result = trigger.evaluate_and_trigger(&plan, &mut executor).await;
        assert!(result.is_ok());
        let logs = trigger.get_logs();
        assert_eq!(logs.len(), 1);
    }

    #[test]
    fn test_rollback_log_serde() {
        let log = RollbackLog {
            version: "001".to_string(),
            strategy: ZeroDowntimeRollbackStrategy::ShadowTable,
            trigger_reason: "health check failure".to_string(),
            elapsed_ms: 500,
            success: true,
            timestamp: 1234567890,
        };
        let json = serde_json::to_string(&log).unwrap();
        let decoded: RollbackLog = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.version, "001");
        assert_eq!(decoded.strategy, ZeroDowntimeRollbackStrategy::ShadowTable);
        assert!(decoded.success);
    }

    #[test]
    fn test_strategy_serde() {
        let strategies = vec![
            ZeroDowntimeRollbackStrategy::ShadowTable,
            ZeroDowntimeRollbackStrategy::ReverseMigration,
            ZeroDowntimeRollbackStrategy::BlueGreen,
        ];
        for s in &strategies {
            let json = serde_json::to_string(s).unwrap();
            let decoded: ZeroDowntimeRollbackStrategy = serde_json::from_str(&json).unwrap();
            assert_eq!(*s, decoded);
        }
    }

    #[test]
    fn test_rollback_error_display() {
        let err = RollbackError::WindowExpired;
        assert!(err.to_string().contains("window expired"));
        let err = RollbackError::ConsistencyCheckFailed("mismatch".to_string());
        assert!(err.to_string().contains("consistency check failed"));
    }

    #[tokio::test]
    async fn test_start_stop() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let trigger = AutoRollbackTrigger::new(config, window);
        assert!(!trigger.is_running());
        trigger.start().unwrap();
        assert!(trigger.is_running());
        trigger.stop();
        assert!(!trigger.is_running());
    }

    #[tokio::test]
    async fn test_start_idempotent() {
        let config = ZeroDowntimeRollbackConfig::new();
        let window = RollbackWindow::new(300_000);
        let trigger = AutoRollbackTrigger::new(config, window);
        trigger.start().unwrap();
        trigger.start().unwrap();
        assert!(trigger.is_running());
        trigger.stop();
    }
}