Skip to content

Commit d577fb2

Browse files
authored
Merge pull request #752 from utopia-php/alter-shared-lock
Alter table LOCK=SHARED
2 parents c34cb23 + 2802051 commit d577fb2

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
@@ -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,25 @@ abstract public function setSupportForAttributes(bool $support): bool;
14341436
*/
14351437
abstract public function getSupportForIntegerBooleans(): bool;
14361438

1439+
/**
1440+
* Does the adapter have support for ALTER TABLE locking modes?
1441+
*
1442+
* When enabled, adapters can specify lock behavior (e.g., LOCK=SHARED)
1443+
* during ALTER TABLE operations to control concurrent access.
1444+
*
1445+
* @return bool
1446+
*/
1447+
abstract public function getSupportForAlterLocks(): bool;
1448+
1449+
/**
1450+
* @param bool $enable
1451+
*
1452+
* @return $this
1453+
*/
1454+
public function enableAlterLocks(bool $enable): self
1455+
{
1456+
$this->alterLocks = $enable;
1457+
1458+
return $this;
1459+
}
14371460
}

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
@@ -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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function createAttribute(string $collection, string $id, string $type, in
247247
{
248248
$id = $this->quote($this->filter($id));
249249
$type = $this->getSQLType($type, $size, $signed, $array, $required);
250-
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type};";
250+
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type} {$this->getLockType()};";
251251
$sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql);
252252

253253
try {
@@ -284,7 +284,7 @@ public function createAttributes(string $collection, array $attributes): bool
284284

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

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

290290
try {
@@ -3514,4 +3514,18 @@ public function setSupportForAttributes(bool $support): bool
35143514
{
35153515
return true;
35163516
}
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+
}
35173531
}

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
@@ -1218,6 +1218,23 @@ public function getTenantPerDocument(): bool
12181218
return $this->adapter->getTenantPerDocument();
12191219
}
12201220

1221+
/**
1222+
* Enable or disable LOCK=SHARED during ALTER TABLE operation
1223+
*
1224+
* Set lock mode when altering tables
1225+
*
1226+
* @param bool $enabled
1227+
* @return static
1228+
*/
1229+
public function enableLocks(bool $enabled): static
1230+
{
1231+
if ($this->adapter->getSupportForAlterLocks()) {
1232+
$this->adapter->enableAlterLocks($enabled);
1233+
}
1234+
1235+
return $this;
1236+
}
1237+
12211238
public function getPreserveDates(): bool
12221239
{
12231240
return $this->preserveDates;

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)