Skip to content

Commit

Permalink
chore: create trigger for workspace delete action
Browse files Browse the repository at this point in the history
  • Loading branch information
appflowy committed Mar 5, 2025
1 parent 0b5517f commit da80414
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
19 changes: 19 additions & 0 deletions libs/database/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 33 additions & 6 deletions migrations/20250305082545_workspace_delete_trigger.sql
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
-- Create the trigger function
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,
'owner_id', OLD.owner_uid
'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 the trigger that calls the function after deleting a workspace
CREATE TRIGGER on_workspace_delete
AFTER DELETE ON public.af_workspace
FOR EACH ROW
EXECUTE FUNCTION workspace_deleted_trigger_function();
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();
11 changes: 10 additions & 1 deletion src/biz/user/user_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand All @@ -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();
Expand Down

0 comments on commit da80414

Please sign in to comment.