[Pkg-owncloud-commits] [php-sabredav] 188/220: Postgres support for CalDAV

David Prévot taffit at moszumanska.debian.org
Thu May 12 01:21:26 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit a5790c95d75e51fd898e7315a3f53d5486bcd72a
Author: Evert Pot <me at evertpot.com>
Date:   Thu Apr 28 14:52:00 2016 +0700

    Postgres support for CalDAV
---
 examples/sql/pgsql.calendars.sql               |  68 +++++++++------
 lib/CalDAV/Backend/PDO.php                     |  21 +++--
 tests/Sabre/CalDAV/Backend/AbstractPDOTest.php | 113 ++++++++++++++++++++-----
 tests/Sabre/CalDAV/Backend/PDOMySQLTest.php    |  32 +------
 tests/Sabre/CalDAV/Backend/PDOPgSqlTest.php    |   9 ++
 tests/Sabre/CalDAV/Backend/PDOSqliteTest.php   |  30 +------
 6 files changed, 160 insertions(+), 113 deletions(-)

diff --git a/examples/sql/pgsql.calendars.sql b/examples/sql/pgsql.calendars.sql
index 6d898d1..67dc41a 100644
--- a/examples/sql/pgsql.calendars.sql
+++ b/examples/sql/pgsql.calendars.sql
@@ -1,24 +1,3 @@
-CREATE TABLE calendars (
-    id SERIAL NOT NULL,
-    principaluri VARCHAR(100),
-    displayname VARCHAR(100),
-    uri VARCHAR(200),
-    synctoken INTEGER NOT NULL DEFAULT 1,
-    description TEXT,
-    calendarorder INTEGER NOT NULL DEFAULT 0,
-    calendarcolor VARCHAR(10),
-    timezone TEXT,
-    components VARCHAR(21),
-    uid VARCHAR(200),
-    transparent SMALLINT NOT NULL DEFAULT '0'
-);
-
-ALTER TABLE ONLY calendars
-    ADD CONSTRAINT calendars_pkey PRIMARY KEY (id);
-
-CREATE UNIQUE INDEX calendars_ukey
-    ON calendars USING btree (principaluri, uri);
-
 CREATE TABLE calendarobjects (
     id SERIAL NOT NULL,
     calendardata BYTEA,
@@ -39,9 +18,46 @@ ALTER TABLE ONLY calendarobjects
 CREATE UNIQUE INDEX calendarobjects_ukey
     ON calendarobjects USING btree (calendarid, uri);
 
-ALTER TABLE ONLY calendarobjects
-    ADD CONSTRAINT calendarobjects_calendarid_fkey FOREIGN KEY (calendarid) REFERENCES calendars(id)
-        ON DELETE CASCADE;
+
+CREATE TABLE calendars (
+    id SERIAL NOT NULL,
+    synctoken INTEGER NOT NULL DEFAULT 1,
+    components VARCHAR(21)
+);
+
+ALTER TABLE ONLY calendars
+    ADD CONSTRAINT calendars_pkey PRIMARY KEY (id);
+
+
+CREATE TABLE calendarinstances (
+    id SERIAL NOT NULL,
+    calendarid INTEGER NOT NULL,
+    principaluri VARCHAR(100),
+    access SMALLINT NOT NULL DEFAULT '1', -- '1 = owner, 2 = read, 3 = readwrite'
+    displayname VARCHAR(100),
+    uri VARCHAR(200),
+    description TEXT,
+    calendarorder INTEGER NOT NULL DEFAULT 0,
+    calendarcolor VARCHAR(10),
+    timezone TEXT,
+    transparent SMALLINT NOT NULL DEFAULT '0',
+    share_href VARCHAR(100),
+    share_displayname VARCHAR(100),
+    share_invitestatus SMALLINT NOT NULL DEFAULT '2' --  '1 = noresponse, 2 = accepted, 3 = declined, 4 = invalid'
+);
+
+ALTER TABLE ONLY calendarinstances
+    ADD CONSTRAINT calendarinstances_pkey PRIMARY KEY (id);
+
+CREATE UNIQUE INDEX calendarinstances_principaluri_uri
+    ON calendarinstances USING btree (principaluri, uri);
+
+
+CREATE UNIQUE INDEX calendarinstances_principaluri_calendarid
+    ON calendarinstances USING btree (principaluri, calendarid);
+
+CREATE UNIQUE INDEX calendarinstances_principaluri_share_href
+    ON calendarinstances USING btree (principaluri, share_href);
 
 CREATE TABLE calendarsubscriptions (
     id SERIAL NOT NULL,
@@ -78,10 +94,6 @@ ALTER TABLE ONLY calendarchanges
 CREATE INDEX calendarchanges_calendarid_synctoken_ix
     ON calendarchanges USING btree (calendarid, synctoken);
 
-ALTER TABLE ONLY calendarchanges
-    ADD CONSTRAINT calendarchanges_calendar_fk FOREIGN KEY (calendarid) REFERENCES calendars(id)
-        ON DELETE CASCADE;
-
 CREATE TABLE schedulingobjects (
     id SERIAL NOT NULL,
     principaluri VARCHAR(255),
diff --git a/lib/CalDAV/Backend/PDO.php b/lib/CalDAV/Backend/PDO.php
index b62120d..53838eb 100644
--- a/lib/CalDAV/Backend/PDO.php
+++ b/lib/CalDAV/Backend/PDO.php
@@ -261,7 +261,10 @@ SQL
         $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarTableName . " (synctoken, components) VALUES (1, ?)");
         $stmt->execute([$components]);
 
-        $calendarId = $this->pdo->lastInsertId();
+        $calendarId = $this->pdo->lastInsertId(
+            $this->calendarTableName . '_id_seq'
+        );
+
         $values[':calendarid'] = $calendarId;
 
         foreach ($this->propertyMap as $xmlName => $dbName) {
@@ -273,9 +276,13 @@ SQL
         }
 
         $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarInstancesTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")");
+
         $stmt->execute($values);
 
-        return [$calendarId, $this->pdo->lastInsertId()];
+        return [
+            $calendarId,
+            $this->pdo->lastInsertId($this->calendarInstancesTableName . '_id_seq')
+        ];
 
     }
 
@@ -433,7 +440,7 @@ SQL
             $result[] = [
                 'id'           => $row['id'],
                 'uri'          => $row['uri'],
-                'lastmodified' => $row['lastmodified'],
+                'lastmodified' => (int)$row['lastmodified'],
                 'etag'         => '"' . $row['etag'] . '"',
                 'size'         => (int)$row['size'],
                 'component'    => strtolower($row['componenttype']),
@@ -476,7 +483,7 @@ SQL
         return [
             'id'            => $row['id'],
             'uri'           => $row['uri'],
-            'lastmodified'  => $row['lastmodified'],
+            'lastmodified'  => (int)$row['lastmodified'],
             'etag'          => '"' . $row['etag'] . '"',
             'size'          => (int)$row['size'],
             'calendardata'  => $row['calendardata'],
@@ -518,7 +525,7 @@ SQL
             $result[] = [
                 'id'           => $row['id'],
                 'uri'          => $row['uri'],
-                'lastmodified' => $row['lastmodified'],
+                'lastmodified' => (int)$row['lastmodified'],
                 'etag'         => '"' . $row['etag'] . '"',
                 'size'         => (int)$row['size'],
                 'calendardata' => $row['calendardata'],
@@ -1154,7 +1161,9 @@ SQL;
         $stmt = $this->pdo->prepare("INSERT INTO " . $this->calendarSubscriptionsTableName . " (" . implode(', ', $fieldNames) . ") VALUES (" . implode(', ', array_keys($values)) . ")");
         $stmt->execute($values);
 
-        return $this->pdo->lastInsertId();
+        return $this->pdo->lastInsertId(
+            $this->calendarSubscriptionsTableName . '_id_seq'
+        );
 
     }
 
diff --git a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
index 5ab300f..35d6da3 100644
--- a/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
+++ b/tests/Sabre/CalDAV/Backend/AbstractPDOTest.php
@@ -9,8 +9,26 @@ use Sabre\DAV\Xml\Element\Sharee;
 
 abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
+    use DAV\DbTestHelperTrait;
+
     protected $pdo;
 
+    function setUp() {
+
+        $this->dropTables([
+            'calendarobjects',
+            'calendars',
+            'calendarinstances',
+            'calendarchanges',
+            'calendarsubscriptions',
+            'schedulingobjects',
+        ]);
+        $this->createSchema('calendars');
+
+        $this->pdo = $this->getDb();
+
+    }
+
     function testConstruct() {
 
         $backend = new PDO($this->pdo);
@@ -216,7 +234,13 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -224,7 +248,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('20120101'),
             'lastoccurence'  => strtotime('20120101') + (3600 * 24),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
     function testGetMultipleObjects() {
@@ -260,12 +284,22 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         foreach ($check as $index => $props) {
 
-            foreach ($props as $key => $value) {
+            foreach ($props as $key => $expected) {
+
+                $actual = $result[$index][$key];
+
+                switch ($key) {
+                    case 'lastmodified' :
+                        $this->assertInternalType('int', $actual);
+                        break;
+                    case 'calendardata' :
+                        if (is_resource($actual)) {
+                            $actual = stream_get_contents($actual);
+                        }
+                        // no break intentional
+                    default :
+                        $this->assertEquals($expected, $actual);
 
-                if ($key !== 'lastmodified') {
-                    $this->assertEquals($value, $result[$index][$key]);
-                } else {
-                    $this->assertTrue(isset($result[$index][$key]));
                 }
 
             }
@@ -312,7 +346,13 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -320,7 +360,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('20120101'),
             'lastoccurence'  => strtotime('20120101') + (3600 * 48),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -352,7 +392,12 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -360,7 +405,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('2012-01-01 10:00:00'),
             'lastoccurence'  => strtotime('2012-01-01 10:00:00'),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -376,7 +421,12 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -384,7 +434,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('2012-01-01 10:00:00'),
             'lastoccurence'  => strtotime('2012-01-01 11:00:00'),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -400,7 +450,12 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -408,7 +463,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('2012-01-01 10:00:00'),
             'lastoccurence'  => strtotime(PDO::MAX_DATE),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -424,7 +479,12 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -432,7 +492,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => strtotime('2012-01-01 10:00:00'),
             'lastoccurence'  => strtotime('2012-01-01 11:00:00') + (3600 * 24 * 999),
             'componenttype'  => 'VEVENT',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -448,7 +508,12 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $backend->createCalendarObject($returnedId, 'random-id', $object);
 
-        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = "random-id"');
+        $result = $this->pdo->query('SELECT etag, size, calendardata, firstoccurence, lastoccurence, componenttype FROM calendarobjects WHERE uri = \'random-id\'');
+        $row = $result->fetch(\PDO::FETCH_ASSOC);
+        if (is_resource($row['calendardata'])) {
+            $row['calendardata'] = stream_get_contents($row['calendardata']);
+        }
+
         $this->assertEquals([
             'etag'           => md5($object),
             'size'           => strlen($object),
@@ -456,7 +521,7 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
             'firstoccurence' => null,
             'lastoccurence'  => null,
             'componenttype'  => 'VTODO',
-        ], $result->fetch(\PDO::FETCH_ASSOC));
+        ], $row);
 
     }
 
@@ -539,6 +604,10 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
 
         $data = $backend->getCalendarObject($returnedId, 'random-id');
 
+        if (is_resource($data['calendardata'])) {
+            $data['calendardata'] = stream_get_contents($data['calendardata']);
+        }
+
         $this->assertEquals($object2, $data['calendardata']);
         $this->assertEquals('random-id', $data['uri']);
 
@@ -1014,6 +1083,9 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
         $result = $backend->getSchedulingObject('principals/user1', 'schedule1.ics');
         foreach ($expected as $k => $v) {
             $this->assertArrayHasKey($k, $result);
+            if (is_resource($result[$k])) {
+                $result[$k] = stream_get_contents($result[$k]);
+            }
             $this->assertEquals($v, $result[$k]);
         }
 
@@ -1022,6 +1094,9 @@ abstract class AbstractPDOTest extends \PHPUnit_Framework_TestCase {
         $this->assertEquals(1, count($results));
         $result = $results[0];
         foreach ($expected as $k => $v) {
+            if (is_resource($result[$k])) {
+                $result[$k] = stream_get_contents($result[$k]);
+            }
             $this->assertEquals($v, $result[$k]);
         }
 
diff --git a/tests/Sabre/CalDAV/Backend/PDOMySQLTest.php b/tests/Sabre/CalDAV/Backend/PDOMySQLTest.php
index 8e0e072..e068ff1 100644
--- a/tests/Sabre/CalDAV/Backend/PDOMySQLTest.php
+++ b/tests/Sabre/CalDAV/Backend/PDOMySQLTest.php
@@ -2,38 +2,8 @@
 
 namespace Sabre\CalDAV\Backend;
 
-require_once 'Sabre/TestUtil.php';
-require_once 'Sabre/CalDAV/TestUtil.php';
-require_once 'Sabre/CalDAV/Backend/AbstractPDOTest.php';
-
 class PDOMySQLTest extends AbstractPDOTest {
 
-    function setup() {
-
-        if (!SABRE_HASMYSQL) $this->markTestSkipped('MySQL driver is not available, or not properly configured');
-        $pdo = \Sabre\TestUtil::getMySQLDB();
-        if (!$pdo) $this->markTestSkipped('Could not connect to mysql database');
-
-        $pdo->query('DROP TABLE IF EXISTS calendarobjects, calendars, calendarinstances, calendarchanges, calendarsubscriptions, schedulingobjects');
-
-        $queries = explode(
-            ';',
-            file_get_contents(__DIR__ . '/../../../../examples/sql/mysql.calendars.sql')
-        );
-
-        foreach ($queries as $query) {
-            $query = trim($query, " \r\n\t");
-            if ($query)
-                $pdo->exec($query);
-        }
-        $this->pdo = $pdo;
-
-    }
-
-    function teardown() {
-
-        $this->pdo = null;
-
-    }
+    public $driver = 'mysql';
 
 }
diff --git a/tests/Sabre/CalDAV/Backend/PDOPgSqlTest.php b/tests/Sabre/CalDAV/Backend/PDOPgSqlTest.php
new file mode 100644
index 0000000..e20b261
--- /dev/null
+++ b/tests/Sabre/CalDAV/Backend/PDOPgSqlTest.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Sabre\CalDAV\Backend;
+
+class PDOPgSqlTest extends AbstractPDOTest {
+
+    public $driver = 'pgsql';
+
+}
diff --git a/tests/Sabre/CalDAV/Backend/PDOSqliteTest.php b/tests/Sabre/CalDAV/Backend/PDOSqliteTest.php
index 4074259..90ad5a1 100644
--- a/tests/Sabre/CalDAV/Backend/PDOSqliteTest.php
+++ b/tests/Sabre/CalDAV/Backend/PDOSqliteTest.php
@@ -2,36 +2,8 @@
 
 namespace Sabre\CalDAV\Backend;
 
-use Sabre\CalDAV;
-
-require_once 'Sabre/CalDAV/Backend/AbstractPDOTest.php';
-
 class PDOSqliteTest extends AbstractPDOTest {
 
-    function setup() {
-
-        if (!SABRE_HASSQLITE) $this->markTestSkipped('SQLite driver is not available');
-
-        if (file_exists(SABRE_TEMPDIR . '/testdb.sqlite'))
-            unlink(SABRE_TEMPDIR . '/testdb.sqlite');
-
-        $pdo = new \PDO('sqlite:' . SABRE_TEMPDIR . '/testdb.sqlite');
-        $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
-
-        // Yup this is definitely not 'fool proof', but good enough for now.
-        $queries = explode(';', file_get_contents(__DIR__ . '/../../../../examples/sql/sqlite.calendars.sql'));
-        foreach ($queries as $query) {
-            $pdo->exec($query);
-        }
-        $this->pdo = $pdo;
-
-    }
-
-    function teardown() {
-
-        $this->pdo = null;
-        unlink(SABRE_TEMPDIR . '/testdb.sqlite');
-
-    }
+    public $driver = 'sqlite';
 
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list