FOSSology  3.2.0rc1
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  Copyright (C) 2012-2014 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
35 function Migrate_20_21($DryRun)
36 {
37  // Check if uploadtree_a already inherits from uploadtree. If so, we are done.
38  $sql = "SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_inherits WHERE inhrelid = 'public.uploadtree_a'::regclass::oid);";
39  $row = RunSQL($sql, $DryRun);
41  foreach ($row as $exist_key => $exist_value) {
42  }
43 
44  if (@$exist_value == 't')
45  {
46  if ($DryRun) {
47  echo __FUNCTION__.": Data previously migrated.\n";
48  }
49  return 0; // migration has already happened
50  }
51 
52  // Is there data in uploadtree? If so then we need to migrate
53  $sql = "select uploadtree_pk from uploadtree limit 1";
54  $row = RunSQL($sql, $DryRun);
55  if (!empty($row))
56  {
57  echo "Migrating existing uploadtree data.\n";
58 
59  // drop uploadtree_a, it was put there by core schema for new installs only.
60  $sql = "drop table uploadtree_a";
61  RunSQL($sql, $DryRun);
62 
63  // rename uploadtree to uploadtree_a
64  $sql = "alter table uploadtree rename to uploadtree_a";
65  RunSQL($sql, $DryRun);
66 
67  // create new uploadtree table
68  $sql = "create table uploadtree (like uploadtree_a INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES)";
69  RunSQL($sql, $DryRun);
70 
71  // Fix the foreign keys that moved when the table was renamed
72  $sql = "alter table uploadtree add foreign key (upload_fk) references upload(upload_pk) on delete cascade";
73  RunSQL($sql, $DryRun);
74  }
75 
76  // Fix the forieign keys removed when the table was renamed
77  $sql = "SELECT conname from pg_constraint where conname= 'uploadtree_a_upload_fk_fkey';";
78  $row = RunSQL($sql, $DryRun);
79  if (empty($row)) {
80  $sql = "alter table uploadtree_a add foreign key (upload_fk) references upload(upload_pk) on delete cascade";
81  RunSQL($sql, $DryRun);
82  }
83 
84  // fix uploadtree_tablename
85  $sql = "update upload set uploadtree_tablename='uploadtree_a' where uploadtree_tablename is null";
86  RunSQL($sql, $DryRun);
87 
88  // have uploadtreee_a inherit uploadtree
89  $sql = "alter table uploadtree_a inherit uploadtree";
90  RunSQL($sql, $DryRun);
91 
92  return 0; // success
93 } // Migrate_20_21
94 
103 function RunSQL($sql, $DryRun)
104 {
105  global $PG_CONN;
106  $row = '';
107 
108  if ($DryRun)
109  echo "DryRun: $sql\n";
110  else
111  {
112  $result = pg_query($PG_CONN, $sql);
113  DBCheckResult($result, $sql, __FILE__, __LINE__);
114  $row = pg_fetch_assoc($result);
115  pg_free_result($result);
116  }
117  return $row;
118 }
119 
RunSQL($sql, $DryRun)
Run a SQL query and return the result array.
Migrate_20_21($DryRun)
Migrate to the uploadtree_a table.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198