From 2b3470d95275ebc6d7bcf181c4de01f1295c7da6 Mon Sep 17 00:00:00 2001
From: Matthieu Napoli <matthieu@mnapoli.fr>
Date: Tue, 11 Nov 2014 16:27:35 +1300
Subject: [PATCH] Added Piwik\Sequence::exists()

---
 core/Sequence.php                          | 12 ++++++++++++
 tests/PHPUnit/Integration/SequenceTest.php | 12 ++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/core/Sequence.php b/core/Sequence.php
index d755ad6d06..fa95b0b07c 100644
--- a/core/Sequence.php
+++ b/core/Sequence.php
@@ -65,6 +65,18 @@ class Sequence
         return $initialValue;
     }
 
+    /**
+     * Returns true if the sequence exist.
+     *
+     * @return bool
+     */
+    public function exists()
+    {
+        $query = $this->db->query('SELECT * FROM ' . $this->getTableName() . ' WHERE name = ?', $this->name);
+
+        return $query->rowCount() > 0;
+    }
+
     /**
      * Get / allocate / reserve a new id for the current sequence. Important: Getting the next id will fail in case
      * no such sequence exists. Make sure to create one if needed, see {@link create()}.
diff --git a/tests/PHPUnit/Integration/SequenceTest.php b/tests/PHPUnit/Integration/SequenceTest.php
index b4ac1a20fc..fbdc1b904b 100644
--- a/tests/PHPUnit/Integration/SequenceTest.php
+++ b/tests/PHPUnit/Integration/SequenceTest.php
@@ -87,6 +87,18 @@ class SequenceTest extends IntegrationTestCase
         $this->assertNull($id);
     }
 
+    public function test_exists_shouldReturnTrueIfSequenceExist()
+    {
+        $sequence = $this->getExistingSequence();
+        $this->assertTrue($sequence->exists());
+    }
+
+    public function test_exists_shouldReturnFalseIfSequenceExist()
+    {
+        $sequence = $this->getEmptySequence();
+        $this->assertFalse($sequence->exists());
+    }
+
     private function assertNextIdGenerated(Sequence $sequence, $expectedId)
     {
         $id = $sequence->getNextId();
-- 
GitLab