diff --git a/resources/sql/autopatches/20180418.alamanc.interface.unique.php b/resources/sql/autopatches/20180418.alamanc.interface.unique.php new file mode 100644 index 0000000000..0ad4fbea12 --- /dev/null +++ b/resources/sql/autopatches/20180418.alamanc.interface.unique.php @@ -0,0 +1,85 @@ +establishConnection('w'); + +queryfx( + $interface_conn, + 'LOCK TABLES %T WRITE, %T WRITE', + $interface_table->getTableName(), + $binding_table->getTableName()); + +$seen = array(); +foreach (new LiskMigrationIterator($interface_table) as $interface) { + $device = $interface->getDevicePHID(); + $network = $interface->getNetworkPHID(); + $address = $interface->getAddress(); + $port = $interface->getPort(); + $key = "{$device}/{$network}/{$address}/{$port}"; + + // If this is the first copy of this row we've seen, mark it as seen and + // move on. + if (empty($seen[$key])) { + $seen[$key] = $interface->getID(); + continue; + } + + $survivor = queryfx_one( + $interface_conn, + 'SELECT * FROM %T WHERE id = %d', + $interface_table->getTableName(), + $seen[$key]); + + $bindings = queryfx_all( + $interface_conn, + 'SELECT * FROM %T WHERE interfacePHID = %s', + $binding_table->getTableName(), + $interface->getPHID()); + + // Repoint bindings to the survivor. + foreach ($bindings as $binding) { + // Check if there's already a binding to the survivor. + $existing = queryfx_one( + $interface_conn, + 'SELECT * FROM %T WHERE interfacePHID = %s and devicePHID = %s and '. + 'servicePHID = %s', + $binding_table->getTableName(), + $survivor['phid'], + $binding['devicePHID'], + $binding['servicePHID']); + + if (!$existing) { + // Reattach this binding to the survivor. + queryfx( + $interface_conn, + 'UPDATE %T SET interfacePHID = %s WHERE id = %d', + $binding_table->getTableName(), + $survivor['phid'], + $binding['id']); + } else { + // Binding to survivor already exists. Remove this now-redundant binding. + queryfx( + $interface_conn, + 'DELETE FROM %T WHERE id = %d', + $binding_table->getTableName(), + $binding['id']); + } + } + + queryfx( + $interface_conn, + 'DELETE FROM %T WHERE id = %d', + $interface_table->getTableName(), + $interface->getID()); +} + +queryfx( + $interface_conn, + 'ALTER TABLE %T ADD UNIQUE KEY `key_unique` '. + '(devicePHID, networkPHID, address, port)', + $interface_table->getTableName()); + +queryfx( + $interface_conn, + 'UNLOCK TABLES'); diff --git a/src/applications/almanac/editor/AlmanacInterfaceEditor.php b/src/applications/almanac/editor/AlmanacInterfaceEditor.php index 865402db36..771076efc9 100644 --- a/src/applications/almanac/editor/AlmanacInterfaceEditor.php +++ b/src/applications/almanac/editor/AlmanacInterfaceEditor.php @@ -1,18 +1,36 @@ true, self::CONFIG_COLUMN_SCHEMA => array( 'address' => 'text64', 'port' => 'uint32', ), self::CONFIG_KEY_SCHEMA => array( 'key_location' => array( 'columns' => array('networkPHID', 'address', 'port'), ), 'key_device' => array( 'columns' => array('devicePHID'), ), + 'key_unique' => array( + 'columns' => array('devicePHID', 'networkPHID', 'address', 'port'), + 'unique' => true, + ), ), ) + parent::getConfiguration(); } public function generatePHID() { return PhabricatorPHID::generateNewPHID( AlmanacInterfacePHIDType::TYPECONST); } public function getDevice() { return $this->assertAttached($this->device); } public function attachDevice(AlmanacDevice $device) { $this->device = $device; return $this; } public function getNetwork() { return $this->assertAttached($this->network); } public function attachNetwork(AlmanacNetwork $network) { $this->network = $network; return $this; } public function toAddress() { return AlmanacAddress::newFromParts( $this->getNetworkPHID(), $this->getAddress(), $this->getPort()); } public function getAddressHash() { return $this->toAddress()->toHash(); } public function renderDisplayAddress() { return $this->getAddress().':'.$this->getPort(); } public function loadIsInUse() { $binding = id(new AlmanacBindingQuery()) ->setViewer(PhabricatorUser::getOmnipotentUser()) ->withInterfacePHIDs(array($this->getPHID())) ->setLimit(1) ->executeOne(); return (bool)$binding; } /* -( PhabricatorPolicyInterface )----------------------------------------- */ public function getCapabilities() { return array( PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT, ); } public function getPolicy($capability) { return $this->getDevice()->getPolicy($capability); } public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { return $this->getDevice()->hasAutomaticCapability($capability, $viewer); } public function describeAutomaticCapability($capability) { $notes = array( pht('An interface inherits the policies of the device it belongs to.'), pht( 'You must be able to view the network an interface resides on to '. 'view the interface.'), ); return $notes; } /* -( PhabricatorExtendedPolicyInterface )--------------------------------- */ public function getExtendedPolicy($capability, PhabricatorUser $viewer) { switch ($capability) { case PhabricatorPolicyCapability::CAN_EDIT: if ($this->getDevice()->isClusterDevice()) { return array( array( new PhabricatorAlmanacApplication(), AlmanacManageClusterServicesCapability::CAPABILITY, ), ); } break; } return array(); } /* -( PhabricatorDestructibleInterface )----------------------------------- */ public function destroyObjectPermanently( PhabricatorDestructionEngine $engine) { $bindings = id(new AlmanacBindingQuery()) ->setViewer($engine->getViewer()) ->withInterfacePHIDs(array($this->getPHID())) ->execute(); foreach ($bindings as $binding) { $engine->destroyObject($binding); } $this->delete(); } /* -( PhabricatorApplicationTransactionInterface )------------------------- */ public function getApplicationTransactionEditor() { return new AlmanacInterfaceEditor(); } public function getApplicationTransactionObject() { return $this; } public function getApplicationTransactionTemplate() { return new AlmanacInterfaceTransaction(); } public function willRenderTimeline( PhabricatorApplicationTransactionView $timeline, AphrontRequest $request) { return $timeline; } /* -( PhabricatorConduitResultInterface )---------------------------------- */ public function getFieldSpecificationsForConduit() { return array( id(new PhabricatorConduitSearchFieldSpecification()) ->setKey('devicePHID') ->setType('phid') ->setDescription(pht('The device the interface is on.')), id(new PhabricatorConduitSearchFieldSpecification()) ->setKey('networkPHID') ->setType('phid') ->setDescription(pht('The network the interface is part of.')), id(new PhabricatorConduitSearchFieldSpecification()) ->setKey('address') ->setType('string') ->setDescription(pht('The address of the interface.')), id(new PhabricatorConduitSearchFieldSpecification()) ->setKey('port') ->setType('int') ->setDescription(pht('The port number of the interface.')), ); } public function getFieldValuesForConduit() { return array( 'devicePHID' => $this->getDevicePHID(), 'networkPHID' => $this->getNetworkPHID(), 'address' => (string)$this->getAddress(), 'port' => (int)$this->getPort(), ); } public function getConduitSearchAttachments() { return array(); } }