Skip to content

Commit 1a344c6

Browse files
committed
Alter locks
1 parent 3f612c9 commit 1a344c6

9 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/Database/Adapter.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ abstract class Adapter
3131

3232
protected int $inTransaction = 0;
3333

34+
protected bool $alterLocks = false;
35+
3436
/**
3537
* @var array<string, mixed>
3638
*/
@@ -1434,4 +1436,23 @@ abstract public function setSupportForAttributes(bool $support): bool;
14341436
*/
14351437
abstract public function getSupportForIntegerBooleans(): bool;
14361438

1439+
/**
1440+
* Does the adapter has support to change default lock mode?
1441+
*
1442+
* @return bool
1443+
*/
1444+
abstract public function getSupportForAlterLocks(): bool;
1445+
1446+
/**
1447+
* @param bool $bool
1448+
*
1449+
* @return $this
1450+
* @throws Exception
1451+
*/
1452+
public function enableLocks(bool $bool): self
1453+
{
1454+
$this->alterLocks = $bool;
1455+
1456+
return $this;
1457+
}
14371458
}

src/Database/Adapter/MariaDB.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,8 +2215,8 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
22152215
return true;
22162216
}
22172217

2218-
public function getLockType(): string
2218+
public function getSupportForAlterLocks(): bool
22192219
{
2220-
return ',LOCK=SHARED';
2220+
return true;
22212221
}
22222222
}

src/Database/Adapter/Mongo.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,4 +3209,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
32093209
{
32103210
return '';
32113211
}
3212+
3213+
public function getSupportForAlterLocks(): bool
3214+
{
3215+
return false;
3216+
}
32123217
}

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,4 +632,9 @@ public function setAuthorization(Authorization $authorization): self
632632
$this->authorization = $authorization;
633633
return $this;
634634
}
635+
636+
public function getSupportForAlterLocks(): bool
637+
{
638+
return $this->delegate(__FUNCTION__, \func_get_args());
639+
}
635640
}

src/Database/Adapter/SQL.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,6 @@ public function createAttribute(string $collection, string $id, string $type, in
259259
}
260260
}
261261

262-
public function getLockType(): string
263-
{
264-
return '';
265-
}
266-
267262
/**
268263
* Create Attributes
269264
*
@@ -3519,4 +3514,18 @@ public function setSupportForAttributes(bool $support): bool
35193514
{
35203515
return true;
35213516
}
3517+
3518+
public function getSupportForAlterLocks(): bool
3519+
{
3520+
return false;
3521+
}
3522+
3523+
public function getLockType(): string
3524+
{
3525+
if ($this->getSupportForAlterLocks() && $this->alterLocks) {
3526+
return ',LOCK=SHARED';
3527+
}
3528+
3529+
return '';
3530+
}
35223531
}

src/Database/Adapter/SQLite.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,8 +1867,8 @@ public function getUpsertStatement(
18671867
return $stmt;
18681868
}
18691869

1870-
public function getLockType(): string
1870+
public function getSupportForAlterLocks(): bool
18711871
{
1872-
return '';
1872+
return false;
18731873
}
18741874
}

src/Database/Database.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,24 @@ public function setSharedTables(bool $sharedTables): static
11471147
return $this;
11481148
}
11491149

1150+
/**
1151+
* Set lock mode
1152+
*
1153+
* Set lock mode when altering tables
1154+
*
1155+
* @param bool $bool
1156+
* @return static
1157+
* @throws Exception
1158+
*/
1159+
public function enableLocks(bool $bool): static
1160+
{
1161+
if ($this->adapter->getSupportForAlterLocks()) {
1162+
$this->adapter->enableLocks($bool);
1163+
}
1164+
1165+
return $this;
1166+
}
1167+
11501168
/**
11511169
* Set Tenant
11521170
*

tests/e2e/Adapter/SharedTables/MariaDBTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public function getDatabase(bool $fresh = false): Database
5353
->setDatabase('utopiaTests')
5454
->setSharedTables(true)
5555
->setTenant(999)
56-
->setNamespace(static::$namespace = '');
56+
->setNamespace(static::$namespace = '')
57+
->enableLocks(true)
58+
;
5759

5860
if ($database->exists()) {
5961
$database->delete();

tests/e2e/Adapter/SharedTables/MySQLTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function getDatabase(): Database
5555
->setDatabase('utopiaTests')
5656
->setSharedTables(true)
5757
->setTenant(999)
58-
->setNamespace(static::$namespace = '');
58+
->setNamespace(static::$namespace = '')
59+
->enableLocks(true)
60+
;
5961

6062
if ($database->exists()) {
6163
$database->delete();

0 commit comments

Comments
 (0)