Remove product literal strings in "pht()", part 21
[phabricator.git] / resources / sql / autopatches / 20180418.almanac.network.unique.php
blobc81c59823e1cb0f649ed898f95dc25095c74f3c5
1 <?php
3 $table = new AlmanacNetwork();
4 $conn = $table->establishConnection('w');
6 queryfx(
7 $conn,
8 'LOCK TABLES %T WRITE',
9 $table->getTableName());
11 $seen = array();
12 foreach (new LiskMigrationIterator($table) as $network) {
13 $name = $network->getName();
15 // If this is the first copy of this row we've seen, mark it as seen and
16 // move on.
17 if (empty($seen[$name])) {
18 $seen[$name] = 1;
19 continue;
22 // Otherwise, rename this row.
23 while (true) {
24 $new_name = $name.'-'.$seen[$name];
25 if (empty($seen[$new_name])) {
26 $network->setName($new_name);
27 try {
28 $network->save();
29 break;
30 } catch (AphrontDuplicateKeyQueryException $ex) {
31 // New name is a dupe of a network we haven't seen yet.
34 $seen[$name]++;
36 $seen[$new_name] = 1;
39 queryfx(
40 $conn,
41 'ALTER TABLE %T ADD UNIQUE KEY `key_name` (name)',
42 $table->getTableName());
44 queryfx(
45 $conn,
46 'UNLOCK TABLES');