tests: Reset Postgres sequences when cloning and truncating
authorBrad Jorsch <bjorsch@wikimedia.org>
Sun, 18 Mar 2018 17:23:58 +0000 (13:23 -0400)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 5 Apr 2018 23:57:24 +0000 (23:57 +0000)
This improves the repeatability of the unit tests by making the ID
values generated depend less on what previous tests might have done.

It also prevents tests from using up sequence numbers for the live DB's
tables.

Change-Id: Iaa8ae1e5cef4b9099bd1b4b8fc806f5af372a7ff

includes/libs/rdbms/database/DatabasePostgres.php
tests/phpunit/MediaWikiTestCase.php

index 32ea375..525d308 100644 (file)
@@ -786,11 +786,75 @@ __INDEXATTR__;
        public function duplicateTableStructure(
                $oldName, $newName, $temporary = false, $fname = __METHOD__
        ) {
-               $newName = $this->addIdentifierQuotes( $newName );
-               $oldName = $this->addIdentifierQuotes( $oldName );
+               $newNameE = $this->addIdentifierQuotes( $newName );
+               $oldNameE = $this->addIdentifierQuotes( $oldName );
+
+               $ret = $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newNameE " .
+                       "(LIKE $oldNameE INCLUDING DEFAULTS INCLUDING INDEXES)", $fname );
+               if ( !$ret ) {
+                       return $ret;
+               }
+
+               $res = $this->query( 'SELECT attname FROM pg_class c'
+                       . ' JOIN pg_namespace n ON (n.oid = c.relnamespace)'
+                       . ' JOIN pg_attribute a ON (a.attrelid = c.oid)'
+                       . ' JOIN pg_attrdef d ON (c.oid=d.adrelid and a.attnum=d.adnum)'
+                       . ' WHERE relkind = \'r\''
+                       . ' AND nspname = ' . $this->addQuotes( $this->getCoreSchema() )
+                       . ' AND relname = ' . $this->addQuotes( $oldName )
+                       . ' AND adsrc LIKE \'nextval(%\'',
+                       $fname
+               );
+               $row = $this->fetchObject( $res );
+               if ( $row ) {
+                       $field = $row->attname;
+                       $newSeq = "{$newName}_{$field}_seq";
+                       $fieldE = $this->addIdentifierQuotes( $field );
+                       $newSeqE = $this->addIdentifierQuotes( $newSeq );
+                       $newSeqQ = $this->addQuotes( $newSeq );
+                       $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " SEQUENCE $newSeqE", $fname );
+                       $this->query(
+                               "ALTER TABLE $newNameE ALTER COLUMN $fieldE SET DEFAULT nextval({$newSeqQ}::regclass)",
+                               $fname
+                       );
+               }
 
-               return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
-                       "(LIKE $oldName INCLUDING DEFAULTS INCLUDING INDEXES)", $fname );
+               return $ret;
+       }
+
+       public function resetSequenceForTable( $table, $fname = __METHOD__ ) {
+               $table = $this->tableName( $table, 'raw' );
+               foreach ( $this->getCoreSchemas() as $schema ) {
+                       $res = $this->query(
+                               'SELECT c.oid FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace)'
+                               . ' WHERE relkind = \'r\''
+                               . ' AND nspname = ' . $this->addQuotes( $schema )
+                               . ' AND relname = ' . $this->addQuotes( $table ),
+                               $fname
+                       );
+                       if ( !$res || !$this->numRows( $res ) ) {
+                               continue;
+                       }
+
+                       $oid = $this->fetchObject( $res )->oid;
+                       $res = $this->query( 'SELECT adsrc FROM pg_attribute a'
+                               . ' JOIN pg_attrdef d ON (a.attrelid=d.adrelid and a.attnum=d.adnum)'
+                               . " WHERE a.attrelid = $oid"
+                               . ' AND adsrc LIKE \'nextval(%\'',
+                               $fname
+                       );
+                       $row = $this->fetchObject( $res );
+                       if ( $row ) {
+                               $this->query(
+                                       'SELECT ' . preg_replace( '/^nextval\((.+)\)$/', 'setval($1,1,false)', $row->adsrc ),
+                                       $fname
+                               );
+                               return true;
+                       }
+                       return false;
+               }
+
+               return false;
        }
 
        public function listTables( $prefix = null, $fname = __METHOD__ ) {
index 0d2b788..ffba861 100644 (file)
@@ -1539,6 +1539,11 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                                        $db->delete( $tbl, '*', __METHOD__ );
                                }
 
+                               if ( $db->getType() === 'postgres' ) {
+                                       // Reset the table's sequence too.
+                                       $db->resetSequenceForTable( $tbl, __METHOD__ );
+                               }
+
                                if ( $tbl === 'page' ) {
                                        // Forget about the pages since they don't
                                        // exist in the DB.