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.
This commit is contained in:
Andrey Antukh 2021-01-09 12:23:21 +01:00 committed by Alonso Torres
parent d32cacf1da
commit 16469daff3
8 changed files with 72 additions and 7 deletions

View file

@ -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,

View file

@ -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);

View file

@ -0,0 +1,3 @@
DROP FUNCTION update_modified_at () CASCADE;
DROP FUNCTION handle_delete ( ) CASCADE;
DROP TABLE pending_to_delete;

View file

@ -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();

View file

@ -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;