feat: add migration for cache table with necessary columns and indexes

This commit is contained in:
GW_MC
2026-05-28 13:13:28 +00:00
parent 52a096d934
commit aa6496de59
5 changed files with 89 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*;
mod m20260526_062833_create_account_table;
mod m20260526_110957_create_transcations_table;
mod m20260528_110733_create_cache_table;
pub struct Migrator;
@@ -11,6 +12,7 @@ impl MigratorTrait for Migrator {
vec![
Box::new(m20260526_062833_create_account_table::Migration),
Box::new(m20260526_110957_create_transcations_table::Migration),
Box::new(m20260528_110733_create_cache_table::Migration),
]
}
}

View File

@@ -0,0 +1,61 @@
use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table("cache")
.comment("2 level cache for general use")
.if_not_exists()
.col(string("category").not_null().comment("Cache category"))
.col(string("key").not_null().comment("Cache key"))
.col(string("value").not_null().comment("Cache value"))
.col(
boolean("is_invalidated")
.not_null()
.comment("Whether the cache is manually invalidated"),
)
.col(string("expire_at").null().comment("Expiration time"))
.col(string("created_at").not_null().comment("Creation time"))
.col(string("updated_at").not_null().comment("Last update time"))
//
.primary_key(Index::create().col("category").col("key"))
//
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_cache_category")
.table("cache")
.col("category")
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_cache_expire_at")
.table("cache")
.col("expire_at")
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table("cache").to_owned())
.await
}
}

View File

@@ -0,0 +1,24 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "cache")]
#[allow(dead_code)]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub category: String,
#[sea_orm(primary_key, auto_increment = false)]
pub key: String,
pub value: String,
pub is_invalidated: bool,
pub expire_at: Option<String>,
pub created_at: String,
pub updated_at: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -4,6 +4,7 @@ pub mod prelude;
pub mod account;
pub mod account_category;
pub mod cache;
pub mod category;
pub mod tag;
pub mod transaction;

View File

@@ -4,6 +4,7 @@
pub use super::account::Entity as Account;
pub use super::account_category::Entity as AccountCategory;
pub use super::cache::Entity as Cache;
pub use super::category::Entity as Category;
pub use super::tag::Entity as Tag;
pub use super::transaction::Entity as Transaction;