37 function cleanTableForeign($dbManager, $tableToClean, $foreignKey, $referenceTable, $referenceKey, $dryRun)
39 if($dbManager == NULL){
40 echo
"No connection object passed!\n";
45 echo
"Table $tableToClean or $referenceTable does not exists, not cleaning!\n";
52 SELECT count(*) AS count FROM $tableToClean 54 SELECT 1 FROM $referenceTable 55 WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey 61 DELETE FROM $tableToClean 63 SELECT 1 FROM $referenceTable 64 WHERE $tableToClean.$foreignKey = $referenceTable.$referenceKey 66 ) SELECT count(*) AS count FROM deleted; 69 return intval($dbManager->getSingleRow($sql, [],
70 "cleanTableForeign." . $tableToClean . $foreignKey .
"." . $referenceTable . $referenceKey)[
'count']);
84 function cleanWithUnique($dbManager, $tableName, $primaryKey, $columnNames, $dryRun)
86 if($dbManager == NULL){
87 echo
"No connection object passed!\n";
92 echo
"Table $tableName does not exists, not cleaning!\n";
99 SELECT count(*) AS count 101 SELECT $primaryKey, ROW_NUMBER() OVER ( 102 PARTITION BY " . implode(
",", $columnNames) .
103 " ORDER BY $primaryKey 112 DELETE FROM $tableName 113 WHERE $primaryKey IN ( 116 SELECT $primaryKey, ROW_NUMBER() OVER ( 117 PARTITION BY " . implode(
",", $columnNames) .
118 " ORDER BY $primaryKey 124 ) SELECT count(*) AS count FROM deleted; 127 return intval($dbManager->getSingleRow($sql, [],
128 "cleanWithUnique." . $tableName .
"." . implode(
".", $columnNames))[
'count']);
138 if(
DB_ConstraintExists(
'group_user_member_user_group_ukey', $GLOBALS[
"SysConf"][
"DBCONF"][
"dbname"])) {
143 echo
"*** Cleaning tables for new constraints ***\n";
146 [
"author",
"agent_fk",
"agent",
"agent_pk"],
147 [
"author",
"pfile_fk",
"pfile",
"pfile_pk"],
148 [
"bucket_container",
"bucket_fk",
"bucket_def",
"bucket_pk"],
149 [
"bucket_file",
"bucket_fk",
"bucket_def",
"bucket_pk"],
150 [
"bucket_file",
"pfile_fk",
"pfile",
"pfile_pk"],
151 [
"copyright",
"agent_fk",
"agent",
"agent_pk"],
152 [
"copyright_decision",
"pfile_fk",
"pfile",
"pfile_pk"],
153 [
"ecc",
"agent_fk",
"agent",
"agent_pk"],
154 [
"ecc",
"pfile_fk",
"pfile",
"pfile_pk"],
155 [
"ecc_decision",
"pfile_fk",
"pfile",
"pfile_pk"],
156 [
"highlight_keyword",
"pfile_fk",
"pfile",
"pfile_pk"],
157 [
"keyword",
"agent_fk",
"agent",
"agent_pk"],
158 [
"keyword",
"pfile_fk",
"pfile",
"pfile_pk"],
159 [
"keyword_decision",
"pfile_fk",
"pfile",
"pfile_pk"],
160 [
"pkg_deb_req",
"pkg_fk",
"pkg_deb",
"pkg_pk"],
161 [
"pkg_rpm_req",
"pkg_fk",
"pkg_rpm",
"pkg_pk"],
162 [
"report_cache",
"report_cache_uploadfk",
"upload",
"upload_pk"],
163 [
"report_info",
"upload_fk",
"upload",
"upload_pk"],
164 [
"reportgen",
"upload_fk",
"upload",
"upload_pk"],
165 [
"upload",
"pfile_fk",
"pfile",
"pfile_pk"],
166 [
"upload_clearing_license",
"upload_fk",
"upload",
"upload_pk"]
168 $dbManager->queryOnce(
"BEGIN;");
171 foreach ($tableMap as $mapRow) {
172 $count +=
cleanTableForeign($dbManager, $mapRow[0], $mapRow[1], $mapRow[2], $mapRow[3], $dryRun);
176 $count +=
cleanWithUnique($dbManager,
"obligation_ref",
"ctid", [
"ob_pk"], $dryRun);
177 $count +=
cleanWithUnique($dbManager,
"report_info",
"ctid", [
"ri_pk"], $dryRun);
180 $count +=
cleanWithUnique($dbManager,
"obligation_ref",
"ob_pk", [
"ob_md5"], $dryRun);
181 $count +=
cleanWithUnique($dbManager,
"group_user_member",
"group_user_member_pk",
182 [
"user_fk",
"group_fk"], $dryRun);
183 $dbManager->queryOnce(
"COMMIT;");
184 echo
"Removed $count rows from tables with new constraints\n";
185 }
catch (Exception $e) {
186 echo
"Something went wrong. Try running postinstall again!\n";
187 $dbManager->queryOnce(
"ROLLBACK;");
Migrate_33_34($dbManager, $dryRun)
cleanWithUnique($dbManager, $tableName, $primaryKey, $columnNames, $dryRun)
Remove redundant rows based on values in columnNames.
DB_TableExists($tableName)
Check if table exists.
DB_ConstraintExists($ConstraintName, $DBName='fossology')
Check if a constraint exists.
cleanTableForeign($dbManager, $tableToClean, $foreignKey, $referenceTable, $referenceKey, $dryRun)
Delete all rows from the table which does not have reference.