1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 05:50:55 +01:00

Make Images in Pholio refer to mocks by PHID instead of ID

Summary:
Ref T11351. In Pholio, we currently use a `mockID`, but a `mockPHID` is generally preferable / more modern / more flexible. In particular, we need PHIDs to load handles and prefer PHIDs when exposing information to the API, and using PHIDs internally makes a bunch of things easier/better/faster and ~nothing harder/worse/slower.

I'll add some inlines about a few things.

Test Plan: Ran migrations, spot-checked database for sanity. Loaded Pholio, saw data unchanged. Created and edited images.

Reviewers: amckinley

Reviewed By: amckinley

Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T11351

Differential Revision: https://secure.phabricator.com/D19914
This commit is contained in:
epriestley 2018-12-19 10:57:26 -08:00
parent 961fd7e849
commit 21f07bf6f7
10 changed files with 88 additions and 49 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_pholio.pholio_image
ADD mockPHID VARBINARY(64);

View file

@ -0,0 +1,35 @@
<?php
// Old images used a "mockID" instead of a "mockPHID" to reference mocks.
// Set the "mockPHID" column to the value that corresponds to the "mockID".
$image = new PholioImage();
$mock = new PholioMock();
$conn = $image->establishConnection('w');
$iterator = new LiskRawMigrationIterator($conn, $image->getTableName());
foreach ($iterator as $image_row) {
if ($image_row['mockPHID']) {
continue;
}
$mock_id = $image_row['mockID'];
$mock_row = queryfx_one(
$conn,
'SELECT phid FROM %R WHERE id = %d',
$mock,
$mock_id);
if (!$mock_row) {
continue;
}
queryfx(
$conn,
'UPDATE %R SET mockPHID = %s WHERE id = %d',
$image,
$mock_row['phid'],
$image_row['id']);
}

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_pholio.pholio_image
DROP mockID;

View file

@ -91,7 +91,7 @@ final class PholioMockEditor extends PhabricatorApplicationTransactionEditor {
$images = $this->getNewImages(); $images = $this->getNewImages();
foreach ($images as $image) { foreach ($images as $image) {
$image->setMockID($object->getID()); $image->setMockPHID($object->getPHID());
$image->save(); $image->save();
} }

View file

@ -65,7 +65,7 @@ final class PhabricatorPholioMockTestDataGenerator
->setActor($author) ->setActor($author)
->applyTransactions($mock, $transactions); ->applyTransactions($mock, $transactions);
foreach ($images as $image) { foreach ($images as $image) {
$image->setMockID($mock->getID()); $image->setMockPHID($mock->getPHID());
$image->save(); $image->save();
} }

View file

@ -32,13 +32,9 @@ final class PholioImagePHIDType extends PhabricatorPHIDType {
foreach ($handles as $phid => $handle) { foreach ($handles as $phid => $handle) {
$image = $objects[$phid]; $image = $objects[$phid];
$id = $image->getID(); $handle
$mock_id = $image->getMockID(); ->setName($image->getName())
$name = $image->getName(); ->setURI($image->getURI());
$handle->setURI("/M{$mock_id}/{$id}/");
$handle->setName($name);
$handle->setFullName($name);
} }
} }

View file

@ -5,8 +5,7 @@ final class PholioImageQuery
private $ids; private $ids;
private $phids; private $phids;
private $mockIDs; private $mockPHIDs;
private $obsolete;
private $needInlineComments; private $needInlineComments;
private $mockCache = array(); private $mockCache = array();
@ -21,13 +20,8 @@ final class PholioImageQuery
return $this; return $this;
} }
public function withMockIDs(array $mock_ids) { public function withMockPHIDs(array $mock_phids) {
$this->mockIDs = $mock_ids; $this->mockPHIDs = $mock_phids;
return $this;
}
public function withObsolete($obsolete) {
$this->obsolete = $obsolete;
return $this; return $this;
} }
@ -69,18 +63,11 @@ final class PholioImageQuery
$this->phids); $this->phids);
} }
if ($this->mockIDs !== null) { if ($this->mockPHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'mockID IN (%Ld)', 'mockPHID IN (%Ls)',
$this->mockIDs); $this->mockPHIDs);
}
if ($this->obsolete !== null) {
$where[] = qsprintf(
$conn,
'isObsolete = %d',
$this->obsolete);
} }
return $where; return $where;
@ -92,16 +79,18 @@ final class PholioImageQuery
if ($this->getMockCache()) { if ($this->getMockCache()) {
$mocks = $this->getMockCache(); $mocks = $this->getMockCache();
} else { } else {
$mock_ids = mpull($images, 'getMockID'); $mock_phids = mpull($images, 'getMockPHID');
// DO NOT set needImages to true; recursion results! // DO NOT set needImages to true; recursion results!
$mocks = id(new PholioMockQuery()) $mocks = id(new PholioMockQuery())
->setViewer($this->getViewer()) ->setViewer($this->getViewer())
->withIDs($mock_ids) ->withPHIDs($mock_phids)
->execute(); ->execute();
$mocks = mpull($mocks, null, 'getID'); $mocks = mpull($mocks, null, 'getPHID');
} }
foreach ($images as $index => $image) { foreach ($images as $index => $image) {
$mock = idx($mocks, $image->getMockID()); $mock = idx($mocks, $image->getMockPHID());
if ($mock) { if ($mock) {
$image->attachMock($mock); $image->attachMock($mock);
} else { } else {

View file

@ -115,18 +115,18 @@ final class PholioMockQuery
$need_inline_comments) { $need_inline_comments) {
assert_instances_of($mocks, 'PholioMock'); assert_instances_of($mocks, 'PholioMock');
$mock_map = mpull($mocks, null, 'getID'); $mock_map = mpull($mocks, null, 'getPHID');
$all_images = id(new PholioImageQuery()) $all_images = id(new PholioImageQuery())
->setViewer($viewer) ->setViewer($viewer)
->setMockCache($mock_map) ->setMockCache($mock_map)
->withMockIDs(array_keys($mock_map)) ->withMockPHIDs(array_keys($mock_map))
->needInlineComments($need_inline_comments) ->needInlineComments($need_inline_comments)
->execute(); ->execute();
$image_groups = mgroup($all_images, 'getMockID'); $image_groups = mgroup($all_images, 'getMockPHID');
foreach ($mocks as $mock) { foreach ($mocks as $mock) {
$mock_images = idx($image_groups, $mock->getID(), array()); $mock_images = idx($image_groups, $mock->getPHID(), array());
$mock->attachAllImages($mock_images); $mock->attachAllImages($mock_images);
$active_images = mfilter($mock_images, 'getIsObsolete', true); $active_images = mfilter($mock_images, 'getIsObsolete', true);
$mock->attachImages(msort($active_images, 'getSequence')); $mock->attachImages(msort($active_images, 'getSequence'));

View file

@ -6,7 +6,7 @@ final class PholioImage extends PholioDAO
PhabricatorExtendedPolicyInterface { PhabricatorExtendedPolicyInterface {
protected $authorPHID; protected $authorPHID;
protected $mockID; protected $mockPHID;
protected $filePHID; protected $filePHID;
protected $name; protected $name;
protected $description; protected $description;
@ -29,7 +29,7 @@ final class PholioImage extends PholioDAO
return array( return array(
self::CONFIG_AUX_PHID => true, self::CONFIG_AUX_PHID => true,
self::CONFIG_COLUMN_SCHEMA => array( self::CONFIG_COLUMN_SCHEMA => array(
'mockID' => 'id?', 'mockPHID' => 'phid?',
'name' => 'text128', 'name' => 'text128',
'description' => 'text', 'description' => 'text',
'sequence' => 'uint32', 'sequence' => 'uint32',
@ -37,14 +37,9 @@ final class PholioImage extends PholioDAO
'replacesImagePHID' => 'phid?', 'replacesImagePHID' => 'phid?',
), ),
self::CONFIG_KEY_SCHEMA => array( self::CONFIG_KEY_SCHEMA => array(
'key_phid' => null, // TODO: There should be a key starting with "mockPHID" here at a
'keyPHID' => array( // minimum, but it's not entirely clear what other columns we should
'columns' => array('phid'), // have as part of the key.
'unique' => true,
),
'mockID' => array(
'columns' => array('mockID', 'isObsolete', 'sequence'),
),
), ),
) + parent::getConfiguration(); ) + parent::getConfiguration();
} }
@ -71,6 +66,10 @@ final class PholioImage extends PholioDAO
return $this->assertAttached($this->mock); return $this->assertAttached($this->mock);
} }
public function hasMock() {
return (bool)$this->getMockPHID();
}
public function attachInlineComments(array $inline_comments) { public function attachInlineComments(array $inline_comments) {
assert_instances_of($inline_comments, 'PholioTransactionComment'); assert_instances_of($inline_comments, 'PholioTransactionComment');
$this->inlineComments = $inline_comments; $this->inlineComments = $inline_comments;
@ -82,6 +81,22 @@ final class PholioImage extends PholioDAO
return $this->inlineComments; return $this->inlineComments;
} }
public function getURI() {
if ($this->hasMock()) {
$mock = $this->getMock();
$mock_uri = $mock->getURI();
$image_id = $this->getID();
return "{$mock_uri}/{$image_id}/";
}
// For now, standalone images have no URI. We could provide one at some
// point, although it's not clear that there's any motivation to do so.
return null;
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */ /* -( PhabricatorPolicyInterface )----------------------------------------- */
@ -96,7 +111,7 @@ final class PholioImage extends PholioDAO
public function getPolicy($capability) { public function getPolicy($capability) {
// If the image is attached to a mock, we use an extended policy to match // If the image is attached to a mock, we use an extended policy to match
// the mock's permissions. // the mock's permissions.
if ($this->getMockID()) { if ($this->hasMock()) {
return PhabricatorPolicies::getMostOpenPolicy(); return PhabricatorPolicies::getMostOpenPolicy();
} }
@ -113,7 +128,7 @@ final class PholioImage extends PholioDAO
public function getExtendedPolicy($capability, PhabricatorUser $viewer) { public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
if ($this->getMockID()) { if ($this->hasMock()) {
return array( return array(
array( array(
$this->getMock(), $this->getMock(),

View file

@ -259,7 +259,7 @@ final class PholioMock extends PholioDAO
$this->openTransaction(); $this->openTransaction();
$images = id(new PholioImageQuery()) $images = id(new PholioImageQuery())
->setViewer($engine->getViewer()) ->setViewer($engine->getViewer())
->withMockIDs(array($this->getID())) ->withMockIDs(array($this->getPHID()))
->execute(); ->execute();
foreach ($images as $image) { foreach ($images as $image) {
$image->delete(); $image->delete();