diff --git a/packages/backend/native-utils/migration/src/lib.rs b/packages/backend/native-utils/migration/src/lib.rs index 5ad23f1625..7a3d14c2eb 100644 --- a/packages/backend/native-utils/migration/src/lib.rs +++ b/packages/backend/native-utils/migration/src/lib.rs @@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*; mod m20230531_180824_drop_reversi; mod m20230627_185451_index_note_url; mod m20230709_000510_move_antenna_to_cache; +mod m20230716_164152_clear_user_cache; pub struct Migrator; @@ -13,6 +14,7 @@ impl MigratorTrait for Migrator { Box::new(m20230531_180824_drop_reversi::Migration), Box::new(m20230627_185451_index_note_url::Migration), Box::new(m20230709_000510_move_antenna_to_cache::Migration), + Box::new(m20230716_164152_clear_user_cache::Migration), ] } } diff --git a/packages/backend/native-utils/migration/src/m20230716_164152_clear_user_cache.rs b/packages/backend/native-utils/migration/src/m20230716_164152_clear_user_cache.rs new file mode 100644 index 0000000000..45352986f6 --- /dev/null +++ b/packages/backend/native-utils/migration/src/m20230716_164152_clear_user_cache.rs @@ -0,0 +1,29 @@ +use redis::Commands; +use sea_orm_migration::prelude::*; +use std::env; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, _manager: &SchemaManager) -> Result<(), DbErr> { + let cache_url = env::var("CACHE_URL").unwrap(); + let prefix = env::var("CACHE_PREFIX").unwrap(); + let client = redis::Client::open(cache_url).unwrap(); + let mut redis_conn = client.get_connection().unwrap(); + let keys: Vec = redis_conn + .keys(format!("{prefix}:cache:userById:*")).unwrap(); + + if !keys.is_empty() { + println!("Deleting {} keys from the cache server.", keys.len()); + redis_conn.del::<_, i32>(keys).unwrap(); + } + + Ok(()) + } + + async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { + Ok(()) + } +}