FOSSology  4.4.0
Open Source License Compliance by Open Source Software
dbmigrate_1.4-2.0.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
25 function Migrate_14_20($DryRun)
26 {
27  global $PG_CONN;
28 
29  /* array of agent_name, ars file name for tables to create */
30  $ARSarray = array("pkgagent" => "pkgagent_ars",
31  "copyright" => "copyright_ars",
32  "mimetype" => "mimetype_ars",
33  "unpack" => "ununpack_ars",
34  "ununpack" => "ununpack_ars");
35 
36  foreach($ARSarray as $agent_name => $ARStablename)
37  {
38  /* Create the ars table if it doesn't exist */
39  CreateARStable($ARStablename, $DryRun);
40 
41  /* Get the agent_pk */
42  $agent_pk = GetAgentKey($agent_name, "");
43 
44  /* Select the jobqueue records for this agent */
45  $sql = "select distinct job_upload_fk, jq_type, jq_starttime, jq_endtime from jobqueue
46  join job on jq_job_fk=job_pk where jq_type='$agent_name' and (jq_end_bits=1) order by jq_type";
47  $result = pg_query($PG_CONN, $sql);
48  DBCheckResult($result, $sql, __FILE__, __LINE__);
49 
50  /* Loop through jobqueue records inserting ars table rows */
51  while ($row = pg_fetch_assoc($result))
52  {
53  $upload_fk = $row['job_upload_fk'];
54 
55  /* prevent duplicate insert */
56  $sql = "select ars_pk from $ARStablename where agent_fk=$agent_pk and upload_fk=$upload_fk and ars_success=true";
57  $checkrec = pg_query($PG_CONN, $sql);
58  DBCheckResult($checkrec, $sql, __FILE__, __LINE__);
59  $num_rows = pg_num_rows($checkrec);
60  pg_free_result($checkrec);
61  if ($num_rows > 0) continue;
62 
63  /* add ars rec */
64  $sql = "insert into $ARStablename (agent_fk, upload_fk, ars_success, ars_starttime, ars_endtime)
65  values ($agent_pk, $upload_fk, true, '$row[jq_starttime]', '$row[jq_endtime]')";
66  if ($DryRun)
67  echo "DryRun: $sql\n";
68  else
69  {
70  $insresult = pg_query($PG_CONN, $sql);
71  DBCheckResult($insresult, $sql, __FILE__, __LINE__);
72  pg_free_result($insresult);
73  }
74  }
75  pg_free_result($result);
76  }
77 
78  return 0;
79 } // Migrate_14_20
80 
81 
89 function CreateARStable($ARStablename)
90 {
91  global $PG_CONN;
92 
93  if (DB_TableExists($ARStablename)) return;
94 
95  $sql = "CREATE TABLE $ARStablename () INHERITS (ars_master)";
96  $result = pg_query($PG_CONN, $sql);
97  DBCheckResult($result, $sql, __FILE__, __LINE__);
98 }
GetAgentKey($agentName, $agentDesc)
Get the latest enabled agent_pk for a given agent.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
DB_TableExists($tableName)
Check if table exists.
Definition: common-db.php:214
CreateARStable($ARStablename)
Create ars table.
Migrate_14_20($DryRun)
Create the new ars tables and populate them from the job/jobqueue data.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN