From 439c8ee79d485e2a8eeb61245e3a4211a0f926be Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 4 Nov 2025 13:14:32 +0200 Subject: [PATCH 1/7] Add LOCK=SHARED --- src/Database/Adapter/MariaDB.php | 5 +++++ src/Database/Adapter/SQL.php | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 533673edb..694e9cf45 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2214,4 +2214,9 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool { return true; } + + public function getLockType(): string + { + return ',LOCK=SHARED'; + } } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index a7121d36d..2cc85a2a5 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -248,7 +248,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 { @@ -260,6 +260,11 @@ public function createAttribute(string $collection, string $id, string $type, in } } + public function getLockType(): string + { + return ''; + } + /** * Create Attributes * @@ -285,7 +290,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 { From 3f612c90b4d7df76095f2de4c26b5ab337d51f3d Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 4 Nov 2025 13:23:45 +0200 Subject: [PATCH 2/7] Ignore SQLite.php --- src/Database/Adapter/SQLite.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index e50bd2068..efe012380 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -1866,4 +1866,9 @@ public function getUpsertStatement( return $stmt; } + + public function getLockType(): string + { + return ''; + } } From 1a344c604d8d1232164dc7966769112871a34799 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Nov 2025 10:18:22 +0200 Subject: [PATCH 3/7] Alter locks --- src/Database/Adapter.php | 21 +++++++++++++++++++ src/Database/Adapter/MariaDB.php | 4 ++-- src/Database/Adapter/Mongo.php | 5 +++++ src/Database/Adapter/Pool.php | 5 +++++ src/Database/Adapter/SQL.php | 19 ++++++++++++----- src/Database/Adapter/SQLite.php | 4 ++-- src/Database/Database.php | 18 ++++++++++++++++ .../e2e/Adapter/SharedTables/MariaDBTest.php | 4 +++- tests/e2e/Adapter/SharedTables/MySQLTest.php | 4 +++- 9 files changed, 73 insertions(+), 11 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 4ac1f10bd..eebeb8a1f 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -31,6 +31,8 @@ abstract class Adapter protected int $inTransaction = 0; + protected bool $alterLocks = false; + /** * @var array */ @@ -1434,4 +1436,23 @@ abstract public function setSupportForAttributes(bool $support): bool; */ abstract public function getSupportForIntegerBooleans(): bool; + /** + * Does the adapter has support to change default lock mode? + * + * @return bool + */ + abstract public function getSupportForAlterLocks(): bool; + + /** + * @param bool $bool + * + * @return $this + * @throws Exception + */ + public function enableLocks(bool $bool): self + { + $this->alterLocks = $bool; + + return $this; + } } diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 694e9cf45..5f6588c2d 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2215,8 +2215,8 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool return true; } - public function getLockType(): string + public function getSupportForAlterLocks(): bool { - return ',LOCK=SHARED'; + return true; } } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 5c5aada43..e4f6a16b4 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -3209,4 +3209,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string { return ''; } + + public function getSupportForAlterLocks(): bool + { + return false; + } } diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index 2723a20db..537463ada 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -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()); + } } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 5bd1b5947..08ce3b63b 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -259,11 +259,6 @@ public function createAttribute(string $collection, string $id, string $type, in } } - public function getLockType(): string - { - return ''; - } - /** * Create Attributes * @@ -3519,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 ''; + } } diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index efe012380..1260ccca0 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -1867,8 +1867,8 @@ public function getUpsertStatement( return $stmt; } - public function getLockType(): string + public function getSupportForAlterLocks(): bool { - return ''; + return false; } } diff --git a/src/Database/Database.php b/src/Database/Database.php index c343191b5..d772eee31 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1147,6 +1147,24 @@ public function setSharedTables(bool $sharedTables): static return $this; } + /** + * Set lock mode + * + * Set lock mode when altering tables + * + * @param bool $bool + * @return static + * @throws Exception + */ + public function enableLocks(bool $bool): static + { + if ($this->adapter->getSupportForAlterLocks()) { + $this->adapter->enableLocks($bool); + } + + return $this; + } + /** * Set Tenant * diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 5205d8b6b..f6574ab0d 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -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(); diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 1824bfcc0..697c42c7e 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -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(); From cbbef110d6a84a2a1bc481b9ecf432edd0df31c0 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Nov 2025 10:33:36 +0200 Subject: [PATCH 4/7] pull main --- src/Database/Adapter.php | 6 +++--- src/Database/Database.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index eebeb8a1f..6883e0175 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1444,14 +1444,14 @@ abstract public function getSupportForIntegerBooleans(): bool; abstract public function getSupportForAlterLocks(): bool; /** - * @param bool $bool + * @param bool $enable * * @return $this * @throws Exception */ - public function enableLocks(bool $bool): self + public function enableAlterLocks(bool $enable): self { - $this->alterLocks = $bool; + $this->alterLocks = $enable; return $this; } diff --git a/src/Database/Database.php b/src/Database/Database.php index 7c7930e16..d828c5712 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1159,7 +1159,7 @@ public function setSharedTables(bool $sharedTables): static public function enableLocks(bool $bool): static { if ($this->adapter->getSupportForAlterLocks()) { - $this->adapter->enableLocks($bool); + $this->adapter->enableAlterLocks($bool); } return $this; From cb2c5a3be024ed150f63277bc779702424442bdf Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Nov 2025 10:35:05 +0200 Subject: [PATCH 5/7] Remove throw --- src/Database/Adapter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 6883e0175..516ed1bb5 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1447,7 +1447,6 @@ abstract public function getSupportForAlterLocks(): bool; * @param bool $enable * * @return $this - * @throws Exception */ public function enableAlterLocks(bool $enable): self { From b17f36f50a64c9b986002db2343729a11939a6ba Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 5 Nov 2025 10:40:29 +0200 Subject: [PATCH 6/7] Add message --- src/Database/Database.php | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index d828c5712..557ab7f38 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1147,24 +1147,6 @@ public function setSharedTables(bool $sharedTables): static return $this; } - /** - * Set lock mode - * - * Set lock mode when altering tables - * - * @param bool $bool - * @return static - * @throws Exception - */ - public function enableLocks(bool $bool): static - { - if ($this->adapter->getSupportForAlterLocks()) { - $this->adapter->enableAlterLocks($bool); - } - - return $this; - } - /** * Set Tenant * @@ -1236,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; From 2802051703e0ef87625238b2ce6c5a833e0f0754 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 5 Nov 2025 22:09:11 +1300 Subject: [PATCH 7/7] Update src/Database/Adapter.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/Database/Adapter.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 516ed1bb5..b86dc431e 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -1437,7 +1437,10 @@ abstract public function setSupportForAttributes(bool $support): bool; abstract public function getSupportForIntegerBooleans(): bool; /** - * Does the adapter has support to change default lock mode? + * 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 */