FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_2.0-2.1.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
24 function Migrate_20_21($DryRun)
25 {
26  // Check if uploadtree_a already inherits from uploadtree. If so, we are done.
27  $sql = "SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_inherits WHERE inhrelid = 'public.uploadtree_a'::regclass::oid);";
28  $row = RunSQL($sql, $DryRun);
30  foreach ($row as $exist_key => $exist_value) {
31  }
32 
33  if (@$exist_value == 't')
34  {
35  if ($DryRun) {
36  echo __FUNCTION__.": Data previously migrated.\n";
37  }
38  return 0; // migration has already happened
39  }
40 
41  // Is there data in uploadtree? If so then we need to migrate
42  $sql = "select uploadtree_pk from uploadtree limit 1";
43  $row = RunSQL($sql, $DryRun);
44  if (!empty($row))
45  {
46  echo "Migrating existing uploadtree data.\n";
47 
48  // drop uploadtree_a, it was put there by core schema for new installs only.
49  $sql = "drop table uploadtree_a";
50  RunSQL($sql, $DryRun);
51 
52  // rename uploadtree to uploadtree_a
53  $sql = "alter table uploadtree rename to uploadtree_a";
54  RunSQL($sql, $DryRun);
55 
56  // create new uploadtree table
57  $sql = "create table uploadtree (like uploadtree_a INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)";
58  RunSQL($sql, $DryRun);
59 
60  // Fix the foreign keys that moved when the table was renamed
61  $sql = "alter table uploadtree add foreign key (upload_fk) references upload(upload_pk) on delete cascade";
62  RunSQL($sql, $DryRun);
63  }
64 
65  // Fix the forieign keys removed when the table was renamed
66  $sql = "SELECT conname from pg_constraint where conname= 'uploadtree_a_upload_fk_fkey';";
67  $row = RunSQL($sql, $DryRun);
68  if (empty($row)) {
69  $sql = "alter table uploadtree_a add foreign key (upload_fk) references upload(upload_pk) on delete cascade";
70  RunSQL($sql, $DryRun);
71  }
72 
73  // fix uploadtree_tablename
74  $sql = "update upload set uploadtree_tablename='uploadtree_a' where uploadtree_tablename is null";
75  RunSQL($sql, $DryRun);
76 
77  // have uploadtreee_a inherit uploadtree
78  $sql = "alter table uploadtree_a inherit uploadtree";
79  RunSQL($sql, $DryRun);
80 
81  return 0; // success
82 } // Migrate_20_21
83 
92 function RunSQL($sql, $DryRun)
93 {
94  global $PG_CONN;
95  $row = '';
96 
97  if ($DryRun)
98  echo "DryRun: $sql\n";
99  else
100  {
101  $result = pg_query($PG_CONN, $sql);
102  DBCheckResult($result, $sql, __FILE__, __LINE__);
103  $row = pg_fetch_assoc($result);
104  pg_free_result($result);
105  }
106  return $row;
107 }
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Migrate_20_21($DryRun)
Migrate to the uploadtree_a table.
RunSQL($sql, $DryRun)
Run a SQL query and return the result array.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN