ferro-deployments 0.2.53

Immutable deployment model and atomic promote for the Ferro framework
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
//! Deployment model, status enum, and the `Deployments` handle.
//!
//! All SQL uses `Statement::from_sql_and_values` with bound `Value::*` params —
//! no string interpolation of caller-supplied data (T-186-04).

use chrono::{DateTime, Utc};
use sea_orm::{ConnectionTrait, DatabaseBackend, DatabaseConnection, Statement, Value};
use serde::{Deserialize, Serialize};

use crate::error::Error;

// ---------------------------------------------------------------------------
// Deployment — an immutable row from the `deployments` table
// ---------------------------------------------------------------------------

/// An immutable deployment row.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Deployment {
    /// Primary key.
    pub id: i64,
    /// Unique identifier (UUID v4 string, stable across retries).
    pub identifier: String,
    /// Scoping key (e.g. tenant slug or app name).
    pub owner_key: String,
    /// Optional VCS ref (branch, tag, commit SHA).
    pub source_ref: Option<String>,
    /// Storage path prefix for the deployment artifact.
    pub artifact_location: Option<String>,
    /// Artifact size in bytes.
    pub byte_size: Option<i64>,
    /// Lifecycle status.
    pub status: DeploymentStatus,
    /// When the artifact was garbage-collected; non-null means the deployment
    /// can no longer be promoted.
    pub artifact_deleted_at: Option<DateTime<Utc>>,
    /// When the deployment entered a terminal state (ready or failed).
    pub terminated_at: Option<DateTime<Utc>>,
    /// Row creation timestamp.
    pub created_at: DateTime<Utc>,
}

// ---------------------------------------------------------------------------
// DeploymentStatus enum
// ---------------------------------------------------------------------------

/// Lifecycle state of a deployment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DeploymentStatus {
    /// Build in progress; artifact not yet available.
    Building,
    /// Artifact uploaded and verified; eligible for promotion.
    Ready,
    /// Build failed; terminal state.
    Failed,
}

impl DeploymentStatus {
    fn from_str(s: &str) -> Result<Self, Error> {
        match s {
            "building" => Ok(Self::Building),
            "ready" => Ok(Self::Ready),
            "failed" => Ok(Self::Failed),
            other => Err(Error::custom(format!("unknown deployment status: {other}"))),
        }
    }
}

// ---------------------------------------------------------------------------
// Deployments handle
// ---------------------------------------------------------------------------

/// Handle to the deployments system, wrapping a `DatabaseConnection`.
///
/// All methods bind caller-supplied strings as `Value::*` parameters — no
/// string interpolation (T-186-04).
#[derive(Clone)]
pub struct Deployments {
    db: DatabaseConnection,
}

impl Deployments {
    /// Create a new `Deployments` handle backed by `db`.
    pub fn new(db: DatabaseConnection) -> Self {
        Self { db }
    }

    /// Insert a new deployment row with status `building`.
    ///
    /// Returns the complete `Deployment` row including its assigned `id`.
    pub async fn create(
        &self,
        owner_key: &str,
        source_ref: Option<&str>,
    ) -> Result<Deployment, Error> {
        let identifier = uuid::Uuid::new_v4().to_string();
        let now = Utc::now();
        let now_iso = now.to_rfc3339();
        let backend = self.db.get_database_backend();

        let (p1, p2, p3, p4) = (
            ph(backend, 1)?,
            ph(backend, 2)?,
            ph(backend, 3)?,
            ph(backend, 4)?,
        );
        let sql = format!(
            "INSERT INTO deployments \
             (identifier, owner_key, source_ref, artifact_location, byte_size, status, \
              artifact_deleted_at, terminated_at, created_at) \
             VALUES ({p1}, {p2}, {p3}, NULL, NULL, 'building', NULL, NULL, {p4}) \
             RETURNING id"
        );
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [
                Value::String(Some(Box::new(identifier.clone()))),
                Value::String(Some(Box::new(owner_key.to_string()))),
                source_ref.map_or(Value::String(None), |s| {
                    Value::String(Some(Box::new(s.to_string())))
                }),
                Value::String(Some(Box::new(now_iso.clone()))),
            ],
        );

        let row = self
            .db
            .query_one(stmt)
            .await
            .map_err(Error::Db)?
            .ok_or_else(|| Error::custom("create: INSERT RETURNING returned no row"))?;

        let id: i64 = row
            .try_get_by::<i64, _>("id")
            .map_err(|e| Error::custom(format!("create: parse id: {e}")))?;

        Ok(Deployment {
            id,
            identifier,
            owner_key: owner_key.to_string(),
            source_ref: source_ref.map(str::to_string),
            artifact_location: None,
            byte_size: None,
            status: DeploymentStatus::Building,
            artifact_deleted_at: None,
            terminated_at: None,
            created_at: now,
        })
    }

    /// Transition a `building` deployment to `ready`, recording the artifact
    /// location and byte size.
    ///
    /// Returns `Err` when the row does not exist or is already in a terminal
    /// state (`ready` or `failed`).
    pub async fn mark_ready(
        &self,
        id: i64,
        artifact_location: &str,
        byte_size: i64,
    ) -> Result<(), Error> {
        let now_iso = Utc::now().to_rfc3339();
        let backend = self.db.get_database_backend();
        let (p1, p2, p3, p4) = (
            ph(backend, 1)?,
            ph(backend, 2)?,
            ph(backend, 3)?,
            ph(backend, 4)?,
        );
        let sql = format!(
            "UPDATE deployments \
             SET status = 'ready', artifact_location = {p1}, byte_size = {p2}, terminated_at = {p3} \
             WHERE id = {p4} AND status = 'building'"
        );
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [
                Value::String(Some(Box::new(artifact_location.to_string()))),
                Value::BigInt(Some(byte_size)),
                Value::String(Some(Box::new(now_iso))),
                Value::BigInt(Some(id)),
            ],
        );
        let result = self.db.execute(stmt).await.map_err(Error::Db)?;
        if result.rows_affected() == 0 {
            // Either the row does not exist or is already terminal.
            // Distinguish: if get() returns NotFound propagate that; else it's an
            // illegal transition (already ready/failed).
            self.get(id).await?;
            return Err(Error::custom(format!(
                "deployment {id} is not in building state; transition to ready rejected"
            )));
        }
        Ok(())
    }

    /// Transition a `building` deployment to `failed`.
    ///
    /// The `error` message is emitted as a tracing warning (no error column in
    /// the schema). Returns `Err` when the row does not exist or is already
    /// terminal.
    pub async fn mark_failed(&self, id: i64, error: &str) -> Result<(), Error> {
        tracing::warn!(
            deployment_id = id,
            error = error,
            "deployment marked failed"
        );
        let now_iso = Utc::now().to_rfc3339();
        let backend = self.db.get_database_backend();
        let (p1, p2) = (ph(backend, 1)?, ph(backend, 2)?);
        let sql = format!(
            "UPDATE deployments \
             SET status = 'failed', terminated_at = {p1} \
             WHERE id = {p2} AND status = 'building'"
        );
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [
                Value::String(Some(Box::new(now_iso))),
                Value::BigInt(Some(id)),
            ],
        );
        let result = self.db.execute(stmt).await.map_err(Error::Db)?;
        if result.rows_affected() == 0 {
            self.get(id).await?;
            return Err(Error::custom(format!(
                "deployment {id} is not in building state; transition to failed rejected"
            )));
        }
        Ok(())
    }

    /// Fetch a deployment by its primary key.
    pub async fn get(&self, id: i64) -> Result<Deployment, Error> {
        let backend = self.db.get_database_backend();
        let p1 = ph(backend, 1)?;
        let sql = format!(
            "SELECT id, identifier, owner_key, source_ref, artifact_location, byte_size, \
             status, artifact_deleted_at, terminated_at, created_at \
             FROM deployments WHERE id = {p1}"
        );
        let stmt = Statement::from_sql_and_values(backend, &sql, [Value::BigInt(Some(id))]);
        let row = self
            .db
            .query_one(stmt)
            .await
            .map_err(Error::Db)?
            .ok_or(Error::NotFound { id })?;
        parse_deployment_row(&row)
    }

    /// List all deployments for `owner_key`, newest first.
    pub async fn list(&self, owner_key: &str) -> Result<Vec<Deployment>, Error> {
        let backend = self.db.get_database_backend();
        let p1 = ph(backend, 1)?;
        let sql = format!(
            "SELECT id, identifier, owner_key, source_ref, artifact_location, byte_size, \
             status, artifact_deleted_at, terminated_at, created_at \
             FROM deployments WHERE owner_key = {p1} ORDER BY id DESC"
        );
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [Value::String(Some(Box::new(owner_key.to_string())))],
        );
        let rows = self.db.query_all(stmt).await.map_err(Error::Db)?;
        rows.iter().map(parse_deployment_row).collect()
    }

    /// Return the currently-active deployment for `owner_key`, or `None` when
    /// no pointer row exists yet.
    pub async fn active(&self, owner_key: &str) -> Result<Option<Deployment>, Error> {
        let backend = self.db.get_database_backend();
        let p1 = ph(backend, 1)?;
        let sql = format!("SELECT deployment_id FROM deployment_pointers WHERE owner_key = {p1}");
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [Value::String(Some(Box::new(owner_key.to_string())))],
        );
        let row = self.db.query_one(stmt).await.map_err(Error::Db)?;
        match row {
            None => Ok(None),
            Some(r) => {
                let deployment_id: i64 = r
                    .try_get_by::<i64, _>("deployment_id")
                    .map_err(|e| Error::custom(format!("active: parse deployment_id: {e}")))?;
                self.get(deployment_id).await.map(Some)
            }
        }
    }

    /// Atomically promote `deployment_id` to be the active deployment for
    /// `owner_key`.
    ///
    /// Guards:
    /// - `Error::NotReady` when the deployment status is not `ready`.
    /// - `Error::ArtifactDeleted` when `artifact_deleted_at` is set.
    ///
    /// Returns the previously-active deployment id, or `None` when this is the
    /// first promotion.
    pub async fn promote(&self, owner_key: &str, deployment_id: i64) -> Result<Option<i64>, Error> {
        let dep = self.get(deployment_id).await?;
        if dep.status != DeploymentStatus::Ready {
            return Err(Error::NotReady { id: deployment_id });
        }
        if dep.artifact_deleted_at.is_some() {
            return Err(Error::ArtifactDeleted { id: deployment_id });
        }
        crate::promote::promote(&self.db, owner_key, deployment_id).await
    }

    /// Roll back to the previous deployment.
    ///
    /// Returns `Err(Error::NoPreviousDeployment)` when the pointer row has no
    /// `previous_deployment_id`.
    pub async fn rollback(&self, owner_key: &str) -> Result<Option<i64>, Error> {
        let backend = self.db.get_database_backend();
        let p1 = ph(backend, 1)?;
        let sql = format!(
            "SELECT previous_deployment_id FROM deployment_pointers WHERE owner_key = {p1}"
        );
        let stmt = Statement::from_sql_and_values(
            backend,
            &sql,
            [Value::String(Some(Box::new(owner_key.to_string())))],
        );
        let row = self.db.query_one(stmt).await.map_err(Error::Db)?;
        let previous_id = match row {
            None => {
                return Err(Error::NoPreviousDeployment {
                    owner_key: owner_key.to_string(),
                })
            }
            Some(r) => {
                let opt: Option<i64> = r
                    .try_get_by::<Option<i64>, _>("previous_deployment_id")
                    .map_err(|e| {
                        Error::custom(format!("rollback: parse previous_deployment_id: {e}"))
                    })?;
                opt
            }
        };
        match previous_id {
            None => Err(Error::NoPreviousDeployment {
                owner_key: owner_key.to_string(),
            }),
            Some(prev_id) => self.promote(owner_key, prev_id).await,
        }
    }
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn parse_deployment_row(row: &sea_orm::QueryResult) -> Result<Deployment, Error> {
    let id: i64 = row
        .try_get_by::<i64, _>("id")
        .map_err(|e| Error::custom(format!("parse id: {e}")))?;
    let identifier: String = row
        .try_get_by::<String, _>("identifier")
        .map_err(|e| Error::custom(format!("parse identifier: {e}")))?;
    let owner_key: String = row
        .try_get_by::<String, _>("owner_key")
        .map_err(|e| Error::custom(format!("parse owner_key: {e}")))?;
    let source_ref: Option<String> = row
        .try_get_by::<Option<String>, _>("source_ref")
        .map_err(|e| Error::custom(format!("parse source_ref: {e}")))?;
    let artifact_location: Option<String> = row
        .try_get_by::<Option<String>, _>("artifact_location")
        .map_err(|e| Error::custom(format!("parse artifact_location: {e}")))?;
    let byte_size: Option<i64> = row
        .try_get_by::<Option<i64>, _>("byte_size")
        .map_err(|e| Error::custom(format!("parse byte_size: {e}")))?;
    let status_str: String = row
        .try_get_by::<String, _>("status")
        .map_err(|e| Error::custom(format!("parse status: {e}")))?;
    let status = DeploymentStatus::from_str(&status_str)?;
    let artifact_deleted_at = parse_optional_timestamp(row, "artifact_deleted_at")?;
    let terminated_at = parse_optional_timestamp(row, "terminated_at")?;
    let created_at = parse_timestamp(row, "created_at")?;

    Ok(Deployment {
        id,
        identifier,
        owner_key,
        source_ref,
        artifact_location,
        byte_size,
        status,
        artifact_deleted_at,
        terminated_at,
        created_at,
    })
}

/// Parse a required timestamp column — Postgres timestamptz or SQLite ISO-8601 text.
fn parse_timestamp(row: &sea_orm::QueryResult, col: &str) -> Result<DateTime<Utc>, Error> {
    if let Ok(dt) = row.try_get_by::<DateTime<Utc>, _>(col) {
        return Ok(dt);
    }
    let s: String = row
        .try_get_by::<String, _>(col)
        .map_err(|e| Error::custom(format!("parse {col}: {e}")))?;
    DateTime::parse_from_rfc3339(&s)
        .map(|dt| dt.with_timezone(&Utc))
        .map_err(|e| Error::custom(format!("parse {col} as rfc3339 ('{s}'): {e}")))
}

/// Parse a nullable timestamp column, returning `Ok(None)` for SQL NULL.
fn parse_optional_timestamp(
    row: &sea_orm::QueryResult,
    col: &str,
) -> Result<Option<DateTime<Utc>>, Error> {
    if let Ok(opt) = row.try_get_by::<Option<DateTime<Utc>>, _>(col) {
        return Ok(opt);
    }
    let s: Option<String> = row
        .try_get_by::<Option<String>, _>(col)
        .map_err(|e| Error::custom(format!("parse {col}: {e}")))?;
    match s {
        None => Ok(None),
        Some(s) => DateTime::parse_from_rfc3339(&s)
            .map(|dt| Some(dt.with_timezone(&Utc)))
            .map_err(|e| Error::custom(format!("parse {col} as rfc3339 ('{s}'): {e}"))),
    }
}

/// SQL placeholder style: Postgres uses `$N`, SQLite uses `?N`.
///
/// Returns `Err(Error::UnsupportedBackend)` for any backend other than
/// Postgres or SQLite (e.g. MySQL uses plain `?` without positional index).
fn ph(backend: DatabaseBackend, n: usize) -> Result<String, Error> {
    match backend {
        DatabaseBackend::Postgres => Ok(format!("${n}")),
        DatabaseBackend::Sqlite => Ok(format!("?{n}")),
        _ => Err(Error::UnsupportedBackend),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use sea_orm::Database;
    use sea_orm_migration::MigratorTrait;

    struct TestMigrator;

    #[async_trait::async_trait]
    impl MigratorTrait for TestMigrator {
        fn migrations() -> Vec<Box<dyn sea_orm_migration::MigrationTrait>> {
            vec![
                Box::new(crate::migration::CreateDeploymentsTable),
                Box::new(crate::migration::CreateDeploymentPointersTable),
            ]
        }
    }

    async fn setup() -> DatabaseConnection {
        let conn = Database::connect("sqlite::memory:")
            .await
            .expect("connect sqlite::memory:");
        TestMigrator::up(&conn, None).await.expect("run migrations");
        conn
    }

    #[tokio::test]
    async fn create_sets_building() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d
            .create("owner:1", Some("abc123"))
            .await
            .expect("create failed");
        assert_eq!(dep.status, DeploymentStatus::Building);
        assert_eq!(dep.owner_key, "owner:1");
        assert_eq!(dep.source_ref.as_deref(), Some("abc123"));
        assert!(!dep.identifier.is_empty(), "identifier must be set");
        assert!(dep.artifact_location.is_none());
        assert!(dep.byte_size.is_none());
    }

    #[tokio::test]
    async fn get_round_trips() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d.create("owner:2", Some("ref-xyz")).await.expect("create");
        let fetched = d.get(dep.id).await.expect("get");
        assert_eq!(fetched.id, dep.id);
        assert_eq!(fetched.identifier, dep.identifier);
        assert_eq!(fetched.owner_key, "owner:2");
        assert_eq!(fetched.source_ref.as_deref(), Some("ref-xyz"));
        assert_eq!(fetched.status, DeploymentStatus::Building);
    }

    #[tokio::test]
    async fn mark_ready_transitions() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d.create("owner:3", None).await.expect("create");
        d.mark_ready(dep.id, "deployments/1/", 4096)
            .await
            .expect("mark_ready failed");
        let fetched = d.get(dep.id).await.expect("get");
        assert_eq!(fetched.status, DeploymentStatus::Ready);
        assert_eq!(fetched.artifact_location.as_deref(), Some("deployments/1/"));
        assert_eq!(fetched.byte_size, Some(4096));
        assert!(
            fetched.terminated_at.is_some(),
            "terminated_at must be set after mark_ready"
        );
    }

    #[tokio::test]
    async fn mark_ready_rejects_terminal() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d.create("owner:4", None).await.expect("create");
        d.mark_ready(dep.id, "path/", 100)
            .await
            .expect("first mark_ready");
        // Second call on an already-ready row must return Err.
        let result = d.mark_ready(dep.id, "path2/", 200).await;
        assert!(
            result.is_err(),
            "mark_ready on terminal row should return Err"
        );
    }

    #[tokio::test]
    async fn mark_failed_transitions() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d.create("owner:5", None).await.expect("create");
        d.mark_failed(dep.id, "build exploded")
            .await
            .expect("mark_failed");
        let fetched = d.get(dep.id).await.expect("get");
        assert_eq!(fetched.status, DeploymentStatus::Failed);
        assert!(
            fetched.terminated_at.is_some(),
            "terminated_at must be set after mark_failed"
        );
    }

    #[tokio::test]
    async fn list_returns_owner_rows() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        d.create("owner:6", None).await.expect("create 1");
        d.create("owner:6", None).await.expect("create 2");
        d.create("other-owner", None).await.expect("create other");

        let rows = d.list("owner:6").await.expect("list");
        assert_eq!(rows.len(), 2, "list should return 2 rows for owner:6");

        let other_rows = d.list("other-owner").await.expect("list other");
        assert_eq!(other_rows.len(), 1);
    }

    #[tokio::test]
    async fn active_returns_none_without_pointer() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let result = d.active("owner:7").await.expect("active");
        assert!(
            result.is_none(),
            "active should be None when no pointer exists"
        );
    }

    #[tokio::test]
    async fn promote_rejects_non_ready() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep = d.create("owner:8", None).await.expect("create");
        // dep is still Building — promote must reject it
        let result = d.promote("owner:8", dep.id).await;
        assert!(
            matches!(result, Err(Error::NotReady { id }) if id == dep.id),
            "promote should return Error::NotReady for a Building deployment, got: {result:?}"
        );
    }

    #[tokio::test]
    async fn promote_rejects_deleted_artifact() {
        let conn = setup().await;
        let d = Deployments::new(conn.clone());
        let dep = d.create("owner:9", None).await.expect("create");
        d.mark_ready(dep.id, "artifacts/9/", 512)
            .await
            .expect("mark_ready");
        // Manually set artifact_deleted_at to simulate GC.
        let now_iso = chrono::Utc::now().to_rfc3339();
        conn.execute(sea_orm::Statement::from_string(
            sea_orm::DatabaseBackend::Sqlite,
            format!(
                "UPDATE deployments SET artifact_deleted_at = '{}' WHERE id = {}",
                now_iso, dep.id
            ),
        ))
        .await
        .expect("set artifact_deleted_at");
        let result = d.promote("owner:9", dep.id).await;
        assert!(
            matches!(result, Err(Error::ArtifactDeleted { id }) if id == dep.id),
            "promote should return Error::ArtifactDeleted, got: {result:?}"
        );
    }

    #[tokio::test]
    async fn promote_returns_previous_id() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep_a = d.create("owner:10", None).await.expect("create a");
        let dep_b = d.create("owner:10", None).await.expect("create b");
        d.mark_ready(dep_a.id, "a/", 1).await.expect("ready a");
        d.mark_ready(dep_b.id, "b/", 2).await.expect("ready b");

        // First promote: no previous.
        let prev = d
            .promote("owner:10", dep_a.id)
            .await
            .expect("first promote");
        assert!(
            prev.is_none(),
            "first promote should return None as previous"
        );

        // Second promote: previous is dep_a.
        let prev2 = d
            .promote("owner:10", dep_b.id)
            .await
            .expect("second promote");
        assert_eq!(
            prev2,
            Some(dep_a.id),
            "second promote should return dep_a.id as previous"
        );
    }

    #[tokio::test]
    async fn rollback_promotes_previous() {
        let conn = setup().await;
        let d = Deployments::new(conn);
        let dep_a = d.create("owner:11", None).await.expect("create a");
        let dep_b = d.create("owner:11", None).await.expect("create b");
        d.mark_ready(dep_a.id, "a/", 1).await.expect("ready a");
        d.mark_ready(dep_b.id, "b/", 2).await.expect("ready b");
        d.promote("owner:11", dep_a.id).await.expect("promote a");
        d.promote("owner:11", dep_b.id).await.expect("promote b");

        // Rollback: should promote dep_a again.
        d.rollback("owner:11").await.expect("rollback");
        let active = d
            .active("owner:11")
            .await
            .expect("active after rollback")
            .expect("should have active deployment");
        assert_eq!(
            active.id, dep_a.id,
            "after rollback, active should be dep_a"
        );
    }
}