Skip to content

Commit 3d80f57

Browse files
committed
added missing docs
1 parent 2a67fd5 commit 3d80f57

1 file changed

Lines changed: 12 additions & 222 deletions

File tree

cot/src/db/migrations.rs

Lines changed: 12 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ mod sorter;
44

55
use std::fmt;
66
use std::fmt::{Debug, Formatter};
7-
use std::future::Future;
87

9-
pub use cot_macros::migration_op;
108
use sea_query::{ColumnDef, StringLen};
119
use thiserror::Error;
1210
use tracing::{Level, info};
@@ -22,13 +20,9 @@ pub enum MigrationEngineError {
2220
/// An error occurred while determining the correct order of migrations.
2321
#[error("error while determining the correct order of migrations")]
2422
MigrationSortError(#[from] MigrationSorterError),
25-
/// A custom error occurred during a migration.
26-
#[error("error running migration: {0}")]
27-
Custom(String),
2823
}
2924

30-
/// A migration engine responsible for managing and applying database
31-
/// migrations.
25+
/// A migration engine that can run migrations.
3226
///
3327
/// # Examples
3428
///
@@ -139,7 +133,7 @@ impl MigrationEngine {
139133
///
140134
/// # Errors
141135
///
142-
/// Returns an error if any of the migrations fail to apply, or if there is
136+
/// Throws an error if any of the migrations fail to apply, or if there is
143137
/// an error while interacting with the database, or if there is an
144138
/// error while marking a migration as applied.
145139
///
@@ -430,32 +424,11 @@ impl Operation {
430424
RemoveModelBuilder::new()
431425
}
432426

433-
/// Returns a builder for a custom operation.
434-
///
435-
/// # Examples
436-
///
437-
/// ```
438-
/// use cot::db::Result;
439-
/// use cot::db::migrations::{MigrationContext, Operation, migration_op};
440-
///
441-
/// #[migration_op]
442-
/// async fn forwards(ctx: MigrationContext<'_>) -> Result<()> {
443-
/// // do something
444-
/// Ok(())
445-
/// }
446-
///
447-
/// const OPERATION: Operation = Operation::custom(forwards).build();
448-
/// ```
449-
#[must_use]
450-
pub const fn custom(forwards: CustomOperationFn) -> CustomBuilder {
451-
CustomBuilder::new(forwards)
452-
}
453-
454427
/// Runs the operation forwards.
455428
///
456429
/// # Errors
457430
///
458-
/// Returns an error if the operation fails to apply.
431+
/// Throws an error if the operation fails to apply.
459432
///
460433
/// # Examples
461434
///
@@ -539,13 +512,6 @@ impl Operation {
539512
let query = sea_query::Table::drop().table(*table_name).to_owned();
540513
database.execute_schema(query).await?;
541514
}
542-
OperationInner::Custom {
543-
forwards,
544-
backwards: _,
545-
} => {
546-
let context = MigrationContext::new(database);
547-
forwards(context).await?;
548-
}
549515
}
550516
Ok(())
551517
}
@@ -555,7 +521,7 @@ impl Operation {
555521
///
556522
/// # Errors
557523
///
558-
/// Returns an error if the operation fails to apply.
524+
/// Throws an error if the operation fails to apply.
559525
///
560526
/// # Examples
561527
///
@@ -635,50 +601,11 @@ impl Operation {
635601
}
636602
database.execute_schema(query).await?;
637603
}
638-
OperationInner::Custom {
639-
forwards: _,
640-
backwards,
641-
} => {
642-
if let Some(backwards) = backwards {
643-
let context = MigrationContext::new(database);
644-
backwards(context).await?;
645-
} else {
646-
return Err(crate::db::DatabaseError::MigrationError(
647-
MigrationEngineError::Custom("Backwards migration not implemented".into()),
648-
));
649-
}
650-
}
651604
}
652605
Ok(())
653606
}
654607
}
655608

656-
/// A context for a custom migration operation.
657-
///
658-
/// This structure provides access to the database and other information that
659-
/// might be needed during a migration.
660-
#[derive(Debug)]
661-
#[non_exhaustive]
662-
pub struct MigrationContext<'a> {
663-
/// The database connection to run the migration against.
664-
pub db: &'a Database,
665-
}
666-
667-
impl<'a> MigrationContext<'a> {
668-
fn new(db: &'a Database) -> Self {
669-
Self { db }
670-
}
671-
}
672-
673-
/// A type alias for a custom migration operation function.
674-
///
675-
/// Typically, you should use the [`migration_op`] attribute macro to define
676-
/// functions of this type.
677-
pub type CustomOperationFn =
678-
for<'a> fn(
679-
MigrationContext<'a>,
680-
) -> std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
681-
682609
#[derive(Debug, Copy, Clone)]
683610
enum OperationInner {
684611
/// Create a new model with the given fields.
@@ -702,16 +629,10 @@ enum OperationInner {
702629
table_name: Identifier,
703630
fields: &'static [Field],
704631
},
705-
<<<<<<< HEAD
706-
Custom {
707-
forwards: CustomOperationFn,
708-
backwards: Option<CustomOperationFn>,
709-
=======
710632
AlterField {
711633
table_name: Identifier,
712634
old_field: Field,
713635
new_field: Field,
714-
>>>>>>> 09f0c87 (feat: Add `AlterField` Operation to Migration Generator)
715636
},
716637
}
717638

@@ -766,12 +687,6 @@ impl Field {
766687

767688
/// Marks the field as a foreign key to the given model and field.
768689
///
769-
/// # Panics
770-
///
771-
/// This function will panic if `on_delete` or `on_update` is set to
772-
/// [`SetNone`](ForeignKeyOnDeletePolicy::SetNone) and the field is not
773-
/// nullable.
774-
///
775690
/// # Cot CLI Usage
776691
///
777692
/// Typically, you shouldn't need to use this directly. Instead, in most
@@ -1643,62 +1558,13 @@ impl RemoveModelBuilder {
16431558
}
16441559
}
16451560

1646-
<<<<<<< HEAD
1647-
/// A builder for a custom operation.
1648-
///
1649-
/// # Examples
1650-
///
1651-
/// ```
1652-
/// use cot::db::Result;
1653-
/// use cot::db::migrations::{MigrationContext, Operation, migration_op};
1654-
///
1655-
/// #[migration_op]
1656-
/// async fn forwards(ctx: MigrationContext<'_>) -> Result<()> {
1657-
/// // do something
1658-
/// Ok(())
1659-
/// }
1660-
///
1661-
/// #[migration_op]
1662-
/// async fn backwards(ctx: MigrationContext<'_>) -> Result<()> {
1663-
/// // undo something
1664-
/// Ok(())
1665-
/// }
1666-
///
1667-
/// const OPERATION: Operation = Operation::custom(forwards).backwards(backwards).build();
1668-
/// ```
1669-
#[derive(Debug, Copy, Clone)]
1670-
pub struct CustomBuilder {
1671-
forwards: CustomOperationFn,
1672-
backwards: Option<CustomOperationFn>,
1673-
}
1674-
1675-
impl CustomBuilder {
1676-
#[must_use]
1677-
const fn new(forwards: CustomOperationFn) -> Self {
1678-
Self {
1679-
forwards,
1680-
backwards: None,
1681-
}
1682-
}
1683-
1684-
/// Sets the backwards operation.
1685-
#[must_use]
1686-
pub const fn backwards(mut self, backwards: CustomOperationFn) -> Self {
1687-
self.backwards = Some(backwards);
1688-
self
1689-
}
1690-
1691-
/// Builds the operation.
1692-
#[must_use]
1693-
pub const fn build(self) -> Operation {
1694-
Operation::new(OperationInner::Custom {
1695-
forwards: self.forwards,
1696-
backwards: self.backwards,
1697-
=======
1561+
/// Returns a builder for an operation that alters a field in a model.
16981562
pub const fn alter_field() -> AlterFieldBuilder {
16991563
AlterFieldBuilder::new()
17001564
}
17011565

1566+
/// A builder for altering a field in a model.
1567+
#[must_use]
17021568
#[derive(Debug, Copy, Clone)]
17031569
pub struct AlterFieldBuilder {
17041570
table_name: Option<Identifier>,
@@ -1715,27 +1581,31 @@ impl AlterFieldBuilder {
17151581
}
17161582
}
17171583

1584+
/// Sets the name of the table to alter the field in.
17181585
pub const fn table_name(mut self, table_name: Identifier) -> Self {
17191586
self.table_name = Some(table_name);
17201587
self
17211588
}
17221589

1590+
/// Sets the old field definition.
17231591
pub const fn old_field(mut self, field: Field) -> Self {
17241592
self.old_field = Some(field);
17251593
self
17261594
}
17271595

1596+
/// Sets the new field definition.
17281597
pub const fn new_field(mut self, field: Field) -> Self {
17291598
self.new_field = Some(field);
17301599
self
17311600
}
17321601

1602+
/// Builds the operation.
1603+
#[must_use]
17331604
pub const fn build(self) -> Operation {
17341605
Operation::new(OperationInner::AlterField {
17351606
table_name: unwrap_builder_option!(self, table_name),
17361607
old_field: unwrap_builder_option!(self, old_field),
17371608
new_field: unwrap_builder_option!(self, new_field),
1738-
>>>>>>> 09f0c87 (feat: Add `AlterField` Operation to Migration Generator)
17391609
})
17401610
}
17411611
}
@@ -2218,86 +2088,6 @@ mod tests {
22182088
}
22192089
}
22202090

2221-
#[cot::test]
2222-
#[cfg_attr(
2223-
miri,
2224-
ignore = "unsupported operation: can't call foreign function `sqlite3_open_v2`"
2225-
)]
2226-
async fn operation_custom() {
2227-
// test only on SQLite because we are using raw SQL
2228-
let test_db = TestDatabase::new_sqlite().await.unwrap();
2229-
2230-
#[migration_op]
2231-
async fn forwards(ctx: MigrationContext<'_>) -> Result<()> {
2232-
ctx.db
2233-
.raw("CREATE TABLE custom_test (id INTEGER PRIMARY KEY)")
2234-
.await?;
2235-
Ok(())
2236-
}
2237-
2238-
let operation = Operation::custom(forwards).build();
2239-
operation.forwards(&test_db.database()).await.unwrap();
2240-
2241-
let result = test_db.database().raw("SELECT * FROM custom_test").await;
2242-
assert!(result.is_ok());
2243-
}
2244-
2245-
#[cot::test]
2246-
#[cfg_attr(
2247-
miri,
2248-
ignore = "unsupported operation: can't call foreign function `sqlite3_open_v2`"
2249-
)]
2250-
async fn operation_custom_backwards() {
2251-
// test only on SQLite because we are using raw SQL
2252-
let test_db = TestDatabase::new_sqlite().await.unwrap();
2253-
2254-
#[migration_op]
2255-
async fn forwards(_ctx: MigrationContext<'_>) -> Result<()> {
2256-
panic!("this should not be called");
2257-
}
2258-
2259-
#[migration_op]
2260-
async fn backwards(ctx: MigrationContext<'_>) -> Result<()> {
2261-
ctx.db.raw("DROP TABLE custom_test_back").await?;
2262-
Ok(())
2263-
}
2264-
2265-
test_db
2266-
.database()
2267-
.raw("CREATE TABLE custom_test_back (id INTEGER PRIMARY KEY)")
2268-
.await
2269-
.unwrap();
2270-
2271-
let operation = Operation::custom(forwards).backwards(backwards).build();
2272-
operation.backwards(&test_db.database()).await.unwrap();
2273-
2274-
let result = test_db
2275-
.database()
2276-
.raw("SELECT * FROM custom_test_back")
2277-
.await;
2278-
assert!(result.is_err());
2279-
}
2280-
2281-
#[cot::test]
2282-
#[cfg_attr(
2283-
miri,
2284-
ignore = "unsupported operation: can't call foreign function `sqlite3_open_v2`"
2285-
)]
2286-
async fn operation_custom_backwards_not_implemented() {
2287-
// test only on SQLite because we are using raw SQL
2288-
let test_db = TestDatabase::new_sqlite().await.unwrap();
2289-
2290-
#[migration_op]
2291-
async fn forwards(_ctx: MigrationContext<'_>) -> Result<()> {
2292-
Ok(())
2293-
}
2294-
2295-
let operation = Operation::custom(forwards).build();
2296-
let result = operation.backwards(&test_db.database()).await;
2297-
2298-
assert!(result.is_err());
2299-
}
2300-
23012091
#[test]
23022092
fn field_new() {
23032093
let field = Field::new(Identifier::new("id"), ColumnType::Integer)

0 commit comments

Comments
 (0)