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
//! Public source-migration command and status contracts.
//!
//! This module owns the bounded Candid surface only. Planning, publication,
//! recovery, and controller authorization remain separate authorities.
use candid::CandidType;
use icydb_schema::{
EntitySourceKey, ExpectedAcceptedHead, SchemaMigrationPlanDigest, TargetDatabaseIdentity,
};
use serde::Deserialize;
const MAX_SCHEMA_MIGRATION_STATUS_CURSOR_BYTES: usize = 256;
pub(in crate::db::schema) const MAX_SCHEMA_MIGRATION_FINDINGS_PER_PAGE: usize = 64;
/// One controller-authorized source-migration operation.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub enum SchemaMigrationCommand {
/// Adopt an exact existing version-1 generated schema without changing it.
Adopt {
/// Exact target database identity observed before confirmation.
expected_database: TargetDatabaseIdentity,
/// Exact accepted head observed before confirmation.
expected_head: ExpectedAcceptedHead,
},
/// Prepare, resume, or complete one bounded migration step.
Advance {
/// Exact target database identity observed by the caller.
expected_database: TargetDatabaseIdentity,
/// Exact predecessor accepted head.
expected_head: ExpectedAcceptedHead,
/// Exact deployed coordinated migration plan.
expected_plan: SchemaMigrationPlanDigest,
/// Exact finding page acknowledged before resuming, when required.
acknowledged_finding_page: Option<u64>,
},
/// Abort a migration before irreversible row rewriting begins.
Abort {
/// Exact target database identity observed by the caller.
expected_database: TargetDatabaseIdentity,
/// Exact predecessor accepted head.
expected_head: ExpectedAcceptedHead,
/// Exact deployed coordinated migration plan.
expected_plan: SchemaMigrationPlanDigest,
},
}
/// Bounded status-page request. Cursors are opaque engine-owned bytes.
#[derive(CandidType, Clone, Debug, Default, Deserialize, Eq, PartialEq)]
pub struct SchemaMigrationStatusRequest {
cursor: Option<Vec<u8>>,
}
impl SchemaMigrationStatusRequest {
/// Construct a status request from an optional opaque cursor.
#[must_use]
pub const fn new(cursor: Option<Vec<u8>>) -> Self {
Self { cursor }
}
/// Borrow the optional opaque cursor.
#[must_use]
pub fn cursor(&self) -> Option<&[u8]> {
self.cursor.as_deref()
}
pub(in crate::db::schema) fn validate(&self) -> bool {
self.cursor
.as_ref()
.is_none_or(|cursor| cursor.len() <= MAX_SCHEMA_MIGRATION_STATUS_CURSOR_BYTES)
}
}
/// Current source-migration lifecycle phase.
#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum SchemaMigrationPhase {
/// Existing generated entities have not been adopted into lineage authority.
Unadopted,
/// Existing version-1 generated entities were adopted exactly.
Adopted,
/// An exact coordinated plan is deployed and ready for its first advance.
Idle,
/// The exact plan and candidate were prepared durably.
Prepared,
/// Historical candidate validation is in progress.
Validating,
/// Validation completed and row rewriting may begin.
ReadyToRewrite,
/// Candidate row layouts are being written.
RewritingRows,
/// Candidate index generations are being rebuilt.
RebuildingIndexes,
/// Complete candidate state is undergoing final validation.
FinalValidation,
/// Candidate authority is crossing the atomic publication boundary.
Publishing,
/// The coordinated target entity versions are accepted.
Applied,
/// Validation produced a terminal rejected result.
Rejected,
/// A pre-rewrite migration was aborted.
Aborted,
}
/// One canonical declared entity-version transition.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct SchemaMigrationEntityTransition {
entity: EntitySourceKey,
from_version: Option<u32>,
to_version: u32,
}
impl SchemaMigrationEntityTransition {
pub(in crate::db::schema) const fn new(
entity: EntitySourceKey,
from_version: Option<u32>,
to_version: u32,
) -> Self {
Self {
entity,
from_version,
to_version,
}
}
/// Borrow the current entity source key.
#[must_use]
pub const fn entity(&self) -> &EntitySourceKey {
&self.entity
}
/// Return the predecessor source version, or `None` for adoption.
#[must_use]
pub const fn from_version(&self) -> Option<u32> {
self.from_version
}
/// Return the declared target source version.
#[must_use]
pub const fn to_version(&self) -> u32 {
self.to_version
}
}
/// Typed migration validation-finding family.
#[derive(CandidType, Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum SchemaMigrationFindingKind {
/// A closed transform failed on one accepted row.
Transform,
/// Candidate unique-index meaning failed.
UniqueIndex,
/// Candidate relation meaning failed.
Relation,
/// Candidate row-constraint meaning failed.
Constraint,
}
/// One bounded accepted-ID migration finding.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct SchemaMigrationFinding {
kind: SchemaMigrationFindingKind,
entity_tag: u64,
primary_key: Vec<u8>,
}
impl SchemaMigrationFinding {
pub(in crate::db::schema) const fn new(
kind: SchemaMigrationFindingKind,
entity_tag: u64,
primary_key: Vec<u8>,
) -> Self {
Self {
kind,
entity_tag,
primary_key,
}
}
/// Return the typed validation-finding family.
#[must_use]
pub const fn kind(&self) -> SchemaMigrationFindingKind {
self.kind
}
/// Return the accepted entity identity associated with the finding.
#[must_use]
pub const fn entity_tag(&self) -> u64 {
self.entity_tag
}
/// Borrow the exact primary-key bytes associated with the finding.
#[must_use]
pub const fn primary_key(&self) -> &[u8] {
self.primary_key.as_slice()
}
}
/// Terminal proof for one adopted or applied source transition.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct SchemaMigrationReceipt {
database_identity: TargetDatabaseIdentity,
plan_digest: Option<SchemaMigrationPlanDigest>,
prior_head: ExpectedAcceptedHead,
accepted_head: ExpectedAcceptedHead,
}
impl SchemaMigrationReceipt {
pub(in crate::db::schema) const fn new(
database_identity: TargetDatabaseIdentity,
plan_digest: Option<SchemaMigrationPlanDigest>,
prior_head: ExpectedAcceptedHead,
accepted_head: ExpectedAcceptedHead,
) -> Self {
Self {
database_identity,
plan_digest,
prior_head,
accepted_head,
}
}
/// Return the target database identity.
#[must_use]
pub const fn database_identity(&self) -> TargetDatabaseIdentity {
self.database_identity
}
/// Return the coordinated plan digest, or `None` for adoption.
#[must_use]
pub const fn plan_digest(&self) -> Option<SchemaMigrationPlanDigest> {
self.plan_digest
}
/// Borrow the predecessor accepted head.
#[must_use]
pub const fn prior_head(&self) -> &ExpectedAcceptedHead {
&self.prior_head
}
/// Borrow the terminal accepted head.
#[must_use]
pub const fn accepted_head(&self) -> &ExpectedAcceptedHead {
&self.accepted_head
}
}
/// One bounded page of deployed source-migration status.
#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct SchemaMigrationStatusPage {
database_identity: TargetDatabaseIdentity,
accepted_head: ExpectedAcceptedHead,
plan_digest: Option<SchemaMigrationPlanDigest>,
phase: SchemaMigrationPhase,
transitions: Vec<SchemaMigrationEntityTransition>,
rows_validated: u64,
rows_rewritten: u64,
indexes_rebuilt: u32,
findings: Vec<SchemaMigrationFinding>,
next_cursor: Option<Vec<u8>>,
terminal_receipt: Option<SchemaMigrationReceipt>,
}
impl SchemaMigrationStatusPage {
#[expect(
clippy::too_many_arguments,
reason = "the constructor mirrors the frozen bounded status-page contract"
)]
pub(in crate::db::schema) fn new(
database_identity: TargetDatabaseIdentity,
accepted_head: ExpectedAcceptedHead,
plan_digest: Option<SchemaMigrationPlanDigest>,
phase: SchemaMigrationPhase,
transitions: Vec<SchemaMigrationEntityTransition>,
rows_validated: u64,
rows_rewritten: u64,
indexes_rebuilt: u32,
findings: Vec<SchemaMigrationFinding>,
next_cursor: Option<Vec<u8>>,
terminal_receipt: Option<SchemaMigrationReceipt>,
) -> Self {
debug_assert!(findings.len() <= MAX_SCHEMA_MIGRATION_FINDINGS_PER_PAGE);
Self {
database_identity,
accepted_head,
plan_digest,
phase,
transitions,
rows_validated,
rows_rewritten,
indexes_rebuilt,
findings,
next_cursor,
terminal_receipt,
}
}
/// Return the target database identity.
#[must_use]
pub const fn database_identity(&self) -> TargetDatabaseIdentity {
self.database_identity
}
/// Borrow the current accepted database head.
#[must_use]
pub const fn accepted_head(&self) -> &ExpectedAcceptedHead {
&self.accepted_head
}
/// Return the deployed plan digest when one is present.
#[must_use]
pub const fn plan_digest(&self) -> Option<SchemaMigrationPlanDigest> {
self.plan_digest
}
/// Return the current lifecycle phase.
#[must_use]
pub const fn phase(&self) -> SchemaMigrationPhase {
self.phase
}
/// Borrow canonical entity transitions.
#[must_use]
pub const fn transitions(&self) -> &[SchemaMigrationEntityTransition] {
self.transitions.as_slice()
}
/// Return rows validated so far.
#[must_use]
pub const fn rows_validated(&self) -> u64 {
self.rows_validated
}
/// Return rows rewritten so far.
#[must_use]
pub const fn rows_rewritten(&self) -> u64 {
self.rows_rewritten
}
/// Return indexes rebuilt so far.
#[must_use]
pub const fn indexes_rebuilt(&self) -> u32 {
self.indexes_rebuilt
}
/// Borrow this page's typed findings.
#[must_use]
pub const fn findings(&self) -> &[SchemaMigrationFinding] {
self.findings.as_slice()
}
/// Borrow the optional next opaque cursor.
#[must_use]
pub fn next_cursor(&self) -> Option<&[u8]> {
self.next_cursor.as_deref()
}
/// Borrow the optional terminal receipt.
#[must_use]
pub const fn terminal_receipt(&self) -> Option<&SchemaMigrationReceipt> {
self.terminal_receipt.as_ref()
}
}