Skip to content

Commit

Permalink
feat: add copy_upload_history db function to migrate user data (#1964)
Browse files Browse the repository at this point in the history
* feat: add copyUserData db function to migrate user data

* rm comment

* refactor copy_user_data to copy_upload_history, add auth key arg

* revert changes to DBClient
  • Loading branch information
yusefnapora authored Jun 16, 2022
1 parent f7ee2bb commit 7edca7b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/api/db/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,37 @@ FROM cargo.aggregate_entries ae
WHERE ae.cid_v1 = ANY (cids)
ORDER BY de.entry_last_updated
$$;


-- Copies upload history from one user id to another.
-- Copied uploads will be associated with new_auth_key, which
-- should be the id of an auth_key belonging to the new user.
CREATE OR REPLACE FUNCTION copy_upload_history(old_user_id BIGINT, new_user_id BIGINT, new_auth_key_id BIGINT) RETURNS void
LANGUAGE plpgsql
volatile
PARALLEL UNSAFE
AS
$$
BEGIN

INSERT INTO upload (
user_id,
key_id,
content_cid,
source_cid,
mime_type,
type,
name,
files,
origins,
meta,
backup_urls,
updated_at,
inserted_at
)
SELECT new_user_id, new_auth_key_id, u.content_cid, u.source_cid, u.mime_type, u.type, u.name, u.files, u.origins, u.meta, u.backup_urls, u.updated_at, u.inserted_at
FROM upload as u
WHERE u.user_id = old_user_id;

END
$$;
32 changes: 32 additions & 0 deletions packages/api/db/migrations/006-add-copy-upload-history-fn.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Copies upload history from one user id to another.
-- Copied uploads will be associated with new_auth_key, which
-- should be the id of an auth_key belonging to the new user.
CREATE OR REPLACE FUNCTION copy_upload_history(old_user_id BIGINT, new_user_id BIGINT, new_auth_key_id BIGINT) RETURNS void
LANGUAGE plpgsql
volatile
PARALLEL UNSAFE
AS
$$
BEGIN

INSERT INTO upload (
user_id,
key_id,
content_cid,
source_cid,
mime_type,
type,
name,
files,
origins,
meta,
backup_urls,
updated_at,
inserted_at
)
SELECT new_user_id, new_auth_key_id, u.content_cid, u.source_cid, u.mime_type, u.type, u.name, u.files, u.origins, u.meta, u.backup_urls, u.updated_at, u.inserted_at
FROM upload as u
WHERE u.user_id = old_user_id;

END
$$;

0 comments on commit 7edca7b

Please sign in to comment.