type-bridge 2.2.0

Public client SDK for TypeBridge
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
//! Generated-package-owned immutable migration catalog inspection.

use std::collections::BTreeSet;
use std::marker::PhantomData;
use std::sync::Arc;

use type_bridge_contract::fingerprint::Fingerprint;
use type_bridge_contract::migration::{MigrationId, MigrationStep};
use type_bridge_schema::{ManagedDeltaContext, SafetyClass};

use crate::error::{Error, Result};
use crate::schema::{Schema, SchemaPackage};
#[cfg(feature = "typedb")]
use crate::session::Database;

/// Immutable replay-verified migration catalog branded by one generated schema.
#[derive(Clone, Debug)]
pub struct MigrationCatalog<S: Schema> {
    inner: type_bridge_schema_migration::MigrationCatalog,
    marker: PhantomData<fn() -> S>,
}

impl<S: Schema> MigrationCatalog<S> {
    /// Construct one validated portable compound identity for catalog planning.
    pub fn identity(&self, app_label: &str, name: &str) -> Result<MigrationId> {
        let _ = self;
        MigrationId::new(app_label, name).map_err(migration_diagnostic)
    }

    /// Return the canonical history-bundle fingerprint.
    #[must_use]
    pub const fn fingerprint(&self) -> &Fingerprint {
        self.inner.fingerprint()
    }

    /// Return the number of replay-verified history entries.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.entries().len()
    }

    /// Report whether the catalog contains no committed migrations.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.entries().is_empty()
    }

    /// Return graph heads in canonical compound-identity order.
    #[must_use]
    pub fn heads(&self) -> &[MigrationId] {
        self.inner.heads()
    }

    /// Inspect one history entry by deterministic topological ordinal.
    #[must_use]
    pub fn entry(&self, index: usize) -> Option<MigrationHistoryEntry<'_>> {
        self.inner
            .entries()
            .get(index)
            .map(|entry| MigrationHistoryEntry { inner: entry })
    }

    /// Build a provider-free forward preview under this generated authority.
    pub fn preview_apply(
        &self,
        applied: impl IntoIterator<Item = MigrationId>,
        targets: Option<Vec<MigrationId>>,
    ) -> Result<MigrationPreview<S>> {
        let applied = applied.into_iter().collect();
        let target = match targets {
            Some(targets) => type_bridge_schema_migration::MigrationApplyTarget::Explicit(
                targets.into_iter().collect(),
            ),
            None => type_bridge_schema_migration::MigrationApplyTarget::DefaultHead,
        };
        self.inner
            .preview_apply(&applied, &target)
            .map(|plan| MigrationPreview {
                inner: Arc::new(MigrationPreviewState {
                    catalog: self.inner.clone(),
                    plan: MigrationPreviewInner::Apply(plan),
                }),
                marker: PhantomData,
            })
            .map_err(plan_error)
    }

    /// Build a provider-free rollback preview for an explicit removal set.
    pub fn preview_rollback(
        &self,
        applied: impl IntoIterator<Item = MigrationId>,
        removals: impl IntoIterator<Item = MigrationId>,
    ) -> Result<MigrationPreview<S>> {
        self.inner
            .preview_rollback(
                &applied.into_iter().collect(),
                &removals.into_iter().collect(),
            )
            .map(|plan| MigrationPreview {
                inner: Arc::new(MigrationPreviewState {
                    catalog: self.inner.clone(),
                    plan: MigrationPreviewInner::Rollback(plan),
                }),
                marker: PhantomData,
            })
            .map_err(plan_error)
    }

    /// Verify the applied ledger and live managed semantics without mutation.
    #[cfg(feature = "typedb")]
    pub async fn verify(
        &self,
        database: &Database<S>,
    ) -> Result<type_bridge_schema_migration::MigrationVerifyReport> {
        type_bridge_schema_migration_typedb::verify_catalog_state(
            Arc::new(database.inner_orm().clone()),
            &self.inner,
        )
        .await
        .map_err(migration_diagnostic)
    }

    /// Read the exact active applied set from this generated catalog's journal.
    #[cfg(feature = "typedb")]
    pub async fn applied_migrations(&self, database: &Database<S>) -> Result<Vec<MigrationId>> {
        type_bridge_schema_migration_typedb::load_catalog_applied_migrations(
            Arc::new(database.inner_orm().clone()),
            &self.inner,
        )
        .await
        .map_err(migration_diagnostic)
    }
}

#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
enum MigrationPreviewInner {
    Apply(type_bridge_schema_migration::VerifiedMigrationApplyPlan),
    Rollback(type_bridge_schema_migration::VerifiedMigrationRollbackPlan),
}

#[derive(Clone, Debug)]
struct MigrationPreviewState {
    catalog: type_bridge_schema_migration::MigrationCatalog,
    plan: MigrationPreviewInner,
}

/// Generated-schema-branded immutable provider-free migration preview.
#[derive(Clone, Debug)]
pub struct MigrationPreview<S: Schema> {
    inner: Arc<MigrationPreviewState>,
    marker: PhantomData<fn() -> S>,
}

impl<S: Schema> MigrationPreview<S> {
    /// Return whether this is a forward apply preview.
    #[must_use]
    pub fn is_apply(&self) -> bool {
        matches!(self.inner.plan, MigrationPreviewInner::Apply(_))
    }

    /// Preview objects never grant provider execution authority.
    #[must_use]
    pub fn execution_authorized(&self) -> bool {
        match &self.inner.plan {
            MigrationPreviewInner::Apply(plan) => plan.execution_authorized(),
            MigrationPreviewInner::Rollback(plan) => plan.execution_authorized(),
        }
    }

    /// Return the number of migrations in execution order.
    #[must_use]
    pub fn len(&self) -> usize {
        match &self.inner.plan {
            MigrationPreviewInner::Apply(plan) => plan.migrations().len(),
            MigrationPreviewInner::Rollback(plan) => plan.rollbacks().len(),
        }
    }

    /// Report whether the preview contains no migration work.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Inspect one migration by deterministic execution ordinal.
    #[must_use]
    pub fn entry(&self, index: usize) -> Option<MigrationPreviewEntry<'_>> {
        match &self.inner.plan {
            MigrationPreviewInner::Apply(plan) => {
                plan.migrations()
                    .get(index)
                    .map(|entry| MigrationPreviewEntry {
                        id: entry.manifest().id(),
                        safety: entry.manifest().safety(),
                        step_count: entry.steps().len(),
                        transaction_group_count: entry.transaction_groups().len(),
                        backfill_count: entry.backfill_step_indices().len(),
                        reversible: entry.manifest().reversible(),
                    })
            }
            MigrationPreviewInner::Rollback(plan) => {
                plan.rollbacks()
                    .get(index)
                    .map(|entry| MigrationPreviewEntry {
                        id: entry.manifest().id(),
                        safety: entry.rollback_safety(),
                        step_count: entry.operations().len(),
                        transaction_group_count: entry.steps().len(),
                        backfill_count: entry.backfills().len(),
                        reversible: true,
                    })
            }
        }
    }

    /// Begin selecting exact transitions that require explicit approval.
    #[must_use]
    pub fn approval_builder(&self) -> MigrationApprovalBuilder<S> {
        MigrationApprovalBuilder {
            preview: Arc::clone(&self.inner),
            selected: BTreeSet::new(),
            marker: PhantomData,
        }
    }

    /// Rebuild a fresh executable plan from approvals owned by this preview.
    pub fn authorize(&self, approvals: &MigrationApprovalSet<S>) -> Result<MigrationPlan<S>> {
        if !Arc::ptr_eq(&self.inner, &approvals.preview) {
            return Err(migration_error("migration_approval_plan_mismatch"));
        }
        let policy = type_bridge_schema_migration::MigrationSafetyPolicy::default_policy();
        let plan = match &self.inner.plan {
            MigrationPreviewInner::Apply(plan) => self
                .inner
                .catalog
                .authorize_apply(
                    &plan.applied_migrations().iter().cloned().collect(),
                    &type_bridge_schema_migration::MigrationApplyTarget::Explicit(
                        plan.target_frontier().iter().cloned().collect(),
                    ),
                    &policy,
                    &approvals.approvals,
                )
                .map(MigrationPreviewInner::Apply),
            MigrationPreviewInner::Rollback(plan) => self
                .inner
                .catalog
                .authorize_rollback(
                    &plan.applied_basis(),
                    &plan
                        .rollbacks()
                        .iter()
                        .map(|entry| entry.manifest().id().clone())
                        .collect(),
                    &policy,
                    &approvals.approvals,
                )
                .map(MigrationPreviewInner::Rollback),
        }
        .map_err(plan_error)?;
        Ok(MigrationPlan {
            catalog: self.inner.catalog.clone(),
            plan,
            marker: PhantomData,
        })
    }
}

/// Mutable exact-approval selection owned by one provider-free preview.
#[derive(Debug)]
pub struct MigrationApprovalBuilder<S: Schema> {
    preview: Arc<MigrationPreviewState>,
    selected: BTreeSet<usize>,
    marker: PhantomData<fn() -> S>,
}

impl<S: Schema> MigrationApprovalBuilder<S> {
    /// Approve one preview entry when the default policy requires approval.
    pub fn approve(&mut self, index: usize) -> Result<()> {
        let safety = preview_entry(&self.preview.plan, index)
            .ok_or_else(|| migration_error("migration_approval_index_invalid"))?
            .safety();
        match type_bridge_schema_migration::MigrationSafetyPolicy::default_policy().decision(safety)
        {
            type_bridge_schema_migration::SafetyPolicyDecision::RequireApproval => {
                self.selected.insert(index);
                Ok(())
            }
            type_bridge_schema_migration::SafetyPolicyDecision::Allow => {
                Err(migration_error("migration_approval_not_required"))
            }
            type_bridge_schema_migration::SafetyPolicyDecision::Reject => {
                Err(migration_error("migration_approval_policy_rejected"))
            }
        }
    }

    /// Freeze the selected exact transitions into an immutable approval set.
    pub fn finish(self) -> Result<MigrationApprovalSet<S>> {
        let approvals = self
            .selected
            .iter()
            .map(|index| match &self.preview.plan {
                MigrationPreviewInner::Apply(plan) => {
                    type_bridge_schema_migration::MigrationApplyApproval::for_manifest(
                        plan.migrations()[*index].manifest(),
                    )
                }
                MigrationPreviewInner::Rollback(plan) => {
                    type_bridge_schema_migration::MigrationApplyApproval::for_rollback(
                        plan.rollbacks()[*index].manifest(),
                        plan.rollbacks()[*index].rollback_safety(),
                    )
                }
            })
            .collect::<std::result::Result<Vec<_>, _>>()
            .map_err(migration_diagnostic)?;
        Ok(MigrationApprovalSet {
            preview: self.preview,
            approvals,
            marker: PhantomData,
        })
    }
}

/// Immutable exact approvals retained with their originating preview owner.
#[derive(Clone, Debug)]
pub struct MigrationApprovalSet<S: Schema> {
    preview: Arc<MigrationPreviewState>,
    approvals: Vec<type_bridge_schema_migration::MigrationApplyApproval>,
    marker: PhantomData<fn() -> S>,
}

impl<S: Schema> MigrationApprovalSet<S> {
    /// Return the number of exact approved transitions.
    #[must_use]
    pub fn len(&self) -> usize {
        self.approvals.len()
    }

    /// Report whether no transitions were explicitly approved.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.approvals.is_empty()
    }
}

/// Generated-schema-branded executable migration plan.
#[derive(Clone, Debug)]
pub struct MigrationPlan<S: Schema> {
    #[cfg_attr(not(feature = "typedb"), allow(dead_code))]
    catalog: type_bridge_schema_migration::MigrationCatalog,
    plan: MigrationPreviewInner,
    marker: PhantomData<fn() -> S>,
}

impl<S: Schema> MigrationPlan<S> {
    /// Confirm that policy and exact approvals granted execution authority.
    #[must_use]
    pub fn execution_authorized(&self) -> bool {
        match &self.plan {
            MigrationPreviewInner::Apply(plan) => plan.execution_authorized(),
            MigrationPreviewInner::Rollback(plan) => plan.execution_authorized(),
        }
    }

    /// Execute this plan through the exact managed database/journal pair.
    #[cfg(feature = "typedb")]
    pub async fn execute(
        &self,
        database: &Database<S>,
        holder: &str,
    ) -> Result<type_bridge_schema_migration::MigrationExecutionReport> {
        self.execute_controlled(
            database,
            holder,
            &type_bridge_schema_migration::MigrationExecutionControl::default(),
        )
        .await
    }

    /// Execute with shared cancellation, absolute deadline, and tightened limits.
    #[cfg(feature = "typedb")]
    pub async fn execute_controlled(
        &self,
        database: &Database<S>,
        holder: &str,
        control: &type_bridge_schema_migration::MigrationExecutionControl,
    ) -> Result<type_bridge_schema_migration::MigrationExecutionReport> {
        let holder = type_bridge_schema_migration::LeaseHolderId::new(holder)
            .map_err(migration_diagnostic)?;
        let database = Arc::new(database.inner_orm().clone());
        match &self.plan {
            MigrationPreviewInner::Apply(plan) => {
                type_bridge_schema_migration_typedb::execute_catalog_apply_plan_controlled(
                    database,
                    &self.catalog,
                    &holder,
                    plan,
                    control,
                )
                .await
                .map(type_bridge_schema_migration::MigrationExecutionReport::from_apply)
                .map_err(migration_diagnostic)
            }
            MigrationPreviewInner::Rollback(plan) => {
                type_bridge_schema_migration_typedb::execute_catalog_rollback_plan_controlled(
                    database,
                    &self.catalog,
                    &holder,
                    plan,
                    control,
                )
                .await
                .map(type_bridge_schema_migration::MigrationExecutionReport::from_rollback)
                .map_err(migration_diagnostic)
            }
        }
    }
}

/// Borrowed bounded inspection of one migration preview entry.
#[derive(Clone, Copy, Debug)]
pub struct MigrationPreviewEntry<'a> {
    id: &'a MigrationId,
    safety: SafetyClass,
    step_count: usize,
    transaction_group_count: usize,
    backfill_count: usize,
    reversible: bool,
}

impl MigrationPreviewEntry<'_> {
    /// Return the compound migration identity.
    pub const fn id(&self) -> &MigrationId {
        self.id
    }
    /// Return the complete forward or reverse safety class.
    pub const fn safety(&self) -> SafetyClass {
        self.safety
    }
    /// Return the complete ordered operation count.
    pub const fn step_count(&self) -> usize {
        self.step_count
    }
    /// Return the schema transaction-group count.
    pub const fn transaction_group_count(&self) -> usize {
        self.transaction_group_count
    }
    /// Return the closed backfill-group count.
    pub const fn backfill_count(&self) -> usize {
        self.backfill_count
    }
    /// Report verified reversibility.
    pub const fn is_reversible(&self) -> bool {
        self.reversible
    }
}

fn preview_entry(plan: &MigrationPreviewInner, index: usize) -> Option<MigrationPreviewEntry<'_>> {
    match plan {
        MigrationPreviewInner::Apply(plan) => {
            plan.migrations()
                .get(index)
                .map(|entry| MigrationPreviewEntry {
                    id: entry.manifest().id(),
                    safety: entry.manifest().safety(),
                    step_count: entry.steps().len(),
                    transaction_group_count: entry.transaction_groups().len(),
                    backfill_count: entry.backfill_step_indices().len(),
                    reversible: entry.manifest().reversible(),
                })
        }
        MigrationPreviewInner::Rollback(plan) => {
            plan.rollbacks()
                .get(index)
                .map(|entry| MigrationPreviewEntry {
                    id: entry.manifest().id(),
                    safety: entry.rollback_safety(),
                    step_count: entry.operations().len(),
                    transaction_group_count: entry.steps().len(),
                    backfill_count: entry.backfills().len(),
                    reversible: true,
                })
        }
    }
}

/// Borrowed immutable view of one catalog history entry.
#[derive(Clone, Copy, Debug)]
pub struct MigrationHistoryEntry<'a> {
    inner: &'a type_bridge_schema_migration::VerifiedMigrationHistoryBundleEntry,
}

impl MigrationHistoryEntry<'_> {
    /// Return the canonical compound migration identity.
    #[must_use]
    pub const fn id(&self) -> &MigrationId {
        self.inner.manifest().id()
    }

    /// Return canonical parent identities.
    #[must_use]
    pub fn parents(&self) -> &[MigrationId] {
        self.inner.manifest().parents()
    }

    /// Return the exact canonical manifest digest.
    #[must_use]
    pub const fn manifest_digest(
        &self,
    ) -> type_bridge_contract::migration::MigrationManifestDigest {
        self.inner.manifest_digest()
    }

    /// Return the number of ordered schema, assertion, and backfill steps.
    #[must_use]
    pub fn step_count(&self) -> usize {
        self.inner.manifest().steps().len()
    }

    /// Return one ordered step for typed inspection.
    #[must_use]
    pub fn step(&self, index: usize) -> Option<&MigrationStep> {
        self.inner.manifest().steps().get(index)
    }

    /// Return the closed safety classification.
    #[must_use]
    pub const fn safety(&self) -> SafetyClass {
        self.inner.manifest().safety()
    }

    /// Report whether the complete migration has a verified reverse.
    #[must_use]
    pub const fn is_reversible(&self) -> bool {
        self.inner.manifest().reversible()
    }
}

impl<S: Schema> SchemaPackage<S> {
    /// Open the generated package's canonical migration-history resource.
    ///
    /// Package authority and every bundled schema/manifest are verified
    /// offline before the catalog is returned. This operation performs no
    /// connection, database, transaction, journal, or provider I/O.
    pub fn open_migration_catalog(&self, bytes: &[u8]) -> Result<MigrationCatalog<S>> {
        let (_projection, authority) = self.verify_and_install_with_authority()?;
        let authority = authority.ok_or_else(|| Error::SchemaVerification {
            message: "migration catalogs require generated schema authority".to_owned(),
            source: None,
        })?;
        let context = ManagedDeltaContext::new(
            authority.managed_scope().id().clone(),
            authority.semantic_profile().id().clone(),
            type_bridge_schema_migration::migration_runtime_capability_vocabulary().map_err(
                |error| Error::SchemaVerification {
                    message: format!(
                        "migration runtime capability vocabulary is invalid [{}]",
                        error.code().as_str()
                    ),
                    source: Some(Box::new(error)),
                },
            )?,
        );
        let inner = type_bridge_schema_migration::MigrationCatalog::open(bytes, &context).map_err(
            |error| Error::SchemaVerification {
                message: format!(
                    "generated migration catalog was rejected [{}]",
                    error.code().as_str()
                ),
                source: Some(Box::new(error)),
            },
        )?;
        Ok(MigrationCatalog {
            inner,
            marker: PhantomData,
        })
    }
}

fn plan_error(error: type_bridge_schema_migration::MigrationApplyPlanError) -> Error {
    let error = match error {
        type_bridge_schema_migration::MigrationApplyPlanError::Contract(diagnostic) => {
            return Error::from_contract_diagnostic(diagnostic);
        }
        error => error,
    };
    let message = match &error {
        type_bridge_schema_migration::MigrationApplyPlanError::Contract(_) => unreachable!(),
        type_bridge_schema_migration::MigrationApplyPlanError::Schema(_) => {
            "generated migration preview schema replay failed".to_owned()
        }
        type_bridge_schema_migration::MigrationApplyPlanError::Lowering(diagnostic) => format!(
            "generated migration preview lowering was rejected [{}]",
            diagnostic.code()
        ),
    };
    Error::SchemaVerification {
        message,
        source: Some(Box::new(error)),
    }
}

fn migration_error(code: &str) -> Error {
    Error::SchemaVerification {
        message: format!("generated migration operation was rejected [{code}]"),
        source: None,
    }
}

fn migration_diagnostic(error: type_bridge_contract::diagnostic::Diagnostic) -> Error {
    Error::from_contract_diagnostic(error)
}