Merge pull request #798 from nextcloud/migration-pre-a2.2

better migration
nickvergessen-patch-1
Maxence Lange 2019-10-14 10:37:15 -01:00 zatwierdzone przez GitHub
commit b6fcc46e11
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 67 dodań i 71 usunięć

Wyświetl plik

@ -32,21 +32,27 @@ namespace OCA\Social\Migration;
use Closure; use Closure;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Schema\SchemaException; use Doctrine\DBAL\Schema\SchemaException;
use Exception; use Exception;
use OCA\Social\Db\StreamDestRequest;
use OCA\Social\Db\StreamRequest;
use OCA\Social\Db\StreamTagsRequest;
use OCP\AppFramework\QueryException;
use OCP\DB\ISchemaWrapper; use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Migration\IOutput; use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep; use OCP\Migration\SimpleMigrationStep;
require_once __DIR__ . '/../../appinfo/autoload.php';
/** /**
* Class Version0002Date20190916000001 * Class Version0002Date20191010000001
* *
* @package OCA\Social\Migration * @package OCA\Social\Migration
*/ */
class Version0002Date20190916000001 extends SimpleMigrationStep { class Version0002Date20191010000001 extends SimpleMigrationStep {
/** @var IDBConnection */ /** @var IDBConnection */
@ -135,14 +141,21 @@ class Version0002Date20190916000001 extends SimpleMigrationStep {
] ]
); );
if (!$table->hasIndex('sat')) { $table->addUniqueIndex(['stream_id', 'actor_id', 'type'], 'sat');
$table->addUniqueIndex(['stream_id', 'actor_id', 'type'], 'sat'); $table->addIndex(['type', 'subtype'], 'ts');
$table->addIndex(['type', 'subtype'], 'ts');
}
} }
$table = $schema->getTable('social_a2_stream'); $table = $schema->getTable('social_a2_stream');
if (!$table->hasColumn('in_reply_to_prim')) {
$table->addColumn(
'in_reply_to_prim', 'string',
[
'notnull' => true,
'length' => 128,
]
);
}
if (!$table->hasColumn('object_id_prim')) { if (!$table->hasColumn('object_id_prim')) {
$table->addColumn( $table->addColumn(
'object_id_prim', 'string', 'object_id_prim', 'string',
@ -296,7 +309,7 @@ class Version0002Date20190916000001 extends SimpleMigrationStep {
} }
$qb = $this->connection->getQueryBuilder(); $qb = $this->connection->getQueryBuilder();
$qb->select('id_prim', 'object_id', 'attributed_to', 'attributed_to_prim') $qb->select('id_prim', 'object_id', 'attributed_to', 'in_reply_to')
->from('social_a2_stream'); ->from('social_a2_stream');
$cursor = $qb->execute(); $cursor = $qb->execute();
@ -312,15 +325,16 @@ class Version0002Date20190916000001 extends SimpleMigrationStep {
* @param array $data * @param array $data
*/ */
private function updateStreamPrim(array $data) { private function updateStreamPrim(array $data) {
if ($data['attributed_to_prim'] !== '') {
return;
}
$update = $this->connection->getQueryBuilder(); $update = $this->connection->getQueryBuilder();
$update->update('social_a2_stream'); $update->update('social_a2_stream');
if ($data['object_id'] !== '') { if ($data['object_id'] !== '') {
$update->set('object_id_prim', $update->createNamedParameter(hash('sha512', $data['object_id']))); $update->set('object_id_prim', $update->createNamedParameter(hash('sha512', $data['object_id'])));
} }
if ($data['in_reply_to'] !== '') {
$update->set(
'in_reply_to_prim', $update->createNamedParameter(hash('sha512', $data['in_reply_to']))
);
}
$update->set( $update->set(
'attributed_to_prim', $update->createNamedParameter(hash('sha512', $data['attributed_to'])) 'attributed_to_prim', $update->createNamedParameter(hash('sha512', $data['attributed_to']))
); );
@ -340,58 +354,32 @@ class Version0002Date20190916000001 extends SimpleMigrationStep {
return; return;
} }
$start = 0; try {
$limit = 1000; $streamRequest = \OC::$server->query(StreamRequest::class);
while (true) { $streamDestRequest = \OC::$server->query(StreamDestRequest::class);
$qb = $this->connection->getQueryBuilder(); $streamTagsRequest = \OC::$server->query(StreamTagsRequest::class);
$qb->select('id_prim', 'to', 'to_array', 'cc', 'bcc', 'attributed_to') } catch (QueryException $e) {
->from('social_a2_stream') \OC::$server->getLogger()
->setMaxResults(1000) ->log(2, 'issue while querying stream* request');
->setFirstResult($start);
$cursor = $qb->execute(); return;
$count = 0;
while ($data = $cursor->fetch()) {
$count++;
$this->insertStreamDest($data);
}
$cursor->closeCursor();
$start += $count;
if ($count < $limit) {
break;
}
} }
}
private function insertStreamDest($data) { $streamDestRequest->emptyStreamDest();
$recipients = []; $streamTagsRequest->emptyStreamTags();
$recipients['to'] = $streams = $streamRequest->getAll();
array_merge(json_decode($data['to_array'], true), [$data['to']], [$data['attributed_to']]);
$recipients['cc'] = array_merge(json_decode($data['cc'], true), json_decode($data['bcc'], true));
$streamId = $data['id_prim']; foreach ($streams as $stream) {
foreach (array_keys($recipients) as $dest) { try {
$subtype = $dest; $streamDestRequest->generateStreamDest($stream);
foreach ($recipients[$dest] as $actorId) { $streamTagsRequest->generateStreamTags($stream);
if ($actorId === '') { } catch (Exception $e) {
continue; \OC::$server->getLogger()
} ->log(
$insert = $this->connection->getQueryBuilder(); 2, '-- ' . get_class($e) . ' - ' . $e->getMessage() . ' - ' . json_encode(
$insert->insert('social_a2_stream_dest'); $stream
)
$insert->setValue('stream_id', $insert->createNamedParameter($streamId)); );
$insert->setValue('actor_id', $insert->createNamedParameter(hash('sha512', $actorId)));
$insert->setValue('type', $insert->createNamedParameter('recipient'));
$insert->setValue('subtype', $insert->createNamedParameter($subtype));
try {
$insert->execute();
} catch (UniqueConstraintViolationException $e) {
\OC::$server->getLogger()
->log(1, 'Social - Duplicate recipient on Stream ' . json_encode($data));
}
} }
} }
} }

Wyświetl plik

@ -40,12 +40,15 @@ use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep; use OCP\Migration\SimpleMigrationStep;
require_once __DIR__ . '/../../appinfo/autoload.php';
/** /**
* Class Version0002Date20190916000002 * Class Version0002Date20191010000002
* *
* @package OCA\Social\Migration * @package OCA\Social\Migration
*/ */
class Version0002Date20190916000002 extends SimpleMigrationStep { class Version0002Date20191010000002 extends SimpleMigrationStep {
/** @var IDBConnection */ /** @var IDBConnection */
@ -81,6 +84,7 @@ class Version0002Date20190916000002 extends SimpleMigrationStep {
$table->addUniqueIndex(['accepted', 'object_id_prim', 'actor_id_prim'], 'aoa'); $table->addUniqueIndex(['accepted', 'object_id_prim', 'actor_id_prim'], 'aoa');
} }
$table = $schema->getTable('social_a2_stream'); $table = $schema->getTable('social_a2_stream');
if (!$table->hasIndex('ipoha')) { if (!$table->hasIndex('ipoha')) {
$table->addUniqueIndex( $table->addUniqueIndex(
@ -100,6 +104,12 @@ class Version0002Date20190916000002 extends SimpleMigrationStep {
'object_id_prim' 'object_id_prim'
); );
} }
if (!$table->hasIndex('in_reply_to_prim')) {
$table->addIndex(
['in_reply_to_prim'],
'in_reply_to_prim'
);
}
if (!$table->hasIndex('attributed_to_prim')) { if (!$table->hasIndex('attributed_to_prim')) {
$table->addIndex( $table->addIndex(
['attributed_to_prim'], ['attributed_to_prim'],
@ -107,15 +117,13 @@ class Version0002Date20190916000002 extends SimpleMigrationStep {
); );
} }
$table = $schema->getTable('social_a2_cache_actors');
if (!$table->hasIndex('i')) {
$table->addUniqueIndex(['id_prim'], 'i');
}
// $table = $schema->getTable('social_a2_stream_action'); $table = $schema->getTable('social_a2_cache_actors');
// if (!$table->hasIndex('sa')) { if ($table->hasIndex('i')) {
// $table->addUniqueIndex(['stream_id_prim', 'actor_id_prim'], 'sa'); $table->renameIndex('i', 'id_prim');
// } } else if (!$table->hasIndex('id_prim')) {
$table->addUniqueIndex(['id_prim'], 'id_prim');
}
return $schema; return $schema;
} }