Odyssey
cleanup-test-records.php
1 <?php
2 /**
3  * Run this script after running generate-loans.php to create random test loans.
4  * This script will clean up all the records you created. Basically it will
5  * DELETE all loans with loanid >= 1000 (local DB only affected.)
6  *
7  */
8 
9 // Needed from the existing system
10 require_once(__DIR__ . '/../../../../../app/web/shared/library/hcuCommon.i');
11 require_once(__DIR__ . '/../../../../../app/web/shared/library/db.postgres.i');
12 require_once(__DIR__ . '/../../../../../app/web/shared/library/hcuEnv.i');
13 
14 $sysenv = LoadSystemEnv("eforms");
15 $dbh = GetDBH($sysenv['db']);
16 
17 /**********************************************************/
18 if (! UserInputOkay()) {
19  echo "Exiting program." . PHP_EOL . PHP_EOL;
20  exit;
21 }
22 
23 try {
24  $ids = GetTestLoans($dbh);
25 } catch (Exception $e) {
26  echo "Exception thrown: {$e->getMessage()}" . PHP_EOL;
27 }
28 
29 foreach ($ids as $loanid) {
30 
31  try {
32  $userid = GetUserId($dbh, $loanid);
33  echo "deleting loan id $loanid user $userid" . PHP_EOL;
34  DeleteUserRecords($dbh, $userid);
35  DeleteLoanRecords($dbh, $loanid);
36 
37  } catch (Exception $e) {
38  echo "Exception thrown: {$e->getMessage()}" . PHP_EOL;
39  break;
40  }
41 }
42 
43 echo PHP_EOL . "All done. Type 'php generate-loans.php' to generate test loan records." . PHP_EOL . PHP_EOL;
44 
45 /**********************************************************/
46 
47 /**
48  * Wait for user input.
49  * @return bool
50  */
51 function UserInputOkay()
52 {
53  $line = null;
54 
55  while ($line !== 'y') {
56 
57  if ($line == 'q') {
58  return false;
59  }
60 
61  echo "
62  Local test loan records cleanup script.
63  This will DELETE any local loan records and associated user records
64  with a loan ID >= 1000. Enter 'y' to continue (not 'Y' or yes, 'y'.)
65  Enter 'q' to exit without changes.
66  ";
67 
68  $handle = fopen ("php://stdin","r");
69  $line = trim(fgets($handle));
70  fclose($handle);
71 
72  }
73 
74  return true;
75 }
76 
77 /**
78  * Get an array of all test loans with ID >= 1000.
79  * @param object $db
80  * @throws Exception
81  * @return array
82  */
83 function GetTestLoans($db)
84 {
85  $test_loans = 1000;
86  $loan_ids = [];
87  $sql = "select loanid from lnappuserresponse where loanid >= $1";
88  $result = pg_query_params($db, $sql, [$test_loans]);
89 
90  if (! $result) {
91  throw new Exception("Exception getting test loans: " . pg_last_error());
92  }
93 
94  $res = pg_fetch_all($result);
95 
96  for ($i = 0; $i < count($res); $i++) {
97  $loan_ids[] = $res[$i]['loanid'];
98  }
99 
100  return $loan_ids;
101 }
102 
103 /**
104  * Get the user id for the current loan in the loop.
105  * @param object $db
106  * @param int $loanid
107  * @throws Exception
108  * @return int
109  */
110 function GetUserId($db, $loanid)
111 {
112  $sql = "select userid from lnappuserresponse where loanid = $1";
113  $result = pg_query_params($db, $sql, [$loanid]);
114 
115  if (! $result) {
116  throw new Exception("Exception getting iser id for loan id $loanid: " . pg_last_error());
117  }
118 
119  $res = pg_fetch_row($result);
120 
121  return (isset($res[0]))? $res[0] : 0;
122 }
123 
124 /**
125  * Delete test user records.
126  * @param object $db
127  * @param int $userid
128  * @throws Exception
129  * @return void
130  */
131 function DeleteUserRecords($db, $userid)
132 {
133  $sql = "delete from lnappuser_questselect where userid = $1";
134  if (! pg_query_params($db, $sql, [$userid])) {
135  throw new Exception("Could not delete loan app question select: " . pg_last_error());
136  }
137 
138  $sql = "delete from lnappuser where userid = $1";
139  if (! pg_query_params($db, $sql, [$userid])) {
140  throw new Exception("Could not delete loan app user record: " . pg_last_error());
141  }
142 }
143 
144 /**
145  * Delete all the loan data for the current test record.
146  * @param object $db
147  * @param int $loanid
148  * @throws Exception
149  * @return void
150  */
151 function DeleteLoanRecords($db, $loanid)
152 {
153  foreach (['lnappschemadetail', 'lnappschemamaster', 'lnappuserresponse'] as $table) {
154 
155  $sql = "delete from $table where loanid = $1";
156 
157  if (! pg_query_params($db, $sql, [$loanid])) {
158  throw new Exception("Could not delete loan app record for table $table: " . pg_last_error());
159  }
160  }
161 }