mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-23 06:55:03 +00:00
Test for advisory locking
This commit is contained in:
parent
f0663e99c3
commit
689c1c77d0
3 changed files with 35 additions and 13 deletions
|
@ -94,26 +94,26 @@ class Database {
|
||||||
case "integer":
|
case "integer":
|
||||||
$type = "int";
|
$type = "int";
|
||||||
case "int":
|
case "int":
|
||||||
$value =& $in;
|
$value = $in;
|
||||||
break;
|
break;
|
||||||
case "float":
|
case "float":
|
||||||
case "double":
|
case "double":
|
||||||
case "real":
|
case "real":
|
||||||
$type = "numeric";
|
$type = "numeric";
|
||||||
case "numeric":
|
case "numeric":
|
||||||
$value =& $in;
|
$value = $in;
|
||||||
break;
|
break;
|
||||||
case "str":
|
case "str":
|
||||||
case "string":
|
case "string":
|
||||||
$type = "text";
|
$type = "text";
|
||||||
case "text":
|
case "text":
|
||||||
$value =& $in;
|
$value = $in;
|
||||||
break;
|
break;
|
||||||
case "json":
|
case "json":
|
||||||
if(is_array($in) || is_object($in)) {
|
if(is_array($in) || is_object($in)) {
|
||||||
$value = json_encode($in);
|
$value = json_encode($in);
|
||||||
} else {
|
} else {
|
||||||
$value =& $in;
|
$value = $in;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "datetime":
|
case "datetime":
|
||||||
|
@ -156,10 +156,10 @@ class Database {
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$type = "text";
|
$type = "text";
|
||||||
$value =& $in;
|
$value = $in;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$this->db->prepare("REPLACE INTO newssync_settings(key,value,type) values(?,?,?)", "str", "str", "str")->run($key, $value, $type);
|
return (bool) $this->db->prepare("REPLACE INTO newssync_settings(key,value,type) values(?,?,?)", "str", "str", "str")->run($key, $value, $type)->changes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function settingRemove(string $key): bool {
|
public function settingRemove(string $key): bool {
|
||||||
|
|
|
@ -8,8 +8,8 @@ abstract class AbstractDriver implements Driver {
|
||||||
|
|
||||||
public function schemaVersion(): int {
|
public function schemaVersion(): int {
|
||||||
try {
|
try {
|
||||||
return $this->data->db->settingGet("schema_version");
|
return (int) $this->query("SELECT value from newssync_settings where key is schema_version")->getValue();
|
||||||
} catch(\Throwable $e) {
|
} catch(Exception $e) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,19 +47,23 @@ abstract class AbstractDriver implements Driver {
|
||||||
if($this->schemaVersion() < 1) return true;
|
if($this->schemaVersion() < 1) return true;
|
||||||
if($this->isLocked()) return false;
|
if($this->isLocked()) return false;
|
||||||
$uuid = UUID::mintStr();
|
$uuid = UUID::mintStr();
|
||||||
if(!$this->data->db->settingSet("lock", $uuid)) return false;
|
try {
|
||||||
|
$this->prepare("INSERT INTO newssync_settings(key,value) values(?,?)", "str", "str")->run("lock", $uuid);
|
||||||
|
} catch(ExceptionInput $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
if($this->data->db->settingGet("lock") != $uuid) return false;
|
return ($this->query("SELECT value from newssync_settings where key is 'lock'")->getValue() == $uuid);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function unlock(): bool {
|
public function unlock(): bool {
|
||||||
return $this->data->db->settingRemove("lock");
|
$this->exec("DELETE from newssync_settings where key is 'lock'");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isLocked(): bool {
|
public function isLocked(): bool {
|
||||||
if($this->schemaVersion() < 1) return false;
|
if($this->schemaVersion() < 1) return false;
|
||||||
return ($this->query("SELECT count(*) from newssync_settings where key = 'lock'")->getSingle() > 0);
|
return ($this->query("SELECT count(*) from newssync_settings where key is 'lock'")->getValue() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prepare(string $query, string ...$paramType): Statement {
|
public function prepare(string $query, string ...$paramType): Statement {
|
||||||
|
|
|
@ -246,4 +246,22 @@ class TestDbDriverSQLite3 extends \PHPUnit\Framework\TestCase {
|
||||||
$this->assertEquals(2, $this->drv->query($select)->getValue());
|
$this->assertEquals(2, $this->drv->query($select)->getValue());
|
||||||
$this->assertEquals(2, $ch->querySingle($select));
|
$this->assertEquals(2, $ch->querySingle($select));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testManipulateAdvisoryLock() {
|
||||||
|
$this->assertFalse($this->drv->isLocked());
|
||||||
|
$this->assertTrue($this->drv->lock());
|
||||||
|
$this->assertFalse($this->drv->isLocked());
|
||||||
|
$this->drv->exec("CREATE TABLE newssync_settings(key primary key, value, type) without rowid; PRAGMA user_version=1");
|
||||||
|
$this->assertTrue($this->drv->lock());
|
||||||
|
$this->assertTrue($this->drv->isLocked());
|
||||||
|
$this->assertFalse($this->drv->lock());
|
||||||
|
$this->drv->exec("PRAGMA user_version=0");
|
||||||
|
$this->assertFalse($this->drv->isLocked());
|
||||||
|
$this->assertTrue($this->drv->lock());
|
||||||
|
$this->assertFalse($this->drv->isLocked());
|
||||||
|
$this->drv->exec("PRAGMA user_version=1");
|
||||||
|
$this->assertTrue($this->drv->isLocked());
|
||||||
|
$this->assertTrue($this->drv->unlock());
|
||||||
|
$this->assertFalse($this->drv->isLocked());
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue