mirror of
https://github.com/AppFlowy-IO/AppFlowy-Cloud.git
synced 2025-04-19 03:24:42 -04:00
Workspace table notification (#1261)
* chore: add delete user notification * chore: create trigger for workspace delete action
This commit is contained in:
parent
eb927831cd
commit
8008959965
3 changed files with 78 additions and 1 deletions
|
@ -770,6 +770,25 @@ pub async fn select_user_owned_workspaces_id<'a, E: Executor<'a, Database = Post
|
|||
Ok(workspace_ids)
|
||||
}
|
||||
|
||||
pub async fn insert_workspace_ids_to_deleted_table<'a, E>(
|
||||
executor: E,
|
||||
workspace_ids: Vec<Uuid>,
|
||||
) -> Result<(), AppError>
|
||||
where
|
||||
E: Executor<'a, Database = Postgres>,
|
||||
{
|
||||
if workspace_ids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let query = "INSERT INTO public.af_workspace_deleted (workspace_id) SELECT unnest($1::uuid[])";
|
||||
sqlx::query(query)
|
||||
.bind(workspace_ids)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_workspace_status<'a, E: Executor<'a, Database = Postgres>>(
|
||||
executor: E,
|
||||
workspace_id: &Uuid,
|
||||
|
|
49
migrations/20250305082545_workspace_delete_trigger.sql
Normal file
49
migrations/20250305082545_workspace_delete_trigger.sql
Normal file
|
@ -0,0 +1,49 @@
|
|||
CREATE TABLE IF NOT EXISTS af_workspace_deleted (
|
||||
workspace_id uuid PRIMARY KEY NOT NULL,
|
||||
deleted_at timestamp with time zone DEFAULT now()
|
||||
);
|
||||
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON public.af_workspace_deleted TO supabase_auth_admin;
|
||||
|
||||
-- Workspace delete trigger
|
||||
CREATE OR REPLACE FUNCTION workspace_deleted_trigger_function()
|
||||
RETURNS trigger AS
|
||||
$$
|
||||
DECLARE
|
||||
payload jsonb;
|
||||
BEGIN
|
||||
payload := jsonb_build_object(
|
||||
'workspace_id', OLD.workspace_id
|
||||
);
|
||||
INSERT INTO public.af_workspace_deleted (workspace_id, deleted_at)
|
||||
VALUES (OLD.workspace_id, now())
|
||||
ON CONFLICT DO NOTHING;
|
||||
PERFORM pg_notify('af_workspace_deleted', payload::text);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER on_workspace_delete
|
||||
AFTER DELETE ON public.af_workspace
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION workspace_deleted_trigger_function();
|
||||
|
||||
-- Delete user trigger
|
||||
CREATE OR REPLACE FUNCTION notify_user_deletion()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
payload TEXT;
|
||||
BEGIN
|
||||
payload := jsonb_build_object(
|
||||
'user_uuid', OLD.uuid::text
|
||||
);
|
||||
|
||||
PERFORM pg_notify('af_user_deleted', payload::text);
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER user_deletion_trigger
|
||||
AFTER DELETE ON public.af_user
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION notify_user_deletion();
|
|
@ -5,10 +5,11 @@ use crate::{biz::workspace::ops::delete_workspace_for_user, config::config::Appl
|
|||
use app_error::ErrorCode;
|
||||
use authentication::jwt::Authorization;
|
||||
use database::file::s3_client_impl::S3BucketStorage;
|
||||
use database::workspace::select_user_owned_workspaces_id;
|
||||
use database::workspace::{insert_workspace_ids_to_deleted_table, select_user_owned_workspaces_id};
|
||||
use gotrue::params::AdminDeleteUserParams;
|
||||
use secrecy::{ExposeSecret, Secret};
|
||||
use shared_entity::response::AppResponseError;
|
||||
use tracing::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
|
@ -36,6 +37,7 @@ pub async fn delete_user(
|
|||
};
|
||||
}
|
||||
|
||||
info!("admin deleting user: {:?}", user_uuid);
|
||||
let admin_token = gotrue_admin.token().await?;
|
||||
gotrue_client
|
||||
.admin_delete_user(
|
||||
|
@ -50,6 +52,13 @@ pub async fn delete_user(
|
|||
|
||||
// spawn tasks to delete all workspaces owned by the user
|
||||
let workspace_ids = select_user_owned_workspaces_id(pg_pool, &user_uuid).await?;
|
||||
|
||||
info!(
|
||||
"saving workspaces: {:?} to deleted workspace table",
|
||||
workspace_ids
|
||||
);
|
||||
insert_workspace_ids_to_deleted_table(pg_pool, workspace_ids.clone()).await?;
|
||||
|
||||
let mut tasks = vec![];
|
||||
for workspace_id in workspace_ids {
|
||||
let cloned_pg_pool = pg_pool.clone();
|
||||
|
|
Loading…
Add table
Reference in a new issue