Skip to content

Commit

Permalink
chore: implement up and down migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Feb 3, 2025
1 parent 32942df commit 5d46cb0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
36 changes: 18 additions & 18 deletions migration/src/m0000210_create_relationship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;

pub(crate) const DATA: [(i32, &str); 14] = [
(0, "ContainedBy"),
(1, "DependencyOf"),
(2, "DevDependencyOf"),
(3, "OptionalDependencyOf"),
(4, "ProvidedDependencyOf"),
(5, "TestDependencyOf"),
(6, "RuntimeDependencyOf"),
(7, "ExampleOf"),
(8, "GeneratedFrom"),
(9, "AncestorOf"),
(10, "VariantOf"),
(11, "BuildToolOf"),
(12, "DevToolOf"),
(13, "DescribedBy"),
];

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
Expand All @@ -27,24 +44,7 @@ impl MigrationTrait for Migration {
)
.await?;

let data = [
(0, "ContainedBy"),
(1, "DependencyOf"),
(2, "DevDependencyOf"),
(3, "OptionalDependencyOf"),
(4, "ProvidedDependencyOf"),
(5, "TestDependencyOf"),
(6, "RuntimeDependencyOf"),
(7, "ExampleOf"),
(8, "GeneratedFrom"),
(9, "AncestorOf"),
(10, "VariantOf"),
(11, "BuildToolOf"),
(12, "DevToolOf"),
(13, "DescribedBy"),
];

for (id, description) in data {
for (id, description) in DATA {
let insert = Query::insert()
.into_table(Relationship::Table)
.columns([Relationship::Id, Relationship::Description])
Expand Down
66 changes: 43 additions & 23 deletions migration/src/m0000860_normalise_relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,57 @@ const DATA: [(i32, &str); 14] = [
(14, "Package"),
];

use super::m0000210_create_relationship::DATA as OLD_DATA;

async fn insert(
manager: &SchemaManager<'_>,
data: &'static [(i32, &'static str)],
) -> Result<(), DbErr> {
for (id, description) in data {
let insert = Query::insert()
.into_table(Relationship::Table)
.columns([Relationship::Id, Relationship::Description])
.values_panic([(*id).into(), (*description).into()])
.on_conflict(
OnConflict::columns([Relationship::Id])
.update_columns([Relationship::Description])
.to_owned(),
)
.to_owned();

manager.exec_stmt(insert).await?;
}

Ok(())
}

async fn delete(
manager: &SchemaManager<'_>,
data: &'static [(i32, &'static str)],
) -> Result<(), DbErr> {
for (id, _) in data {
let insert = Query::delete()
.from_table(Relationship::Table)
.and_where(Expr::col(Relationship::Id).lt(*id))
.to_owned();

manager.exec_stmt(insert).await?;
}
Ok(())
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
for (id, description) in DATA {
let insert = Query::insert()
.into_table(Relationship::Table)
.columns([Relationship::Id, Relationship::Description])
.values_panic([id.into(), description.into()])
.on_conflict(
OnConflict::columns([Relationship::Id])
.update_columns([Relationship::Description])
.to_owned(),
)
.to_owned();

manager.exec_stmt(insert).await?;
}
delete(manager, &OLD_DATA).await?;
insert(manager, &DATA).await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
for (id, _) in DATA {
let insert = Query::delete()
.from_table(Relationship::Table)
.and_where(Expr::col(Relationship::Id).lt(id))
.to_owned();

manager.exec_stmt(insert).await?;
}

delete(manager, &DATA).await?;
insert(manager, &OLD_DATA).await?;
Ok(())
}
}
Expand Down

0 comments on commit 5d46cb0

Please sign in to comment.