FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
dbmigrate_3.6-3.7.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2019 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
35 function moveCandidateLicenseMap($dbManager, $obPk, $verbose)
36 {
37  if($dbManager == NULL){
38  echo "No connection object passed!\n";
39  return false;
40  }
41 
42  $sql = "SELECT rf_fk FROM obligation_map WHERE rf_fk NOT IN (" .
43  "SELECT rf_pk FROM ONLY license_ref) AND ob_fk = $1;";
44  $statement = __METHOD__ . ".getLicenseList";
45  $licenses = $dbManager->getRows($sql, array($obPk), $statement);
46  foreach ($licenses as $license) {
47  $dbManager->begin();
48  if ($verbose) {
49  echo "* Moving license " . $license['rf_fk'] . " to candidate map of " .
50  "obligation $obPk *\n";
51  }
52  $sql = "SELECT om_pk FROM obligation_candidate_map WHERE " .
53  "ob_fk = $1 AND rf_fk = $2;";
54  $statement = __METHOD__ . ".checkMaping";
55  $exists = $dbManager->getSingleRow($sql, array($obPk, $license['rf_fk']),
56  $statement);
57  if (empty($exists)) {
58  $statement = __METHOD__ . ".insertCandidateMap";
59  $dbManager->insertTableRow("obligation_candidate_map", array(
60  "ob_fk" => $obPk,
61  "rf_fk" => $license['rf_fk']
62  ), $statement);
63  }
64 
65  $sql = "DELETE FROM obligation_map WHERE ob_fk = $1 AND rf_fk = $2;";
66  $statement = __METHOD__ . ".removeMap";
67  $dbManager->getSingleRow($sql, array($obPk, $license['rf_fk']), $statement);
68  $dbManager->commit();
69  }
70 }
71 
77 function checkMigrate3637Required($dbManager)
78 {
79  if($dbManager == NULL){
80  echo "No connection object passed!\n";
81  return false;
82  }
83  $requiredTables = array(
84  "obligation_map",
85  "obligation_candidate_map",
86  "license_ref"
87  );
88  $migRequired = true;
89  foreach ($requiredTables as $table) {
90  if (DB_TableExists($table) != 1) {
91  $migRequired = false;
92  break;
93  }
94  }
95  if ($migRequired) {
96  $sql = "SELECT count(*) AS cnt FROM obligation_map WHERE rf_fk NOT IN (" .
97  "SELECT rf_pk FROM ONLY license_ref);";
98  $row = $dbManager->getSingleRow($sql);
99  $migRequired = false;
100  if (array_key_exists("cnt", $row) && $row["cnt"] > 0) {
101  $migRequired = true;
102  }
103  }
104 
105  return $migRequired;
106 }
107 
113 function moveObligation($dbManager, $verbose)
114 {
115  if($dbManager == NULL){
116  echo "No connection object passed!\n";
117  return false;
118  }
119 
120  $sql = "SELECT ob_pk FROM obligation_ref;";
121  $obligations = $dbManager->getRows($sql);
122  foreach ($obligations as $obligation) {
123  moveCandidateLicenseMap($dbManager, $obligation['ob_pk'], $verbose);
124  }
125 }
126 
133 {
134  if($dbManager == NULL){
135  echo "No connection object passed!\n";
136  return false;
137  }
138  if (DB_TableExists("upload_reuse") != 1) {
139  return false;
140  }
141  $stmt = __METHOD__;
142  $sql = "SELECT exists(SELECT 1 FROM upload_reuse WHERE reuse_mode = $1 LIMIT 1)::int";
143  $row = $dbManager->getSingleRow($sql, array(8), $stmt);
144 
145  if ($row['exists']) {
146  echo "*** Changing enhance reuse with main license value to 6 from 8 ***\n";
147  $stmt = __METHOD__."ReplaceValuesFrom8to6";
148  $dbManager->prepare($stmt,
149  "UPDATE upload_reuse
150  SET reuse_mode = $1 WHERE reuse_mode = $2"
151  );
152  $dbManager->freeResult($dbManager->execute($stmt, array(6,8)));
153  return true;
154  }
155  return false;
156 }
162 function Migrate_36_37($dbManager, $verbose)
163 {
165  if (! checkMigrate3637Required($dbManager)) {
166  // Migration not required
167  return;
168  }
169  try {
170  echo "*** Moving candidate licenses from obligation map ***\n";
171  moveObligation($dbManager, $verbose);
172  } catch (Exception $e) {
173  echo "Something went wrong. Try running postinstall again!\n";
174  $dbManager->rollback();
175  }
176 }
migrateReuseValueForEnhanceWithMainLicense($dbManager)
Migrate_36_37($dbManager, $verbose)
moveObligation($dbManager, $verbose)
Get all obligations and move licenses.
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:225
moveCandidateLicenseMap($dbManager, $obPk, $verbose)
Move licenses from obligation map to obligation candidate map.
checkMigrate3637Required($dbManager)