icydb-core 0.180.17

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
use super::{
    SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmission,
    SchemaDdlMutationAdmissionError, SchemaDdlMutationTarget,
};
use crate::db::schema::{
    AcceptedSchemaSnapshot, FieldId, PersistedFieldSnapshot, PersistedSchemaSnapshot,
    SchemaFieldDefault, SchemaFieldSlot, SchemaRowLayout,
};

///
/// SchemaFieldAdditionTarget
///
/// Accepted additive-field target admitted for SQL DDL publication.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SchemaFieldAdditionTarget {
    field_id: FieldId,
    name: String,
    slot: SchemaFieldSlot,
}

impl SchemaFieldAdditionTarget {
    /// Build one additive-field DDL target from accepted field metadata.
    #[must_use]
    fn from_field(field: &PersistedFieldSnapshot) -> Self {
        Self {
            field_id: field.id(),
            name: field.name().to_string(),
            slot: field.slot(),
        }
    }

    /// Return the accepted field ID.
    #[must_use]
    pub(in crate::db) const fn field_id(&self) -> FieldId {
        self.field_id
    }

    /// Borrow the accepted field name.
    #[must_use]
    pub(in crate::db) const fn name(&self) -> &str {
        self.name.as_str()
    }

    /// Return the accepted row slot.
    #[must_use]
    pub(in crate::db) const fn slot(&self) -> SchemaFieldSlot {
        self.slot
    }
}

///
/// SchemaFieldDropTarget
///
/// Accepted retained-slot field removal target admitted for SQL DDL publication.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SchemaFieldDropTarget {
    field_id: FieldId,
    name: String,
    slot: SchemaFieldSlot,
}

impl SchemaFieldDropTarget {
    /// Build one field-drop DDL target from accepted field metadata.
    #[must_use]
    fn from_field(field: &PersistedFieldSnapshot) -> Self {
        Self {
            field_id: field.id(),
            name: field.name().to_string(),
            slot: field.slot(),
        }
    }

    /// Return the accepted field ID.
    #[must_use]
    pub(in crate::db) const fn field_id(&self) -> FieldId {
        self.field_id
    }

    /// Borrow the accepted field name.
    #[must_use]
    pub(in crate::db) const fn name(&self) -> &str {
        self.name.as_str()
    }

    /// Return the retired accepted row slot.
    #[must_use]
    pub(in crate::db) const fn slot(&self) -> SchemaFieldSlot {
        self.slot
    }
}

///
/// SchemaFieldDefaultTarget
///
/// Accepted field-default metadata target admitted for SQL DDL publication.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SchemaFieldDefaultTarget {
    field_id: FieldId,
    name: String,
}

impl SchemaFieldDefaultTarget {
    /// Build one field-default DDL target from accepted field metadata.
    #[must_use]
    fn from_field(field: &PersistedFieldSnapshot) -> Self {
        Self {
            field_id: field.id(),
            name: field.name().to_string(),
        }
    }

    /// Return the accepted field ID.
    #[must_use]
    pub(in crate::db) const fn field_id(&self) -> FieldId {
        self.field_id
    }

    /// Borrow the accepted field name.
    #[must_use]
    pub(in crate::db) const fn name(&self) -> &str {
        self.name.as_str()
    }
}

///
/// SchemaFieldNullabilityTarget
///
/// Accepted field-nullability metadata target admitted for SQL DDL publication.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SchemaFieldNullabilityTarget {
    field_id: FieldId,
    name: String,
}

impl SchemaFieldNullabilityTarget {
    /// Build one field-nullability DDL target from accepted field metadata.
    #[must_use]
    fn from_field(field: &PersistedFieldSnapshot) -> Self {
        Self {
            field_id: field.id(),
            name: field.name().to_string(),
        }
    }

    /// Return the accepted field ID.
    #[must_use]
    pub(in crate::db) const fn field_id(&self) -> FieldId {
        self.field_id
    }

    /// Borrow the accepted field name.
    #[must_use]
    pub(in crate::db) const fn name(&self) -> &str {
        self.name.as_str()
    }
}

///
/// SchemaFieldRenameTarget
///
/// Accepted field-name metadata target admitted for SQL DDL publication.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) struct SchemaFieldRenameTarget {
    field_id: FieldId,
    old_name: String,
    new_name: String,
}

impl SchemaFieldRenameTarget {
    /// Build one field-rename DDL target from accepted before metadata and
    /// schema-owned target naming.
    #[must_use]
    fn from_field_name(before: &PersistedFieldSnapshot, new_name: &str) -> Self {
        Self {
            field_id: before.id(),
            old_name: before.name().to_string(),
            new_name: new_name.to_string(),
        }
    }

    /// Return the accepted field ID.
    #[must_use]
    pub(in crate::db) const fn field_id(&self) -> FieldId {
        self.field_id
    }

    /// Borrow the accepted source field name.
    #[must_use]
    pub(in crate::db) const fn old_name(&self) -> &str {
        self.old_name.as_str()
    }

    /// Borrow the accepted target field name.
    #[must_use]
    pub(in crate::db) const fn new_name(&self) -> &str {
        self.new_name.as_str()
    }
}

/// Field drop candidate resolution failures for SQL DDL-authored schema
/// mutations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum SchemaDdlFieldDropCandidateError {
    /// No accepted field matches the requested SQL column name.
    Unknown,
    /// The requested accepted field is part of the primary key.
    PrimaryKey,
    /// The requested accepted field is generated-owned.
    Generated,
    /// The requested accepted field is still referenced by an accepted index.
    Indexed(String),
}

/// Field default candidate resolution failures for SQL DDL-authored schema
/// mutations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum SchemaDdlFieldDefaultCandidateError {
    /// No accepted field matches the requested SQL column name.
    Unknown,
    /// The requested accepted field is generated-owned.
    Generated,
    /// The requested accepted field is required and currently has a default.
    Required,
}

/// Field nullability candidate resolution failures for SQL DDL-authored schema
/// mutations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum SchemaDdlFieldNullabilityCandidateError {
    /// No accepted field matches the requested SQL column name.
    Unknown,
    /// The requested accepted field is generated-owned.
    Generated,
}

/// Field rename candidate resolution failures for SQL DDL-authored schema
/// mutations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::db) enum SchemaDdlFieldRenameCandidateError {
    /// No accepted source field matches the requested SQL column name.
    Unknown,
    /// An accepted field already uses the requested target column name.
    Duplicate,
    /// The requested accepted field is generated-owned.
    Generated,
}

/// Admit one SQL DDL field addition through the schema-owned mutation request
/// boundary. Publication policy is validated against the full accepted-before
/// and accepted-after snapshots before execution stores the derived snapshot.
pub(in crate::db) fn admit_sql_ddl_field_addition_candidate(
    field: &PersistedFieldSnapshot,
) -> SchemaDdlMutationAdmission {
    SchemaDdlMutationAdmission {
        target: SchemaDdlMutationTarget::FieldAddition(SchemaFieldAdditionTarget::from_field(
            field,
        )),
    }
}

/// Admit one SQL DDL retained-slot field drop.
#[must_use]
pub(in crate::db) fn admit_sql_ddl_field_drop_candidate(
    field: &PersistedFieldSnapshot,
) -> SchemaDdlMutationAdmission {
    SchemaDdlMutationAdmission {
        target: SchemaDdlMutationTarget::FieldDrop(SchemaFieldDropTarget::from_field(field)),
    }
}

/// Admit one SQL DDL field-default metadata change.
pub(in crate::db) fn admit_sql_ddl_field_default_candidate(
    field: &PersistedFieldSnapshot,
) -> SchemaDdlMutationAdmission {
    SchemaDdlMutationAdmission {
        target: SchemaDdlMutationTarget::FieldDefaultChange(SchemaFieldDefaultTarget::from_field(
            field,
        )),
    }
}

/// Admit one SQL DDL field-nullability metadata candidate.
#[must_use]
pub(in crate::db) fn admit_sql_ddl_field_nullability_candidate(
    field: &PersistedFieldSnapshot,
) -> SchemaDdlMutationAdmission {
    SchemaDdlMutationAdmission {
        target: SchemaDdlMutationTarget::FieldNullabilityChange(
            SchemaFieldNullabilityTarget::from_field(field),
        ),
    }
}

/// Admit one SQL DDL field-rename metadata candidate.
#[must_use]
pub(in crate::db) fn admit_sql_ddl_field_rename_candidate(
    before: &PersistedFieldSnapshot,
    new_name: &str,
) -> SchemaDdlMutationAdmission {
    SchemaDdlMutationAdmission {
        target: SchemaDdlMutationTarget::FieldRename(SchemaFieldRenameTarget::from_field_name(
            before, new_name,
        )),
    }
}

fn resolve_sql_ddl_field_drop_dependent_index(
    accepted_before: &AcceptedSchemaSnapshot,
    field_id: FieldId,
) -> Option<String> {
    accepted_before
        .persisted_snapshot()
        .indexes()
        .iter()
        .find(|index| index.key().references_field(field_id))
        .map(|index| index.name().to_string())
}

/// Resolve one accepted SQL DDL field-drop candidate and reject primary-key,
/// generated-owned, or index-referenced fields before the frontend can derive a
/// catalog mutation.
pub(in crate::db) fn resolve_sql_ddl_field_drop_candidate(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
) -> Result<PersistedFieldSnapshot, SchemaDdlFieldDropCandidateError> {
    let accepted = accepted_before.persisted_snapshot();
    let field = accepted
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlFieldDropCandidateError::Unknown)?;

    if accepted.primary_key_field_ids().contains(&field.id()) {
        return Err(SchemaDdlFieldDropCandidateError::PrimaryKey);
    }

    if field.generated() {
        return Err(SchemaDdlFieldDropCandidateError::Generated);
    }

    if let Some(index_name) =
        resolve_sql_ddl_field_drop_dependent_index(accepted_before, field.id())
    {
        return Err(SchemaDdlFieldDropCandidateError::Indexed(index_name));
    }

    Ok(field.clone())
}

/// Resolve one accepted SQL DDL SET DEFAULT candidate and reject
/// generated-owned fields before default encoding or mutation derivation.
pub(in crate::db) fn resolve_sql_ddl_field_set_default_candidate(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
) -> Result<PersistedFieldSnapshot, SchemaDdlFieldDefaultCandidateError> {
    let field = accepted_before
        .persisted_snapshot()
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlFieldDefaultCandidateError::Unknown)?;

    if field.generated() {
        return Err(SchemaDdlFieldDefaultCandidateError::Generated);
    }

    Ok(field.clone())
}

/// Resolve one accepted SQL DDL DROP DEFAULT candidate. Missing defaults are
/// returned so SQL can report the existing true no-op behavior, while real
/// generated-owned or required-field changes reject before mutation derivation.
pub(in crate::db) fn resolve_sql_ddl_field_drop_default_candidate(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
) -> Result<PersistedFieldSnapshot, SchemaDdlFieldDefaultCandidateError> {
    let field = accepted_before
        .persisted_snapshot()
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlFieldDefaultCandidateError::Unknown)?;

    if field.default().is_none() {
        return Ok(field.clone());
    }

    if field.generated() {
        return Err(SchemaDdlFieldDefaultCandidateError::Generated);
    }

    if !field.nullable() {
        return Err(SchemaDdlFieldDefaultCandidateError::Required);
    }

    Ok(field.clone())
}

/// Resolve one accepted SQL DDL nullability candidate. Matching nullability is
/// returned so SQL can preserve true no-op behavior before generated ownership
/// rejects only actual changes.
pub(in crate::db) fn resolve_sql_ddl_field_nullability_candidate(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
    nullable: bool,
) -> Result<PersistedFieldSnapshot, SchemaDdlFieldNullabilityCandidateError> {
    let field = accepted_before
        .persisted_snapshot()
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlFieldNullabilityCandidateError::Unknown)?;

    if field.nullable() != nullable && field.generated() {
        return Err(SchemaDdlFieldNullabilityCandidateError::Generated);
    }

    Ok(field.clone())
}

/// Resolve one accepted SQL DDL field-rename candidate. Same-name renames are
/// returned as true no-ops before generated ownership rejects actual renames.
pub(in crate::db) fn resolve_sql_ddl_field_rename_candidate(
    accepted_before: &AcceptedSchemaSnapshot,
    old_name: &str,
    new_name: &str,
) -> Result<PersistedFieldSnapshot, SchemaDdlFieldRenameCandidateError> {
    let accepted = accepted_before.persisted_snapshot();
    let field = accepted
        .fields()
        .iter()
        .find(|field| field.name() == old_name)
        .ok_or(SchemaDdlFieldRenameCandidateError::Unknown)?;

    if old_name == new_name {
        return Ok(field.clone());
    }

    if accepted
        .fields()
        .iter()
        .any(|field| field.name() == new_name)
    {
        return Err(SchemaDdlFieldRenameCandidateError::Duplicate);
    }

    if field.generated() {
        return Err(SchemaDdlFieldRenameCandidateError::Generated);
    }

    Ok(field.clone())
}

/// Derive and admit the accepted-after schema snapshot for one SQL DDL
/// additive field candidate.
pub(in crate::db) fn derive_sql_ddl_field_addition_accepted_after(
    accepted_before: &AcceptedSchemaSnapshot,
    field: PersistedFieldSnapshot,
) -> Result<SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmissionError> {
    let before = accepted_before.persisted_snapshot();
    let mut fields = before.fields().to_vec();
    fields.push(field.clone());
    let mut field_to_slot = before.row_layout().field_to_slot().to_vec();
    field_to_slot.push((field.id(), field.slot()));
    let persisted_after = PersistedSchemaSnapshot::new_with_primary_key_fields_and_indexes(
        before.version(),
        before.entity_path().to_string(),
        before.entity_name().to_string(),
        before.primary_key_field_ids().to_vec(),
        SchemaRowLayout::new_with_retired_slots(
            before.row_layout().version(),
            field_to_slot,
            before.row_layout().retired_field_slots().to_vec(),
        ),
        fields,
        before.indexes().to_vec(),
    );
    let accepted_after = AcceptedSchemaSnapshot::try_new(persisted_after)
        .map_err(|_| SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let admission = admit_sql_ddl_field_addition_candidate(&field);

    Ok(SchemaDdlAcceptedSnapshotDerivation {
        accepted_after,
        admission,
    })
}

/// Derive and admit the accepted-after schema snapshot for one SQL DDL
/// retained-slot field drop.
pub(in crate::db) fn derive_sql_ddl_field_drop_accepted_after(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
) -> Result<SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmissionError> {
    let before = accepted_before.persisted_snapshot();
    let before_field = before
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlMutationAdmissionError::UnsupportedExecutionPath)?;
    let fields = before
        .fields()
        .iter()
        .filter(|field| field.id() != before_field.id())
        .cloned()
        .collect();
    let row_layout = before
        .row_layout()
        .clone_retiring_field(before_field.id())
        .ok_or(SchemaDdlMutationAdmissionError::UnsupportedExecutionPath)?;
    let persisted_after = PersistedSchemaSnapshot::new_with_primary_key_fields_and_indexes(
        before.version(),
        before.entity_path().to_string(),
        before.entity_name().to_string(),
        before.primary_key_field_ids().to_vec(),
        row_layout,
        fields,
        before.indexes().to_vec(),
    );
    let accepted_after = AcceptedSchemaSnapshot::try_new(persisted_after)
        .map_err(|_| SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let admission = admit_sql_ddl_field_drop_candidate(before_field);

    Ok(SchemaDdlAcceptedSnapshotDerivation {
        accepted_after,
        admission,
    })
}

/// Derive and admit the accepted-after schema snapshot for one SQL DDL
/// field-default metadata change.
pub(in crate::db) fn derive_sql_ddl_field_default_accepted_after(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
    default: SchemaFieldDefault,
) -> Result<SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmissionError> {
    let before = accepted_before.persisted_snapshot();
    let before_field = before
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlMutationAdmissionError::UnsupportedExecutionPath)?;
    let fields = before
        .fields()
        .iter()
        .map(|field| {
            if field.id() == before_field.id() {
                field.clone_with_default(default.clone())
            } else {
                field.clone()
            }
        })
        .collect();
    let persisted_after = PersistedSchemaSnapshot::new_with_primary_key_fields_and_indexes(
        before.version(),
        before.entity_path().to_string(),
        before.entity_name().to_string(),
        before.primary_key_field_ids().to_vec(),
        before.row_layout().clone(),
        fields,
        before.indexes().to_vec(),
    );
    let accepted_after = AcceptedSchemaSnapshot::try_new(persisted_after)
        .map_err(|_| SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let after_field = accepted_after
        .persisted_snapshot()
        .fields()
        .iter()
        .find(|field| field.id() == before_field.id())
        .ok_or(SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let admission = admit_sql_ddl_field_default_candidate(after_field);

    Ok(SchemaDdlAcceptedSnapshotDerivation {
        accepted_after,
        admission,
    })
}

/// Derive and admit the accepted-after schema snapshot for one SQL DDL
/// field-nullability metadata change.
pub(in crate::db) fn derive_sql_ddl_field_nullability_accepted_after(
    accepted_before: &AcceptedSchemaSnapshot,
    field_name: &str,
    nullable: bool,
) -> Result<SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmissionError> {
    let before = accepted_before.persisted_snapshot();
    let before_field = before
        .fields()
        .iter()
        .find(|field| field.name() == field_name)
        .ok_or(SchemaDdlMutationAdmissionError::UnsupportedExecutionPath)?;
    let fields = before
        .fields()
        .iter()
        .map(|field| {
            if field.id() == before_field.id() {
                field.clone_with_nullable(nullable)
            } else {
                field.clone()
            }
        })
        .collect();
    let persisted_after = PersistedSchemaSnapshot::new_with_primary_key_fields_and_indexes(
        before.version(),
        before.entity_path().to_string(),
        before.entity_name().to_string(),
        before.primary_key_field_ids().to_vec(),
        before.row_layout().clone(),
        fields,
        before.indexes().to_vec(),
    );
    let accepted_after = AcceptedSchemaSnapshot::try_new(persisted_after)
        .map_err(|_| SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let after_field = accepted_after
        .persisted_snapshot()
        .fields()
        .iter()
        .find(|field| field.id() == before_field.id())
        .ok_or(SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let admission = admit_sql_ddl_field_nullability_candidate(after_field);

    Ok(SchemaDdlAcceptedSnapshotDerivation {
        accepted_after,
        admission,
    })
}

/// Derive and admit the accepted-after schema snapshot for one SQL DDL
/// field-rename metadata change.
pub(in crate::db) fn derive_sql_ddl_field_rename_accepted_after(
    accepted_before: &AcceptedSchemaSnapshot,
    old_name: &str,
    new_name: &str,
) -> Result<SchemaDdlAcceptedSnapshotDerivation, SchemaDdlMutationAdmissionError> {
    let before = accepted_before.persisted_snapshot();
    let before_field = before
        .fields()
        .iter()
        .find(|field| field.name() == old_name)
        .ok_or(SchemaDdlMutationAdmissionError::UnsupportedExecutionPath)?;
    let fields = before
        .fields()
        .iter()
        .map(|field| {
            if field.id() == before_field.id() {
                field.clone_with_name(new_name.to_string())
            } else {
                field.clone()
            }
        })
        .collect();
    let indexes = before
        .indexes()
        .iter()
        .map(|index| {
            index.clone_with_renamed_field_path_root(
                before_field.id(),
                before_field.name(),
                new_name,
            )
        })
        .collect();
    let persisted_after = PersistedSchemaSnapshot::new_with_primary_key_fields_and_indexes(
        before.version(),
        before.entity_path().to_string(),
        before.entity_name().to_string(),
        before.primary_key_field_ids().to_vec(),
        before.row_layout().clone(),
        fields,
        indexes,
    );
    let accepted_after = AcceptedSchemaSnapshot::try_new(persisted_after)
        .map_err(|_| SchemaDdlMutationAdmissionError::AcceptedAfterRejected)?;
    let admission = admit_sql_ddl_field_rename_candidate(before_field, new_name);

    Ok(SchemaDdlAcceptedSnapshotDerivation {
        accepted_after,
        admission,
    })
}