tork-orm-core 0.1.0

Core runtime for the Tork ORM: dialect-agnostic query model, typed columns, and database drivers.
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! The schema manager and its fluent builders.
//!
//! A [`SchemaManager`] is handed to a migration's `up`/`down`. Its builders assemble
//! DDL and either run it against the database (execute mode) or buffer the rendered
//! SQL without touching the database (collect mode). Collect mode is what lets a
//! migration's DDL be previewed and hashed into a stable checksum.

use crate::dialect::{Dialect, DialectKind, SqlType};
use crate::driver::ExecuteResult;
use crate::executor::Executor;
use crate::index::{IndexColumn, IndexDef};
use crate::query::expr::Expr;
use crate::row::Row;
use crate::value::Value;

use super::ddl::{ColumnSpec, DefaultValue, ForeignKeyAction, ForeignKeySpec, TableDef};
use super::{render, BoxFuture};

/// An object-safe view of an [`Executor`], so [`SchemaManager`] need not be generic
/// over the executor type. Mirrors the pattern used for preloading.
pub trait DynExecutor: Sync {
    /// Returns the dialect to render for.
    fn dialect(&self) -> &dyn Dialect;
    /// Runs a statement that returns no rows.
    fn execute<'a>(
        &'a self,
        sql: String,
        params: Vec<Value>,
    ) -> BoxFuture<'a, crate::Result<ExecuteResult>>;
    /// Runs a row-returning query.
    fn fetch_all<'a>(
        &'a self,
        sql: String,
        params: Vec<Value>,
    ) -> BoxFuture<'a, crate::Result<Vec<Row>>>;
}

impl<E: Executor + Sync> DynExecutor for E {
    fn dialect(&self) -> &dyn Dialect {
        Executor::dialect(self)
    }

    fn execute<'a>(
        &'a self,
        sql: String,
        params: Vec<Value>,
    ) -> BoxFuture<'a, crate::Result<ExecuteResult>> {
        Box::pin(Executor::execute(self, sql, params))
    }

    fn fetch_all<'a>(
        &'a self,
        sql: String,
        params: Vec<Value>,
    ) -> BoxFuture<'a, crate::Result<Vec<Row>>> {
        Box::pin(Executor::fetch_all(self, sql, params))
    }
}

/// Where a [`SchemaManager`]'s rendered DDL goes.
enum Mode<'e> {
    /// Run each statement against the database.
    Execute(&'e dyn DynExecutor),
    /// Buffer rendered statements without running them.
    Collect(Vec<String>),
}

/// Builds and applies schema changes for one migration.
///
/// # Examples
///
/// ```
/// use tork_orm_core::dialect::SqliteDialect;
/// use tork_orm_core::migration::{Column, SchemaManager};
///
/// # async fn run() -> tork_orm_core::Result<()> {
/// let dialect = SqliteDialect::new();
/// let mut schema = SchemaManager::collect(&dialect);
/// schema
///     .create_table("users")
///     .column(Column::new("id").bigint().primary_key().auto_increment())
///     .execute()
///     .await?;
/// let sql = schema.into_collected();
/// assert_eq!(sql[0], "CREATE TABLE \"users\" (\"id\" INTEGER PRIMARY KEY AUTOINCREMENT)");
/// # Ok(())
/// # }
/// ```
pub struct SchemaManager<'e> {
    mode: Mode<'e>,
    dialect: &'e dyn Dialect,
}

impl<'e> SchemaManager<'e> {
    /// Creates a manager that runs DDL against `executor`.
    pub(crate) fn executing(executor: &'e dyn DynExecutor) -> Self {
        Self {
            dialect: executor.dialect(),
            mode: Mode::Execute(executor),
        }
    }

    /// Creates a manager that buffers rendered SQL for `dialect` without running it.
    pub fn collect(dialect: &'e dyn Dialect) -> Self {
        Self {
            mode: Mode::Collect(Vec::new()),
            dialect,
        }
    }

    /// Consumes a collect-mode manager, returning the rendered statements.
    ///
    /// Returns an empty vector for an execute-mode manager.
    pub fn into_collected(self) -> Vec<String> {
        match self.mode {
            Mode::Collect(buffer) => buffer,
            Mode::Execute(_) => Vec::new(),
        }
    }

    /// Sends rendered statements to the database, or buffers them in collect mode.
    ///
    /// Takes `&mut self` (rather than `&self` with interior mutability) so the
    /// future this is awaited in only needs to be `Send`, never `Sync`.
    async fn dispatch(&mut self, statements: Vec<String>) -> crate::Result<()> {
        let executor = match &mut self.mode {
            Mode::Execute(executor) => *executor,
            Mode::Collect(buffer) => {
                buffer.extend(statements);
                return Ok(());
            }
        };
        for statement in statements {
            executor.execute(statement, Vec::new()).await?;
        }
        Ok(())
    }

    /// Begins a `CREATE TABLE`.
    pub fn create_table(&mut self, name: &str) -> CreateTable<'_, 'e> {
        CreateTable {
            schema: self,
            def: TableDef::new(name),
        }
    }

    /// Begins a `DROP TABLE`.
    pub fn drop_table(&mut self, name: &str) -> DropTable<'_, 'e> {
        DropTable {
            schema: self,
            name: name.to_string(),
            if_exists: false,
        }
    }

    /// Begins a `CREATE INDEX` named `name`.
    ///
    /// Set the table with [`CreateIndex::on_table`] and the columns with
    /// [`CreateIndex::column`] / [`CreateIndex::columns`] before calling
    /// [`CreateIndex::execute`].
    pub fn create_index(&mut self, name: &str) -> CreateIndex<'_, 'e> {
        CreateIndex {
            schema: self,
            table: String::new(),
            def: IndexDef::new(name),
            if_not_exists: false,
        }
    }

    /// Begins a `DROP INDEX` for the index named `name`.
    pub fn drop_index(&mut self, name: &str) -> DropIndex<'_, 'e> {
        DropIndex {
            schema: self,
            name: name.to_string(),
            if_exists: false,
        }
    }

    /// Begins a `CREATE TRIGGER` named `name`.
    ///
    /// A thin convenience that renders the trigger header (`CREATE TRIGGER <name>
    /// <timing> <event> ON <table> [FOR EACH ROW]`) followed by a raw action body.
    /// Trigger bodies are dialect-specific (PostgreSQL `EXECUTE FUNCTION f()`,
    /// SQLite `BEGIN ... END`), so the body is the caller's responsibility — as is
    /// any function the trigger calls, created via [`raw`](Self::raw).
    pub fn create_trigger(&mut self, name: &str) -> CreateTrigger<'_, 'e> {
        CreateTrigger {
            schema: self,
            name: name.to_string(),
            timing: TriggerTiming::Before,
            event: TriggerEvent::Insert,
            table: String::new(),
            for_each_row: false,
            body: String::new(),
        }
    }

    /// Begins a `DROP TRIGGER` for the trigger named `name`.
    pub fn drop_trigger(&mut self, name: &str) -> DropTrigger<'_, 'e> {
        DropTrigger {
            schema: self,
            name: name.to_string(),
            table: None,
            if_exists: false,
        }
    }

    /// Runs verbatim SQL. The caller owns its correctness and escaping.
    pub async fn raw(&mut self, sql: &str) -> crate::Result<()> {
        self.dispatch(vec![sql.to_string()]).await
    }

    /// Runs verbatim SQL only when the target dialect matches `kind`.
    ///
    /// On a non-matching dialect it contributes nothing, in both execute and
    /// collect modes, so checksums stay stable across the dialects a migration
    /// does not target.
    pub async fn raw_for(&mut self, kind: DialectKind, sql: &str) -> crate::Result<()> {
        if self.dialect.kind() == kind {
            self.dispatch(vec![sql.to_string()]).await
        } else {
            Ok(())
        }
    }
}

/// A `CREATE TABLE` builder.
pub struct CreateTable<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    def: TableDef,
}

impl CreateTable<'_, '_> {
    /// Adds `IF NOT EXISTS`.
    pub fn if_not_exists(mut self) -> Self {
        self.def.if_not_exists = true;
        self
    }

    /// Adds a column.
    pub fn column(mut self, column: Column) -> Self {
        self.def.columns.push(column.into_spec());
        self
    }

    /// Declares a composite primary key over the named columns.
    pub fn primary_key(mut self, columns: &[&str]) -> Self {
        self.def.primary_key = columns.iter().map(|c| c.to_string()).collect();
        self
    }

    /// Adds a foreign key constraint.
    pub fn foreign_key(mut self, foreign_key: ForeignKey) -> Self {
        self.def.foreign_keys.push(foreign_key.into_spec());
        self
    }

    /// Adds a table-level `CHECK (...)` constraint. The expression is rendered
    /// verbatim, so write it in SQL: `.check("price_cents >= 0")`.
    pub fn check(mut self, expression: impl Into<String>) -> Self {
        self.def.checks.push(expression.into());
        self
    }

    /// Adds `created_at` and `updated_at` timestamp columns defaulting to the
    /// current time.
    pub fn timestamps(mut self) -> Self {
        self.def.columns.push(timestamp_column("created_at"));
        self.def.columns.push(timestamp_column("updated_at"));
        self
    }

    /// Renders and applies the statement.
    pub async fn execute(self) -> crate::Result<()> {
        let statements = render::create_table(self.schema.dialect, &self.def)?;
        self.schema.dispatch(statements).await
    }
}

/// A `DROP TABLE` builder.
pub struct DropTable<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    name: String,
    if_exists: bool,
}

impl DropTable<'_, '_> {
    /// Adds `IF EXISTS`.
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }

    /// Renders and applies the statement.
    pub async fn execute(self) -> crate::Result<()> {
        let statement = render::drop_table(self.schema.dialect, &self.name, self.if_exists);
        self.schema.dispatch(vec![statement]).await
    }
}

/// When a trigger fires relative to the row event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TriggerTiming {
    /// `BEFORE` the event.
    Before,
    /// `AFTER` the event.
    After,
}

impl TriggerTiming {
    fn as_sql(self) -> &'static str {
        match self {
            TriggerTiming::Before => "BEFORE",
            TriggerTiming::After => "AFTER",
        }
    }
}

/// The row event a trigger fires on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TriggerEvent {
    /// `INSERT`.
    Insert,
    /// `UPDATE`.
    Update,
    /// `DELETE`.
    Delete,
}

impl TriggerEvent {
    fn as_sql(self) -> &'static str {
        match self {
            TriggerEvent::Insert => "INSERT",
            TriggerEvent::Update => "UPDATE",
            TriggerEvent::Delete => "DELETE",
        }
    }
}

/// A `CREATE TRIGGER` builder.
///
/// # Examples
///
/// ```
/// use tork_orm_core::dialect::PostgresDialect;
/// use tork_orm_core::migration::{SchemaManager, TriggerEvent};
///
/// # async fn run() -> tork_orm_core::Result<()> {
/// let dialect = PostgresDialect::new();
/// let mut schema = SchemaManager::collect(&dialect);
/// schema
///     .create_trigger("set_updated_at")
///     .before()
///     .event(TriggerEvent::Update)
///     .on("users")
///     .for_each_row()
///     .body("EXECUTE FUNCTION touch_updated_at()")
///     .execute()
///     .await?;
/// assert_eq!(
///     schema.into_collected()[0],
///     "CREATE TRIGGER \"set_updated_at\" BEFORE UPDATE ON \"users\" \
///FOR EACH ROW EXECUTE FUNCTION touch_updated_at()"
/// );
/// # Ok(())
/// # }
/// ```
pub struct CreateTrigger<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    name: String,
    timing: TriggerTiming,
    event: TriggerEvent,
    table: String,
    for_each_row: bool,
    body: String,
}

impl CreateTrigger<'_, '_> {
    /// Sets the firing time.
    pub fn timing(mut self, timing: TriggerTiming) -> Self {
        self.timing = timing;
        self
    }

    /// Sugar for `BEFORE`.
    pub fn before(self) -> Self {
        self.timing(TriggerTiming::Before)
    }

    /// Sugar for `AFTER`.
    pub fn after(self) -> Self {
        self.timing(TriggerTiming::After)
    }

    /// Sets the row event.
    pub fn event(mut self, event: TriggerEvent) -> Self {
        self.event = event;
        self
    }

    /// Sets the table the trigger is attached to.
    pub fn on(mut self, table: &str) -> Self {
        self.table = table.to_string();
        self
    }

    /// Adds `FOR EACH ROW`.
    pub fn for_each_row(mut self) -> Self {
        self.for_each_row = true;
        self
    }

    /// Sets the raw, dialect-specific action body (e.g. PostgreSQL
    /// `EXECUTE FUNCTION f()` or SQLite `BEGIN ... END`).
    pub fn body(mut self, body: &str) -> Self {
        self.body = body.to_string();
        self
    }

    /// Renders and applies the `CREATE TRIGGER`.
    pub async fn execute(self) -> crate::Result<()> {
        let mut sql = String::from("CREATE TRIGGER ");
        self.schema.dialect.quote_identifier(&self.name, &mut sql);
        sql.push(' ');
        sql.push_str(self.timing.as_sql());
        sql.push(' ');
        sql.push_str(self.event.as_sql());
        sql.push_str(" ON ");
        self.schema.dialect.quote_identifier(&self.table, &mut sql);
        if self.for_each_row {
            sql.push_str(" FOR EACH ROW");
        }
        if !self.body.is_empty() {
            sql.push(' ');
            sql.push_str(&self.body);
        }
        self.schema.dispatch(vec![sql]).await
    }
}

/// A `DROP TRIGGER` builder.
pub struct DropTrigger<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    name: String,
    table: Option<String>,
    if_exists: bool,
}

impl DropTrigger<'_, '_> {
    /// Adds `IF EXISTS`.
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }

    /// Names the table the trigger is on (required by PostgreSQL: `DROP TRIGGER
    /// name ON table`; ignored by SQLite).
    pub fn on(mut self, table: &str) -> Self {
        self.table = Some(table.to_string());
        self
    }

    /// Renders and applies the `DROP TRIGGER`.
    pub async fn execute(self) -> crate::Result<()> {
        let mut sql = String::from("DROP TRIGGER ");
        if self.if_exists {
            sql.push_str("IF EXISTS ");
        }
        self.schema.dialect.quote_identifier(&self.name, &mut sql);
        // PostgreSQL requires the table; SQLite does not accept it.
        if self.schema.dialect.kind() == DialectKind::Postgres {
            if let Some(table) = &self.table {
                sql.push_str(" ON ");
                self.schema.dialect.quote_identifier(table, &mut sql);
            }
        }
        self.schema.dispatch(vec![sql]).await
    }
}

/// A `CREATE INDEX` builder.
///
/// # Examples
///
/// ```
/// use tork_orm_core::dialect::SqliteDialect;
/// use tork_orm_core::migration::{IndexColumn, SchemaManager};
///
/// # async fn run() -> tork_orm_core::Result<()> {
/// let dialect = SqliteDialect::new();
/// let mut schema = SchemaManager::collect(&dialect);
/// schema
///     .create_index("idx_posts_user_created")
///     .on_table("posts")
///     .unique()
///     .columns([IndexColumn::new("user_id"), IndexColumn::new("created_at").desc()])
///     .execute()
///     .await?;
/// let sql = schema.into_collected();
/// assert_eq!(
///     sql[0],
///     "CREATE UNIQUE INDEX \"idx_posts_user_created\" ON \"posts\" \
///      (\"user_id\", \"created_at\" DESC)"
/// );
/// # Ok(())
/// # }
/// ```
pub struct CreateIndex<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    table: String,
    def: IndexDef,
    if_not_exists: bool,
}

impl CreateIndex<'_, '_> {
    /// Sets the table the index is on.
    pub fn on_table(mut self, table: &str) -> Self {
        self.table = table.to_string();
        self
    }

    /// Marks the index `UNIQUE`.
    pub fn unique(mut self) -> Self {
        self.def.unique = true;
        self
    }

    /// Adds a single column.
    pub fn column(mut self, column: IndexColumn) -> Self {
        self.def.columns.push(column);
        self
    }

    /// Adds several columns at once.
    pub fn columns(mut self, columns: impl IntoIterator<Item = IndexColumn>) -> Self {
        self.def.columns.extend(columns);
        self
    }

    /// Sets the index method (`USING`); supported only on backends that have one.
    pub fn method(mut self, method: impl Into<String>) -> Self {
        self.def.method = Some(method.into());
        self
    }

    /// Adds covering columns (`INCLUDE`); supported only on backends that have them.
    pub fn include<I, S>(mut self, columns: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.def
            .include
            .extend(columns.into_iter().map(Into::into));
        self
    }

    /// Restricts the index to rows matching `predicate` (a partial index).
    pub fn where_(mut self, predicate: Expr) -> Self {
        self.def.predicate = Some(predicate);
        self
    }

    /// Adds `IF NOT EXISTS`.
    pub fn if_not_exists(mut self) -> Self {
        self.if_not_exists = true;
        self
    }

    /// Renders and applies the statement.
    pub async fn execute(self) -> crate::Result<()> {
        let statement =
            render::create_index(self.schema.dialect, &self.table, &self.def, self.if_not_exists)?;
        self.schema.dispatch(vec![statement]).await
    }
}

/// A `DROP INDEX` builder.
pub struct DropIndex<'a, 'e> {
    schema: &'a mut SchemaManager<'e>,
    name: String,
    if_exists: bool,
}

impl DropIndex<'_, '_> {
    /// Adds `IF EXISTS`.
    pub fn if_exists(mut self) -> Self {
        self.if_exists = true;
        self
    }

    /// Renders and applies the statement.
    pub async fn execute(self) -> crate::Result<()> {
        let statement = render::drop_index(self.schema.dialect, &self.name, self.if_exists);
        self.schema.dispatch(vec![statement]).await
    }
}

/// Builds a `created_at`/`updated_at` style timestamp column.
fn timestamp_column(name: &str) -> ColumnSpec {
    ColumnSpec {
        name: name.to_string(),
        ty: SqlType::Timestamp,
        nullable: false,
        primary_key: false,
        auto_increment: false,
        unique: false,
        default: Some(DefaultValue::CurrentTimestamp),
    }
}

/// A column definition for a migration.
///
/// Distinct from the query-side `Column<M, T>`; this one builds DDL. Migration
/// files bring it in with `use tork_orm::migration::*`.
pub struct Column {
    spec: ColumnSpec,
}

impl Column {
    /// Starts a nullable column named `name` (set a type before using it).
    pub fn new(name: impl Into<String>) -> Self {
        let mut spec = ColumnSpec::new(name, SqlType::Text);
        spec.nullable = true;
        Self { spec }
    }

    /// Sets the type to a 32-bit integer.
    pub fn integer(mut self) -> Self {
        self.spec.ty = SqlType::Integer;
        self
    }

    /// Sets the type to a 64-bit integer.
    pub fn bigint(mut self) -> Self {
        self.spec.ty = SqlType::BigInt;
        self
    }

    /// Sets the type to bounded text of at most `length`.
    pub fn varchar(mut self, length: u32) -> Self {
        debug_assert!(length > 0, "varchar length must be > 0");
        self.spec.ty = SqlType::Varchar(length);
        self
    }

    /// Sets the type to unbounded text.
    pub fn text(mut self) -> Self {
        self.spec.ty = SqlType::Text;
        self
    }

    /// Sets the type to a boolean.
    pub fn boolean(mut self) -> Self {
        self.spec.ty = SqlType::Boolean;
        self
    }

    /// Sets the type to a floating point number.
    pub fn real(mut self) -> Self {
        self.spec.ty = SqlType::Real;
        self
    }

    /// Sets the type to a timestamp.
    pub fn timestamp(mut self) -> Self {
        self.spec.ty = SqlType::Timestamp;
        self
    }

    /// Sets the type to a binary blob.
    pub fn blob(mut self) -> Self {
        self.spec.ty = SqlType::Blob;
        self
    }

    /// Sets the type to a JSON document (`JSONB` on PostgreSQL, `JSON` on MySQL,
    /// `TEXT` on SQLite).
    pub fn json(mut self) -> Self {
        self.spec.ty = SqlType::Json;
        self
    }

    /// Sets the type to a UUID (`UUID` on PostgreSQL, `CHAR(36)` on MySQL,
    /// `TEXT` on SQLite).
    pub fn uuid(mut self) -> Self {
        self.spec.ty = SqlType::Uuid;
        self
    }

    /// Sets the type to an enum constrained to `variants`.
    ///
    /// Rendered as a native `ENUM(...)` on MySQL and as a text column with a
    /// `CHECK (... IN (...))` constraint elsewhere. Both arguments are `'static`,
    /// so the usual call passes string literals:
    /// `Column::new("status").enum_type("status", &["active", "inactive"])`.
    pub fn enum_type(
        mut self,
        name: &'static str,
        variants: &'static [&'static str],
    ) -> Self {
        self.spec.ty = SqlType::Enum { name, variants };
        self
    }

    /// Marks the column `NOT NULL`.
    pub fn not_null(mut self) -> Self {
        self.spec.nullable = false;
        self
    }

    /// Marks the column nullable.
    pub fn nullable(mut self) -> Self {
        self.spec.nullable = true;
        self
    }

    /// Marks the column (part of) the primary key.
    pub fn primary_key(mut self) -> Self {
        self.spec.primary_key = true;
        self
    }

    /// Marks the column auto-incrementing (with `primary_key`).
    pub fn auto_increment(mut self) -> Self {
        self.spec.auto_increment = true;
        self
    }

    /// Adds a `UNIQUE` constraint.
    pub fn unique(mut self) -> Self {
        self.spec.unique = true;
        self
    }

    /// Sets a default value.
    pub fn default(mut self, value: impl Into<DefaultValue>) -> Self {
        self.spec.default = Some(value.into());
        self
    }

    /// Consumes the builder, returning the column spec.
    fn into_spec(self) -> ColumnSpec {
        self.spec
    }
}

/// A foreign key constraint builder.
///
/// # Examples
///
/// ```
/// use tork_orm_core::migration::{ForeignKey, ForeignKeyAction};
///
/// let fk = ForeignKey::new()
///     .from("posts", "user_id")
///     .to("users", "id")
///     .on_delete(ForeignKeyAction::Cascade);
/// # let _ = fk;
/// ```
pub struct ForeignKey {
    spec: ForeignKeySpec,
}

impl ForeignKey {
    /// Starts an empty foreign key.
    pub fn new() -> Self {
        Self {
            spec: ForeignKeySpec {
                columns: Vec::new(),
                ref_table: String::new(),
                ref_columns: Vec::new(),
                on_delete: ForeignKeyAction::NoAction,
                on_update: ForeignKeyAction::NoAction,
            },
        }
    }

    /// Adds a local column. The `table` is the table being created; it is accepted
    /// for readability and is otherwise implied.
    pub fn from(mut self, table: &str, column: &str) -> Self {
        let _ = table;
        self.spec.columns.push(column.to_string());
        self
    }

    /// Adds the referenced table and column.
    pub fn to(mut self, table: &str, column: &str) -> Self {
        self.spec.ref_table = table.to_string();
        self.spec.ref_columns.push(column.to_string());
        self
    }

    /// Sets the `ON DELETE` action.
    pub fn on_delete(mut self, action: ForeignKeyAction) -> Self {
        self.spec.on_delete = action;
        self
    }

    /// Sets the `ON UPDATE` action.
    pub fn on_update(mut self, action: ForeignKeyAction) -> Self {
        self.spec.on_update = action;
        self
    }

    /// Consumes the builder, returning the spec.
    fn into_spec(self) -> ForeignKeySpec {
        self.spec
    }
}

impl Default for ForeignKey {
    fn default() -> Self {
        Self::new()
    }
}