public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 2/2] database: use foreign keys for rule and object group attributes
Date: Thu, 22 Feb 2024 13:40:51 +0100	[thread overview]
Message-ID: <20240222124051.324714-3-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20240222124051.324714-1-s.ivanov@proxmox.com>

the change is small, reduces code and lets the task of referential
integrity to postgresql

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 src/PMG/Cluster.pm | 2 --
 src/PMG/DBTools.pm | 6 ++----
 src/PMG/RuleDB.pm  | 6 ------
 3 files changed, 2 insertions(+), 12 deletions(-)

diff --git a/src/PMG/Cluster.pm b/src/PMG/Cluster.pm
index f468618..17ba44d 100644
--- a/src/PMG/Cluster.pm
+++ b/src/PMG/Cluster.pm
@@ -529,14 +529,12 @@ sub sync_ruledb_from_master {
 
 	$ldb->do("DELETE FROM Rule");
 	$ldb->do("DELETE FROM RuleGroup");
 	$ldb->do("DELETE FROM ObjectGroup");
 	$ldb->do("DELETE FROM Object");
 	$ldb->do("DELETE FROM Attribut");
-	$ldb->do("DELETE FROM Objectgroup_Attributes");
-	$ldb->do("DELETE FROM Rule_Attributes");
 
 	eval {
 	    $rdb->begin_work;
 
 	    # read a consistent snapshot
 	    $rdb->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
diff --git a/src/PMG/DBTools.pm b/src/PMG/DBTools.pm
index 3e814dc..6112566 100644
--- a/src/PMG/DBTools.pm
+++ b/src/PMG/DBTools.pm
@@ -294,25 +294,25 @@ my $userprefs_ctablecmd =  <<__EOD;
     CREATE INDEX UserPrefs_MTime_Index ON UserPrefs (MTime);
 
 __EOD
 
 my $rule_attributes_cmd = <<__EOD;
     CREATE TABLE Rule_Attributes (
-      Rule_ID INTEGER NOT NULL,
+      Rule_ID INTEGER NOT NULL REFERENCES Rule (ID) ON DELETE CASCADE,
       Name VARCHAR(20) NOT NULL,
       Value BYTEA NULL,
       PRIMARY KEY (Rule_ID, Name)
     );
 
     CREATE INDEX Rule_Attributes_Rule_ID_Index ON Rule_Attributes(Rule_ID);
 
 __EOD
 
 my $object_group_attributes_cmd = <<__EOD;
     CREATE TABLE Objectgroup_Attributes (
-      Objectgroup_ID INTEGER NOT NULL,
+      Objectgroup_ID INTEGER NOT NULL REFERENCES Objectgroup (ID) ON DELETE CASCADE,
       Name VARCHAR(20) NOT NULL,
       Value BYTEA NULL,
       PRIMARY KEY (Objectgroup_ID, Name)
     );
 
     CREATE INDEX Objectgroup_Attributes_Objectgroup_ID_Index ON Objectgroup_Attributes(Objectgroup_ID);
@@ -632,14 +632,12 @@ sub init_ruledb {
 	my $glids = "SELECT object.ID FROM Object, Objectgroup WHERE " .
 	    "objectgroup_id = objectgroup.id and class = 'greylist'";
 
 	$dbh->do(
 	    "DELETE FROM Rule;"
 	    ." DELETE FROM RuleGroup;"
-	    ." DELETE FROM Rule_Attributes;"
-	    ." DELETE FROM Objectgroup_Attributes;"
 	    ." DELETE FROM Attribut WHERE Object_ID NOT IN ($glids);"
 	    ." DELETE FROM Object WHERE ID NOT IN ($glids);"
 	    ." DELETE FROM Objectgroup WHERE class != 'greylist';"
 	);
     }
 
diff --git a/src/PMG/RuleDB.pm b/src/PMG/RuleDB.pm
index e5fe56e..315f79b 100644
--- a/src/PMG/RuleDB.pm
+++ b/src/PMG/RuleDB.pm
@@ -273,15 +273,12 @@ sub delete_group {
 	$self->{dbh}->do("DELETE FROM ObjectGroup " .
 			 "WHERE ID = ?", undef, $groupid);
 
 	$self->{dbh}->do("DELETE FROM RuleGroup " .
 			 "WHERE Objectgroup_ID = ?", undef, $groupid);
 
-	$self->{dbh}->do("DELETE FROM Objectgroup_Attributes " .
-			 "WHERE Objectgroup_ID = ?", undef, $groupid);
-
 	$sth = $self->{dbh}->prepare("SELECT * FROM Object " .
 				      "where Objectgroup_ID = ?");
 	$sth->execute($groupid);
 
 	while (my $ref = $sth->fetchrow_hashref()) {
 	    $self->{dbh}->do("DELETE FROM Attribut " .
@@ -769,15 +766,12 @@ sub delete_rule {
 	$self->{dbh}->begin_work;
 
 	$self->{dbh}->do("DELETE FROM Rule " .
 			 "WHERE ID = ?", undef, $ruleid);
 	$self->{dbh}->do("DELETE FROM RuleGroup " .
 			 "WHERE Rule_ID = ?", undef, $ruleid);
-	$self->{dbh}->do("DELETE FROM Rule_Attributes " .
-			 "WHERE Rule_ID = ?", undef, $ruleid);
-
 	$self->{dbh}->commit;
     };
     if (my $err = $@) {
 	$self->{dbh}->rollback;
 	syslog('err', $err);
 	return undef;
-- 
2.39.2





  parent reply	other threads:[~2024-02-22 12:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-22 12:40 [pmg-devel] [PATCH pmg-api 0/2] fix table order on backup and add foreign key constraint for new tables Stoiko Ivanov
2024-02-22 12:40 ` [pmg-devel] [PATCH pmg-api 1/2] backup: reorder tables in order of dependency Stoiko Ivanov
2024-02-22 12:40 ` Stoiko Ivanov [this message]
2024-02-22 13:22 ` [pmg-devel] [PATCH pmg-api 0/2] fix table order on backup and add foreign key constraint for new tables Dominik Csapak
2024-02-22 13:29 ` [pmg-devel] applied: " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240222124051.324714-3-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal