26 function cleanTableForeign($dbManager, $tableToClean, $foreignKey, $referenceTable, $referenceKey, $dryRun)
28 if($dbManager == NULL){
29 echo
"No connection object passed!\n";
34 echo
"Table $tableToClean or $referenceTable does not exists, not cleaning!\n";
41 SELECT count(*) AS count FROM $tableToClean
43 SELECT 1 FROM $referenceTable
44 WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey
50 DELETE FROM $tableToClean
52 SELECT 1 FROM $referenceTable
53 WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey
55 ) SELECT count(*) AS count FROM deleted;
58 return intval($dbManager->getSingleRow($sql, [],
59 "cleanTableForeign." . $tableToClean . $foreignKey .
"." . $referenceTable . $referenceKey)[
'count']);
73 function cleanWithUnique($dbManager, $tableName, $primaryKey, $columnNames, $dryRun)
75 if($dbManager == NULL){
76 echo
"No connection object passed!\n";
81 echo
"Table $tableName does not exists, not cleaning!\n";
88 SELECT count(*) AS count
90 SELECT $primaryKey, ROW_NUMBER() OVER (
91 PARTITION BY " . implode(
",", $columnNames) .
92 " ORDER BY $primaryKey
101 DELETE FROM $tableName
102 WHERE $primaryKey IN (
105 SELECT $primaryKey, ROW_NUMBER() OVER (
106 PARTITION BY " . implode(
",", $columnNames) .
107 " ORDER BY $primaryKey
113 ) SELECT count(*) AS count FROM deleted;
116 return intval($dbManager->getSingleRow($sql, [],
117 "cleanWithUnique." . $tableName .
"." . implode(
".", $columnNames))[
'count']);
127 if(
DB_ConstraintExists(
'group_user_member_user_group_ukey', $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"])) {
132 echo
"*** Cleaning tables for new constraints ***\n";
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"]
157 $dbManager->queryOnce(
"BEGIN;");
160 foreach ($tableMap as $mapRow) {
161 $count +=
cleanTableForeign($dbManager, $mapRow[0], $mapRow[1], $mapRow[2], $mapRow[3], $dryRun);
165 $count +=
cleanWithUnique($dbManager,
"obligation_ref",
"ctid", [
"ob_pk"], $dryRun);
166 $count +=
cleanWithUnique($dbManager,
"report_info",
"ctid", [
"ri_pk"], $dryRun);
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;");
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
DB_TableExists($tableName)
Check if table exists.
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.