diff --git a/src/LiteDb.php b/src/LiteDb.php index 1bf1aef..a5c5a6b 100644 --- a/src/LiteDb.php +++ b/src/LiteDb.php @@ -78,6 +78,7 @@ class LiteDb call_user_func($upgradeHandler, $this, $this->version); // Write the new version number to the database $this->dbSetMeta(self::META_SCHEMA_VERSION, $version); + $this->version = $version; } // Refresh the database version, for debug purposes @@ -179,6 +180,16 @@ class LiteDb return $this->stores[$storeName]; } + public function hasStore(string $storeName): bool + { + return array_key_exists($storeName, $this->storeMeta); + } + + public function getVersion(): int + { + return $this->version; + } + /** * * @param string diff --git a/tests/LiteDbTest.php b/tests/LiteDbTest.php new file mode 100644 index 0000000..0744a50 --- /dev/null +++ b/tests/LiteDbTest.php @@ -0,0 +1,49 @@ +createStore("test"); + }); + $this->assertTrue($db->hasStore("test")); + $this->assertFalse($db->hasStore("test2")); + $this->assertEquals(1, $db->getVersion()); + } + + public function testUpgradingDatabase() + { + $db = new LiteDb("/tmp/litedbtest.db", 2, function ($db, $v) { + $db->createStore("test2"); + }); + $this->assertTrue($db->hasStore("test")); + $this->assertTrue($db->hasStore("test2")); + $this->assertEquals(2, $db->getVersion()); + } + + public function testUsingIndexes() + { + $db = new LiteDb("/tmp/litedbtest.db", 3, function ($db, $v) { + $db->createStore("test3") + ->addIndex("foo", [ 'unique'=>true, 'primary'=>true ]); + }); + $this->assertTrue($db->hasStore("test3")); + $this->assertEquals(3, $db->getVersion()); + + $store = $db->getStore("test3"); + $this->assertInstanceOf(ObjectStore::class, $store); + + $store->add([ 'foo'=>42, 'bar'=>314 ]); + + $record = $store->get(42); + + $this->assertEquals(['foo'=>42, 'bar'=>314], $record); + } + +} \ No newline at end of file