]> git.ipfire.org Git - dbl.git/commitdiff
domains: Reference the list directly
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Dec 2025 18:17:40 +0000 (18:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 30 Dec 2025 18:17:40 +0000 (18:17 +0000)
This simplifies the queries substantially and we will only have to
select from one single, although larger table.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.sql
src/dnsbl/domains.py
src/dnsbl/lists.py
src/dnsbl/sources.py

index 599ab9b118255a161030c6c23e35c995f4af8ed3..6df255e6c6d495c2eab8305d88d03ffe122f4f50 100644 (file)
@@ -2,7 +2,7 @@
 -- PostgreSQL database dump
 --
 
-\restrict 9n7trKLmkNUVbvGuw4jSXAOGEnNoIEkdn1vooVDVC0KJpHkgAgdEvezLD9YkXew
+\restrict Ve94ffDejRZQh0k9F68Ox20B8hRjYq5z5defUdh578csjd9BHIC5XKZB0q1nuan
 
 -- Dumped from database version 17.6 (Debian 17.6-0+deb13u1)
 -- Dumped by pg_dump version 17.6 (Debian 17.6-0+deb13u1)
@@ -75,6 +75,7 @@ CREATE TABLE public.checker_domains (
 
 CREATE TABLE public.domains (
     id integer NOT NULL,
+    list_id integer NOT NULL,
     name text NOT NULL,
     source_id integer,
     added_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
@@ -335,6 +336,13 @@ ALTER TABLE ONLY public.sources
 CREATE INDEX api_keys_prefix ON public.api_keys USING btree (prefix) WHERE (deleted_at IS NULL);
 
 
+--
+-- Name: domains_list_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX domains_list_id ON public.domains USING btree (list_id) WHERE (removed_at IS NULL);
+
+
 --
 -- Name: domains_search; Type: INDEX; Schema: public; Owner: -
 --
@@ -377,6 +385,14 @@ CREATE INDEX reports_open ON public.reports USING btree (name) WHERE (closed_at
 CREATE UNIQUE INDEX sources_unique ON public.sources USING btree (list_id, url) WHERE (deleted_at IS NULL);
 
 
+--
+-- Name: domains domains_list_id; Type: FK CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.domains
+    ADD CONSTRAINT domains_list_id FOREIGN KEY (list_id) REFERENCES public.lists(id);
+
+
 --
 -- Name: domains domains_report_id; Type: FK CONSTRAINT; Schema: public; Owner: -
 --
@@ -413,5 +429,5 @@ ALTER TABLE ONLY public.sources
 -- PostgreSQL database dump complete
 --
 
-\unrestrict 9n7trKLmkNUVbvGuw4jSXAOGEnNoIEkdn1vooVDVC0KJpHkgAgdEvezLD9YkXew
+\unrestrict Ve94ffDejRZQh0k9F68Ox20B8hRjYq5z5defUdh578csjd9BHIC5XKZB0q1nuan
 
index 5feeb3bbda2e5b3be2df87a128132d0b551badc5..f9697a1a33a23cea778fa39e5a3b50c07770a85f 100644 (file)
@@ -33,6 +33,12 @@ class Domain(sqlmodel.SQLModel, database.BackendMixin, table=True):
        # ID
        id: int = sqlmodel.Field(primary_key=True)
 
+       # List ID
+       list_id: int = sqlmodel.Field(foreign_key="lists.id")
+
+       # List
+       list: "List" = sqlmodel.Relationship()
+
        # Name
        name: str
 
index 8071c31bd6437ab4d4941b8b98d00d14ea8f9103..523fdb8eb44e6cdbc8dc2c63d930539cec10ec14 100644 (file)
@@ -206,24 +206,14 @@ class List(sqlmodel.SQLModel, database.BackendMixin, table=True):
                        .distinct(
                                domains.Domain.name,
                        )
-                       .select_from(
-                               sources.Source,
-                       )
-                       .join(
-                               domains.Domain,
-                               domains.Domain.source_id == sources.Source.id,
-                       )
                        .join(
                                checker.CheckerDomain,
                                checker.CheckerDomain.name == domains.Domain.name,
                                isouter=True,
                        )
                        .where(
-                               # Only select sources that belong to this list
-                               sources.Source.list_id == self.id,
-
-                               # Ignore deleted sources
-                               sources.Source.deleted_at == None,
+                               # Select only domains from this list
+                               domains.Domain.list == self,
 
                                # Ignore domains that have been removed
                                domains.Domain.removed_at == None,
index 43bea5fcb9109aa4590ed57b41ff57f5fe352b77..86c06ceaac244bf314ede2248d0e90e4a42a1d50 100644 (file)
@@ -171,6 +171,22 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
                self.deleted_at = sqlmodel.func.current_timestamp()
                self.deleted_by = deleted_by
 
+               # Mark all domains as removed
+               stmt = (
+                       sqlmodel
+                       .update(
+                               domains.Domain,
+                       )
+                       .where(
+                               domains.Domain.source_id == self.id,
+                               domains.Domain.removed_at == None,
+                       )
+                       .values(
+                               removed_at=sqlmodel.func.current_timestamp(),
+                       )
+               )
+               self.backend.db.execute(stmt)
+
                # Log action
                log.info(_("Source '%s' has been deleted from '%s'") % (self, self.list))
 
@@ -419,6 +435,7 @@ class Source(sqlmodel.SQLModel, database.BackendMixin, table=True):
                # Create a generator to format the values
                _domains = (
                        {
+                               "list_id"   : self.list.id,
                                "source_id" : self.id,
                                "name"      : domain,
                        }