Path: blob/master/resources/sql/autopatches/20140212.dx.1.armageddon.php
12250 views
<?php12$conn_w = id(new DifferentialRevision())->establishConnection('w');3$rows = new LiskRawMigrationIterator($conn_w, 'differential_comment');45$content_source = PhabricatorContentSource::newForSource(6PhabricatorOldWorldContentSource::SOURCECONST)->serialize();78echo pht('Migrating Differential comments to modern storage...')."\n";9foreach ($rows as $row) {10$id = $row['id'];11echo pht('Migrating comment %d...', $id)."\n";1213$revision = id(new DifferentialRevision())->load($row['revisionID']);14if (!$revision) {15echo pht('No revision, continuing.')."\n";16continue;17}1819$revision_phid = $revision->getPHID();2021$comments = queryfx_all(22$conn_w,23'SELECT * FROM %T WHERE legacyCommentID = %d',24'differential_transaction_comment',25$id);2627$main_comments = array();28$inline_comments = array();2930foreach ($comments as $comment) {31if ($comment['changesetID']) {32$inline_comments[] = $comment;33} else {34$main_comments[] = $comment;35}36}3738$metadata = json_decode($row['metadata'], true);39if (!is_array($metadata)) {40$metadata = array();41}4243$key_cc = 'added-ccs';44$key_add_rev = 'added-reviewers';45$key_rem_rev = 'removed-reviewers';46$key_diff_id = 'diff-id';4748$xactions = array();4950// Build the main action transaction.51switch ($row['action']) {52case DifferentialAction::ACTION_COMMENT:53case DifferentialAction::ACTION_ADDREVIEWERS:54case DifferentialAction::ACTION_ADDCCS:55case DifferentialAction::ACTION_UPDATE:56case DifferentialTransaction::TYPE_INLINE:57// These actions will have their transactions created by other rules.58break;59default:60// Otherwise, this is a normal action (like an accept or reject).61$xactions[] = array(62'type' => DifferentialTransaction::TYPE_ACTION,63'old' => null,64'new' => $row['action'],65);66break;67}6869// Build the diff update transaction, if one exists.70$diff_id = idx($metadata, $key_diff_id);71if (!is_scalar($diff_id)) {72$diff_id = null;73}7475if ($diff_id || $row['action'] == DifferentialAction::ACTION_UPDATE) {76$xactions[] = array(77'type' => DifferentialRevisionUpdateTransaction::TRANSACTIONTYPE,78'old' => null,79'new' => $diff_id,80);81}8283// Build the add/remove reviewers transaction, if one exists.84$add_rev = idx($metadata, $key_add_rev, array());85if (!is_array($add_rev)) {86$add_rev = array();87}88$rem_rev = idx($metadata, $key_rem_rev, array());89if (!is_array($rem_rev)) {90$rem_rev = array();91}9293if ($add_rev || $rem_rev) {94$old = array();95foreach ($rem_rev as $phid) {96if (!is_scalar($phid)) {97continue;98}99$old[$phid] = array(100'src' => $revision_phid,101'type' => DifferentialRevisionHasReviewerEdgeType::EDGECONST,102'dst' => $phid,103);104}105106$new = array();107foreach ($add_rev as $phid) {108if (!is_scalar($phid)) {109continue;110}111$new[$phid] = array(112'src' => $revision_phid,113'type' => DifferentialRevisionHasReviewerEdgeType::EDGECONST,114'dst' => $phid,115);116}117118$xactions[] = array(119'type' => PhabricatorTransactions::TYPE_EDGE,120'old' => $old,121'new' => $new,122'meta' => array(123'edge:type' => DifferentialRevisionHasReviewerEdgeType::EDGECONST,124),125);126}127128// Build the CC transaction, if one exists.129$add_cc = idx($metadata, $key_cc, array());130if (!is_array($add_cc)) {131$add_cc = array();132}133134if ($add_cc) {135$xactions[] = array(136'type' => PhabricatorTransactions::TYPE_SUBSCRIBERS,137'old' => array(),138'new' => array_fuse($add_cc),139);140}141142143// Build the main comment transaction.144foreach ($main_comments as $main) {145$xactions[] = array(146'type' => PhabricatorTransactions::TYPE_COMMENT,147'old' => null,148'new' => null,149'phid' => $main['transactionPHID'],150'comment' => $main,151);152}153154// Build inline comment transactions.155foreach ($inline_comments as $inline) {156$xactions[] = array(157'type' => DifferentialTransaction::TYPE_INLINE,158'old' => null,159'new' => null,160'phid' => $inline['transactionPHID'],161'comment' => $inline,162);163}164165foreach ($xactions as $xaction) {166// Generate a new PHID, if we don't already have one from the comment167// table. We pregenerated into the comment table to make this a little168// easier, so we only need to write to one table.169$xaction_phid = idx($xaction, 'phid');170if (!$xaction_phid) {171$xaction_phid = PhabricatorPHID::generateNewPHID(172PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST,173DifferentialRevisionPHIDType::TYPECONST);174}175unset($xaction['phid']);176177$comment_phid = null;178$comment_version = 0;179if (idx($xaction, 'comment')) {180$comment_phid = $xaction['comment']['phid'];181$comment_version = 1;182}183184$old = idx($xaction, 'old');185$new = idx($xaction, 'new');186$meta = idx($xaction, 'meta', array());187188queryfx(189$conn_w,190'INSERT INTO %T (phid, authorPHID, objectPHID, viewPolicy, editPolicy,191commentPHID, commentVersion, transactionType, oldValue, newValue,192contentSource, metadata, dateCreated, dateModified)193VALUES (%s, %s, %s, %s, %s, %ns, %d, %s, %ns, %ns, %s, %s, %d, %d)',194'differential_transaction',195196// PHID, authorPHID, objectPHID197$xaction_phid,198(string)$row['authorPHID'],199$revision_phid,200201// viewPolicy, editPolicy, commentPHID, commentVersion202'public',203(string)$row['authorPHID'],204$comment_phid,205$comment_version,206207// transactionType, oldValue, newValue, contentSource, metadata208$xaction['type'],209json_encode($old),210json_encode($new),211$content_source,212json_encode($meta),213214// dates215$row['dateCreated'],216$row['dateModified']);217}218219}220echo pht('Done.')."\n";221222223