1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-19 16:58:48 +02:00
phorge-phorge/resources/sql/autopatches/20210802.legalpad_document_signature.02.phid-populate.php
James Brown a4948ec800 Add conduit endpoints for querying legalpad
Summary:
This diff adds conduit methods for searching for legalpad documents and signatures. This is very helpful for auditing who's actually signed a document. It also fixes the "contributorPHIDs" constraint in the existing search engine.

In order to expose legalpad signatures through Conduit, this adds a `phid` column to the `legalpad_documentsignature` table. It includes a migration (in the style of many previous phid-adding migrations) to actually populate the column.

Test Plan: We run this on my company's internal fork and it seems to work okay. I don't think any other conduit methods anywhere have tests (???), but if you can point me at one I'm glad to write a unit test!

Reviewers: O1 Blessed Committers, speck

Reviewed By: O1 Blessed Committers, speck

Subscribers: 20after4, speck, tobiaswiese

Differential Revision: https://we.phorge.it/D25018
2021-09-04 13:53:13 -04:00

79 lines
1.7 KiB
PHP

<?php
$phid_type = PhabricatorLegalpadDocumentSignaturePHIDType::TYPECONST;
$docsig_table = new LegalpadDocumentSignature();
$conn = $docsig_table->establishConnection('w');
$table_name = $docsig_table->getTableName();
$chunk_size = 4096;
$temporary_table = 'tmp_20210802_docsig_id_map';
try {
queryfx(
$conn,
'CREATE TEMPORARY TABLE %T (
docsig_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
docsig_phid VARBINARY(64) NOT NULL)',
$temporary_table);
} catch (AphrontAccessDeniedQueryException $ex) {
throw new PhutilProxyException(
pht(
'Failed to "CREATE TEMPORARY TABLE". You may need to "GRANT" the '.
'current MySQL user this permission.'),
$ex);
}
$table_iterator = id(new LiskRawMigrationIterator($conn, $table_name))
->setPageSize($chunk_size);
$chunk_iterator = new PhutilChunkedIterator($table_iterator, $chunk_size);
foreach ($chunk_iterator as $chunk) {
$map = array();
foreach ($chunk as $docsig_row) {
$phid = $docsig_row['phid'];
if (strlen($phid)) {
continue;
}
$phid = PhabricatorPHID::generateNewPHID($phid_type);
$id = $docsig_row['id'];
$map[(int)$id] = $phid;
}
if (!$map) {
continue;
}
$sql = array();
foreach ($map as $docsig_id => $docsig_phid) {
$sql[] = qsprintf(
$conn,
'(%d, %s)',
$docsig_id,
$docsig_phid);
}
queryfx(
$conn,
'TRUNCATE TABLE %T',
$temporary_table);
queryfx(
$conn,
'INSERT INTO %T (docsig_id, docsig_phid) VALUES %LQ',
$temporary_table,
$sql);
queryfx(
$conn,
'UPDATE %T c JOIN %T x ON c.id = x.docsig_id
SET c.phid = x.docsig_phid',
$table_name,
$temporary_table);
}