Changeset View
Standalone View
resources/sql/autopatches/20140725.audit.1.migxactions.php
- This file was added.
<?php | |||||
$conn_w = id(new PhabricatorAuditTransaction())->establishConnection('w'); | |||||
$rows = new LiskRawMigrationIterator($conn_w, 'audit_comment'); | |||||
$content_source = PhabricatorContentSource::newForSource( | |||||
PhabricatorContentSource::SOURCE_LEGACY, | |||||
array())->serialize(); | |||||
echo "Migrating Audit comments to modern storage...\n"; | |||||
foreach ($rows as $row) { | |||||
$id = $row['id']; | |||||
echo "Migrating comment {$id}...\n"; | |||||
$comments = queryfx_all( | |||||
$conn_w, | |||||
'SELECT * FROM %T WHERE legacyCommentID = %d', | |||||
'audit_transaction_comment', | |||||
$id); | |||||
$main_comments = array(); | |||||
$inline_comments = array(); | |||||
foreach ($comments as $comment) { | |||||
if ($comment['pathID']) { | |||||
$inline_comments[] = $comment; | |||||
} else { | |||||
$main_comments[] = $comment; | |||||
} | |||||
} | |||||
$metadata = json_decode($row['metadata'], true); | |||||
if (!is_array($metadata)) { | |||||
joshuaspence: Maybe use `phutil_json_decode`? (Although we wouldn't expect the field to contain invalid JSON) | |||||
$metadata = array(); | |||||
} | |||||
$xactions = array(); | |||||
// Build the main action transaction. | |||||
switch ($row['action']) { | |||||
case PhabricatorAuditActionConstants::ADD_AUDITORS: | |||||
$phids = idx($metadata, 'added-auditors', array()); | |||||
$xactions[] = array( | |||||
Not Done Inline ActionsI was thinking you should replace 'added-auditors' with PhabricatorAuditComment::METADATA_ADDED_AUDITORS... although I guess you can't do this because PhabricatorAuditComment is going away joshuaspence: I was thinking you should replace `'added-auditors'` with `PhabricatorAuditComment… | |||||
Not Done Inline ActionsYeah, this is the reasoning -- the constant is getting wiped out soon. epriestley: Yeah, this is the reasoning -- the constant is getting wiped out soon. | |||||
'type' => $row['action'], | |||||
'old' => null, | |||||
'new' => array_fuse($phids), | |||||
); | |||||
break; | |||||
Not Done Inline ActionsShould we do this if $phids is an empty array? joshuaspence: Should we do this if `$phids` is an empty array? | |||||
Not Done Inline ActionsFor this and the array_fuse() below, I have to do followups for Auditors (possibly moving it to edges) and CCs (definitely moving it to subscriptions), so these are kind of temporary states for the data until that can happen. In cases like this where it's slightly simpler not to make an adjustment for now, like this, I've tried to keep this migration as simple as possible (e.g., the rest of this migration is maybe easier to reason about if $xaction always exists, and debugging migration issues might also be easier). The eventual followup can make adjustments like stripping empty rows. In the array_fuse() case below, I think fusing made something a little easier for now. This will definitely move to subscriptions so the data format should be temporary. epriestley: For this and the `array_fuse()` below, I have to do followups for Auditors (possibly moving it… | |||||
case PhabricatorAuditActionConstants::ADD_CCS: | |||||
$phids = idx($metadata, 'added-ccs', array()); | |||||
$xactions[] = array( | |||||
'type' => $row['action'], | |||||
'old' => null, | |||||
'new' => array_fuse($phids), | |||||
); | |||||
Not Done Inline ActionsIs array_fuse necessary? joshuaspence: Is `array_fuse` necessary? | |||||
break; | |||||
case PhabricatorAuditActionConstants::COMMENT: | |||||
case PhabricatorAuditActionConstants::INLINE: | |||||
// These actions will have their transactions created by other rules. | |||||
break; | |||||
default: | |||||
// Otherwise, this is an accept/concern/etc action. | |||||
$xactions[] = array( | |||||
'type' => PhabricatorAuditActionConstants::ACTION, | |||||
'old' => null, | |||||
'new' => $row['action'], | |||||
); | |||||
break; | |||||
} | |||||
// Build the main comment transaction. | |||||
foreach ($main_comments as $main) { | |||||
$xactions[] = array( | |||||
Not Done Inline ActionsI'd probably prefer $comment as the iterator variable, but meh. joshuaspence: I'd probably prefer `$comment` as the iterator variable, but meh. | |||||
'type' => PhabricatorTransactions::TYPE_COMMENT, | |||||
'old' => null, | |||||
'new' => null, | |||||
'phid' => $main['transactionPHID'], | |||||
'comment' => $main, | |||||
); | |||||
} | |||||
// Build inline comment transactions. | |||||
foreach ($inline_comments as $inline) { | |||||
$xactions[] = array( | |||||
'type' => PhabricatorAuditActionConstants::INLINE, | |||||
'old' => null, | |||||
'new' => null, | |||||
'phid' => $inline['transactionPHID'], | |||||
'comment' => $inline, | |||||
); | |||||
} | |||||
foreach ($xactions as $xaction) { | |||||
// Generate a new PHID, if we don't already have one from the comment | |||||
// table. We pregenerated into the comment table to make this a little | |||||
// easier, so we only need to write to one table. | |||||
$xaction_phid = idx($xaction, 'phid'); | |||||
if (!$xaction_phid) { | |||||
$xaction_phid = PhabricatorPHID::generateNewPHID( | |||||
PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST, | |||||
PhabricatorRepositoryCommitPHIDType::TYPECONST); | |||||
} | |||||
unset($xaction['phid']); | |||||
$comment_phid = null; | |||||
$comment_version = 0; | |||||
if (idx($xaction, 'comment')) { | |||||
$comment_phid = $xaction['comment']['phid']; | |||||
$comment_version = 1; | |||||
} | |||||
$old = idx($xaction, 'old'); | |||||
$new = idx($xaction, 'new'); | |||||
$meta = idx($xaction, 'meta', array()); | |||||
queryfx( | |||||
$conn_w, | |||||
'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy, | |||||
commentPHID, commentVersion, transactionType, oldValue, newValue, | |||||
contentSource, metadata, dateCreated, dateModified) | |||||
VALUES (%s, %s, %s, %s, %s, %ns, %d, %s, %ns, %ns, %s, %s, %d, %d)', | |||||
'audit_transaction', | |||||
// PHID, authorPHID, objectPHID | |||||
$xaction_phid, | |||||
$row['actorPHID'], | |||||
$row['targetPHID'], | |||||
Not Done Inline ActionsIs the cast necessary? joshuaspence: Is the cast necessary? | |||||
// viewPolicy, editPolicy, commentPHID, commentVersion | |||||
'public', | |||||
$row['actorPHID'], | |||||
$comment_phid, | |||||
Not Done Inline ActionsAs above joshuaspence: As above | |||||
Not Done Inline ActionsAh, no, these aren't necessary. The only risky value here is null. These casts came from the other migration, where author/actor was nullable for bizarre legacy reasons. epriestley: Ah, no, these aren't necessary. The only risky value here is `null`. These casts came from the… | |||||
$comment_version, | |||||
// transactionType, oldValue, newValue, contentSource, metadata | |||||
$xaction['type'], | |||||
json_encode($old), | |||||
json_encode($new), | |||||
$content_source, | |||||
json_encode($meta), | |||||
// dates | |||||
$row['dateCreated'], | |||||
$row['dateModified']); | |||||
} | |||||
} | |||||
echo "Done.\n"; |
Maybe use phutil_json_decode? (Although we wouldn't expect the field to contain invalid JSON)