jenkins-bot has submitted this change and it was merged.
Change subject: SQLite support for PDO backend
......................................................................
SQLite support for PDO backend
Also run PDO unit tests with both supported drivers.
Change-Id: I6def3c3b0e56a3d0608478bb11516077d054d695
---
M src/PHPQueue/Backend/PDO.php
R test/PHPQueue/Backend/PDOBaseTest.php
A test/PHPQueue/Backend/PDOMysqlTest.php
A test/PHPQueue/Backend/PDOSqliteTest.php
4 files changed, 70 insertions(+), 27 deletions(-)
Approvals:
XenoRyet: Looks good to me, approved
jenkins-bot: Verified
diff --git a/src/PHPQueue/Backend/PDO.php b/src/PHPQueue/Backend/PDO.php
index 5530022..c8d4f1c 100644
--- a/src/PHPQueue/Backend/PDO.php
+++ b/src/PHPQueue/Backend/PDO.php
@@ -72,10 +72,11 @@
public function push($data)
{
- $sql = sprintf('INSERT INTO `%s` (`data`, `timestamp`) VALUES (?,
now())', $this->db_table);
+ $sql = sprintf('INSERT INTO `%s` (`data`, `timestamp`) VALUES (?, ?)',
$this->db_table);
$sth = $this->getConnection()->prepare($sql);
$_tmp = json_encode($data);
$sth->bindParam(1, $_tmp, \PDO::PARAM_STR);
+ $sth->bindParam(2, self::getTimeStamp(), \PDO::PARAM_STR);
try {
$success = $sth->execute();
if (!$success) {
@@ -97,14 +98,20 @@
public function set($id, $data, $properties=array())
{
- $sql = sprintf('REPLACE INTO `%s` (`id`, `data`) VALUES (?, ?)',
$this->db_table);
+ $sql = sprintf('REPLACE INTO `%s` (`id`, `data`, `timestamp`) VALUES
(?, ?, ?)', $this->db_table);
$sth = $this->getConnection()->prepare($sql);
$_tmp = json_encode($data);
$sth->bindParam(1, $id, \PDO::PARAM_INT);
$sth->bindParam(2, $_tmp, \PDO::PARAM_STR);
+ $sth->bindParam(3, self::getTimeStamp(), \PDO::PARAM_STR);
$sth->execute();
}
+ protected static function getTimeStamp()
+ {
+ $now = new \DateTime('now', new \DateTimeZone('UTC'));
+ return $now->format('Y-m-d\TH:i:s.u');
+ }
/**
* @return array|null The retrieved record, or null if nothing was found.
*/
@@ -151,14 +158,14 @@
$data = $this->pop();
if (!is_callable($callback)) {
- throw new RuntimeException("Bad callback passed to " .
__METHOD__);
+ throw new \RuntimeException("Bad callback passed to " .
__METHOD__);
}
call_user_func($callback, $data);
$this->getConnection()->commit();
return $data;
} catch (\Exception $ex) {
- $this->getConnection()->rollback();
+ $this->getConnection()->rollBack();
throw $ex;
}
}
@@ -205,6 +212,13 @@
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;",
$table_name);
break;
+ case 'sqlite':
+ $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (
+ `id` INTEGER PRIMARY KEY,
+ `data` text NULL,
+ `timestamp` datetime NOT NULL
+ );", $table_name);
+ break;
default:
throw new BackendException('Unknown database driver: ' .
$this->getDriverName());
}
@@ -214,7 +228,8 @@
return true;
}
- protected function getDriverName() {
+ protected function getDriverName()
+ {
return $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME);
}
diff --git a/test/PHPQueue/Backend/PDOTest.php
b/test/PHPQueue/Backend/PDOBaseTest.php
similarity index 80%
rename from test/PHPQueue/Backend/PDOTest.php
rename to test/PHPQueue/Backend/PDOBaseTest.php
index 9149025..dd96d66 100644
--- a/test/PHPQueue/Backend/PDOTest.php
+++ b/test/PHPQueue/Backend/PDOBaseTest.php
@@ -1,38 +1,20 @@
<?php
namespace PHPQueue\Backend;
-class PDOTest extends \PHPUnit_Framework_TestCase
+abstract class PDOBaseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PDO
*/
- private $object;
+ protected $object;
public function setUp()
{
parent::setUp();
- if (!class_exists('\PDO')) {
- $this->markTestSkipped('PDO extension is not installed');
- }
- $options = array(
- 'connection_string' => 'mysql:host=localhost;dbname=phpqueuetest'
- , 'db_table' => 'pdotest'
- , 'pdo_options' => array(
- \PDO::ATTR_PERSISTENT => true
- )
- );
- // Check that the database exists, and politely skip if not.
- try {
- new \PDO($options['connection_string']);
- } catch ( \PDOException $ex ) {
- $this->markTestSkipped('Database access failed: ' .
$ex->getMessage());
+ if ( !class_exists( '\PDO' ) ) {
+ $this->markTestSkipped( 'PDO extension is not installed' );
}
-
- $this->object = new PHPQueue\Backend\PDO($options);
- // Create table
- $this->assertTrue($this->object->createTable('pdotest'));
- $this->object->clearAll();
}
public function tearDown()
diff --git a/test/PHPQueue/Backend/PDOMysqlTest.php
b/test/PHPQueue/Backend/PDOMysqlTest.php
new file mode 100644
index 0000000..e19b1bb
--- /dev/null
+++ b/test/PHPQueue/Backend/PDOMysqlTest.php
@@ -0,0 +1,28 @@
+<?php
+namespace PHPQueue\Backend;
+
+class PDOMysqlTest extends PDOBaseTest {
+
+ public function setUp()
+ {
+ $options = array(
+ 'connection_string' =>
'mysql:host=localhost;dbname=phpqueuetest'
+ , 'db_table' => 'pdotest'
+ , 'pdo_options' => array(
+ \PDO::ATTR_PERSISTENT => true
+ )
+ );
+
+ // Check that the database exists, and politely skip if not.
+ try {
+ new \PDO($options['connection_string']);
+ } catch ( \PDOException $ex ) {
+ $this->markTestSkipped('Database access failed: ' .
$ex->getMessage());
+ }
+
+ $this->object = new PDO($options);
+ // Create table
+ $this->assertTrue($this->object->createTable('pdotest'));
+ $this->object->clearAll();
+ }
+}
diff --git a/test/PHPQueue/Backend/PDOSqliteTest.php
b/test/PHPQueue/Backend/PDOSqliteTest.php
new file mode 100644
index 0000000..9a7f527
--- /dev/null
+++ b/test/PHPQueue/Backend/PDOSqliteTest.php
@@ -0,0 +1,18 @@
+<?php
+namespace PHPQueue\Backend;
+
+class PDOTestSqlite extends PDOBaseTest {
+
+ public function setUp()
+ {
+ $options = array(
+ 'connection_string' => 'sqlite::memory:'
+ , 'db_table' => 'pdotest'
+ );
+ $this->object = new PDO($options);
+ // Create table
+ $this->assertTrue($this->object->createTable('pdotest'));
+ $this->object->clearAll();
+ }
+}
+
--
To view, visit https://gerrit.wikimedia.org/r/286773
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I6def3c3b0e56a3d0608478bb11516077d054d695
Gerrit-PatchSet: 3
Gerrit-Project: wikimedia/fundraising/php-queue
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits