Skip to content

Commit

Permalink
Update delete workspace to delete expense fields (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
NileshPant1999 authored Apr 17, 2023
1 parent 365eb71 commit 68f9661
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
209 changes: 209 additions & 0 deletions sql/functions/delete-workspace-v2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
DROP FUNCTION if exists delete_workspace_v2;

CREATE OR REPLACE FUNCTION delete_workspace_v2(IN _workspace_id integer) RETURNS void AS $$
DECLARE
rcount integer;
BEGIN
RAISE NOTICE 'Deleting data from workspace % ', _workspace_id;

DELETE
FROM task_logs tl
WHERE tl.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % task_logs', rcount;

DELETE
FROM errors er
where er.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % errors', rcount;

DELETE
FROM last_export_details l
where l.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % last_export_details', rcount;

DELETE
FROM bill_lineitems bl
WHERE bl.bill_id IN (
SELECT b.id FROM bills b WHERE b.expense_group_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
)
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % bill_lineitems', rcount;

DELETE
FROM bills b
WHERE b.expense_group_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % bills', rcount;

DELETE
FROM bank_transaction_lineitems btl
WHERE btl.bank_transaction_id IN (
SELECT bt.id FROM bank_transactions bt WHERE bt.expense_group_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
)
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % bank_transaction_lineitems', rcount;

DELETE
FROM bank_transactions bt
WHERE bt.expense_group_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % bank_transactions', rcount;

DELETE
FROM payments p
WHERE p.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % payments', rcount;

DELETE
FROM reimbursements r
WHERE r.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % reimbursements', rcount;

DELETE
FROM expenses e
WHERE e.id IN (
SELECT expense_id FROM expense_groups_expenses ege WHERE ege.expensegroup_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
)
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expenses', rcount;

DELETE
FROM expense_groups_expenses ege
WHERE ege.expensegroup_id IN (
SELECT eg.id FROM expense_groups eg WHERE eg.workspace_id = _workspace_id
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_groups_expenses', rcount;

DELETE
FROM expense_groups eg
WHERE eg.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_groups', rcount;

DELETE
FROM tenant_mappings tm
WHERE tm.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % tenant_mappings', rcount;

DELETE
FROM mappings m
WHERE m.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % mappings', rcount;

DELETE
FROM mapping_settings ms
WHERE ms.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % mapping_settings', rcount;

DELETE
FROM general_mappings gm
WHERE gm.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % general_mappings', rcount;

DELETE
FROM workspace_general_settings wgs
WHERE wgs.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % workspace_general_settings', rcount;

DELETE
FROM expense_group_settings egs
WHERE egs.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_group_settings', rcount;

DELETE
FROM fyle_credentials fc
WHERE fc.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % fyle_credentials', rcount;

DELETE
FROM xero_credentials xc
WHERE xc.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % xero_credentials', rcount;

DELETE
FROM expense_attributes ea
WHERE ea.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_attributes', rcount;

DELETE
FROM destination_attributes da
WHERE da.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % destination_attributes', rcount;

DELETE
FROM expense_fields ef
WHERE ef.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_fields', rcount;

DELETE
FROM workspace_schedules wsch
WHERE wsch.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % workspace_schedules', rcount;

DELETE
FROM django_q_schedule dqs
WHERE dqs.args = _workspace_id::varchar(255);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % django_q_schedule', rcount;

DELETE
FROM auth_tokens aut
WHERE aut.user_id IN (
SELECT u.id FROM users u WHERE u.id IN (
SELECT wu.user_id FROM workspaces_user wu WHERE workspace_id = _workspace_id
)
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % auth_tokens', rcount;

DELETE
FROM workspaces_user wu
WHERE workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % workspaces_user', rcount;

DELETE
FROM users u
WHERE u.id IN (
SELECT wu.user_id FROM workspaces_user wu WHERE workspace_id = _workspace_id
);
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % users', rcount;

DELETE
FROM workspaces w
WHERE w.id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % workspaces', rcount;

RETURN;
END
$$ LANGUAGE plpgsql;
6 changes: 6 additions & 0 deletions sql/functions/delete-workspace.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ BEGIN
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % destination_attributes', rcount;

DELETE
FROM expense_fields ef
WHERE ef.workspace_id = _workspace_id;
GET DIAGNOSTICS rcount = ROW_COUNT;
RAISE NOTICE 'Deleted % expense_fields', rcount;

DELETE
FROM workspace_schedules wsch
WHERE wsch.workspace_id = _workspace_id;
Expand Down

0 comments on commit 68f9661

Please sign in to comment.