]> git.donarmstrong.com Git - debbugs.git/commitdiff
include function in instalsql for bin ver/src pkg linking
authorDon Armstrong <don@donarmstrong.com>
Sun, 19 Aug 2018 20:36:07 +0000 (13:36 -0700)
committerDon Armstrong <don@donarmstrong.com>
Sun, 19 Aug 2018 20:36:07 +0000 (13:36 -0700)
bin/debbugs-installsql

index 1ecccb48534799372ef9b5de4241ef2c0f9d7031..2cf023d5ae62b93b88e6b87ffb07eb2f9dccce1d 100755 (executable)
@@ -158,6 +158,59 @@ if ($options{current_version}) {
     exit 0;
 } elsif ($options{install}) {
     $dh->prepare_install;
+    $schema->storage->
+       dbh_do(sub {my ($s,$dbh) = @_;
+                   prepare_execute($dbh,<<'SQL');});
+CREATE OR REPLACE FUNCTION bin_ver_to_src_pkg(bin_ver INT) RETURNS INT
+  AS $src_pkg_from_bin_ver$
+  DECLARE
+  src_pkg int;
+  BEGIN
+       SELECT sv.src_pkg INTO STRICT src_pkg
+              FROM bin_ver bv JOIN src_ver sv ON bv.src_ver=sv.id
+              WHERE bv.id=bin_ver;
+       RETURN src_pkg;
+  END
+  $src_pkg_from_bin_ver$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION src_ver_to_src_pkg(src_ver INT) RETURNS INT
+  AS $src_ver_to_src_pkg$
+  DECLARE
+  src_pkg int;
+  BEGIN
+       SELECT sv.src_pkg INTO STRICT src_pkg
+              FROM src_ver sv WHERE sv.id=src_ver;
+       RETURN src_pkg;
+  END
+  $src_ver_to_src_pkg$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION update_bin_pkg_src_pkg_bin_ver () RETURNS TRIGGER
+  AS $update_bin_pkg_src_pkg_bin_ver$
+  DECLARE
+  src_ver_rows integer;
+  BEGIN
+  IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' )  THEN
+     -- if there is still a bin_ver with this src_pkg, then do nothing
+     PERFORM * FROM bin_ver bv JOIN src_ver sv ON bv.src_ver = sv.id
+           WHERE sv.id = OLD.src_ver LIMIT 2;
+     GET DIAGNOSTICS src_ver_rows = ROW_COUNT;
+     IF (src_ver_rows <= 1) THEN
+        DELETE FROM bin_pkg_src_pkg
+              WHERE bin_pkg=OLD.bin_pkg AND
+                    src_pkg=src_ver_to_src_pkg(OLD.src_ver);
+     END IF;
+  END IF;
+  IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
+     BEGIN
+     INSERT INTO bin_pkg_src_pkg (bin_pkg,src_pkg)
+       VALUES (NEW.bin_pkg,src_ver_to_src_pkg(NEW.src_ver))
+       ON CONFLICT (bin_pkg,src_pkg) DO NOTHING;
+     END;
+  END IF;
+  RETURN NULL;
+  END
+  $update_bin_pkg_src_pkg_bin_ver$ LANGUAGE plpgsql;
+SQL
     $dh->install;
     ## this is lame, but because the current release of DeploymentHandler does
     ## not support WHERE or quoted indexes properly (fixed in git), we create
@@ -165,6 +218,7 @@ if ($options{current_version}) {
     $schema->storage->
        dbh_do(sub{my ($s,$dbh) = @_;
                   prepare_execute($dbh,<<SQL);
+
 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
  bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
 CREATE UNIQUE INDEX bug_status_cache_bug_suite_idx ON