Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14014578
D19388.id46372.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D19388.id46372.diff
View Options
diff --git a/resources/sql/autopatches/20180418.alamanc.interface.unique.php b/resources/sql/autopatches/20180418.alamanc.interface.unique.php
new file mode 100644
--- /dev/null
+++ b/resources/sql/autopatches/20180418.alamanc.interface.unique.php
@@ -0,0 +1,73 @@
+<?php
+
+$table = new AlmanacInterface();
+$conn = $table->establishConnection('w');
+
+queryfx(
+ $conn,
+ 'LOCK TABLES %T WRITE',
+ $table->getTableName());
+
+$seen = array();
+foreach (new LiskMigrationIterator($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;
+ }
+
+ // Find the interface that already has this key.
+ $survivor = id(new AlmanacInterfaceQuery())
+ ->setViewer(PhabricatorUser::getOmnipotentUser())
+ ->withIDs(array($seen[$key]))
+ ->executeOne();
+
+ // Find any bindings that reference the interface to be destroyed.
+ $bindings = id(new AlmanacBindingQuery())
+ ->setViewer(PhabricatorUser::getOmnipotentUser())
+ ->withInterfacePHIDs(array($interface->getPHID()))
+ ->execute();
+
+ // Repoint bindings to the survivor.
+ foreach ($bindings as $binding) {
+ // Check if there's already a binding for the survivor.
+ $existing = id(new AlmanacBindingQuery())
+ ->setViewer(PhabricatorUser::getOmnipotentUser())
+ ->withInterfacePHIDs(array($survivor->getPHID()))
+ ->withDevicePHIDs(array($binding->getDevicePHID()))
+ ->withServicePHIDs(array($binding->getServicePHID()))
+ ->execute();
+
+ if (!$existing) {
+ // Reattach this binding to the survivor.
+ $binding->attachInterface($survivor);
+ $binding->setInterfacePHID($survivor->getPHID());
+ $binding->save();
+ } else {
+ // Binding to survivor already exists. Remove this now-redundant binding.
+ id(new PhabricatorDestructionEngine())
+ ->destroyObject($binding);
+ }
+ }
+
+ // Now we can destroy the interface.
+ id(new PhabricatorDestructionEngine())
+ ->destroyObject($interface);
+}
+
+queryfx(
+ $conn,
+ 'ALTER TABLE %T ADD UNIQUE KEY `key_unique` '.
+ '(devicePHID, networkPHID, address, port)',
+ $table->getTableName());
+
+queryfx(
+ $conn,
+ 'UNLOCK TABLES');
diff --git a/src/applications/almanac/storage/AlmanacInterface.php b/src/applications/almanac/storage/AlmanacInterface.php
--- a/src/applications/almanac/storage/AlmanacInterface.php
+++ b/src/applications/almanac/storage/AlmanacInterface.php
@@ -35,6 +35,10 @@
'key_device' => array(
'columns' => array('devicePHID'),
),
+ 'key_unique' => array(
+ 'columns' => array('devicePHID', 'networkPHID', 'address', 'port'),
+ 'unique' => true,
+ ),
),
) + parent::getConfiguration();
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 4, 6:12 AM (2 w, 2 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6758177
Default Alt Text
D19388.id46372.diff (2 KB)
Attached To
Mode
D19388: Add unique constraint to AlmanacInterfaces
Attached
Detach File
Event Timeline
Log In to Comment