FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_4.0-4.1.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
15 
20 const PROCESS_TABLES = array(
21  "copyright",
22  "author",
23  "ecc",
24  "keyword",
25  "copyright_event",
26  "author_event",
27  "ecc_event",
28  "keyword_event"
29 );
30 
35 $GLOBALS['MIG_4041_TOTAL_RECORDS'] = 0;
36 
42 function checkMigrate4041Required($dbManager)
43 {
44  global $MIG_4041_TOTAL_RECORDS;
45  if ($dbManager == NULL) {
46  echo "No connection object passed!\n";
47  return false;
48  }
49 
50  $migRequired = true;
51  foreach (PROCESS_TABLES as $table) {
52  if (DB_TableExists($table) != 1) {
53  $migRequired = false;
54  break;
55  }
56  }
57  if ($migRequired) {
58  $count = 0;
59  foreach (PROCESS_TABLES as $table) {
60  $sql = "SELECT count(*) AS cnt FROM $table " .
61  "WHERE hash = 'd41d8cd98f00b204e9800998ecf8427e';"; // hash = md5('')
62  $result = $dbManager->getSingleRow($sql, [],
63  __METHOD__ . ".checkMig." . $table);
64  $count += intval($result['cnt']);
65  }
66  $MIG_4041_TOTAL_RECORDS = $count;
67  if ($count == 0) {
68  $migRequired = false;
69  }
70  }
71 
72  return $migRequired;
73 }
74 
80 function fixEmptyContentHash($dbManager)
81 {
82  global $MIG_4041_TOTAL_RECORDS;
83  $updated = 0;
84  foreach (PROCESS_TABLES as $table) {
85  $sql = "WITH temp_h AS (" .
86  "UPDATE $table SET " .
87  "content = NULL, hash = NULL " .
88  "WHERE (" .
89  "hash = 'd41d8cd98f00b204e9800998ecf8427e' OR content = ''" .
90  ") RETURNING 1 AS c) " .
91  "SELECT sum(c) AS cnt FROM temp_h;";
92  $statement = __METHOD__ . ".fixHash." . $table;
93  $dbManager->begin();
94  $result = $dbManager->getSingleRow($sql, [], $statement);
95  $dbManager->commit();
96  $updated += intval($result['cnt']);
97  }
98  echo "*** Corrected hash of $updated/$MIG_4041_TOTAL_RECORDS entries from " .
99  count(PROCESS_TABLES) . " tables ***\n";
100 }
101 
106 function Migrate_40_41($dbManager)
107 {
108  if (!checkMigrate4041Required($dbManager)) {
109  // Migration not required
110  return;
111  }
112  try {
113  fixEmptyContentHash($dbManager);
114  } catch (Exception $e) {
115  echo "Something went wrong. Try running postinstall again!\n";
116  $dbManager->rollback();
117  }
118 }
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214
const PROCESS_TABLES
fixEmptyContentHash($dbManager)
checkMigrate4041Required($dbManager)
Migrate_40_41($dbManager)