diff --git a/resources/sql/autopatches/20140722.audit.4.migtext.php b/resources/sql/autopatches/20140722.audit.4.migtext.php new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20140722.audit.4.migtext.php @@ -0,0 +1,61 @@ +<?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 comment text to modern storage...\n"; +foreach ($rows as $row) { + $id = $row['id']; + echo "Migrating Audit comment {$id}...\n"; + if (!strlen($row['content'])) { + echo "Comment has no text, continuing.\n"; + continue; + } + + $xaction_phid = PhabricatorPHID::generateNewPHID( + PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST, + PhabricatorRepositoryCommitPHIDType::TYPECONST); + + $comment_phid = PhabricatorPHID::generateNewPHID( + PhabricatorPHIDConstants::PHID_TYPE_XCMT, + PhabricatorRepositoryCommitPHIDType::TYPECONST); + + queryfx( + $conn_w, + 'INSERT IGNORE INTO %T + (phid, transactionPHID, authorPHID, viewPolicy, editPolicy, + commentVersion, content, contentSource, isDeleted, + dateCreated, dateModified, commitPHID, pathID, + legacyCommentID) + VALUES (%s, %s, %s, %s, %s, + %d, %s, %s, %d, + %d, %d, %s, %nd, + %d)', + 'audit_transaction_comment', + + // phid, transactionPHID, authorPHID, viewPolicy, editPolicy + $comment_phid, + $xaction_phid, + $row['actorPHID'], + 'public', + $row['actorPHID'], + + // commentVersion, content, contentSource, isDeleted + 1, + $row['content'], + $content_source, + 0, + + // dateCreated, dateModified, commitPHID, pathID, legacyCommentID + $row['dateCreated'], + $row['dateModified'], + $row['targetPHID'], + null, + $row['id']); +} + +echo "Done.\n"; diff --git a/src/applications/audit/storage/PhabricatorAuditComment.php b/src/applications/audit/storage/PhabricatorAuditComment.php --- a/src/applications/audit/storage/PhabricatorAuditComment.php +++ b/src/applications/audit/storage/PhabricatorAuditComment.php @@ -12,16 +12,40 @@ protected $actorPHID; protected $targetPHID; protected $action; - protected $content; + protected $content = ''; protected $metadata = array(); + private $proxyComment; + public static function loadComments( PhabricatorUser $viewer, $commit_phid) { - return id(new PhabricatorAuditComment())->loadAllWhere( + $comments = id(new PhabricatorAuditComment())->loadAllWhere( 'targetPHID = %s', $commit_phid); + + if ($comments) { + $table = new PhabricatorAuditTransactionComment(); + $conn_r = $table->establishConnection('r'); + + $data = queryfx_all( + $conn_r, + 'SELECT * FROM %T WHERE legacyCommentID IN (%Ld) AND pathID IS NULL', + $table->getTableName(), + mpull($comments, 'getID')); + $texts = $table->loadAllFromArray($data); + $texts = mpull($texts, null, 'getLegacyCommentID'); + + foreach ($comments as $comment) { + $text = idx($texts, $comment->getID()); + if ($text) { + $comment->setProxyComment($text); + } + } + } + + return $comments; } public function getConfiguration() { @@ -38,6 +62,70 @@ } + public function getContent() { + return $this->getProxyComment()->getContent(); + } + + public function setContent($content) { + // NOTE: We no longer read this field, but there's no cost to continuing + // to write it in case something goes horribly wrong, since it makes it + // far easier to back out of this. + $this->content = $content; + $this->getProxyComment()->setContent($content); + return $this; + } + + private function getProxyComment() { + if (!$this->proxyComment) { + $this->proxyComment = new PhabricatorAuditTransactionComment(); + } + return $this->proxyComment; + } + + public function setProxyComment(PhabricatorAuditTransactionComment $proxy) { + if ($this->proxyComment) { + throw new Exception(pht('You can not overwrite a proxy comment.')); + } + $this->proxyComment = $proxy; + return $this; + } + + public function setTargetPHID($target_phid) { + $this->getProxyComment()->setCommitPHID($target_phid); + return parent::setTargetPHID($target_phid); + } + + public function save() { + $this->openTransaction(); + $result = parent::save(); + + if (strlen($this->getContent())) { + $content_source = PhabricatorContentSource::newForSource( + PhabricatorContentSource::SOURCE_LEGACY, + array()); + + $xaction_phid = PhabricatorPHID::generateNewPHID( + PhabricatorApplicationTransactionTransactionPHIDType::TYPECONST, + PhabricatorRepositoryCommitPHIDType::TYPECONST); + + $proxy = $this->getProxyComment(); + $proxy + ->setAuthorPHID($this->getActorPHID()) + ->setViewPolicy('public') + ->setEditPolicy($this->getActorPHID()) + ->setContentSource($content_source) + ->setCommentVersion(1) + ->setLegacyCommentID($this->getID()) + ->setTransactionPHID($xaction_phid) + ->save(); + } + + $this->saveTransaction(); + + return $result; + } + + /* -( PhabricatorMarkupInterface Implementation )-------------------------- */ diff --git a/src/applications/audit/storage/PhabricatorAuditInlineComment.php b/src/applications/audit/storage/PhabricatorAuditInlineComment.php --- a/src/applications/audit/storage/PhabricatorAuditInlineComment.php +++ b/src/applications/audit/storage/PhabricatorAuditInlineComment.php @@ -33,7 +33,8 @@ $commit_phid) { $inlines = id(new PhabricatorAuditTransactionComment())->loadAllWhere( - 'authorPHID = %s AND commitPHID = %s AND transactionPHID IS NULL', + 'authorPHID = %s AND commitPHID = %s AND transactionPHID IS NULL + AND pathID IS NOT NULL', $viewer->getPHID(), $commit_phid); @@ -45,7 +46,8 @@ $commit_phid) { $inlines = id(new PhabricatorAuditTransactionComment())->loadAllWhere( - 'commitPHID = %s AND transactionPHID IS NOT NULL', + 'commitPHID = %s AND transactionPHID IS NOT NULL + AND pathID IS NOT NULL', $commit_phid); return self::buildProxies($inlines); @@ -58,7 +60,8 @@ if ($path_id === null) { $inlines = id(new PhabricatorAuditTransactionComment())->loadAllWhere( - 'commitPHID = %s AND (transactionPHID IS NOT NULL OR authorPHID = %s)', + 'commitPHID = %s AND (transactionPHID IS NOT NULL OR authorPHID = %s) + AND pathID IS NOT NULL', $commit_phid, $viewer->getPHID()); } else {