FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_3.3-3.4.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2018 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
26 function cleanTableForeign($dbManager, $tableToClean, $foreignKey, $referenceTable, $referenceKey, $dryRun)
27 {
28  if($dbManager == NULL){
29  echo "No connection object passed!\n";
30  return false;
31  }
32  if(!(DB_TableExists($tableToClean) == 1 && DB_TableExists($referenceTable) == 1)) {
33  // Table does not exists (migrating from old version)
34  echo "Table $tableToClean or $referenceTable does not exists, not cleaning!\n";
35  return 0;
36  }
37 
38  $sql = "";
39  if($dryRun) {
40  $sql = "
41 SELECT count(*) AS count FROM $tableToClean
42 WHERE NOT EXISTS (
43  SELECT 1 FROM $referenceTable
44  WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey
45 );
46 ";
47  } else {
48  $sql = "
49 WITH deleted AS (
50  DELETE FROM $tableToClean
51  WHERE NOT EXISTS (
52  SELECT 1 FROM $referenceTable
53  WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey
54  ) RETURNING 1
55 ) SELECT count(*) AS count FROM deleted;
56 ";
57  }
58  return intval($dbManager->getSingleRow($sql, [],
59  "cleanTableForeign." . $tableToClean . $foreignKey . "." . $referenceTable . $referenceKey)['count']);
60 }
61 
73 function cleanWithUnique($dbManager, $tableName, $primaryKey, $columnNames, $dryRun)
74 {
75  if($dbManager == NULL){
76  echo "No connection object passed!\n";
77  return false;
78  }
79  if(DB_TableExists($tableName) != 1) {
80  // Table does not exists (migrating from old version)
81  echo "Table $tableName does not exists, not cleaning!\n";
82  return 0;
83  }
84 
85  $sql = "";
86  if($dryRun) {
87  $sql = "
88 SELECT count(*) AS count
89 FROM (
90  SELECT $primaryKey, ROW_NUMBER() OVER (
91  PARTITION BY " . implode(",", $columnNames) .
92  " ORDER BY $primaryKey
93  ) AS rnum
94  FROM $tableName
95 ) a
96 WHERE a.rnum > 1;
97 ";
98  } else {
99  $sql = "
100 WITH deleted AS (
101  DELETE FROM $tableName
102  WHERE $primaryKey IN (
103  SELECT $primaryKey
104  FROM (
105  SELECT $primaryKey, ROW_NUMBER() OVER (
106  PARTITION BY " . implode(",", $columnNames) .
107  " ORDER BY $primaryKey
108  ) AS rnum
109  FROM $tableName
110  ) a
111  WHERE a.rnum > 1
112  ) RETURNING 1
113 ) SELECT count(*) AS count FROM deleted;
114 ";
115  }
116  return intval($dbManager->getSingleRow($sql, [],
117  "cleanWithUnique." . $tableName . "." . implode(".", $columnNames))['count']);
118 }
119 
125 function Migrate_33_34($dbManager, $dryRun)
126 {
127  if(DB_ConstraintExists('group_user_member_user_group_ukey', $GLOBALS["SysConf"]["DBCONF"]["dbname"])) {
128  // The last constraint also cleared, no need for re-run
129  return;
130  }
131  try {
132  echo "*** Cleaning tables for new constraints ***\n";
133  $count = 0;
134  $tableMap = [
135  ["author", "agent_fk", "agent", "agent_pk"],
136  ["author", "pfile_fk", "pfile", "pfile_pk"],
137  ["bucket_container", "bucket_fk", "bucket_def", "bucket_pk"],
138  ["bucket_file", "bucket_fk", "bucket_def", "bucket_pk"],
139  ["bucket_file", "pfile_fk", "pfile", "pfile_pk"],
140  ["copyright", "agent_fk", "agent", "agent_pk"],
141  ["copyright_decision", "pfile_fk", "pfile", "pfile_pk"],
142  ["ecc", "agent_fk", "agent", "agent_pk"],
143  ["ecc", "pfile_fk", "pfile", "pfile_pk"],
144  ["ecc_decision", "pfile_fk", "pfile", "pfile_pk"],
145  ["highlight_keyword", "pfile_fk", "pfile", "pfile_pk"],
146  ["keyword", "agent_fk", "agent", "agent_pk"],
147  ["keyword", "pfile_fk", "pfile", "pfile_pk"],
148  ["keyword_decision", "pfile_fk", "pfile", "pfile_pk"],
149  ["pkg_deb_req", "pkg_fk", "pkg_deb", "pkg_pk"],
150  ["pkg_rpm_req", "pkg_fk", "pkg_rpm", "pkg_pk"],
151  ["report_cache", "report_cache_uploadfk", "upload", "upload_pk"],
152  ["report_info", "upload_fk", "upload", "upload_pk"],
153  ["reportgen", "upload_fk", "upload", "upload_pk"],
154  ["upload", "pfile_fk", "pfile", "pfile_pk"],
155  ["upload_clearing_license", "upload_fk", "upload", "upload_pk"]
156  ];
157  $dbManager->queryOnce("BEGIN;");
158 
159  // Foreign key constraints
160  foreach ($tableMap as $mapRow) {
161  $count += cleanTableForeign($dbManager, $mapRow[0], $mapRow[1], $mapRow[2], $mapRow[3], $dryRun);
162  }
163 
164  // Primary constraints
165  $count += cleanWithUnique($dbManager, "obligation_ref", "ctid", ["ob_pk"], $dryRun);
166  $count += cleanWithUnique($dbManager, "report_info", "ctid", ["ri_pk"], $dryRun);
167 
168  // Unique constraints
169  $count += cleanWithUnique($dbManager, "obligation_ref", "ob_pk", ["ob_md5"], $dryRun);
170  $count += cleanWithUnique($dbManager, "group_user_member", "group_user_member_pk",
171  ["user_fk", "group_fk"], $dryRun);
172  $dbManager->queryOnce("COMMIT;");
173  echo "Removed $count rows from tables with new constraints\n";
174  } catch (Exception $e) {
175  echo "Something went wrong. Try running postinstall again!\n";
176  $dbManager->queryOnce("ROLLBACK;");
177  }
178 }
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
Definition: common-db.php:264
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214
cleanWithUnique($dbManager, $tableName, $primaryKey, $columnNames, $dryRun)
Remove redundant rows based on values in columnNames.
Migrate_33_34($dbManager, $dryRun)
cleanTableForeign($dbManager, $tableToClean, $foreignKey, $referenceTable, $referenceKey, $dryRun)
Delete all rows from the table which does not have reference.