Skip to content

Commit 7b6b0ce

Browse files
committed
Sync locks
1 parent 3b39f56 commit 7b6b0ce

9 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/Database/Adapter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class Adapter
3030

3131
protected int $inTransaction = 0;
3232

33+
protected bool $alterLocks = false;
34+
3335
/**
3436
* @var array<string, mixed>
3537
*/
@@ -1412,4 +1414,25 @@ abstract public function setSupportForAttributes(bool $support): bool;
14121414
*/
14131415
abstract public function getSupportForIntegerBooleans(): bool;
14141416

1417+
/**
1418+
* Does the adapter have support for ALTER TABLE locking modes?
1419+
*
1420+
* When enabled, adapters can specify lock behavior (e.g., LOCK=SHARED)
1421+
* during ALTER TABLE operations to control concurrent access.
1422+
*
1423+
* @return bool
1424+
*/
1425+
abstract public function getSupportForAlterLocks(): bool;
1426+
1427+
/**
1428+
* @param bool $enable
1429+
*
1430+
* @return $this
1431+
*/
1432+
public function enableAlterLocks(bool $enable): self
1433+
{
1434+
$this->alterLocks = $enable;
1435+
1436+
return $this;
1437+
}
14151438
}

src/Database/Adapter/MariaDB.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2220,4 +2220,9 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
22202220
{
22212221
return true;
22222222
}
2223+
2224+
public function getSupportForAlterLocks(): bool
2225+
{
2226+
return true;
2227+
}
22232228
}

src/Database/Adapter/Mongo.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,4 +3211,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
32113211
{
32123212
return '';
32133213
}
3214+
3215+
public function getSupportForAlterLocks(): bool
3216+
{
3217+
return false;
3218+
}
32143219
}

src/Database/Adapter/Pool.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,9 @@ public function getSupportForIntegerBooleans(): bool
619619
{
620620
return $this->delegate(__FUNCTION__, \func_get_args());
621621
}
622+
623+
public function getSupportForAlterLocks(): bool
624+
{
625+
return $this->delegate(__FUNCTION__, \func_get_args());
626+
}
622627
}

src/Database/Adapter/SQL.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function createAttribute(string $collection, string $id, string $type, in
248248
{
249249
$id = $this->quote($this->filter($id));
250250
$type = $this->getSQLType($type, $size, $signed, $array, $required);
251-
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type};";
251+
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type} {$this->getLockType()};";
252252
$sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql);
253253

254254
try {
@@ -285,7 +285,7 @@ public function createAttributes(string $collection, array $attributes): bool
285285

286286
$columns = \implode(', ADD COLUMN ', $parts);
287287

288-
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$columns};";
288+
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$columns} {$this->getLockType()};";
289289
$sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql);
290290

291291
try {
@@ -3515,4 +3515,18 @@ public function setSupportForAttributes(bool $support): bool
35153515
{
35163516
return true;
35173517
}
3518+
3519+
public function getSupportForAlterLocks(): bool
3520+
{
3521+
return false;
3522+
}
3523+
3524+
public function getLockType(): string
3525+
{
3526+
if ($this->getSupportForAlterLocks() && $this->alterLocks) {
3527+
return ',LOCK=SHARED';
3528+
}
3529+
3530+
return '';
3531+
}
35183532
}

src/Database/Adapter/SQLite.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,4 +1866,9 @@ public function getUpsertStatement(
18661866

18671867
return $stmt;
18681868
}
1869+
1870+
public function getSupportForAlterLocks(): bool
1871+
{
1872+
return false;
1873+
}
18691874
}

src/Database/Database.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,23 @@ public function getTenantPerDocument(): bool
11861186
return $this->adapter->getTenantPerDocument();
11871187
}
11881188

1189+
/**
1190+
* Enable or disable LOCK=SHARED during ALTER TABLE operation
1191+
*
1192+
* Set lock mode when altering tables
1193+
*
1194+
* @param bool $enabled
1195+
* @return static
1196+
*/
1197+
public function enableLocks(bool $enabled): static
1198+
{
1199+
if ($this->adapter->getSupportForAlterLocks()) {
1200+
$this->adapter->enableAlterLocks($enabled);
1201+
}
1202+
1203+
return $this;
1204+
}
1205+
11891206
public function getPreserveDates(): bool
11901207
{
11911208
return $this->preserveDates;

tests/e2e/Adapter/SharedTables/MariaDBTest.php

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

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

tests/e2e/Adapter/SharedTables/MySQLTest.php

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

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

0 commit comments

Comments
 (0)