krab_core 0.6.0

Shared runtime for the Krab framework: config, HTTP, database, telemetry, resilience, signals, and protocol governance
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
#[cfg(test)]
mod tests {
    use crate::db::{
        detect_migration_drift, enforce_migration_governance, enforce_promotion_policy,
        record_rollback_rehearsal, rollback_to_version, run_versioned_migrations, DbPool,
        Migration, MigrationFailurePolicy, MigrationGovernanceConfig, PromotionConfig,
    };
    use anyhow::Result;
    use serial_test::serial;
    use sqlx::postgres::PgPoolOptions;

    fn require_db_tests() -> bool {
        std::env::var("KRAB_REQUIRE_DB_TESTS")
            .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
            .unwrap_or(false)
    }

    /// Connect to the test database (`DATABASE_URL`, defaulting to a local
    /// `krab_test`), or skip the calling test.
    ///
    /// The skip is deliberately LOUD and controllable: these tests used to
    /// early-return silently on connection failure, so a CI runner with no
    /// Postgres reported the whole migration-governance suite as passing
    /// without executing a single statement. Now:
    ///
    /// - `KRAB_REQUIRE_DB_TESTS=1` (CI mode): a connection failure PANICS —
    ///   an unreachable database fails the suite instead of greenwashing it.
    /// - otherwise: an unmistakable `SKIPPED` line goes to stderr before the
    ///   test returns early.
    async fn test_pool_or_skip(test_name: &str) -> Option<DbPool> {
        // `KRAB_TEST_DATABASE_URL` wins over `DATABASE_URL`: the secret-policy
        // tests in `config.rs` legitimately set, clear, and overwrite
        // `DATABASE_URL` while exercising sourcing rules, so in a full-suite
        // run the ambient `DATABASE_URL` is unreliable by design. A dedicated
        // variable nothing else touches keeps the live-database connection
        // stable; `DATABASE_URL` and the localhost default remain fallbacks
        // for single-test gate runs and CI compose files.
        let url = std::env::var("KRAB_TEST_DATABASE_URL")
            .or_else(|_| std::env::var("DATABASE_URL"))
            .unwrap_or_else(|_| "postgres://postgres@localhost:5432/krab_test".to_string());
        match PgPoolOptions::new().max_connections(1).connect(&url).await {
            Ok(pool) => Some(pool),
            Err(err) => {
                if require_db_tests() {
                    panic!(
                        "KRAB_REQUIRE_DB_TESTS is set but the test database at '{url}' is \
                         unreachable for {test_name}: {err}"
                    );
                }
                eprintln!(
                    "SKIPPED {test_name}: test database at '{url}' not available ({err}). \
                     This test executed NOTHING. Set KRAB_REQUIRE_DB_TESTS=1 to make an \
                     unreachable database a hard failure (CI mode)."
                );
                None
            }
        }
    }

    async fn clean_test_db(pool: &DbPool) -> Result<()> {
        sqlx::query("DROP TABLE IF EXISTS krab_migration_policy_audit, krab_migration_schema_ownership, krab_migration_rollback_rehearsals, krab_migrations, krab_migration_environment, user_audit_log, user_profiles, users, multi_stmt_items")
            .execute(pool)
            .await?;
        Ok(())
    }

    fn test_users_service_migrations() -> Vec<Migration> {
        vec![
            Migration {
                version: 1,
                name: "create_users",
                sql: "CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, username TEXT NOT NULL UNIQUE, email TEXT NOT NULL UNIQUE, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now())",
                rollback_sql: Some("DROP TABLE IF EXISTS users"),
                critical: true,
                destructive: false,
            },
            Migration {
                version: 2,
                name: "create_users_created_at_index",
                sql: "CREATE INDEX IF NOT EXISTS idx_users_created_at ON users(created_at)",
                rollback_sql: Some("DROP INDEX IF EXISTS idx_users_created_at"),
                critical: false,
                destructive: false,
            },
            Migration {
                version: 3,
                name: "create_user_profiles",
                sql: "CREATE TABLE IF NOT EXISTS user_profiles (user_id TEXT PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, display_name TEXT, bio TEXT, avatar_url TEXT, updated_at TIMESTAMPTZ NOT NULL DEFAULT now())",
                rollback_sql: Some("DROP TABLE IF EXISTS user_profiles"),
                critical: false,
                destructive: false,
            },
            Migration {
                version: 4,
                name: "create_user_audit_log",
                sql: "CREATE TABLE IF NOT EXISTS user_audit_log (id BIGSERIAL PRIMARY KEY, user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE, action TEXT NOT NULL, actor_sub TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT now())",
                rollback_sql: Some("DROP TABLE IF EXISTS user_audit_log"),
                critical: false,
                destructive: false,
            },
            Migration {
                version: 5,
                name: "create_user_audit_log_created_at_index",
                sql: "CREATE INDEX IF NOT EXISTS idx_user_audit_log_created_at ON user_audit_log(created_at)",
                rollback_sql: Some("DROP INDEX IF EXISTS idx_user_audit_log_created_at"),
                critical: false,
                destructive: false,
            },
            Migration {
                version: 6,
                name: "add_tenant_id_to_users",
                sql: "ALTER TABLE users ADD COLUMN IF NOT EXISTS tenant_id TEXT",
                rollback_sql: Some("ALTER TABLE users DROP COLUMN IF EXISTS tenant_id"),
                critical: true,
                destructive: false,
            },
            Migration {
                version: 7,
                name: "create_users_tenant_id_index",
                sql: "CREATE INDEX IF NOT EXISTS idx_users_tenant_id ON users(tenant_id)",
                rollback_sql: Some("DROP INDEX IF EXISTS idx_users_tenant_id"),
                critical: false,
                destructive: false,
            },
        ]
    }

    #[tokio::test]
    #[serial]
    async fn test_migration_lifecycle() {
        let Some(pool) = test_pool_or_skip("test_migration_lifecycle").await else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let migrations = test_users_service_migrations();

        // 1. Run migrations
        let report = run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("migration run failed");

        assert_eq!(report.applied_versions.len(), 7);
        assert_eq!(report.applied_versions, vec![1, 2, 3, 4, 5, 6, 7]);

        // 2. Verify drift detection shows clean state
        let drift = detect_migration_drift(&pool, &migrations)
            .await
            .expect("drift detection failed");

        assert!(drift.missing_versions.is_empty());
        assert!(drift.unexpected_versions.is_empty());
        assert!(drift.checksum_mismatches.is_empty());
    }

    #[tokio::test]
    #[serial]
    async fn test_migration_rollback() {
        let Some(pool) = test_pool_or_skip("test_migration_rollback").await else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let migrations = test_users_service_migrations();

        // 1. Apply all
        run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("migration run failed");

        // 2. Rollback to version 2
        rollback_to_version(&pool, &migrations, 2)
            .await
            .expect("rollback failed");

        // 3. Verify state
        let drift = detect_migration_drift(&pool, &migrations)
            .await
            .expect("drift detection failed");

        // Versions 3, 4, 5 should be missing
        assert!(drift.missing_versions.contains(&3));
        assert!(drift.missing_versions.contains(&4));
        assert!(drift.missing_versions.contains(&5));
        assert!(!drift.missing_versions.contains(&2));
    }

    #[tokio::test]
    #[serial]
    async fn test_drift_detection() {
        let Some(pool) = test_pool_or_skip("test_drift_detection").await else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let mut migrations = test_users_service_migrations();

        // 1. Apply initial set
        run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("migration run failed");

        // 2. Simulate drift: add a new migration definition but don't apply it
        migrations.push(Migration {
            version: 999,
            name: "drift_test",
            sql: "SELECT 1",
            rollback_sql: None,
            critical: false,
            destructive: false,
        });

        let drift = detect_migration_drift(&pool, &migrations)
            .await
            .expect("drift detection failed");

        assert!(drift.missing_versions.contains(&999));
    }

    #[tokio::test]
    #[serial]
    async fn test_governance_release_requires_rehearsal_artifact() {
        let Some(pool) =
            test_pool_or_skip("test_governance_release_requires_rehearsal_artifact").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let cfg = MigrationGovernanceConfig {
            service_name: "service_users".to_string(),
            environment: "staging".to_string(),
            allow_apply: true,
            release_environments: vec!["staging".to_string(), "prod".to_string()],
            require_rollback_rehearsal_in_release: true,
            drift_tolerance_threshold: 0,
        };

        let result = enforce_migration_governance(&pool, &cfg).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    #[serial]
    async fn test_governance_release_passes_with_rehearsal_artifact() {
        let Some(pool) =
            test_pool_or_skip("test_governance_release_passes_with_rehearsal_artifact").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        record_rollback_rehearsal(
            &pool,
            "service_users",
            "staging",
            3,
            "s3://evidence/rollback-rehearsal-2026-02-27.json",
            true,
        )
        .await
        .expect("failed to record rollback rehearsal");

        let cfg = MigrationGovernanceConfig {
            service_name: "service_users".to_string(),
            environment: "staging".to_string(),
            allow_apply: true,
            release_environments: vec!["staging".to_string(), "prod".to_string()],
            require_rollback_rehearsal_in_release: true,
            drift_tolerance_threshold: 0,
        };

        enforce_migration_governance(&pool, &cfg)
            .await
            .expect("governance should pass with rehearsal artifact");
    }

    #[tokio::test]
    #[serial]
    async fn test_legacy_checksum_rows_are_rewritten_not_flagged() {
        let Some(pool) =
            test_pool_or_skip("test_legacy_checksum_rows_are_rewritten_not_flagged").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let migrations = test_users_service_migrations();
        run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("migration run failed");

        // Simulate a row written before the SHA-256 switch: same SQL, legacy
        // DefaultHasher checksum format.
        let legacy = crate::db::postgres::legacy_checksum(migrations[0].sql);
        sqlx::query("UPDATE krab_migrations SET checksum = $1 WHERE version = $2")
            .bind(&legacy)
            .bind(migrations[0].version)
            .execute(&pool)
            .await
            .expect("failed to plant legacy checksum");

        // Drift detection must upgrade the row in place, not flag it.
        let drift = detect_migration_drift(&pool, &migrations)
            .await
            .expect("drift detection failed");
        assert!(
            drift.checksum_mismatches.is_empty(),
            "legacy checksum must be upgraded, not reported as drift: {:?}",
            drift.checksum_mismatches
        );

        let stored: String =
            sqlx::query_scalar("SELECT checksum FROM krab_migrations WHERE version = $1")
                .bind(migrations[0].version)
                .fetch_one(&pool)
                .await
                .expect("failed to read back checksum");
        assert_eq!(
            stored,
            crate::db::postgres::checksum(migrations[0].sql),
            "row must be rewritten to the SHA-256 checksum"
        );

        // A re-run of the migrations must also accept-and-rewrite.
        sqlx::query("UPDATE krab_migrations SET checksum = $1 WHERE version = $2")
            .bind(&legacy)
            .bind(migrations[0].version)
            .execute(&pool)
            .await
            .expect("failed to re-plant legacy checksum");

        let report = run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("re-run over a legacy checksum row must not fail");
        assert!(report.skipped_versions.contains(&migrations[0].version));

        let stored: String =
            sqlx::query_scalar("SELECT checksum FROM krab_migrations WHERE version = $1")
                .bind(migrations[0].version)
                .fetch_one(&pool)
                .await
                .expect("failed to read back checksum");
        assert_eq!(stored, crate::db::postgres::checksum(migrations[0].sql));
    }

    #[tokio::test]
    #[serial]
    async fn test_true_checksum_mismatch_is_still_flagged() {
        let Some(pool) = test_pool_or_skip("test_true_checksum_mismatch_is_still_flagged").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        let migrations = test_users_service_migrations();
        run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("migration run failed");

        // Neither the SHA-256 nor the legacy checksum of this SQL: real drift.
        sqlx::query(
            "UPDATE krab_migrations SET checksum = 'not-any-known-format' WHERE version = $1",
        )
        .bind(migrations[0].version)
        .execute(&pool)
        .await
        .expect("failed to plant drifted checksum");

        let drift = detect_migration_drift(&pool, &migrations)
            .await
            .expect("drift detection failed");
        assert!(drift.checksum_mismatches.contains(&migrations[0].version));

        let rerun =
            run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt).await;
        assert!(
            rerun.is_err(),
            "a genuine checksum mismatch must still fail the migration run"
        );
    }

    #[tokio::test]
    #[serial]
    async fn test_multi_statement_migration_applies_and_rolls_back() {
        let Some(pool) =
            test_pool_or_skip("test_multi_statement_migration_applies_and_rolls_back").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        // A single migration whose body holds TWO statements. Under the
        // prepared-statement protocol (`sqlx::query`) this fails at runtime;
        // it must execute via the simple-query protocol (`sqlx::raw_sql`).
        let migrations = vec![Migration {
            version: 1,
            name: "create_items_with_index",
            sql: "CREATE TABLE multi_stmt_items (id BIGSERIAL PRIMARY KEY, label TEXT NOT NULL); \
                  CREATE INDEX idx_multi_stmt_items_label ON multi_stmt_items(label);",
            rollback_sql: Some(
                "DROP INDEX IF EXISTS idx_multi_stmt_items_label; \
                 DROP TABLE IF EXISTS multi_stmt_items;",
            ),
            critical: true,
            destructive: false,
        }];

        let report = run_versioned_migrations(&pool, &migrations, MigrationFailurePolicy::Halt)
            .await
            .expect("multi-statement migration must apply");
        assert_eq!(report.applied_versions, vec![1]);

        // Both statements executed: table AND index exist.
        let table_exists: bool = sqlx::query_scalar(
            "SELECT EXISTS(SELECT 1 FROM pg_tables WHERE tablename = 'multi_stmt_items')",
        )
        .fetch_one(&pool)
        .await
        .expect("failed to check table existence");
        assert!(table_exists, "first statement (CREATE TABLE) must execute");

        let index_exists: bool = sqlx::query_scalar(
            "SELECT EXISTS(SELECT 1 FROM pg_indexes WHERE indexname = 'idx_multi_stmt_items_label')",
        )
        .fetch_one(&pool)
        .await
        .expect("failed to check index existence");
        assert!(index_exists, "second statement (CREATE INDEX) must execute");

        // Multi-statement rollback bodies must execute the same way.
        rollback_to_version(&pool, &migrations, 0)
            .await
            .expect("multi-statement rollback must execute");

        let table_exists: bool = sqlx::query_scalar(
            "SELECT EXISTS(SELECT 1 FROM pg_tables WHERE tablename = 'multi_stmt_items')",
        )
        .fetch_one(&pool)
        .await
        .expect("failed to re-check table existence");
        assert!(!table_exists, "rollback must drop the table");
    }

    #[tokio::test]
    #[serial]
    async fn test_governance_deny_records_audit_row_then_errors() {
        let Some(pool) =
            test_pool_or_skip("test_governance_deny_records_audit_row_then_errors").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        // Drive the deny through the real env knob, exactly as an operator
        // would set it. Safe here: the whole db suite is #[serial].
        std::env::set_var("DB_MIGRATION_ALLOW_APPLY", "false");
        let from_env = MigrationGovernanceConfig::from_env();
        std::env::remove_var("DB_MIGRATION_ALLOW_APPLY");
        assert!(!from_env.allow_apply);

        let cfg = MigrationGovernanceConfig {
            service_name: "service_users".to_string(),
            // "dev" is not a release environment, so the rehearsal gate stays
            // out of the picture and the deny is attributable to allow_apply.
            environment: "dev".to_string(),
            ..from_env
        };

        let err = enforce_migration_governance(&pool, &cfg)
            .await
            .expect_err("DB_MIGRATION_ALLOW_APPLY=false must deny with an error, not Ok(())");
        let message = format!("{err:#}");
        assert!(
            message.contains("DB_MIGRATION_ALLOW_APPLY"),
            "deny error should name the governing variable: {message}"
        );

        // Record-then-deny: the audit row must exist despite the error.
        let decision: String = sqlx::query_scalar(
            "SELECT decision FROM krab_migration_policy_audit
             WHERE service_name = $1 AND policy_name = 'migration_governance'
             ORDER BY id DESC LIMIT 1",
        )
        .bind("service_users")
        .fetch_one(&pool)
        .await
        .expect("audit row must be written before the deny error");
        assert_eq!(decision, "deny");
    }

    #[tokio::test]
    #[serial]
    async fn test_promotion_unknown_recorded_environment_errors() {
        let Some(pool) =
            test_pool_or_skip("test_promotion_unknown_recorded_environment_errors").await
        else {
            return;
        };
        clean_test_db(&pool).await.expect("failed to clean db");

        // Plant a recorded environment outside the ladder, as a misconfigured
        // deployment would ("production" instead of "prod"). This used to
        // resolve to index 0 = "local", making any target look like a forward
        // promotion.
        sqlx::query(
            "CREATE TABLE IF NOT EXISTS krab_migration_environment (environment TEXT PRIMARY KEY, updated_at TIMESTAMPTZ NOT NULL DEFAULT now())",
        )
        .execute(&pool)
        .await
        .expect("failed to create environment table");
        sqlx::query("INSERT INTO krab_migration_environment (environment) VALUES ('production')")
            .execute(&pool)
            .await
            .expect("failed to plant unknown environment");

        let cfg = PromotionConfig {
            environment: "prod".to_string(),
            allow_apply: true,
        };
        let err = enforce_promotion_policy(&pool, &cfg)
            .await
            .expect_err("an unknown recorded environment must error, not pass as 'local'");
        let message = format!("{err:#}");
        assert!(
            message.contains("production"),
            "error should name the unknown environment: {message}"
        );
        assert!(
            message.contains("staging"),
            "error should list the known ladder: {message}"
        );
    }

    #[test]
    fn test_enforce_drift_policy() {
        use crate::db::{enforce_drift_policy, MigrationDriftReport};

        let report_clean = MigrationDriftReport::default();
        assert!(enforce_drift_policy(&report_clean, 0).is_ok());

        let report_unexpected = MigrationDriftReport {
            unexpected_versions: vec![999],
            ..Default::default()
        };
        // Allowed threshold is 1
        assert!(enforce_drift_policy(&report_unexpected, 1).is_ok());
        // Threshold 0 should fail
        assert!(enforce_drift_policy(&report_unexpected, 0).is_err());

        // Mismatches or missing versions should always fail
        let report_missing = MigrationDriftReport {
            missing_versions: vec![1],
            ..Default::default()
        };
        assert!(enforce_drift_policy(&report_missing, 1).is_err());
    }
}