FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_copyright-event.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2021 Siemens AG
4  Author: Shaheem Azmal M MD <shaheem.azmal@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
15 
20 const MAX_SIZE_OF_ROW = 100000;
21 
26 const TABLE_NAMES = array(
27  "copyright" => "copyright_event",
28  "author" => "author_event",
29  "ecc" => "ecc_event",
30  "keyword" => "keyword_event"
31 );
32 
38 function insertDataInToEventTables($dbManager)
39 {
40  if ($dbManager == NULL) {
41  echo "No connection object passed!\n";
42  return false;
43  }
44  foreach (TABLE_NAMES as $table => $tableEvent) {
45  $sql = "SELECT count(*) AS cnt FROM ";
46  $statement = __METHOD__ . ".getCountsFor";
47  $length = 0;
48  $row = $dbManager->getSingleRow($sql . $table ." AS cp
49  INNER JOIN uploadtree AS ut ON cp.pfile_fk = ut.pfile_fk
50  WHERE cp.is_enabled=false;", array(), $statement . $table);
51  $length = intval($row['cnt']);
52 
53  if (!empty($length)) {
54  echo "*** Inserting $length records from $table to $tableEvent table ***\n";
55  } else {
56  echo "*** Table $table already migrated to $tableEvent table ***\n";
57  continue;
58  }
59 
60  $tablePk = $table."_pk";
61  $tableFk = $table."_fk";
62  $i = 0;
63  $j = MAX_SIZE_OF_ROW;
64  $num = 0;
65  $statement = __METHOD__ . ".updateContentFor.$tableEvent";
66  $dbManager->queryOnce("DROP FUNCTION IF EXISTS migrate_".$table."_events_event(upperuploadtreeid int, loweruploadtreeid int)", $statement."drop");
67  $sql = "
68  CREATE OR REPLACE FUNCTION migrate_".$table."_events_event(upperuploadtreeid int, loweruploadtreeid int) RETURNS VOID AS
69  $$
70  BEGIN
71  INSERT INTO $tableEvent (upload_fk, $tableFk, uploadtree_fk)
72  SELECT upload_fk, $tablePk, uploadtree_pk FROM $table as cp
73  INNER JOIN uploadtree AS ut ON cp.pfile_fk = ut.pfile_fk
74  WHERE ut.uploadtree_pk >= loweruploadtreeid
75  AND ut.uploadtree_pk < upperuploadtreeid
76  AND cp.is_enabled=false
77  ORDER BY ut.uploadtree_pk;
78  END
79  $$
80  LANGUAGE 'plpgsql';";
81  $dbManager->queryOnce($sql, $statement.'plPGsqlfunction');
82  while ($num <= $length) {
83  $startTime = microtime(true);
84  $dbManager->begin();
85  $statementName = __METHOD__."insert from function".$table;
86  $dbManager->getSingleRow("SELECT 1 FROM migrate_".$table."_events_event($1, $2)", array($j, $i), $statementName);
87  $rows = $dbManager->getSingleRow("SELECT count(*) AS cnt, max(uploadtree_fk) AS uploadtree FROM $tableEvent", array(), $statement . $tableEvent. $table);
88  $num = intval($rows['cnt']);
89  if (!empty($rows['uploadtree'])) {
90  $i = $rows['uploadtree'];
91  } else {
92  $i = $j;
93  }
94  $j += MAX_SIZE_OF_ROW;
95  $dbManager->commit();
96  $endTime = microtime(true);
97  $totalTime = ($endTime - $startTime);
98  echo "Inserted $num / $length rows to $tableEvent table in ".gmdate("i", $totalTime)." minutes and ".gmdate("s", $totalTime)." seconds. \n";
99  }
100  $dbManager->begin();
101  $sqlTable = "UPDATE $table SET is_enabled=true WHERE is_enabled=false";
102  $dbManager->queryOnce($sqlTable, $statement."Update");
103  $dbManager->commit();
104  }
105 }
106 
112 function checkIfMigratePossible($dbManager)
113 {
114  if ($dbManager == NULL){
115  echo "No connection object passed!\n";
116  return false;
117  }
118 
119  $migPossible = true;
120  foreach (TABLE_NAMES as $table => $tableEvent) {
121  if (DB_TableExists($table) != 1) {
122  $migPossible = false;
123  break;
124  }
125  if (DB_TableExists($tableEvent) != 1) {
126  $migPossible = false;
127  break;
128  }
129  if (DB_ColExists($table, 'is_enabled') != 1) {
130  $migPossible = false;
131  break;
132  }
133  }
134  return $migPossible;
135 }
136 
141 {
142  if (! checkIfMigratePossible($dbManager)) {
143  // Migration not possible
144  return;
145  }
146  try {
147  insertDataInToEventTables($dbManager);
148  } catch (Exception $e) {
149  echo "Something went wrong. Try running postinstall again!\n";
150  $dbManager->rollback();
151  }
152 }
DB_ColExists($tableName, $colName, $DBName='fossology')
Check if a column exists.
Definition: common-db.php:240
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214