Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97254057
20180418.almanac.network.unique.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jan 3, 19:55
Size
955 B
Mime Type
text/x-php
Expires
Sun, Jan 5, 19:55 (2 d)
Engine
blob
Format
Raw Data
Handle
23365795
Attached To
rPH Phabricator
20180418.almanac.network.unique.php
View Options
<?php
$table
=
new
AlmanacNetwork
();
$conn
=
$table
->
establishConnection
(
'w'
);
queryfx
(
$conn
,
'LOCK TABLES %T WRITE'
,
$table
->
getTableName
());
$seen
=
array
();
foreach
(
new
LiskMigrationIterator
(
$table
)
as
$network
)
{
$name
=
$network
->
getName
();
// If this is the first copy of this row we've seen, mark it as seen and
// move on.
if
(
empty
(
$seen
[
$name
]))
{
$seen
[
$name
]
=
1
;
continue
;
}
// Otherwise, rename this row.
while
(
true
)
{
$new_name
=
$name
.
'-'
.
$seen
[
$name
];
if
(
empty
(
$seen
[
$new_name
]))
{
$network
->
setName
(
$new_name
);
try
{
$network
->
save
();
break
;
}
catch
(
AphrontDuplicateKeyQueryException
$ex
)
{
// New name is a dupe of a network we haven't seen yet.
}
}
$seen
[
$name
]++;
}
$seen
[
$new_name
]
=
1
;
}
queryfx
(
$conn
,
'ALTER TABLE %T ADD UNIQUE KEY `key_name` (name)'
,
$table
->
getTableName
());
queryfx
(
$conn
,
'UNLOCK TABLES'
);
Event Timeline
Log In to Comment