From 906d51cfb9f13f764a3aaa7bff7bf1afec623d7d Mon Sep 17 00:00:00 2001 From: Aevann Date: Wed, 23 Oct 2024 22:49:00 +0300 Subject: [PATCH] dedup table rows --- files/classes/media.py | 7 +++---- migrations/20241022-cleanup-videos.sql | 3 +++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/files/classes/media.py b/files/classes/media.py index 0c0366c23..bf67486ce 100644 --- a/files/classes/media.py +++ b/files/classes/media.py @@ -27,10 +27,9 @@ class Media(Base): class MediaUsage(Base): __tablename__ = "media_usages" - id = Column(Integer, primary_key=True) - filename = Column(String, ForeignKey("media.filename")) - post_id = Column(Integer, ForeignKey("posts.id")) - comment_id = Column(Integer, ForeignKey("comments.id")) + filename = Column(String, ForeignKey("media.filename"), primary_key=True) + post_id = Column(Integer, ForeignKey("posts.id"), primary_key=True) + comment_id = Column(Integer, ForeignKey("comments.id"), primary_key=True) created_utc = Column(Integer) deleted_utc = Column(Integer) removed_utc = Column(Integer) diff --git a/migrations/20241022-cleanup-videos.sql b/migrations/20241022-cleanup-videos.sql index 1221cfd97..d22ddb060 100644 --- a/migrations/20241022-cleanup-videos.sql +++ b/migrations/20241022-cleanup-videos.sql @@ -16,6 +16,9 @@ CREATE SEQUENCE public.media_usages_id_seq NO MAXVALUE CACHE 1; +ALTER TABLE ONLY public.media_usages + ADD CONSTRAINT group_media_usages_pkey PRIMARY KEY (filename, post_id, comment_id); + ALTER SEQUENCE public.media_usages_id_seq OWNED BY public.media_usages.id; ALTER TABLE ONLY public.media_usages ALTER COLUMN id SET DEFAULT nextval('public.media_usages_id_seq'::regclass);