From aa6496de59362f792ee0419971de7af27f58fdfe Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Thu, 28 May 2026 13:13:28 +0000 Subject: [PATCH] feat: add migration for cache table with necessary columns and indexes --- crates/migration/src/lib.rs | 2 + .../m20260528_110733_create_cache_table.rs | 61 +++++++++++++++++++ src-tauri/src/db/entities/cache.rs | 24 ++++++++ src-tauri/src/db/entities/mod.rs | 1 + src-tauri/src/db/entities/prelude.rs | 1 + 5 files changed, 89 insertions(+) create mode 100644 crates/migration/src/m20260528_110733_create_cache_table.rs create mode 100644 src-tauri/src/db/entities/cache.rs diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index dfbb7a0..a2db3bd 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -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), ] } } diff --git a/crates/migration/src/m20260528_110733_create_cache_table.rs b/crates/migration/src/m20260528_110733_create_cache_table.rs new file mode 100644 index 0000000..b984a16 --- /dev/null +++ b/crates/migration/src/m20260528_110733_create_cache_table.rs @@ -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 + } +} diff --git a/src-tauri/src/db/entities/cache.rs b/src-tauri/src/db/entities/cache.rs new file mode 100644 index 0000000..c550f62 --- /dev/null +++ b/src-tauri/src/db/entities/cache.rs @@ -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, + pub created_at: String, + pub updated_at: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entities/mod.rs b/src-tauri/src/db/entities/mod.rs index dd10269..1a6ff07 100644 --- a/src-tauri/src/db/entities/mod.rs +++ b/src-tauri/src/db/entities/mod.rs @@ -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; diff --git a/src-tauri/src/db/entities/prelude.rs b/src-tauri/src/db/entities/prelude.rs index 9045208..bb5b112 100644 --- a/src-tauri/src/db/entities/prelude.rs +++ b/src-tauri/src/db/entities/prelude.rs @@ -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;