From 154d4dd71727ecbcf9fe9d1e242200e9fc433ed0 Mon Sep 17 00:00:00 2001 From: Athos Couto Date: Tue, 26 Sep 2023 15:36:11 -0300 Subject: [PATCH] Make sure database tokens are invalidated once a DB is destroyed --- internal/cmd/cache.go | 4 ++++ internal/cmd/utils.go | 5 ++++- internal/settings/cache.go | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/cmd/cache.go b/internal/cmd/cache.go index 082c6a27..9f6a07b2 100644 --- a/internal/cmd/cache.go +++ b/internal/cmd/cache.go @@ -84,6 +84,10 @@ func dbTokenCache(dbID string) string { return token } +func invalidateDbTokenCache() { + settings.SetCacheRaw(DATABASE_TOKEN_KEY_PREFIX[:len(DATABASE_TOKEN_KEY_PREFIX)-1], struct{}{}) +} + const ORG_CACHE_KEY = "organizations" const GROUP_CACHE_KEY = "groups" const GROUP_CACHE_TTL_SECONDS = 30 * 60 diff --git a/internal/cmd/utils.go b/internal/cmd/utils.go index 4c7fd9b0..504c874d 100644 --- a/internal/cmd/utils.go +++ b/internal/cmd/utils.go @@ -132,6 +132,10 @@ func printTable(header []string, data [][]string) { } func destroyDatabase(client *turso.Client, name string) error { + invalidateDatabasesCache() + invalidateDbTokenCache() + settings.PersistChanges() + start := time.Now() s := prompt.Spinner(fmt.Sprintf("Destroying database %s... ", internal.Emph(name))) defer s.Stop() @@ -143,7 +147,6 @@ func destroyDatabase(client *turso.Client, name string) error { elapsed := time.Since(start) fmt.Printf("Destroyed database %s in %d seconds.\n", internal.Emph(name), int(elapsed.Seconds())) - invalidateDatabasesCache() return nil } diff --git a/internal/settings/cache.go b/internal/settings/cache.go index 7f6cbd81..1c0e6313 100644 --- a/internal/settings/cache.go +++ b/internal/settings/cache.go @@ -20,6 +20,15 @@ func cacheKey(key string) string { return "cache." + key } +func SetCacheRaw[T any](key string, value T) error { + if _, err := ReadSettings(); err != nil { + return err + } + viper.Set(cacheKey(key), value) + settings.changed = true + return nil +} + func SetCache[T any](key string, ttl int64, value T) error { exp := time.Now().Unix() + ttl return SetCacheWithExp(key, exp, value)