drizzle-migrations 0.1.16

Migration infrastructure for drizzle-rs
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
690
691
692
693
694
695
696
697
698
//! Parser types - shared data structures for all dialects
//!
//! Each parsed entity keeps two views of the source:
//!
//! * **Textual compat surface** — `attr` / `attrs` / `ty` hold the original
//!   source text (sliced by span), so string-oriented consumers (round-trip
//!   introspection tests, debug output) keep working unchanged.
//! * **Structured spec** — `spec` fields hold the attribute data interpreted
//!   with the *same semantics as the procedural macros* (case-insensitive
//!   markers, normalized referential actions, resolved SQL types). Snapshot
//!   building reads only the structured spec; the textual surface is never
//!   used for semantic decisions.

use std::collections::HashMap;

use drizzle_types::Dialect;

// =============================================================================
// Structured column / table specs (macro-equivalent interpretation)
// =============================================================================

/// A parsed `default = <literal>` value, mirroring the literal kinds the
/// table macros accept (`sqlite::field::build_sql_definition` /
/// `postgres::field::parse_column_attribute`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParsedDefault {
    /// Integer literal token text (e.g. `42`).
    Int(String),
    /// Float literal token text (e.g. `3.14`).
    Float(String),
    /// Boolean literal.
    Bool(bool),
    /// String literal *value* (unescaped, without the Rust quotes).
    Str(String),
    /// Any other expression (e.g. `default = -1`). The macros silently drop
    /// these; the snapshot builder mirrors that and emits a warning.
    Unsupported(String),
}

/// A `references = Table::column` target.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedReference {
    /// Referenced table struct ident (e.g. `Users`).
    pub table: String,
    /// Referenced field ident (e.g. `id`).
    pub column: String,
}

/// `generated(stored|virtual, "expr")` data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedGenerated {
    /// Raw SQL expression as written (no parenthesization applied).
    pub expression: String,
    /// `true` for STORED, `false` for VIRTUAL.
    pub stored: bool,
}

/// `identity(...)` data (`PostgreSQL` only).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedIdentity {
    /// `true` for `GENERATED ALWAYS`, `false` for `BY DEFAULT`.
    pub always: bool,
    /// START WITH value.
    pub start: Option<String>,
    /// INCREMENT BY value.
    pub increment: Option<String>,
    /// MINVALUE.
    pub min_value: Option<String>,
    /// MAXVALUE.
    pub max_value: Option<String>,
    /// CACHE size.
    pub cache: Option<i32>,
    /// CYCLE flag.
    pub cycle: bool,
}

/// `PostgreSQL` serial pseudo-type markers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SerialKind {
    Smallserial,
    Serial,
    Bigserial,
}

/// Structured, macro-equivalent interpretation of a field's column attributes
/// plus the resolved SQL type for the owning table's dialect.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ColumnSpec {
    /// `primary` / `PRIMARY_KEY` marker.
    pub primary: bool,
    /// `unique` marker.
    pub unique: bool,
    /// `autoincrement` marker (`SQLite` only).
    pub autoincrement: bool,
    /// `json` marker.
    pub json: bool,
    /// `jsonb` marker.
    pub jsonb: bool,
    /// `enum` marker.
    pub enum_marker: bool,
    /// `serial` / `smallserial` / `bigserial` (`PostgreSQL` only).
    pub serial: Option<SerialKind>,
    /// `identity(...)` (`PostgreSQL` only).
    pub identity: Option<ParsedIdentity>,
    /// `generated(...)`.
    pub generated: Option<ParsedGenerated>,
    /// `default = <literal>`.
    pub default: Option<ParsedDefault>,
    /// `default_sql = "..."` raw SQL (not normalized; the snapshot builder
    /// applies the dialect-specific normalization the macros use).
    pub default_sql: Option<String>,
    /// `default_fn = ...` present (runtime-only default; no DDL impact).
    pub has_default_fn: bool,
    /// `check = "..."` column-level check expression.
    pub check: Option<String>,
    /// `references = Table::column`.
    pub references: Option<ParsedReference>,
    /// Normalized ON DELETE action (`CASCADE`, `SET NULL`, ...), exactly as
    /// the macros normalize (`SET_NULL` → `SET NULL`).
    pub on_delete: Option<String>,
    /// Normalized ON UPDATE action.
    pub on_update: Option<String>,
    /// ON DELETE action exactly as written in source (compat accessor).
    pub on_delete_raw: Option<String>,
    /// ON UPDATE action exactly as written in source (compat accessor).
    pub on_update_raw: Option<String>,
    /// `deferrable` FK marker (`PostgreSQL` only).
    pub deferrable: bool,
    /// `initially_deferred` FK marker (`PostgreSQL` only).
    pub initially_deferred: bool,
    /// Explicit column name from `name = "..."`.
    pub explicit_name: Option<String>,
    /// `collate = ...` (SQLite uppercases bare idents; Postgres keeps them
    /// verbatim — mirrored from the respective macros).
    pub collate: Option<String>,
    /// `relation = "..."` reverse-relation accessor name (no DDL impact).
    pub relation: Option<String>,
    /// Whether the Rust type is `Option<T>` (matched by last path segment,
    /// so `std::option::Option<T>` is recognized too).
    pub nullable: bool,
    /// Doc comment (used as the column comment for `PostgreSQL`).
    pub comment: Option<String>,
    /// Raw `key = value` pairs as written (key, raw value source text) for
    /// the textual `attr_value` compat accessor.
    pub named_values: Vec<(String, String)>,
    /// Resolved `SQLite` storage type (`INTEGER`, `TEXT`, ...). `None` for
    /// fields on non-SQLite tables.
    pub sqlite_type: Option<String>,
    /// Resolved `PostgreSQL` SQL type (`INTEGER`, `TEXT`, enum type name,
    /// ...). `None` for fields on non-Postgres tables. Array element type
    /// for `Vec<T>` columns (dimensions carried separately).
    pub pg_type: Option<String>,
    /// `PostgreSQL` array dimensions (`Some(1)` for `Vec<T>`).
    pub pg_dimensions: Option<i32>,
    /// The Rust type was unknown to the macro type tables; the macros defer
    /// the SQL type to a trait const the parser cannot evaluate.
    pub is_custom_type: bool,
}

/// Composite `FOREIGN_KEY(...)` table attribute.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CompositeFkSpec {
    /// Source field idents.
    pub source_columns: Vec<String>,
    /// Target table struct ident.
    pub target_table: String,
    /// Target field idents.
    pub target_columns: Vec<String>,
    /// Raw ON DELETE string (table-level FK actions are free-form strings in
    /// the macros; no normalization is applied there).
    pub on_delete: Option<String>,
    /// Raw ON UPDATE string.
    pub on_update: Option<String>,
    /// DEFERRABLE (`PostgreSQL` only).
    pub deferrable: bool,
    /// INITIALLY DEFERRED (`PostgreSQL` only).
    pub initially_deferred: bool,
}

/// Table-level `UNIQUE(...)` attribute.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TableUniqueSpec {
    /// Field idents.
    pub columns: Vec<String>,
    /// Explicit constraint name.
    pub name: Option<String>,
    /// NULLS NOT DISTINCT (`PostgreSQL` only).
    pub nulls_not_distinct: bool,
    /// DEFERRABLE (`PostgreSQL` only).
    pub deferrable: bool,
    /// INITIALLY DEFERRED (`PostgreSQL` only).
    pub initially_deferred: bool,
}

/// Table-level `CHECK(...)` attribute.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TableCheckSpec {
    /// Explicit constraint name.
    pub name: Option<String>,
    /// Check expression.
    pub expr: String,
}

/// Structured, macro-equivalent interpretation of the table attribute.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TableSpec {
    /// Explicit table name from `name = "..."`.
    pub explicit_name: Option<String>,
    /// `schema = "..."` (`PostgreSQL` only).
    pub schema: Option<String>,
    /// `strict` (`SQLite` only).
    pub strict: bool,
    /// `without_rowid` (`SQLite` only).
    pub without_rowid: bool,
    /// `UNLOGGED` (`PostgreSQL` only).
    pub unlogged: bool,
    /// `TEMPORARY` (`PostgreSQL` only).
    pub temporary: bool,
    /// `RLS` (`PostgreSQL` only).
    pub rls: bool,
    /// `INHERITS = "..."` (`PostgreSQL` only).
    pub inherits: Option<String>,
    /// `TABLESPACE = "..."` (`PostgreSQL` only).
    pub tablespace: Option<String>,
    /// Composite `FOREIGN_KEY(...)` attributes in declaration order.
    pub composite_fks: Vec<CompositeFkSpec>,
    /// Table-level `UNIQUE(...)` attributes in declaration order.
    pub unique_constraints: Vec<TableUniqueSpec>,
    /// Table-level `CHECK(...)` attributes in declaration order.
    pub check_constraints: Vec<TableCheckSpec>,
    /// Doc comment (used as the table comment for `PostgreSQL`).
    pub comment: Option<String>,
    /// Raw `key = value` pairs for the textual `attr_value` compat accessor.
    pub named_values: Vec<(String, String)>,
}

/// Structured index attribute data.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct IndexSpec {
    /// `unique` marker.
    pub unique: bool,
    /// `concurrent` marker (`PostgreSQL` only).
    pub concurrent: bool,
    /// Explicit `method = "..."` (`PostgreSQL` only). `None` when unwritten —
    /// PostgreSQL's implicit btree default is never materialized (the macro
    /// and the renderer both treat absence as btree).
    pub method: Option<String>,
    /// `where = "..."` partial-index predicate.
    pub where_clause: Option<String>,
    /// `tablespace = "..."` (`PostgreSQL` only).
    pub tablespace: Option<String>,
    /// `name = "..."` — parser extension: the index macros derive the name
    /// from the struct ident and accept no `name` attribute, but the parser
    /// honors one when present so hand-annotated schema files keep working.
    pub explicit_name: Option<String>,
    /// `(table struct ident, field ident)` pairs in declaration order.
    pub column_refs: Vec<(String, String)>,
}

// =============================================================================
// Parsed entities
// =============================================================================

/// Parsed table struct from schema code
#[derive(Debug, Clone, Default)]
pub struct ParsedTable {
    /// Struct name (`PascalCase`)
    pub name: String,
    /// Full table attribute source text (e.g. `#[SQLiteTable(strict)]`)
    pub attr: String,
    /// Parsed fields
    pub fields: Vec<ParsedField>,
    /// Detected dialect
    pub dialect: Dialect,
    /// Structured table attribute data (macro-equivalent semantics).
    pub spec: TableSpec,
    /// Position of the item in the parsed source (used for deterministic
    /// snapshot entity ordering).
    pub(crate) order: usize,
}

/// Parsed index struct from schema code
#[derive(Debug, Clone, Default)]
pub struct ParsedIndex {
    /// Struct name (`PascalCase`)
    pub name: String,
    /// Full index attribute source text (e.g. `#[SQLiteIndex(unique)]`)
    pub attr: String,
    /// Column references (e.g. `["Users::id", "Users::name"]`)
    pub columns: Vec<String>,
    /// Detected dialect
    pub dialect: Dialect,
    /// Structured index attribute data.
    pub spec: IndexSpec,
    /// Source position, see [`ParsedTable::order`].
    pub(crate) order: usize,
}

/// Parsed schema struct from schema code
#[derive(Debug, Clone, Default)]
pub struct ParsedSchema {
    /// Struct name (e.g., "`AppSchema`")
    pub name: String,
    /// Schema members (`field_name` -> `type_name` source text)
    pub members: HashMap<String, String>,
    /// Detected dialect
    pub dialect: Dialect,
    /// Member type names reduced to their last path segment, in field order.
    pub(crate) member_types: Vec<String>,
}

/// Parsed field from a table/view struct
#[derive(Debug, Clone, Default)]
pub struct ParsedField {
    /// Field name (as written in Rust)
    pub name: String,
    /// Rust type source text (e.g., "i64", "Option<String>")
    pub ty: String,
    /// Field attribute source texts (e.g., `#[column(primary)]`); doc
    /// comments and `cfg` attributes are not included.
    pub attrs: Vec<String>,
    /// Structured column attribute data (macro-equivalent semantics).
    pub spec: ColumnSpec,
}

/// Parsed enum deriving `SQLiteEnum` / `PostgresEnum`.
#[derive(Debug, Clone, Default)]
pub struct ParsedEnum {
    /// Enum ident. For `PostgreSQL` this is also the SQL enum type name —
    /// the derive emits `CREATE TYPE <ident> AS ENUM (...)` verbatim.
    pub name: String,
    /// Variant idents in declaration order.
    pub variants: Vec<String>,
    /// Detected dialect
    pub dialect: Dialect,
    /// `#[postgres_enum(schema = "...")]` (`PostgreSQL` only). `None` means
    /// the type is created in `public`.
    pub schema: Option<String>,
    /// Source position, see [`ParsedTable::order`].
    pub(crate) order: usize,
}

/// Parsed `#[SQLiteView]` / `#[PostgresView]` struct.
#[derive(Debug, Clone, Default)]
pub struct ParsedView {
    /// Struct name (`PascalCase`)
    pub name: String,
    /// Full view attribute source text
    pub attr: String,
    /// Detected dialect
    pub dialect: Dialect,
    /// Explicit `name = "..."`.
    pub explicit_name: Option<String>,
    /// `schema = "..."` (`PostgreSQL` only).
    pub schema: Option<String>,
    /// `definition = "..."` literal SQL. `None` when the view uses the
    /// expression / `query(...)` forms, which the parser cannot evaluate
    /// (a warning is recorded in that case).
    pub definition: Option<String>,
    /// The view has a definition the parser cannot represent (expression or
    /// `query(...)` DSL form).
    pub has_opaque_definition: bool,
    /// `MATERIALIZED` (`PostgreSQL` only).
    pub materialized: bool,
    /// `EXISTING`.
    pub existing: bool,
    /// `WITH_NO_DATA` (`PostgreSQL` only).
    pub with_no_data: bool,
    /// `USING = "..."` (`PostgreSQL` only).
    pub using: Option<String>,
    /// `TABLESPACE = "..."` (`PostgreSQL` only).
    pub tablespace: Option<String>,
    /// Source position, see [`ParsedTable::order`].
    pub(crate) order: usize,
}

/// Parsed `#[PostgresPolicy]` struct.
#[derive(Debug, Clone, Default)]
pub struct ParsedPolicy {
    /// Struct name (`PascalCase`)
    pub name: String,
    /// Full policy attribute source text
    pub attr: String,
    /// Detected dialect
    pub dialect: Dialect,
    /// Target table struct ident (the single tuple field).
    pub table: String,
    /// Explicit `NAME = "..."`.
    pub explicit_name: Option<String>,
    /// Normalized `AS` clause (`PERMISSIVE` / `RESTRICTIVE`).
    pub as_clause: Option<String>,
    /// Normalized `FOR` clause (`ALL` / `SELECT` / ...).
    pub for_clause: Option<String>,
    /// `TO(...)` roles in declaration order.
    pub to: Vec<String>,
    /// `USING = "..."` expression.
    pub using: Option<String>,
    /// `WITH_CHECK = "..."` expression.
    pub with_check: Option<String>,
    /// Source position, see [`ParsedTable::order`].
    pub(crate) order: usize,
}

/// Result of parsing schema code
#[derive(Debug, Clone, Default)]
pub struct ParseResult {
    /// Parsed table structs, keyed `"<dialect>:<StructName>"`
    pub tables: HashMap<String, ParsedTable>,
    /// Parsed index structs, keyed `"<dialect>:<StructName>"`
    pub indexes: HashMap<String, ParsedIndex>,
    /// Parsed schema struct (if present)
    pub schema: Option<ParsedSchema>,
    /// Detected dialect (based on first found table/schema)
    pub dialect: Dialect,
    /// Parsed enums, keyed `"<dialect>:<EnumName>"`
    pub enums: HashMap<String, ParsedEnum>,
    /// Parsed views, keyed `"<dialect>:<StructName>"`
    pub views: HashMap<String, ParsedView>,
    /// Parsed policies, keyed `"<dialect>:<StructName>"`
    pub policies: HashMap<String, ParsedPolicy>,
    /// Non-fatal notes: constructs the parser recognized but cannot fully
    /// represent (opaque view definitions, cfg-divergent duplicates,
    /// custom column types, unsupported default expressions, ...).
    pub warnings: Vec<String>,
    /// Hard failures: source that failed to parse or attributes the macros
    /// would reject. Entities are still emitted best-effort, but callers
    /// should surface these to the user.
    pub errors: Vec<String>,
}

// =============================================================================
// Implementations
// =============================================================================

impl ParsedTable {
    /// Get a field by name
    #[must_use]
    pub fn field(&self, name: &str) -> Option<&ParsedField> {
        self.fields.iter().find(|f| f.name == name)
    }

    /// Textual check whether the table attribute source contains `attr`.
    ///
    /// This is a loose substring check kept for compatibility; semantic
    /// queries should use [`ParsedTable::is_strict`] and friends, which read
    /// the structured spec.
    #[must_use]
    pub fn has_table_attr(&self, attr: &str) -> bool {
        self.attr.contains(attr)
    }

    /// Get the raw value of a table attribute assignment (e.g. `schema = "auth"`).
    #[must_use]
    pub fn attr_value(&self, key: &str) -> Option<String> {
        self.spec
            .named_values
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(key))
            .map(|(_, v)| v.clone())
    }

    /// Get the `PostgreSQL` schema name if set on `#[PostgresTable(...)]`.
    #[must_use]
    pub fn schema_name(&self) -> Option<String> {
        self.spec.schema.clone()
    }

    /// Check if table is marked as `SQLite` STRICT.
    #[must_use]
    pub const fn is_strict(&self) -> bool {
        self.spec.strict
    }

    /// Check if table is marked as `SQLite` WITHOUT ROWID.
    #[must_use]
    pub const fn is_without_rowid(&self) -> bool {
        self.spec.without_rowid
    }

    /// Get all field names
    #[must_use]
    pub fn field_names(&self) -> Vec<&str> {
        self.fields.iter().map(|f| f.name.as_str()).collect()
    }
}

impl ParsedIndex {
    /// Check if index is unique
    #[must_use]
    pub const fn is_unique(&self) -> bool {
        self.spec.unique
    }

    /// Check if `PostgreSQL` index is created concurrently.
    #[must_use]
    pub const fn is_concurrent(&self) -> bool {
        self.spec.concurrent
    }

    /// Get `PostgreSQL` index method (e.g. `btree`, `gin`) if explicitly set.
    #[must_use]
    pub fn method(&self) -> Option<String> {
        self.spec.method.clone()
    }

    /// Get the partial-index `WHERE` clause if explicitly set.
    #[must_use]
    pub fn where_clause(&self) -> Option<String> {
        self.spec.where_clause.clone()
    }

    /// Get the table name from the first column reference
    #[must_use]
    pub fn table_name(&self) -> Option<&str> {
        self.spec
            .column_refs
            .first()
            .map(|(table, _)| table.as_str())
    }
}

impl ParsedField {
    /// Textual check whether any attribute source contains `attr`.
    ///
    /// Loose substring check kept for compatibility (round-trip tests match
    /// exact generated attribute text with it). Semantic queries should use
    /// [`ParsedField::is_primary_key`] and friends.
    #[must_use]
    pub fn has_attr(&self, attr: &str) -> bool {
        self.attrs.iter().any(|a| a.contains(attr))
    }

    /// Get the raw value of an attribute assignment (e.g., `default = 42` -> Some("42"))
    #[must_use]
    pub fn attr_value(&self, key: &str) -> Option<String> {
        self.spec
            .named_values
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(key))
            .map(|(_, v)| v.clone())
    }

    /// Get all raw attribute key-value pairs as a `HashMap`
    #[must_use]
    pub fn attr_values(&self) -> HashMap<String, String> {
        self.spec.named_values.iter().cloned().collect()
    }

    /// Get the combined column attribute string
    #[must_use]
    pub fn column_attr(&self) -> String {
        self.attrs.join(", ")
    }

    /// Check if field is nullable (`Option<T>`, matched by last path segment)
    #[must_use]
    pub const fn is_nullable(&self) -> bool {
        self.spec.nullable
    }

    /// Check if field is a primary key
    #[must_use]
    pub const fn is_primary_key(&self) -> bool {
        self.spec.primary
    }

    /// Check if field has autoincrement
    #[must_use]
    pub const fn is_autoincrement(&self) -> bool {
        self.spec.autoincrement
    }

    /// Check if field is unique
    #[must_use]
    pub const fn is_unique(&self) -> bool {
        self.spec.unique
    }

    /// Get the default value if present, as the raw source text of the
    /// literal (string defaults keep their Rust quotes here; SQL quoting
    /// happens in the snapshot builder).
    #[must_use]
    pub fn default_value(&self) -> Option<String> {
        self.attr_value("default")
            .or_else(|| match self.spec.default.as_ref()? {
                ParsedDefault::Int(s) | ParsedDefault::Float(s) => Some(s.clone()),
                ParsedDefault::Bool(b) => Some(b.to_string()),
                ParsedDefault::Str(s) => Some(format!("{s:?}")),
                ParsedDefault::Unsupported(raw) => Some(raw.clone()),
            })
    }

    /// Get the references target if present (e.g., "`Users::id`")
    #[must_use]
    pub fn references(&self) -> Option<String> {
        self.spec
            .references
            .as_ref()
            .map(|r| format!("{}::{}", r.table, r.column))
    }

    /// Get the `on_delete` action exactly as written (e.g. `cascade`).
    ///
    /// The normalized SQL action (`CASCADE`, `SET NULL`, ...) lives in
    /// `spec.on_delete`.
    #[must_use]
    pub fn on_delete(&self) -> Option<String> {
        self.spec.on_delete_raw.clone()
    }

    /// Get the `on_update` action exactly as written.
    #[must_use]
    pub fn on_update(&self) -> Option<String> {
        self.spec.on_update_raw.clone()
    }
}

impl ParseResult {
    /// Get a table by name and dialect
    #[must_use]
    pub fn table(&self, name: &str, dialect: Dialect) -> Option<&ParsedTable> {
        self.tables.get(&entity_key(dialect, name))
    }

    /// Get an index by name and dialect
    #[must_use]
    pub fn index(&self, name: &str, dialect: Dialect) -> Option<&ParsedIndex> {
        self.indexes.get(&entity_key(dialect, name))
    }

    /// Get an enum by name and dialect
    #[must_use]
    pub fn parsed_enum(&self, name: &str, dialect: Dialect) -> Option<&ParsedEnum> {
        self.enums.get(&entity_key(dialect, name))
    }

    /// Get a view by name and dialect
    #[must_use]
    pub fn view(&self, name: &str, dialect: Dialect) -> Option<&ParsedView> {
        self.views.get(&entity_key(dialect, name))
    }

    /// Get a policy by name and dialect
    #[must_use]
    pub fn policy(&self, name: &str, dialect: Dialect) -> Option<&ParsedPolicy> {
        self.policies.get(&entity_key(dialect, name))
    }

    /// Get all tables for a specific dialect
    pub fn tables_for_dialect(&self, dialect: Dialect) -> impl Iterator<Item = &ParsedTable> {
        let prefix = format!("{}:", dialect_key(dialect));
        self.tables
            .iter()
            .filter(move |(k, _)| k.starts_with(&prefix))
            .map(|(_, v)| v)
    }

    /// Get all indexes for a specific dialect
    pub fn indexes_for_dialect(&self, dialect: Dialect) -> impl Iterator<Item = &ParsedIndex> {
        let prefix = format!("{}:", dialect_key(dialect));
        self.indexes
            .iter()
            .filter(move |(k, _)| k.starts_with(&prefix))
            .map(|(_, v)| v)
    }

    /// Get all table names (without dialect prefix)
    #[must_use]
    pub fn table_names(&self) -> Vec<&str> {
        self.tables
            .keys()
            .filter_map(|s| s.split(':').nth(1))
            .collect()
    }

    /// Get all index names (without dialect prefix)
    #[must_use]
    pub fn index_names(&self) -> Vec<&str> {
        self.indexes
            .keys()
            .filter_map(|s| s.split(':').nth(1))
            .collect()
    }
}

/// Get the key prefix for a dialect
pub(crate) const fn dialect_key(dialect: Dialect) -> &'static str {
    match dialect {
        Dialect::SQLite => "sqlite",
        Dialect::PostgreSQL => "postgres",
        Dialect::MySQL => "mysql",
    }
}

/// Build a `"<dialect>:<name>"` map key.
pub(crate) fn entity_key(dialect: Dialect, name: &str) -> String {
    format!("{}:{}", dialect_key(dialect), name)
}