1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-28 00:10:57 +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'),
$lease->getResourceType());
$resource = id(new DrydockResourceQuery())
->setViewer($this->getViewer())
->withIDs(array($lease->getResourceID()))
->executeOne();
if ($resource !== null) {
$view->addProperty(
pht('Resource'),
$this->getViewer()->renderHandle($resource->getPHID()));
$resource_phid = $lease->getResourcePHID();
if ($resource_phid) {
$resource_display = $viewer->renderHandle($resource_phid);
} else {
$view->addProperty(
pht('Resource'),
pht('No Resource'));
$resource_display = phutil_tag('em', array(), pht('No Resource'));
}
$view->addProperty(pht('Resource'), $resource_display);
$until = $lease->getUntil();
if ($until) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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