gaman-core 0.3.22

Offline core engine for Gaman migrations
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
use super::{
    Column, ColumnRef, Constraint, EnumDef, ExtensionDef, ForeignKey, FunctionDef, Index,
    OpaqueMeta, PrimaryKey, Schema, SchemaLoadError, Table, TableOptionsMeta, TriggerDef, ViewDef,
    names, schema_qualified_key,
};
use crate::column_type::ColumnType;
use crate::dialects::Dialect;

/// Map a Rust type to a table definition.
pub trait IntoTable {
    fn into_table(dialect: &Dialect) -> Table;
}

pub struct ColumnBuilder {
    col: Column,
}

impl ColumnBuilder {
    fn with_reference(
        mut self,
        name: Option<String>,
        table: impl Into<String>,
        column: impl Into<String>,
    ) -> Self {
        self.col.references = Some(ColumnRef {
            table: table.into(),
            column: column.into(),
            name,
            on_delete: None,
            on_update: None,
        });
        self
    }

    pub fn nullable(mut self) -> Self {
        self.col.nullable = true;
        self
    }

    pub fn not_null(mut self) -> Self {
        self.col.nullable = false;
        self
    }

    pub fn primary_key(mut self) -> Self {
        self.col.primary_key = true;
        self.col.nullable = false;
        self
    }

    pub fn default(mut self, expr: impl Into<String>) -> Self {
        self.col.default = Some(expr.into());
        self
    }

    /// Set a generated column expression.
    pub fn generated(mut self, expr: impl Into<String>) -> Self {
        let expr = expr.into();
        if !expr.trim().is_empty() {
            self.col.generated = Some(expr);
        }
        self
    }

    pub fn references(self, table: impl Into<String>, column: impl Into<String>) -> Self {
        self.with_reference(None, table, column)
    }

    pub fn references_named(
        self,
        name: impl Into<String>,
        table: impl Into<String>,
        column: impl Into<String>,
    ) -> Self {
        self.with_reference(Some(name.into()), table, column)
    }

    /// Set the inline foreign-key `ON DELETE` action.
    pub fn on_delete(mut self, action: impl Into<String>) -> Self {
        if let Some(reference) = &mut self.col.references {
            let action = action.into();
            if !action.trim().is_empty() {
                reference.on_delete = Some(action);
            }
        }
        self
    }

    /// Set the inline foreign-key `ON UPDATE` action.
    pub fn on_update(mut self, action: impl Into<String>) -> Self {
        if let Some(reference) = &mut self.col.references {
            let action = action.into();
            if !action.trim().is_empty() {
                reference.on_update = Some(action);
            }
        }
        self
    }

    pub fn check(mut self, expr: impl Into<String>) -> Self {
        self.col.check = Some(expr.into());
        self
    }

    fn finish(self) -> Column {
        self.col
    }
}

pub struct TableBuilder {
    table: Table,
}

impl TableBuilder {
    fn push_foreign_key(
        mut self,
        name: String,
        from_columns: impl IntoIterator<Item = impl Into<String>>,
        to_table: impl Into<String>,
        to_columns: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.table
            .foreign_keys
            .push(ForeignKey::new(name, from_columns, to_table, to_columns));
        self
    }

    fn push_index(mut self, name: impl Into<String>, columns: &[&str], unique: bool) -> Self {
        self.table.indexes.push(Index {
            name: name.into(),
            columns: columns.iter().map(|s| s.to_string()).collect(),
            unique,
            predicate: None,
            opaque: OpaqueMeta::default(),
        });
        self
    }

    fn push_index_with(
        mut self,
        name: impl Into<String>,
        columns: &[&str],
        unique: bool,
        f: impl FnOnce(Index) -> Index,
    ) -> Self {
        let index = Index {
            name: name.into(),
            columns: columns.iter().map(|s| s.to_string()).collect(),
            unique,
            predicate: None,
            opaque: OpaqueMeta::default(),
        };
        self.table.indexes.push(f(index));
        self
    }

    pub fn new(name: impl Into<String>) -> Self {
        let name = name.into();
        Self {
            table: Table {
                name,
                schema: None,
                primary_key: None,
                columns: vec![],
                foreign_keys: vec![],
                indexes: vec![],
                constraints: vec![],
                triggers: vec![],
                options: TableOptionsMeta::default(),
            },
        }
    }

    pub fn schema(mut self, schema: impl Into<String>) -> Self {
        self.table.schema = Some(schema.into());
        self
    }

    pub fn column(
        mut self,
        name: impl Into<String>,
        col_type: impl Into<String>,
        f: impl FnOnce(ColumnBuilder) -> ColumnBuilder,
    ) -> Self {
        let b = ColumnBuilder {
            col: Column {
                name: name.into(),
                col_type: col_type.into(),
                ..Default::default()
            },
        };
        self.table.columns.push(f(b).finish());
        self
    }

    /// Add a column whose SQL type and default nullability are inferred from a Rust type.
    ///
    /// The caller supplies the dialect because type mapping is dialect-specific.
    /// The closure runs after inference and can override nullability or add
    /// primary-key, default, reference, and check metadata.
    pub fn column_from_type<T: ColumnType>(
        mut self,
        dialect: &Dialect,
        name: impl Into<String>,
        f: impl FnOnce(ColumnBuilder) -> ColumnBuilder,
    ) -> Self {
        let desc = T::column_desc(dialect);
        let b = ColumnBuilder {
            col: Column {
                name: name.into(),
                col_type: desc.sql_type.to_string(),
                nullable: desc.nullable,
                ..Default::default()
            },
        };
        self.table.columns.push(f(b).finish());
        self
    }

    /// Shorthand: adds a `bigserial` primary key column named `id`.
    pub fn id(self) -> Self {
        self.column("id", "bigserial", |c| c.primary_key())
    }

    pub fn foreign_key(
        self,
        from: impl Into<String>,
        to_table: impl Into<String>,
        to_column: impl Into<String>,
    ) -> Self {
        let from = from.into();
        let name = names::foreign_key(&self.table.name, &[from.as_str()]);
        self.push_foreign_key(name, [from], to_table, [to_column.into()])
    }

    /// Add a foreign key and let the caller set advanced metadata.
    pub fn foreign_key_with(
        self,
        from: impl Into<String>,
        to_table: impl Into<String>,
        to_column: impl Into<String>,
        f: impl FnOnce(ForeignKey) -> ForeignKey,
    ) -> Self {
        let from = from.into();
        let name = names::foreign_key(&self.table.name, &[from.as_str()]);
        let foreign_key = ForeignKey::single(name, from, to_table, to_column);
        self.foreign_key_obj(f(foreign_key))
    }

    pub fn foreign_key_named(
        self,
        fk_name: impl Into<String>,
        from: impl Into<String>,
        to_table: impl Into<String>,
        to_column: impl Into<String>,
    ) -> Self {
        self.push_foreign_key(fk_name.into(), [from.into()], to_table, [to_column.into()])
    }

    /// Add a named foreign key and let the caller set advanced metadata.
    pub fn foreign_key_named_with(
        self,
        fk_name: impl Into<String>,
        from: impl Into<String>,
        to_table: impl Into<String>,
        to_column: impl Into<String>,
        f: impl FnOnce(ForeignKey) -> ForeignKey,
    ) -> Self {
        let foreign_key = ForeignKey::single(fk_name, from, to_table, to_column);
        self.foreign_key_obj(f(foreign_key))
    }

    pub fn foreign_key_columns(
        self,
        from_columns: &[&str],
        to_table: impl Into<String>,
        to_columns: &[&str],
    ) -> Self {
        let name = names::foreign_key(&self.table.name, from_columns);
        self.push_foreign_key(
            name,
            from_columns.iter().copied(),
            to_table,
            to_columns.iter().copied(),
        )
    }

    /// Add a composite foreign key and let the caller set advanced metadata.
    pub fn foreign_key_columns_with(
        self,
        from_columns: &[&str],
        to_table: impl Into<String>,
        to_columns: &[&str],
        f: impl FnOnce(ForeignKey) -> ForeignKey,
    ) -> Self {
        let name = names::foreign_key(&self.table.name, from_columns);
        let foreign_key = ForeignKey::new(
            name,
            from_columns.iter().copied(),
            to_table,
            to_columns.iter().copied(),
        );
        self.foreign_key_obj(f(foreign_key))
    }

    pub fn foreign_key_named_columns(
        self,
        fk_name: impl Into<String>,
        from_columns: &[&str],
        to_table: impl Into<String>,
        to_columns: &[&str],
    ) -> Self {
        self.push_foreign_key(
            fk_name.into(),
            from_columns.iter().copied(),
            to_table,
            to_columns.iter().copied(),
        )
    }

    /// Add a named composite foreign key and let the caller set advanced metadata.
    pub fn foreign_key_named_columns_with(
        self,
        fk_name: impl Into<String>,
        from_columns: &[&str],
        to_table: impl Into<String>,
        to_columns: &[&str],
        f: impl FnOnce(ForeignKey) -> ForeignKey,
    ) -> Self {
        let foreign_key = ForeignKey::new(
            fk_name,
            from_columns.iter().copied(),
            to_table,
            to_columns.iter().copied(),
        );
        self.foreign_key_obj(f(foreign_key))
    }

    /// Add a fully constructed foreign key.
    pub fn foreign_key_obj(mut self, foreign_key: ForeignKey) -> Self {
        self.table.foreign_keys.push(foreign_key);
        self
    }

    pub fn index_columns(self, columns: &[&str]) -> Self {
        let name = names::index(&self.table.name, columns);
        self.push_index(name, columns, false)
    }

    /// Add an index with generated name and advanced metadata.
    pub fn index_columns_with(self, columns: &[&str], f: impl FnOnce(Index) -> Index) -> Self {
        let name = names::index(&self.table.name, columns);
        self.push_index_with(name, columns, false, f)
    }

    pub fn unique_index_columns(self, columns: &[&str]) -> Self {
        let name = names::index(&self.table.name, columns);
        self.push_index(name, columns, true)
    }

    /// Add a unique index with generated name and advanced metadata.
    pub fn unique_index_columns_with(
        self,
        columns: &[&str],
        f: impl FnOnce(Index) -> Index,
    ) -> Self {
        let name = names::index(&self.table.name, columns);
        self.push_index_with(name, columns, true, f)
    }

    pub fn index(self, name: impl Into<String>, columns: &[&str]) -> Self {
        self.push_index(name, columns, false)
    }

    /// Add an index and let the caller set advanced metadata.
    pub fn index_with(
        self,
        name: impl Into<String>,
        columns: &[&str],
        f: impl FnOnce(Index) -> Index,
    ) -> Self {
        self.push_index_with(name, columns, false, f)
    }

    pub fn unique_index(self, name: impl Into<String>, columns: &[&str]) -> Self {
        self.push_index(name, columns, true)
    }

    /// Add a unique index and let the caller set advanced metadata.
    pub fn unique_index_with(
        self,
        name: impl Into<String>,
        columns: &[&str],
        f: impl FnOnce(Index) -> Index,
    ) -> Self {
        self.push_index_with(name, columns, true, f)
    }

    pub fn check(mut self, name: impl Into<String>, expression: impl Into<String>) -> Self {
        self.table.constraints.push(Constraint::Check {
            name: name.into(),
            expression: expression.into(),
        });
        self
    }

    pub fn check_expr(self, expression: impl Into<String>) -> Self {
        let name = names::table_check(&self.table.name);
        self.check(name, expression)
    }

    pub fn unique(mut self, name: impl Into<String>, columns: &[&str]) -> Self {
        self.table.constraints.push(Constraint::Unique {
            name: name.into(),
            columns: columns.iter().map(|s| s.to_string()).collect(),
        });
        self
    }

    pub fn unique_columns(self, columns: &[&str]) -> Self {
        let name = names::unique(&self.table.name, columns);
        self.unique(name, columns)
    }

    pub fn primary_key(mut self, name: impl Into<String>, columns: &[&str]) -> Self {
        self.table.primary_key = Some(PrimaryKey {
            name: name.into(),
            columns: columns.iter().map(|s| s.to_string()).collect(),
        });
        self
    }

    pub fn primary_key_columns(self, columns: &[&str]) -> Self {
        let name = names::primary_key(&self.table.name);
        self.primary_key(name, columns)
    }

    pub fn trigger(self, trigger: TriggerDef) -> Self {
        let mut this = self;
        this.table.triggers.push(trigger);
        this
    }

    pub fn build(mut self) -> Table {
        if self.table.primary_key.is_none() {
            let columns: Vec<String> = self
                .table
                .columns
                .iter()
                .filter(|column| column.primary_key)
                .map(|column| column.name.clone())
                .collect();
            if !columns.is_empty() {
                self.table.primary_key = Some(PrimaryKey {
                    name: names::primary_key(&self.table.name),
                    columns,
                });
            }
        }
        if let Some(pk) = &self.table.primary_key {
            for column in &mut self.table.columns {
                column.primary_key = pk.columns.iter().any(|name| name == &column.name);
                if column.primary_key {
                    column.nullable = false;
                }
            }
        }
        self.table
    }
}

pub struct SchemaBuilder {
    dialect: Dialect,
    state: Schema,
}

impl SchemaBuilder {
    fn insert_extension(mut self, name: impl Into<String>, version: Option<String>) -> Self {
        let name = name.into();
        self.state.extensions.insert(
            name.clone(),
            ExtensionDef {
                name,
                schema: None,
                version,
                opaque: OpaqueMeta::default(),
            },
        );
        self
    }

    pub fn new(dialect: Dialect) -> Self {
        Self {
            dialect,
            state: Schema::default(),
        }
    }

    /// Add a table from any type that implements [`IntoTable`].
    pub fn table<T: IntoTable>(mut self) -> Self {
        let t = T::into_table(&self.dialect);
        let key = schema_qualified_key(&t.name, t.schema.as_deref());
        self.state.tables.insert(key, t);
        self
    }

    /// Add a fully constructed modeled table definition.
    pub fn table_def(mut self, table: Table) -> Self {
        let key = schema_qualified_key(&table.name, table.schema.as_deref());
        self.state.tables.insert(key, table);
        self
    }

    pub fn extension(self, name: impl Into<String>) -> Self {
        self.insert_extension(name, None)
    }

    pub fn extension_versioned(self, name: impl Into<String>, version: impl Into<String>) -> Self {
        self.insert_extension(name, Some(version.into()))
    }

    /// Add a fully constructed extension definition.
    pub fn extension_def(mut self, extension: ExtensionDef) -> Self {
        let key = schema_qualified_key(&extension.name, extension.schema.as_deref());
        self.state.extensions.insert(key, extension);
        self
    }

    pub fn view(mut self, name: impl Into<String>, definition: impl Into<String>) -> Self {
        let name = name.into();
        self.state.views.insert(
            name.clone(),
            ViewDef {
                name,
                schema: None,
                definition: definition.into(),
                opaque: OpaqueMeta::default(),
            },
        );
        self
    }

    /// Add a fully constructed view definition.
    pub fn view_def(mut self, view: ViewDef) -> Self {
        let key = schema_qualified_key(&view.name, view.schema.as_deref());
        self.state.views.insert(key, view);
        self
    }

    pub fn function(mut self, f: FunctionDef) -> Self {
        let key = schema_qualified_key(&f.name, f.schema.as_deref());
        self.state.functions.insert(key, f);
        self
    }

    pub fn enum_type(mut self, name: impl Into<String>, values: &[&str]) -> Self {
        let name = name.into();
        self.state.enums.insert(
            name.clone(),
            EnumDef {
                name,
                schema: None,
                values: values.iter().map(|s| s.to_string()).collect(),
                opaque: OpaqueMeta::default(),
            },
        );
        self
    }

    /// Add a fully constructed enum definition.
    pub fn enum_def(mut self, enum_def: EnumDef) -> Self {
        let key = schema_qualified_key(&enum_def.name, enum_def.schema.as_deref());
        self.state.enums.insert(key, enum_def);
        self
    }

    /// Build a schema through the same normalize and prepare lifecycle as file
    /// and SQL ingestion.
    pub fn build(self) -> Result<Schema, SchemaLoadError> {
        let dialect = self.dialect;
        self.build_raw().prepare_loaded(dialect)
    }

    /// Build a raw normalized schema without dialect preparation.
    ///
    /// This is intended for internal tests and low-level tooling that need to
    /// inspect pre-validation model state. User-facing builder paths should use
    /// [`SchemaBuilder::build`].
    pub(crate) fn build_raw(self) -> Schema {
        self.state
    }
}