|
| 1 | +<?php |
| 2 | + |
| 3 | +$conn_w = id(new DifferentialRevision())->establishConnection('w'); |
| 4 | +$rows = new LiskRawMigrationIterator($conn_w, 'differential_comment'); |
| 5 | + |
| 6 | +$content_source = PhabricatorContentSource::newForSource( |
| 7 | + PhabricatorContentSource::SOURCE_LEGACY, |
| 8 | + array())->serialize(); |
| 9 | + |
| 10 | +echo "Migrating Differential comments to modern storage...\n"; |
| 11 | +foreach ($rows as $row) { |
| 12 | + $id = $row['id']; |
| 13 | + echo "Migrating comment {$id}...\n"; |
| 14 | + |
| 15 | + $revision = id(new DifferentialRevision())->load($row['revisionID']); |
| 16 | + if (!$revision) { |
| 17 | + echo "No revision, continuing.\n"; |
| 18 | + continue; |
| 19 | + } |
| 20 | + |
| 21 | + $revision_phid = $revision->getPHID(); |
| 22 | + |
| 23 | + $comments = queryfx_all( |
| 24 | + $conn_w, |
| 25 | + 'SELECT * FROM %T WHERE legacyCommentID = %d', |
| 26 | + 'differential_transaction_comment', |
| 27 | + $id); |
| 28 | + |
| 29 | + $main_comments = array(); |
| 30 | + $inline_comments = array(); |
| 31 | + |
| 32 | + foreach ($comments as $comment) { |
| 33 | + if ($comment['changesetID']) { |
| 34 | + $inline_comments[] = $comment; |
| 35 | + } else { |
| 36 | + $main_comments[] = $comment; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + $metadata = json_decode($row['metadata'], true); |
| 41 | + if (!is_array($metadata)) { |
| 42 | + $metadata = array(); |
| 43 | + } |
| 44 | + |
| 45 | + $key_cc = DifferentialComment::METADATA_ADDED_CCS; |
| 46 | + $key_add_rev = DifferentialComment::METADATA_ADDED_REVIEWERS; |
| 47 | + $key_rem_rev = DifferentialComment::METADATA_REMOVED_REVIEWERS; |
| 48 | + $key_diff_id = DifferentialComment::METADATA_DIFF_ID; |
| 49 | + |
| 50 | + $xactions = array(); |
| 51 | + |
| 52 | + // Build the main action transaction. |
| 53 | + switch ($row['action']) { |
| 54 | + case DifferentialAction::ACTION_COMMENT: |
| 55 | + case DifferentialAction::ACTION_ADDREVIEWERS: |
| 56 | + case DifferentialAction::ACTION_ADDCCS: |
| 57 | + case DifferentialAction::ACTION_UPDATE: |
| 58 | + case DifferentialTransaction::TYPE_INLINE: |
| 59 | + // These actions will have their transactions created by other rules. |
| 60 | + break; |
| 61 | + default: |
| 62 | + // Otherwise, this is a normal action (like an accept or reject). |
| 63 | + $xactions[] = array( |
| 64 | + 'type' => DifferentialTransaction::TYPE_ACTION, |
| 65 | + 'old' => null, |
| 66 | + 'new' => $row['action'], |
| 67 | + ); |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + // Build the diff update transaction, if one exists. |
| 72 | + $diff_id = idx($metadata, $key_diff_id); |
| 73 | + if (!is_scalar($diff_id)) { |
| 74 | + $diff_id = null; |
| 75 | + } |
| 76 | + |
| 77 | + if ($diff_id || $row['action'] == DifferentialAction::ACTION_UPDATE) { |
| 78 | + $xactions[] = array( |
| 79 | + 'type' => DifferentialTransaction::TYPE_UPDATE, |
| 80 | + 'old' => null, |
| 81 | + 'new' => $diff_id, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // Build the add/remove reviewers transaction, if one exists. |
| 86 | + $add_rev = idx($metadata, $key_add_rev, array()); |
| 87 | + if (!is_array($add_rev)) { |
| 88 | + $add_rev = array(); |
| 89 | + } |
| 90 | + $rem_rev = idx($metadata, $key_rem_rev, array()); |
| 91 | + if (!is_array($rem_rev)) { |
| 92 | + $rem_rev = array(); |
| 93 | + } |
| 94 | + |
| 95 | + if ($add_rev || $rem_rev) { |
| 96 | + $old = array(); |
| 97 | + foreach ($rem_rev as $phid) { |
| 98 | + if (!is_scalar($phid)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + $old[$phid] = array( |
| 102 | + 'src' => $revision_phid, |
| 103 | + 'type' => PhabricatorEdgeConfig::TYPE_DREV_HAS_REVIEWER, |
| 104 | + 'dst' => $phid, |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + $new = array(); |
| 109 | + foreach ($add_rev as $phid) { |
| 110 | + if (!is_scalar($phid)) { |
| 111 | + continue; |
| 112 | + } |
| 113 | + $new[$phid] = array( |
| 114 | + 'src' => $revision_phid, |
| 115 | + 'type' => PhabricatorEdgeConfig::TYPE_DREV_HAS_REVIEWER, |
| 116 | + 'dst' => $phid, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + $xactions[] = array( |
| 121 | + 'type' => PhabricatorTransactions::TYPE_EDGE, |
| 122 | + 'old' => $old, |
| 123 | + 'new' => $new, |
| 124 | + 'meta' => array( |
| 125 | + 'edge:type' => PhabricatorEdgeConfig::TYPE_DREV_HAS_REVIEWER, |
| 126 | + ), |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + // Build the CC transaction, if one exists. |
| 131 | + $add_cc = idx($metadata, $key_cc, array()); |
| 132 | + if (!is_array($add_cc)) { |
| 133 | + $add_cc = array(); |
| 134 | + } |
| 135 | + |
| 136 | + if ($add_cc) { |
| 137 | + $xactions[] = array( |
| 138 | + 'type' => PhabricatorTransactions::TYPE_SUBSCRIBERS, |
| 139 | + 'old' => array(), |
| 140 | + 'new' => array_fuse($add_cc), |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + // Build the main comment transaction. |
| 146 | + foreach ($main_comments as $main) { |
| 147 | + $xactions[] = array( |
| 148 | + 'type' => PhabricatorTransactions::TYPE_COMMENT, |
| 149 | + 'old' => null, |
| 150 | + 'new' => null, |
| 151 | + 'phid' => $main['transactionPHID'], |
| 152 | + 'comment' => $main, |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + // Build inline comment transactions. |
| 157 | + foreach ($inline_comments as $inline) { |
| 158 | + $xactions[] = array( |
| 159 | + 'type' => DifferentialTransaction::TYPE_INLINE, |
| 160 | + 'old' => null, |
| 161 | + 'new' => null, |
| 162 | + 'phid' => $inline['transactionPHID'], |
| 163 | + 'comment' => $inline, |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + foreach ($xactions as $xaction) { |
| 168 | + // Generate a new PHID, if we don't already have one from the comment |
| 169 | + // table. We pregenerated into the comment table to make this a little |
| 170 | + // easier, so we only need to write to one table. |
| 171 | + $xaction_phid = idx($xaction, 'phid'); |
| 172 | + if (!$xaction_phid) { |
| 173 | + $xaction_phid = PhabricatorPHID::generateNewPHID( |
| 174 | + PhabricatorApplicationTransactionPHIDTypeTransaction::TYPECONST, |
| 175 | + DifferentialPHIDTypeRevision::TYPECONST); |
| 176 | + } |
| 177 | + unset($xaction['phid']); |
| 178 | + |
| 179 | + $comment_phid = null; |
| 180 | + $comment_version = 0; |
| 181 | + if (idx($xaction, 'comment')) { |
| 182 | + $comment_phid = $xaction['comment']['phid']; |
| 183 | + $comment_version = 1; |
| 184 | + } |
| 185 | + |
| 186 | + $old = idx($xaction, 'old'); |
| 187 | + $new = idx($xaction, 'new'); |
| 188 | + $meta = idx($xaction, 'meta', array()); |
| 189 | + |
| 190 | + queryfx( |
| 191 | + $conn_w, |
| 192 | + 'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy, |
| 193 | + commentPHID, commentVersion, transactionType, oldValue, newValue, |
| 194 | + contentSource, metadata, dateCreated, dateModified) |
| 195 | + VALUES (%s, %s, %s, %s, %s, %ns, %d, %s, %ns, %ns, %s, %s, %d, %d)', |
| 196 | + 'differential_transaction', |
| 197 | + |
| 198 | + // PHID, authorPHID, objectPHID |
| 199 | + $xaction_phid, |
| 200 | + (string)$row['authorPHID'], |
| 201 | + $revision_phid, |
| 202 | + |
| 203 | + // viewPolicy, editPolicy, commentPHID, commentVersion |
| 204 | + 'public', |
| 205 | + (string)$row['authorPHID'], |
| 206 | + $comment_phid, |
| 207 | + $comment_version, |
| 208 | + |
| 209 | + // transactionType, oldValue, newValue, contentSource, metadata |
| 210 | + $xaction['type'], |
| 211 | + json_encode($old), |
| 212 | + json_encode($new), |
| 213 | + $content_source, |
| 214 | + json_encode($meta), |
| 215 | + |
| 216 | + // dates |
| 217 | + $row['dateCreated'], |
| 218 | + $row['dateModified']); |
| 219 | + } |
| 220 | + |
| 221 | +} |
| 222 | +echo "Done.\n"; |
0 commit comments