Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / resources / sql / autopatches / 20170915.ref.01.migrate.php
blob21102fa4ab469e89eae0622e9d81e03a7a855f32
1 <?php
3 $table = new PhabricatorRepositoryRefCursor();
4 $conn = $table->establishConnection('w');
6 $map = array();
7 foreach (new LiskMigrationIterator($table) as $ref) {
8 $repository_phid = $ref->getRepositoryPHID();
9 $ref_type = $ref->getRefType();
10 $ref_hash = $ref->getRefNameHash();
12 $ref_key = "{$repository_phid}/{$ref_type}/{$ref_hash}";
14 if (!isset($map[$ref_key])) {
15 $map[$ref_key] = array(
16 'id' => $ref->getID(),
17 'type' => $ref_type,
18 'hash' => $ref_hash,
19 'repositoryPHID' => $repository_phid,
20 'positions' => array(),
24 // NOTE: When this migration runs, the table will have "commitIdentifier" and
25 // "isClosed" fields. Later, it won't. Since they'll be removed, we can't
26 // rely on being able to access them via the object. Instead, run a separate
27 // raw query to read them.
29 $row = queryfx_one(
30 $conn,
31 'SELECT commitIdentifier, isClosed FROM %T WHERE id = %d',
32 $ref->getTableName(),
33 $ref->getID());
35 $map[$ref_key]['positions'][] = array(
36 'identifier' => $row['commitIdentifier'],
37 'isClosed' => (int)$row['isClosed'],
41 // Now, write all the position rows.
42 $position_table = new PhabricatorRepositoryRefPosition();
43 foreach ($map as $ref_key => $spec) {
44 $id = $spec['id'];
45 foreach ($spec['positions'] as $position) {
46 queryfx(
47 $conn,
48 'INSERT IGNORE INTO %T (cursorID, commitIdentifier, isClosed)
49 VALUES (%d, %s, %d)',
50 $position_table->getTableName(),
51 $id,
52 $position['identifier'],
53 $position['isClosed']);
57 // Finally, delete all the redundant RefCursor rows (rows with the same name)
58 // so we can add proper unique keys in the next migration.
59 foreach ($map as $ref_key => $spec) {
60 queryfx(
61 $conn,
62 'DELETE FROM %T WHERE refType = %s
63 AND refNameHash = %s
64 AND repositoryPHID = %s
65 AND id != %d',
66 $table->getTableName(),
67 $spec['type'],
68 $spec['hash'],
69 $spec['repositoryPHID'],
70 $spec['id']);