jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/349180 )

Change subject: Only remove the right constraints on import
......................................................................


Only remove the right constraints on import

Once we start reading constraints from statements (which will also be
written to the database), the CSV file will no longer be the sole source
of constraints in the repository, so the import shouldn’t remove
constraints that didn’t come from the CSV file. Since we don’t track the
source of the constraints, and the script which extracts constraints
from templates does not produce stable UUIDs that can be compared
between database and CSV file, we distinguish between constraints to
delete and to keep via the format of their constraint IDs: the template
extractor produces regular UUIDs, whereas constraints from statements
will have a statement ID (entity ID, dollar sign, UUID).

The deletion of the old constraints is no longer batched; testing
indicates that even for the set of all Wikidata constraints (about 10000
lines in the CSV file), the deletion completes in less than 0.1 seconds,
so I think this is acceptable.

The test for the script is updated to check that old constraints with
proper UUIDs are deleted, but constraints with other kinds of IDs are
kept. This requires that the test CSV file gets proper UUIDs as
constraint IDs.

Change-Id: Iadcae75bc9384ea5bf0ac49f7fddf6a49f3bab0e
---
M includes/ConstraintRepository.php
M maintenance/UpdateConstraintsTable.php
M tests/phpunit/Maintenance/UpdateConstraintsTableTest.php
M tests/phpunit/Maintenance/constraints.csv
4 files changed, 54 insertions(+), 14 deletions(-)

Approvals:
  jenkins-bot: Verified
  Thiemo Mättig (WMDE): Looks good to me, approved



diff --git a/includes/ConstraintRepository.php 
b/includes/ConstraintRepository.php
index dc39e52..7ff0182 100644
--- a/includes/ConstraintRepository.php
+++ b/includes/ConstraintRepository.php
@@ -4,6 +4,7 @@
 
 use DBUnexpectedError;
 use InvalidArgumentException;
+use Wikimedia\Rdbms\LikeMatch;
 use ResultWrapper;
 use Wikibase\DataModel\Entity\PropertyId;
 
@@ -55,6 +56,34 @@
        }
 
        /**
+        * @param LikeMatch $any should be IDatabase::anyChar()
+        */
+       private function uuidPattern( LikeMatch $any ) {
+               return array_merge(
+                       array_fill( 0, 8, $any ), [ '-' ],
+                       array_fill( 0, 4, $any ), [ '-' ],
+                       array_fill( 0, 4, $any ), [ '-' ],
+                       array_fill( 0, 4, $any ), [ '-' ],
+                       array_fill( 0, 12, $any )
+               );
+       }
+
+       /**
+        * Delete all constraints where the constraint ID is a UUID
+        * (formatted in groups of 8-4-4-4-12 digits).
+        *
+        * @throws DBUnexpectedError
+        */
+       public function deleteWhereConstraintIdIsUuid() {
+               $db = wfGetDB( DB_MASTER );
+               $db->delete(
+                       CONSTRAINT_TABLE,
+                       // WHERE constraint_guid LIKE 
________-____-____-____-____________
+                       'constraint_guid ' . $db->buildLike( 
$this->uuidPattern( $db->anyChar() ) )
+               );
+       }
+
+       /**
         * @param int $batchSize
         *
         * @throws InvalidArgumentException
diff --git a/maintenance/UpdateConstraintsTable.php 
b/maintenance/UpdateConstraintsTable.php
index f66bda5..7880eff 100644
--- a/maintenance/UpdateConstraintsTable.php
+++ b/maintenance/UpdateConstraintsTable.php
@@ -44,7 +44,7 @@
                }
 
                $constraintRepo = 
ConstraintReportFactory::getDefaultInstance()->getConstraintRepository();
-               $constraintRepo->deleteAll( $this->mBatchSize );
+               $constraintRepo->deleteWhereConstraintIdIsUuid();
                $this->insertValues( $constraintRepo, $csvFile );
                fclose( $csvFile );
        }
diff --git a/tests/phpunit/Maintenance/UpdateConstraintsTableTest.php 
b/tests/phpunit/Maintenance/UpdateConstraintsTableTest.php
index 301e89b..5e65d03 100644
--- a/tests/phpunit/Maintenance/UpdateConstraintsTableTest.php
+++ b/tests/phpunit/Maintenance/UpdateConstraintsTableTest.php
@@ -26,14 +26,16 @@
        public function addDBData() {
                $this->db->delete( CONSTRAINT_TABLE, '*' );
                $this->db->insert( CONSTRAINT_TABLE, [
+                       // a constraint imported from a template (UUID)
                        [
-                               'constraint_guid' => 'foo',
+                               'constraint_guid' => 
'afbbe0c2-2bc4-47b6-958c-a318a53814ac',
                                'pid' => 42,
                                'constraint_type_qid' => 'TestConstraint',
                                'constraint_parameters' => '{}'
                        ],
+                       // a constraint imported from a statement (statement ID)
                        [
-                               'constraint_guid' => 'bar',
+                               'constraint_guid' => 
'P2$2892c48c-53e5-40ef-94a2-274ebf35075c',
                                'pid' => 42,
                                'constraint_type_qid' => 'TestConstraint',
                                'constraint_parameters' => '{}'
@@ -61,24 +63,33 @@
                        ],
                        [],
                        [
+                               // existing constraint imported from a 
statement is not deleted
                                [
-                                       'baz',
+                                       
'P2$2892c48c-53e5-40ef-94a2-274ebf35075c',
+                                       42,
+                                       'TestConstraint',
+                                       '{}'
+                               ],
+                               // new constraints
+                               [
+                                       'c3d49df6-a4f1-413d-903d-57907aa439f9',
                                        '42',
                                        'ConstraintFromCsv',
                                        '{"foo":"bar"}'
                                ],
                                [
-                                       'foobar',
-                                       '42',
-                                       'ConstraintFromCsv',
-                                       '{"foobar":"bar"}'
-                               ],
-                               [
-                                       'foobaz',
+                                       'daa9c35c-0455-4c8d-8804-a73cd78fcc4a',
                                        '42',
                                        'ConstraintFromCsv',
                                        '{"bar":"baz"}'
                                ],
+                               [
+                                       'e28b1517-a7f6-4479-bdc8-6687e944ba31',
+                                       '42',
+                                       'ConstraintFromCsv',
+                                       '{"foobar":"bar"}'
+                               ],
+                               // existing constrant imported from a template 
is deleted
                        ]
                );
        }
diff --git a/tests/phpunit/Maintenance/constraints.csv 
b/tests/phpunit/Maintenance/constraints.csv
index c832c5a..cfb52a4 100644
--- a/tests/phpunit/Maintenance/constraints.csv
+++ b/tests/phpunit/Maintenance/constraints.csv
@@ -1,3 +1,3 @@
-"baz","42","ConstraintFromCsv","{""foo"":""bar""}"
-"foobaz","42","ConstraintFromCsv","{""bar"":""baz""}"
-"foobar","42","ConstraintFromCsv","{""foobar"":""bar""}"
\ No newline at end of file
+"c3d49df6-a4f1-413d-903d-57907aa439f9","42","ConstraintFromCsv","{""foo"":""bar""}"
+"daa9c35c-0455-4c8d-8804-a73cd78fcc4a","42","ConstraintFromCsv","{""bar"":""baz""}"
+"e28b1517-a7f6-4479-bdc8-6687e944ba31","42","ConstraintFromCsv","{""foobar"":""bar""}"
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/349180
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iadcae75bc9384ea5bf0ac49f7fddf6a49f3bab0e
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to