From 709b44e4809bef288a1a3a0e2e30c9fb40e97ebb Mon Sep 17 00:00:00 2001 From: Julia Seweryn Date: Wed, 30 Oct 2024 14:25:50 +0100 Subject: [PATCH] fixed --- database/migrations/0002_team.sql | 2 +- database/src/tables/registered_app/select.rs | 16 ---------------- database/src/tables/team/update.rs | 12 ++++++++++++ server/src/bin/grafana_restore.rs | 6 +++++- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/database/migrations/0002_team.sql b/database/migrations/0002_team.sql index 5065b679..ed4fd046 100644 --- a/database/migrations/0002_team.sql +++ b/database/migrations/0002_team.sql @@ -1,6 +1,6 @@ CREATE TABLE team( team_id TEXT NOT NULL UNIQUE, - grafana_id TEXT NOT NULL UNIQUE, + grafana_id TEXT UNIQUE, team_name TEXT NOT NULL, personal BOOLEAN NOT NULL, subscription subscription, diff --git a/database/src/tables/registered_app/select.rs b/database/src/tables/registered_app/select.rs index 10a2bf37..87765983 100644 --- a/database/src/tables/registered_app/select.rs +++ b/database/src/tables/registered_app/select.rs @@ -34,20 +34,4 @@ impl Db { .await .map_err(|e| e.into()); } - - pub async fn get_registered_app_by_team_id( - &self, - team_id: &String, - ) -> Result, DbError> { - let query = format!( - "SELECT * FROM {REGISTERED_APPS_TABLE_NAME} WHERE team_id = $1 AND deactivated_at IS NULL" - ); - let typed_query = query_as::<_, DbRegisteredApp>(&query); - - return typed_query - .bind(&team_id) - .fetch_all(&self.connection_pool) - .await - .map_err(|e| e.into()); - } } diff --git a/database/src/tables/team/update.rs b/database/src/tables/team/update.rs index 135ed10d..9db0a7a7 100644 --- a/database/src/tables/team/update.rs +++ b/database/src/tables/team/update.rs @@ -115,6 +115,18 @@ impl Db { Err(e) => Err(e).map_err(|e| e.into()), } } + + pub async fn clear_all_grafana_ids(&self) -> Result<(), DbError> { + let query_body = + format!("UPDATE {TEAM_TABLE_NAME} SET grafana_id = NULL WHERE deactivated_at IS NULL",); + + let query_result = query(&query_body).execute(&self.connection_pool).await; + + match query_result { + Ok(_) => Ok(()), + Err(e) => Err(e).map_err(|e| e.into()), + } + } } #[cfg(feature = "cloud_integration_tests")] diff --git a/server/src/bin/grafana_restore.rs b/server/src/bin/grafana_restore.rs index 7a789288..efd4d1bf 100644 --- a/server/src/bin/grafana_restore.rs +++ b/server/src/bin/grafana_restore.rs @@ -25,6 +25,10 @@ async fn main() { // Setup template dashboards import_template_dashboards(&grafana_client_conf).await; + if let Err(err) = db.clear_all_grafana_ids().await { + panic!("Failed to clear grafana ids in database: {:?}", err); + } + let teams = match db.get_all_teams().await { Ok(teams) => teams, Err(e) => { @@ -84,7 +88,7 @@ async fn main() { } }; } - let apps = match db.get_registered_app_by_team_id(&team.team_id).await { + let apps = match db.get_registered_apps_by_team_id(&team.team_id).await { Ok(apps) => apps, Err(e) => { panic!("Failed to get apps. Error: {:?}", e);