Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract class Adapter

protected int $inTransaction = 0;

protected bool $alterLocks = false;

/**
* @var array<string, mixed>
*/
Expand Down Expand Up @@ -1434,4 +1436,25 @@ abstract public function setSupportForAttributes(bool $support): bool;
*/
abstract public function getSupportForIntegerBooleans(): bool;

/**
* Does the adapter have support for ALTER TABLE locking modes?
*
* When enabled, adapters can specify lock behavior (e.g., LOCK=SHARED)
* during ALTER TABLE operations to control concurrent access.
*
* @return bool
*/
abstract public function getSupportForAlterLocks(): bool;

/**
* @param bool $enable
*
* @return $this
*/
public function enableAlterLocks(bool $enable): self
{
$this->alterLocks = $enable;

return $this;
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -2220,4 +2220,9 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
{
return true;
}

public function getSupportForAlterLocks(): bool
{
return true;
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3211,4 +3211,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
{
return '';
}

public function getSupportForAlterLocks(): bool
{
return false;
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,9 @@ public function setAuthorization(Authorization $authorization): self
$this->authorization = $authorization;
return $this;
}

public function getSupportForAlterLocks(): bool
{
return $this->delegate(__FUNCTION__, \func_get_args());
}
}
18 changes: 16 additions & 2 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function createAttribute(string $collection, string $id, string $type, in
{
$id = $this->quote($this->filter($id));
$type = $this->getSQLType($type, $size, $signed, $array, $required);
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type};";
$sql = "ALTER TABLE {$this->getSQLTable($collection)} ADD COLUMN {$id} {$type} {$this->getLockType()};";
$sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql);

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

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

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

try {
Expand Down Expand Up @@ -3514,4 +3514,18 @@ public function setSupportForAttributes(bool $support): bool
{
return true;
}

public function getSupportForAlterLocks(): bool
{
return false;
}

public function getLockType(): string
{
if ($this->getSupportForAlterLocks() && $this->alterLocks) {
return ',LOCK=SHARED';
}

return '';
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1866,4 +1866,9 @@ public function getUpsertStatement(

return $stmt;
}

public function getSupportForAlterLocks(): bool
{
return false;
}
}
17 changes: 17 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,23 @@ public function getTenantPerDocument(): bool
return $this->adapter->getTenantPerDocument();
}

/**
* Enable or disable LOCK=SHARED during ALTER TABLE operation
*
* Set lock mode when altering tables
*
* @param bool $enabled
* @return static
*/
public function enableLocks(bool $enabled): static
{
if ($this->adapter->getSupportForAlterLocks()) {
$this->adapter->enableAlterLocks($enabled);
}

return $this;
}

public function getPreserveDates(): bool
{
return $this->preserveDates;
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/Adapter/SharedTables/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function getDatabase(bool $fresh = false): Database
->setDatabase('utopiaTests')
->setSharedTables(true)
->setTenant(999)
->setNamespace(static::$namespace = '');
->setNamespace(static::$namespace = '')
->enableLocks(true)
;

if ($database->exists()) {
$database->delete();
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/Adapter/SharedTables/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function getDatabase(): Database
->setDatabase('utopiaTests')
->setSharedTables(true)
->setTenant(999)
->setNamespace(static::$namespace = '');
->setNamespace(static::$namespace = '')
->enableLocks(true)
;

if ($database->exists()) {
$database->delete();
Expand Down