use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20260318_000001_create_todo_items"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(TodoItems::Table)
.if_not_exists()
.col(ColumnDef::new(TodoItems::Id).string().not_null().primary_key())
.col(ColumnDef::new(TodoItems::Title).string().not_null())
.col(ColumnDef::new(TodoItems::Note).text().not_null().default(""))
.col(ColumnDef::new(TodoItems::Status).string().not_null().default("pending"))
.col(ColumnDef::new(TodoItems::Priority).integer().not_null().default(4))
.col(ColumnDef::new(TodoItems::CreatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(TodoItems::UpdatedAt).timestamp_with_time_zone().not_null())
.col(ColumnDef::new(TodoItems::CompletedAt).timestamp_with_time_zone().null())
.col(ColumnDef::new(TodoItems::DeletedAt).timestamp_with_time_zone().null())
.to_owned(),
)
.await?;
manager.create_index(
Index::create()
.name("idx_todo_items_deleted_at")
.table(TodoItems::Table)
.col(TodoItems::DeletedAt)
.to_owned(),
)
.await?;
manager.create_index(
Index::create()
.name("idx_todo_items_priority_created")
.table(TodoItems::Table)
.col(TodoItems::DeletedAt)
.col(TodoItems::Priority)
.col(TodoItems::CreatedAt)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(TodoItems::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum TodoItems {
Table,
Id,
Title,
Note,
Status,
Priority,
CreatedAt,
UpdatedAt,
CompletedAt,
DeletedAt,
}