1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-28 16:30:59 +01:00

Give Drydock leases a resourcePHID instead of a resourceID

Summary:
Ref T9252. Leases currently have a `resourceID`, but this is a bit nonstandard and generally less flexible than giving them a `resourcePHID`.

In particular, a `resourcePHID` is easier to use when rendering interfaces, since you can get handles out of a PHID.

Add a PHID column, copy over all the PHIDs that correspond to existing IDs, then drop the ID column.

Test Plan:
  - Browsed web UIs.
  - Inspected database during/after migration.
  - Grepped for `resourceID`.
  - Allocated a new lease with `bin/drydock lease`.

Reviewers: chad, hach-que

Reviewed By: hach-que

Maniphest Tasks: T9252

Differential Revision: https://secure.phabricator.com/D14151
This commit is contained in:
epriestley 2015-09-24 04:19:27 -07:00
parent 309aadc595
commit c6aade4392
10 changed files with 39 additions and 44 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_drydock.drydock_lease
ADD resourcePHID VARBINARY(64);

View file

@ -0,0 +1,5 @@
UPDATE
{$NAMESPACE}_drydock.drydock_lease l,
{$NAMESPACE}_drydock.drydock_resource r
SET l.resourcePHID = r.phid
WHERE l.resourceID = r.id;

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_drydock.drydock_lease
DROP resourceID;

View file

@ -110,20 +110,13 @@ final class DrydockLeaseViewController extends DrydockLeaseController {
pht('Resource Type'), pht('Resource Type'),
$lease->getResourceType()); $lease->getResourceType());
$resource = id(new DrydockResourceQuery()) $resource_phid = $lease->getResourcePHID();
->setViewer($this->getViewer()) if ($resource_phid) {
->withIDs(array($lease->getResourceID())) $resource_display = $viewer->renderHandle($resource_phid);
->executeOne();
if ($resource !== null) {
$view->addProperty(
pht('Resource'),
$this->getViewer()->renderHandle($resource->getPHID()));
} else { } else {
$view->addProperty( $resource_display = phutil_tag('em', array(), pht('No Resource'));
pht('Resource'),
pht('No Resource'));
} }
$view->addProperty(pht('Resource'), $resource_display);
$until = $lease->getUntil(); $until = $lease->getUntil();
if ($until) { if ($until) {

View file

@ -27,7 +27,7 @@ final class DrydockResourceViewController extends DrydockResourceController {
$leases = id(new DrydockLeaseQuery()) $leases = id(new DrydockLeaseQuery())
->setViewer($viewer) ->setViewer($viewer)
->withResourceIDs(array($resource->getID())) ->withResourcePHIDs(array($resource->getPHID()))
->execute(); ->execute();
$lease_list = id(new DrydockLeaseListView()) $lease_list = id(new DrydockLeaseListView())

View file

@ -4,7 +4,7 @@ final class DrydockLeaseQuery extends DrydockQuery {
private $ids; private $ids;
private $phids; private $phids;
private $resourceIDs; private $resourcePHIDs;
private $statuses; private $statuses;
private $datasourceQuery; private $datasourceQuery;
private $needCommands; private $needCommands;
@ -19,8 +19,8 @@ final class DrydockLeaseQuery extends DrydockQuery {
return $this; return $this;
} }
public function withResourceIDs(array $ids) { public function withResourcePHIDs(array $phids) {
$this->resourceIDs = $ids; $this->resourcePHIDs = $phids;
return $this; return $this;
} }
@ -43,22 +43,24 @@ final class DrydockLeaseQuery extends DrydockQuery {
} }
protected function willFilterPage(array $leases) { protected function willFilterPage(array $leases) {
$resource_ids = array_filter(mpull($leases, 'getResourceID')); $resource_phids = array_filter(mpull($leases, 'getResourcePHID'));
if ($resource_ids) { if ($resource_phids) {
$resources = id(new DrydockResourceQuery()) $resources = id(new DrydockResourceQuery())
->setParentQuery($this) ->setParentQuery($this)
->setViewer($this->getViewer()) ->setViewer($this->getViewer())
->withIDs(array_unique($resource_ids)) ->withPHIDs(array_unique($resource_phids))
->execute(); ->execute();
$resources = mpull($resources, null, 'getPHID');
} else { } else {
$resources = array(); $resources = array();
} }
foreach ($leases as $key => $lease) { foreach ($leases as $key => $lease) {
$resource = null; $resource = null;
if ($lease->getResourceID()) { if ($lease->getResourcePHID()) {
$resource = idx($resources, $lease->getResourceID()); $resource = idx($resources, $lease->getResourcePHID());
if (!$resource) { if (!$resource) {
$this->didRejectResult($lease);
unset($leases[$key]); unset($leases[$key]);
continue; continue;
} }
@ -72,11 +74,11 @@ final class DrydockLeaseQuery extends DrydockQuery {
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = parent::buildWhereClauseParts($conn); $where = parent::buildWhereClauseParts($conn);
if ($this->resourceIDs !== null) { if ($this->resourcePHIDs !== null) {
$where[] = qsprintf( $where[] = qsprintf(
$conn, $conn,
'resourceID IN (%Ld)', 'resourcePHID IN (%Ls)',
$this->resourceIDs); $this->resourcePHIDs);
} }
if ($this->ids !== null) { if ($this->ids !== null) {

View file

@ -3,7 +3,7 @@
final class DrydockLease extends DrydockDAO final class DrydockLease extends DrydockDAO
implements PhabricatorPolicyInterface { implements PhabricatorPolicyInterface {
protected $resourceID; protected $resourcePHID;
protected $resourceType; protected $resourceType;
protected $until; protected $until;
protected $ownerPHID; protected $ownerPHID;
@ -64,13 +64,11 @@ final class DrydockLease extends DrydockDAO
'until' => 'epoch?', 'until' => 'epoch?',
'resourceType' => 'text128', 'resourceType' => 'text128',
'ownerPHID' => 'phid?', 'ownerPHID' => 'phid?',
'resourceID' => 'id?', 'resourcePHID' => 'phid?',
), ),
self::CONFIG_KEY_SCHEMA => array( self::CONFIG_KEY_SCHEMA => array(
'key_phid' => null, 'key_resource' => array(
'phid' => array( 'columns' => array('resourcePHID', 'status'),
'columns' => array('phid'),
'unique' => true,
), ),
), ),
) + parent::getConfiguration(); ) + parent::getConfiguration();
@ -219,7 +217,7 @@ final class DrydockLease extends DrydockDAO
$this->openTransaction(); $this->openTransaction();
$this $this
->setResourceID($resource->getID()) ->setResourcePHID($resource->getPHID())
->setStatus($new_status) ->setStatus($new_status)
->save(); ->save();

View file

@ -462,10 +462,10 @@ final class DrydockAllocatorWorker extends DrydockWorker {
'acquireLease()')); 'acquireLease()'));
} }
$lease_id = $lease->getResourceID(); $lease_phid = $lease->getResourcePHID();
$resource_id = $resource->getID(); $resource_phid = $resource->getPHID();
if ($lease_id !== $resource_id) { if ($lease_phid !== $resource_phid) {
// TODO: Destroy the lease? // TODO: Destroy the lease?
throw new Exception( throw new Exception(
pht( pht(

View file

@ -20,17 +20,10 @@ final class DrydockLeaseWorker extends DrydockWorker {
$actual_status)); $actual_status));
} }
$resource_id = $lease->getResourceID(); $resource = $lease->getResource();
$resource = id(new DrydockResourceQuery())
->setViewer($this->getViewer())
->withIDs(array($resource_id))
->executeOne();
if (!$resource) { if (!$resource) {
throw new PhabricatorWorkerPermanentFailureException( throw new PhabricatorWorkerPermanentFailureException(
pht( pht('Trying to activate lease with no resource.'));
'Trying to activate lease on invalid resource ("%s").',
$resource_id));
} }
$resource_status = $resource->getStatus(); $resource_status = $resource->getStatus();

View file

@ -72,7 +72,7 @@ final class DrydockResourceUpdateWorker extends DrydockWorker {
$leases = id(new DrydockLeaseQuery()) $leases = id(new DrydockLeaseQuery())
->setViewer($viewer) ->setViewer($viewer)
->withResourceIDs(array($resource->getID())) ->withResourcePHIDs(array($resource->getPHID()))
->withStatuses($statuses) ->withStatuses($statuses)
->execute(); ->execute();