Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/305568

Change subject: Base class for dbs, they can create tables
......................................................................

Base class for dbs, they can create tables

Move common functionality for DamagedDatabase and PendingDatabase into
base SmashPigDatabase class. Add a createTable() method and use it in
test setup rather than referencing paths. Rearrange sql files to make
it easy to find the driver-specific script.

Also change default database name from pending to smashpig

Could do like the PDO PHP-Queue backend and try creating the table on
insert errors.

Change-Id: I49b15430c3a2bebecaebfd4f0ddb9437ee9a32f1
---
M Core/DataStores/DamagedDatabase.php
M Core/DataStores/PendingDatabase.php
A Core/DataStores/SmashPigDatabase.php
M PaymentProviders/Adyen/Tests/AdyenTestConfiguration.php
R Schema/mysql/001_CreatePendingTable.sql
R Schema/mysql/002_CreateDamagedTable.sql
R Schema/sqlite/001_CreatePendingTable.sql
R Schema/sqlite/002_CreateDamagedTable.sql
M SmashPig.yaml
M Tests/DamagedDatabaseTest.php
M Tests/PendingDatabaseTest.php
D Tests/PendingDatabaseTestConfiguration.php
M Tests/QueueConsumerTest.php
A Tests/SmashPigDatabaseTestConfiguration.php
R Tests/data/config_smashpig_db.yaml
15 files changed, 103 insertions(+), 100 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig 
refs/changes/68/305568/1

diff --git a/Core/DataStores/DamagedDatabase.php 
b/Core/DataStores/DamagedDatabase.php
index 312ffa6..d6069ef 100644
--- a/Core/DataStores/DamagedDatabase.php
+++ b/Core/DataStores/DamagedDatabase.php
@@ -9,32 +9,7 @@
 /**
  * Data store containing messages which were not successfully processed
  */
-class DamagedDatabase {
-
-       /**
-        * @var PDO
-        * We do the silly singleton thing for convenient testing with in-memory
-        * databases that would otherwise not be shared between components.
-        */
-       protected static $db;
-
-       protected function __construct() {
-               if ( !self::$db ) {
-                       $config = Context::get()->getConfiguration();
-                       self::$db = $config->object( 'data-store/damaged-db' );
-               }
-       }
-
-       /**
-        * @return PDO
-        */
-       public function getDatabase() {
-               return self::$db;
-       }
-
-       public static function get() {
-               return new DamagedDatabase();
-       }
+class DamagedDatabase extends SmashPigDatabase {
 
        protected function validateMessage( $message ) {
                if (
@@ -57,7 +32,10 @@
         * @throws SmashPigException if insert fails
         */
        public function storeMessage(
-               $message, $originalQueue, $error = '', $retryDate = null
+               $message,
+               $originalQueue,
+               $error = '',
+               $retryDate = null
        ) {
                $this->validateMessage( $message );
 
@@ -95,6 +73,7 @@
 
                $insert = "INSERT INTO damaged ( $fieldList )
                        VALUES ( $paramList );";
+
                $prepared = self::$db->prepare( $insert );
 
                foreach ( $dbRecord as $field => $value ) {
@@ -117,14 +96,17 @@
         * @return array|null Records with retry_date prior to now
         */
        public function fetchRetryMessages( $limit ) {
-               $prepared = self::$db->prepare( '
+               $prepared = self::$db->prepare(
+                       '
                        SELECT * FROM damaged
                        WHERE retry_date < :now
                        ORDER BY retry_date ASC
                        LIMIT ' . $limit
                );
                $prepared->bindValue(
-                       ':now', UtcDate::getUtcDatabaseString(), PDO::PARAM_STR
+                       ':now',
+                       UtcDate::getUtcDatabaseString(),
+                       PDO::PARAM_STR
                );
                $prepared->execute();
                $rows = $prepared->fetchAll( PDO::FETCH_ASSOC );
@@ -140,7 +122,8 @@
         * @param array $message
         */
        public function deleteMessage( $message ) {
-               $prepared = self::$db->prepare( '
+               $prepared = self::$db->prepare(
+                       '
                        DELETE FROM damaged
                        WHERE id = :id'
                );
@@ -182,4 +165,12 @@
                $message['original_queue'] = $row['original_queue'];
                return $message;
        }
+
+       protected function getConfigKey() {
+               return 'data-store/damaged-db';
+       }
+
+       protected function getTableScriptFile() {
+               return '002_CreateDamagedTable.sql';
+       }
 }
diff --git a/Core/DataStores/PendingDatabase.php 
b/Core/DataStores/PendingDatabase.php
index 598499b..cc8b313 100644
--- a/Core/DataStores/PendingDatabase.php
+++ b/Core/DataStores/PendingDatabase.php
@@ -12,37 +12,7 @@
 /**
  * Data store containing messages waiting to be finalized.
  */
-class PendingDatabase {
-
-       /**
-        * @var PDO
-        * We do the silly singleton thing for convenient testing with in-memory
-        * databases that would otherwise not be shared between components.
-        */
-       protected static $db;
-
-       protected function __construct() {
-               $config = Context::get()->getConfiguration();
-               if ( !self::$db ) {
-                       self::$db = $config->object( 'data-store/pending-db' );
-               }
-       }
-
-       /**
-        * @return PDO
-        */
-       public function getDatabase() {
-               return self::$db;
-       }
-
-       public static function get() {
-               $config = Context::get()->getConfiguration();
-               if ( $config->nodeExists( 'data-store/pending-db' ) ) {
-                       // TODO: remove after transition to new pending queue
-                       return new PendingDatabase();
-               }
-               return null;
-       }
+class PendingDatabase extends SmashPigDatabase {
 
        protected function validateMessage( $message ) {
                if (
@@ -288,4 +258,12 @@
                        ' WHERE id = :id';
                return $update;
        }
+
+       protected function getConfigKey() {
+               return 'data-store/pending-db';
+       }
+
+       protected function getTableScriptFile() {
+               return '001_CreatePendingTable.sql';
+       }
 }
diff --git a/Core/DataStores/SmashPigDatabase.php 
b/Core/DataStores/SmashPigDatabase.php
new file mode 100644
index 0000000..d27d5bc
--- /dev/null
+++ b/Core/DataStores/SmashPigDatabase.php
@@ -0,0 +1,49 @@
+<?php namespace SmashPig\Core\DataStores;
+
+use PDO;
+use SmashPig\Core\Context;
+
+abstract class SmashPigDatabase {
+
+       /**
+        * @var PDO
+        * We do the silly singleton thing for convenient testing with in-memory
+        * databases that would otherwise not be shared between components.
+        */
+       protected static $db;
+
+       protected function __construct() {
+               $config = Context::get()->getConfiguration();
+               if ( !self::$db ) {
+                       self::$db = $config->object( $this->getConfigKey() );
+               }
+       }
+
+       public static function get() {
+               return new static();
+       }
+
+       /**
+        * @return PDO
+        */
+       public function getDatabase() {
+               return self::$db;
+       }
+
+       public function createTable() {
+               $driver = $this->getDatabase()->getAttribute( 
PDO::ATTR_DRIVER_NAME );
+               $path = __DIR__ . '/../../Schema/'
+                       . $driver . '/' . $this->getTableScriptFile();
+               $this->getDatabase()->exec( file_get_contents( $path ) );
+       }
+
+       /**
+        * @return string Key in configuration pointing to backing PDO object
+        */
+       abstract protected function getConfigKey();
+
+       /**
+        * @return string Name of file (no directory) containing table creation 
SQL
+        */
+       abstract protected function getTableScriptFile();
+}
diff --git a/PaymentProviders/Adyen/Tests/AdyenTestConfiguration.php 
b/PaymentProviders/Adyen/Tests/AdyenTestConfiguration.php
index 652e6ad..e87a310 100644
--- a/PaymentProviders/Adyen/Tests/AdyenTestConfiguration.php
+++ b/PaymentProviders/Adyen/Tests/AdyenTestConfiguration.php
@@ -25,12 +25,7 @@
                );
                $config->override( $override );
 
-               // Create sqlite schema
-               $sql = file_get_contents( __DIR__ . 
'/../../../Schema/sqlite/001_CreatePendingTable.sqlite.sql' );
-               $db = PendingDatabase::get();
-               if ( $db ) {
-                       $db->getDatabase()->exec( $sql );
-               }
+               PendingDatabase::get()->createTable();
 
                return $config;
        }
diff --git a/Schema/001_CreatePendingTable.sql 
b/Schema/mysql/001_CreatePendingTable.sql
similarity index 100%
rename from Schema/001_CreatePendingTable.sql
rename to Schema/mysql/001_CreatePendingTable.sql
diff --git a/Schema/002_CreateDamagedTable.sql 
b/Schema/mysql/002_CreateDamagedTable.sql
similarity index 100%
rename from Schema/002_CreateDamagedTable.sql
rename to Schema/mysql/002_CreateDamagedTable.sql
diff --git a/Schema/sqlite/001_CreatePendingTable.sqlite.sql 
b/Schema/sqlite/001_CreatePendingTable.sql
similarity index 100%
rename from Schema/sqlite/001_CreatePendingTable.sqlite.sql
rename to Schema/sqlite/001_CreatePendingTable.sql
diff --git a/Schema/sqlite/002_CreateDamagedTable.sqlite.sql 
b/Schema/sqlite/002_CreateDamagedTable.sql
similarity index 100%
rename from Schema/sqlite/002_CreateDamagedTable.sqlite.sql
rename to Schema/sqlite/002_CreateDamagedTable.sql
diff --git a/SmashPig.yaml b/SmashPig.yaml
index 0fcfc34..5b1656a 100644
--- a/SmashPig.yaml
+++ b/SmashPig.yaml
@@ -44,12 +44,12 @@
         pending-db:
             class: PDO
             inst-args:
-                - 'mysql:host=127.0.0.1;dbname=pending'
+                - 'mysql:host=127.0.0.1;dbname=smashpig'
 
         damaged-db:
             class: PDO
             inst-args:
-                - 'mysql:host=127.0.0.1;dbname=pending'
+                - 'mysql:host=127.0.0.1;dbname=smashpig'
 
         refund:
             class: SmashPig\Core\DataStores\MultiQueueWriter
diff --git a/Tests/DamagedDatabaseTest.php b/Tests/DamagedDatabaseTest.php
index 856325d..289a730 100644
--- a/Tests/DamagedDatabaseTest.php
+++ b/Tests/DamagedDatabaseTest.php
@@ -15,12 +15,10 @@
 
        public function setUp() {
                parent::setUp();
-               Context::initWithLogger( new PendingDatabaseTestConfiguration() 
);
+               $config = new SmashPigDatabaseTestConfiguration();
+               Context::initWithLogger( $config );
                $this->db = DamagedDatabase::get();
-
-               // Create sqlite schema
-               $sql = file_get_contents( __DIR__ . 
'/../Schema/sqlite/002_CreateDamagedTable.sqlite.sql' );
-               $this->db->getDatabase()->exec( $sql );
+               $this->db->createTable();
        }
 
        public function tearDown() {
diff --git a/Tests/PendingDatabaseTest.php b/Tests/PendingDatabaseTest.php
index bd87277..0d87f77 100644
--- a/Tests/PendingDatabaseTest.php
+++ b/Tests/PendingDatabaseTest.php
@@ -15,12 +15,10 @@
 
        public function setUp() {
                parent::setUp();
-               Context::initWithLogger( new PendingDatabaseTestConfiguration() 
);
+               $config = new SmashPigDatabaseTestConfiguration();
+               Context::initWithLogger( $config );
                $this->db = PendingDatabase::get();
-
-               // Create sqlite schema
-               $sql = file_get_contents( __DIR__ . 
'/../Schema/sqlite/001_CreatePendingTable.sqlite.sql' );
-               $this->db->getDatabase()->exec( $sql );
+               $this->db->createTable();
        }
 
        public function tearDown() {
diff --git a/Tests/PendingDatabaseTestConfiguration.php 
b/Tests/PendingDatabaseTestConfiguration.php
deleted file mode 100644
index c4c14d0..0000000
--- a/Tests/PendingDatabaseTestConfiguration.php
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-namespace SmashPig\Tests;
-
-use SmashPig\Core\Configuration;
-
-class PendingDatabaseTestConfiguration extends Configuration {
-       public function __construct() {
-               parent::__construct(
-                       'default',
-                       __DIR__ . '/data/config_pending_db.yaml'
-               );
-       }
-}
diff --git a/Tests/QueueConsumerTest.php b/Tests/QueueConsumerTest.php
index 871b2ed..bcd78b5 100644
--- a/Tests/QueueConsumerTest.php
+++ b/Tests/QueueConsumerTest.php
@@ -25,13 +25,9 @@
                Context::initWithLogger( new QueueTestConfiguration() );
                $this->queue = BaseQueueConsumer::getQueue( 'test' );
                $this->queue->createTable( 'test' );
-               $this->damaged = DamagedDatabase::get()->getDatabase();
-
-               // Create sqlite schema
-               $sql = file_get_contents(
-                       __DIR__ . 
'/../Schema/sqlite/002_CreateDamagedTable.sqlite.sql'
-               );
-               $this->damaged->exec( $sql );
+               $damagedDb = DamagedDatabase::get();
+               $damagedDb->createTable();
+               $this->damaged = $damagedDb->getDatabase();
        }
 
        public function testEmptyQueue() {
diff --git a/Tests/SmashPigDatabaseTestConfiguration.php 
b/Tests/SmashPigDatabaseTestConfiguration.php
new file mode 100644
index 0000000..d064fc1
--- /dev/null
+++ b/Tests/SmashPigDatabaseTestConfiguration.php
@@ -0,0 +1,12 @@
+<?php namespace SmashPig\Tests;
+
+use SmashPig\Core\Configuration;
+
+class SmashPigDatabaseTestConfiguration extends Configuration {
+       public function __construct() {
+               parent::__construct(
+                       'default',
+                       __DIR__ . '/data/config_smashpig_db.yaml'
+               );
+       }
+}
diff --git a/Tests/data/config_pending_db.yaml 
b/Tests/data/config_smashpig_db.yaml
similarity index 100%
rename from Tests/data/config_pending_db.yaml
rename to Tests/data/config_smashpig_db.yaml

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I49b15430c3a2bebecaebfd4f0ddb9437ee9a32f1
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Ejegg <eeggles...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to