From 16469daff3cb698bd9486d0dd4479fc71623766d Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sat, 9 Jan 2021 12:23:21 +0100 Subject: [PATCH] :sparkles: Many improvements to the database layer. - Proper handling of referenced tables deletion. - Proper handling of storage referenced tables deletion. - Remove of obsolete tables and triggers. --- backend/src/app/migrations.clj | 9 ++++ .../sql/0003-add-project-tables.sql | 2 - .../0036-mod-storage-referenced-tables.sql | 5 -- .../sql/0037-del-obsolete-triggers.sql | 3 ++ .../0038-add-storage-on-delete-triggers.sql | 50 +++++++++++++++++++ .../sql/0039-fix-some-on-delete-triggers.sql | 9 ++++ ...bles.sql => XXXX-drop-obsolete-tables.sql} | 0 backend/src/app/rpc/mutations/teams.clj | 1 + 8 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 backend/src/app/migrations/sql/0037-del-obsolete-triggers.sql create mode 100644 backend/src/app/migrations/sql/0038-add-storage-on-delete-triggers.sql create mode 100644 backend/src/app/migrations/sql/0039-fix-some-on-delete-triggers.sql rename backend/src/app/migrations/sql/{0037-mod-storage-related-tables.sql => XXXX-drop-obsolete-tables.sql} (100%) diff --git a/backend/src/app/migrations.clj b/backend/src/app/migrations.clj index 165d9ca33..f74c9f2d8 100644 --- a/backend/src/app/migrations.clj +++ b/backend/src/app/migrations.clj @@ -122,6 +122,15 @@ {:name "0036-mod-storage-referenced-tables" :fn (mg/resource "app/migrations/sql/0036-mod-storage-referenced-tables.sql")} + + {:name "0037-del-obsolete-triggers" + :fn (mg/resource "app/migrations/sql/0037-del-obsolete-triggers.sql")} + + {:name "0038-add-storage-on-delete-triggers" + :fn (mg/resource "app/migrations/sql/0038-add-storage-on-delete-triggers.sql")} + + {:name "0039-fix-some-on-delete-triggers" + :fn (mg/resource "app/migrations/sql/0039-fix-some-on-delete-triggers.sql")} ]) diff --git a/backend/src/app/migrations/sql/0003-add-project-tables.sql b/backend/src/app/migrations/sql/0003-add-project-tables.sql index 925f7c8b0..e78e443ac 100644 --- a/backend/src/app/migrations/sql/0003-add-project-tables.sql +++ b/backend/src/app/migrations/sql/0003-add-project-tables.sql @@ -106,8 +106,6 @@ CREATE TRIGGER file_image__on_delete__tgr AFTER DELETE ON file_image FOR EACH ROW EXECUTE PROCEDURE handle_delete(); - - CREATE TABLE page ( id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), file_id uuid NOT NULL REFERENCES file(id) ON DELETE CASCADE, diff --git a/backend/src/app/migrations/sql/0036-mod-storage-referenced-tables.sql b/backend/src/app/migrations/sql/0036-mod-storage-referenced-tables.sql index fabd0d479..23226f392 100644 --- a/backend/src/app/migrations/sql/0036-mod-storage-referenced-tables.sql +++ b/backend/src/app/migrations/sql/0036-mod-storage-referenced-tables.sql @@ -1,8 +1,3 @@ --- Complete migration consists of: --- - Move all file_media_objects and file_media_thumbnail to new storage. --- - Replace the relative paths to the storage id's on all files/pages. --- - Adapt frontend code to properly resolve url using the ids instead of paths. - -- Profile ALTER TABLE profile ADD COLUMN photo_id uuid NULL REFERENCES storage_object(id) ON DELETE SET NULL; CREATE INDEX profile__photo_id__idx ON profile(photo_id); diff --git a/backend/src/app/migrations/sql/0037-del-obsolete-triggers.sql b/backend/src/app/migrations/sql/0037-del-obsolete-triggers.sql new file mode 100644 index 000000000..0728a305e --- /dev/null +++ b/backend/src/app/migrations/sql/0037-del-obsolete-triggers.sql @@ -0,0 +1,3 @@ +DROP FUNCTION update_modified_at () CASCADE; +DROP FUNCTION handle_delete ( ) CASCADE; +DROP TABLE pending_to_delete; diff --git a/backend/src/app/migrations/sql/0038-add-storage-on-delete-triggers.sql b/backend/src/app/migrations/sql/0038-add-storage-on-delete-triggers.sql new file mode 100644 index 000000000..6276a36f9 --- /dev/null +++ b/backend/src/app/migrations/sql/0038-add-storage-on-delete-triggers.sql @@ -0,0 +1,50 @@ +CREATE FUNCTION on_delete_profile() + RETURNS TRIGGER AS $func$ + BEGIN + UPDATE storage_object + SET deleted_at = now() + WHERE id = OLD.photo_id; + + RETURN OLD; + END; +$func$ LANGUAGE plpgsql; + +CREATE FUNCTION on_delete_team() + RETURNS TRIGGER AS $func$ + BEGIN + UPDATE storage_object + SET deleted_at = now() + WHERE id = OLD.photo_id; + + RETURN OLD; + END; +$func$ LANGUAGE plpgsql; + +CREATE FUNCTION on_delete_file_media_object() + RETURNS TRIGGER AS $func$ + BEGIN + UPDATE storage_object + SET deleted_at = now() + WHERE id = OLD.media_id; + + IF OLD.thumbnail_id IS NOT NULL THEN + UPDATE storage_object + SET deleted_at = now() + WHERE id = OLD.thumbnail_id; + END IF; + + RETURN OLD; + END; +$func$ LANGUAGE plpgsql; + +CREATE TRIGGER profile__on_delete__tgr + AFTER DELETE ON profile + FOR EACH ROW EXECUTE PROCEDURE on_delete_profile(); + +CREATE TRIGGER team__on_delete__tgr + AFTER DELETE ON team + FOR EACH ROW EXECUTE PROCEDURE on_delete_team(); + +CREATE TRIGGER file_media_object__on_delete__tgr + AFTER DELETE ON file_media_object + FOR EACH ROW EXECUTE PROCEDURE on_delete_file_media_object(); diff --git a/backend/src/app/migrations/sql/0039-fix-some-on-delete-triggers.sql b/backend/src/app/migrations/sql/0039-fix-some-on-delete-triggers.sql new file mode 100644 index 000000000..591ccadc1 --- /dev/null +++ b/backend/src/app/migrations/sql/0039-fix-some-on-delete-triggers.sql @@ -0,0 +1,9 @@ +ALTER TABLE file_library_rel + DROP CONSTRAINT file_library_rel_library_file_id_fkey, + ADD CONSTRAINT file_library_rel_library_file_id_fkey + FOREIGN KEY (library_file_id) REFERENCES file(id) ON DELETE CASCADE; + +ALTER TABLE team_profile_rel + DROP CONSTRAINT team_profile_rel_profile_id_fkey, + ADD CONSTRAINT team_profile_rel_profile_id_fkey + FOREIGN KEY (profile_id) REFERENCES profile(id) ON DELETE CASCADE; diff --git a/backend/src/app/migrations/sql/0037-mod-storage-related-tables.sql b/backend/src/app/migrations/sql/XXXX-drop-obsolete-tables.sql similarity index 100% rename from backend/src/app/migrations/sql/0037-mod-storage-related-tables.sql rename to backend/src/app/migrations/sql/XXXX-drop-obsolete-tables.sql diff --git a/backend/src/app/rpc/mutations/teams.clj b/backend/src/app/rpc/mutations/teams.clj index 76545d798..4804d2ab9 100644 --- a/backend/src/app/rpc/mutations/teams.clj +++ b/backend/src/app/rpc/mutations/teams.clj @@ -141,6 +141,7 @@ (db/delete! conn :team {:id id}) nil))) + ;; --- Mutation: Tean Update Role (declare retrieve-team-member)