Skip to content

Commit dad61c2

Browse files
committed
update as suggested by xavier
revert
1 parent 8b6d8c7 commit dad61c2

File tree

1 file changed

+87
-72
lines changed

1 file changed

+87
-72
lines changed

tools/single_use/SaveUserIDToInstrumentData.php

Lines changed: 87 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,103 @@
2626
}
2727

2828
echo "\n\nQuerying data...\n";
29-
$db = \NDB_Factory::singleton()->database();
30-
// Query CommentIDs with DECS 'Complete', and
31-
// userID for latest changes made to the instrument data
32-
$history = $db->pselect(
33-
"SELECT h1.tbl, h1.primaryVals AS CommentID, h1.userID
34-
FROM history h1
35-
INNER JOIN
29+
30+
$db = \NDB_Factory::singleton()->database();
31+
32+
foreach(\Utility::getAllInstruments() as $table => $name) {
33+
// Query entries in the history table for the latest change
34+
// made to the Data column of the flag table, for every CommentID.
35+
$history = $db->pselect(
36+
"SELECT h1.primaryVals AS CommentID, h1.userID, JSON_MERGE_PATCH(
37+
f.Data, CONCAT('{\"UserID\": \"', h1.userID, '\"}')
38+
) AS new_Data
39+
FROM $table
40+
LEFT JOIN flag f USING (CommentID)
41+
LEFT JOIN history h1 ON (f.CommentID=h1.primaryVals)
42+
LEFT JOIN
3643
(
3744
SELECT primaryVals, MAX(changeDate) AS max_date
3845
FROM history
39-
WHERE primaryVals IN (
40-
SELECT primaryVals
41-
FROM history
42-
WHERE col = 'Data_entry_completion_status'
43-
AND primaryCols = 'CommentID'
44-
AND new = 'Complete'
45-
)
46+
WHERE tbl = 'flag'
47+
AND col = 'Data'
48+
AND userID <> 'unknown'
4649
GROUP BY primaryVals
47-
) h2
48-
ON h1.primaryVals = h2.primaryVals
50+
) h2 USING (primaryVals)
51+
WHERE h1.tbl = 'flag'
52+
AND h1.col = 'Data'
53+
AND h1.userID <> 'unknown'
4954
AND h1.changeDate = h2.max_date
50-
WHERE userID <> 'unknown'
51-
AND tbl <> 'flag'
52-
AND col <> 'CommentID'
53-
GROUP BY h1.primaryVals, h1.changeDate, h1.userID",
54-
array()
55-
);
56-
if (empty($history)) {
57-
echo "\n\nThere is no data to import.\n";
58-
die();
59-
}
60-
echo "\n\nThe following data can be imported into the instrument tables:\n";
61-
foreach ($history as $index => $row) {
62-
$result = $index + 1;
63-
echo "\n\nResult $result:\n";
64-
print_r($row);
65-
66-
$table = $row['tbl'];
67-
$commentID = $row['CommentID'];
68-
$userID = $row['userID'];
69-
70-
// Extract old data from flag so we can update it with merge so that we
71-
// don't overwrite it
72-
$oldData = $db->pselectOne(
73-
"SELECT Data FROM flag WHERE CommentID=:cid",
74-
array('cid' => $commentID)
75-
);
76-
echo "\n\nOld Data: \n";
77-
print_r($oldData);
78-
79-
// (The PDO driver seems to return null as "null" for JSON column types)
80-
if (!empty($oldData) && $oldData !== "null") {
81-
$oldData = json_decode($oldData, true);
55+
AND $table.UserID IS NULL",
56+
array()
57+
);
58+
if (empty($history)) {
59+
echo "\n\nThere is no data to import into $table.\n";
60+
continue;
8261
} else {
83-
$oldData = array();
62+
echo "\n\nThe following data can be imported into $table:\n";
63+
foreach ($history as $index => $row) {
64+
$result = $index + 1;
65+
echo "\n\nResult $result:\n";
66+
print_r($row);
67+
}
8468
}
85-
$newData = array_merge(
86-
$oldData ?? array(),
87-
array('UserID' => $userID)
88-
);
89-
echo "\n\nNew Data: \n";
90-
print_r(json_encode($newData));
9169

70+
// Update the intsrument table
9271
if ($confirm) {
93-
echo "\n\nImporting data into respective instrument tables and commentID flag entries...\n";
94-
// Update instrument table
95-
$db->update(
96-
$table,
97-
array('UserID' => $userID),
98-
array('CommentID' => $commentID)
99-
);
72+
echo "\n\nImporting data into $table...\n";
73+
$table_query = "UPDATE $table
74+
LEFT JOIN flag f USING (CommentID)
75+
LEFT JOIN history h1 ON (f.CommentID=h1.primaryVals)
76+
LEFT JOIN
77+
(
78+
SELECT primaryVals, MAX(changeDate) AS max_date
79+
FROM history
80+
WHERE tbl = 'flag'
81+
AND col = 'Data'
82+
AND userID <> 'unknown'
83+
GROUP BY primaryVals
84+
) h2 USING (primaryVals)
85+
SET $table.UserID = h1.userID
86+
WHERE h1.tbl = 'flag'
87+
AND h1.col = 'Data'
88+
AND h1.userID <> 'unknown'
89+
AND h1.changeDate = h2.max_date
90+
AND $table.UserID IS NULL";
10091

101-
// Save the JSON to the flag.Data column.
102-
//
103-
// json_encode ensures that this is safe. If we use the safe wrapper,
104-
// HTML encoding the quotation marks will make it invalid JSON.
105-
$db->unsafeUpdate(
106-
"flag",
107-
array("Data" => json_encode($newData)),
108-
array('CommentID' => $commentID)
109-
);
110-
echo "\n\nDone. $userID saved as UserID in $table and flag tables for CommentID $commentID.\n";
92+
$flag_query = "UPDATE flag f
93+
LEFT JOIN $table USING (CommentID)
94+
LEFT JOIN history h1 ON (f.CommentID=h1.primaryVals)
95+
LEFT JOIN
96+
(
97+
SELECT primaryVals, MAX(changeDate) AS max_date
98+
FROM history
99+
WHERE tbl = 'flag'
100+
AND col = 'Data'
101+
AND userID <> 'unknown'
102+
GROUP BY primaryVals
103+
) h2 USING (primaryVals)
104+
SET f.Data = JSON_MERGE_PATCH(
105+
f.Data, CONCAT('{\"UserID\": \"', h1.userID, '\"}')
106+
)
107+
WHERE h1.tbl = 'flag'
108+
AND h1.col = 'Data'
109+
AND h1.userID <> 'unknown'
110+
AND h1.changeDate = h2.max_date
111+
AND $table.UserID IS NULL";
112+
113+
$stmt_table = $db->prepare($table_query);
114+
$stmt_flag = $db->prepare($flag_query);
115+
try {
116+
$db->beginTransaction();
117+
$stmt_table->execute();
118+
$stmt_table->execute();
119+
$db->commit();
120+
echo "\n\nData import done for $table.\n";
121+
} catch (Exception $e) {
122+
$db->rollBack();
123+
$print("$table was not updated.");
124+
$print($e->getMessage());
125+
}
111126
}
112127
}
113128

0 commit comments

Comments
 (0)