diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 84889d15d..cf5e8494f 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -148,49 +148,49 @@ public function createCollection(string $name, array $attributes = [], array $in $indexStrings[$key] = "{$indexType} `{$indexId}` ({$indexAttributes}),"; } - $collection = " - CREATE TABLE {$this->getSQLTable($id)} ( - _id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, - _uid VARCHAR(255) NOT NULL, - _createdAt DATETIME(3) DEFAULT NULL, - _updatedAt DATETIME(3) DEFAULT NULL, - _permissions MEDIUMTEXT DEFAULT NULL, - PRIMARY KEY (_id), - " . \implode(' ', $attributeStrings) . " - " . \implode(' ', $indexStrings) . " - "; - if ($this->sharedTables) { - $collection .= " - _tenant INT(11) UNSIGNED DEFAULT NULL, - UNIQUE KEY _uid (_uid, _tenant), + $indexSql = ' + _tenant BIGINT UNSIGNED DEFAULT NULL, + UNIQUE KEY (_uid, _tenant), KEY _created_at (_tenant, _createdAt), KEY _updated_at (_tenant, _updatedAt), - KEY _tenant_id (_tenant, _id) - "; + KEY _tenant_id (_tenant, _id), + '; } else { - $collection .= " + $indexSql = ' UNIQUE KEY _uid (_uid), KEY _created_at (_createdAt), - KEY _updated_at (_updatedAt) - "; + KEY _updated_at (_updatedAt), + '; } - $collection .= ")"; + $collection = " + CREATE TABLE {$this->getSQLTable($id)} ( + `_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `_uid` VARCHAR(255) NOT NULL, + `_createdAt` DATETIME(3) DEFAULT NULL, + `_updatedAt` DATETIME(3) DEFAULT NULL, + `_permissions` MEDIUMTEXT DEFAULT NULL, + ".$indexSql." + ". \implode(' ', $attributeStrings) . " + ". \implode(' ', $indexStrings) . " + PRIMARY KEY (`_id`) + )"; + $collection = $this->trigger(Database::EVENT_COLLECTION_CREATE, $collection); $permissions = " CREATE TABLE {$this->getSQLTable($id . '_perms')} ( - _id int(11) UNSIGNED NOT NULL AUTO_INCREMENT, - _type VARCHAR(12) NOT NULL, - _permission VARCHAR(255) NOT NULL, - _document VARCHAR(255) NOT NULL, + `_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + `_type` VARCHAR(12) NOT NULL, + `_permission` VARCHAR(255) NOT NULL, + `_document` VARCHAR(255) NOT NULL, PRIMARY KEY (_id), "; if ($this->sharedTables) { $permissions .= " - _tenant INT(11) UNSIGNED DEFAULT NULL, + _tenant BIGINT UNSIGNED DEFAULT NULL, UNIQUE INDEX _index1 (_document, _tenant, _type, _permission), INDEX _permission (_tenant, _permission, _type) "; diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index d47478c12..0514dc757 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -197,11 +197,11 @@ public function createCollection(string $name, array $attributes = [], array $in $attributeStrings[] = "\"{$attrId}\" {$attrType}, "; } - $sqlTenant = $this->sharedTables ? '_tenant INTEGER DEFAULT NULL,' : ''; + $sqlTenant = $this->sharedTables ? '_tenant BIGINT DEFAULT NULL,' : ''; $collection = " CREATE TABLE {$this->getSQLTable($id)} ( - _id SERIAL NOT NULL, + _id BIGSERIAL NOT NULL, _uid VARCHAR(255) NOT NULL, ". $sqlTenant ." \"_createdAt\" TIMESTAMP(3) DEFAULT NULL, @@ -231,8 +231,8 @@ public function createCollection(string $name, array $attributes = [], array $in $permissions = " CREATE TABLE {$this->getSQLTable($id . '_perms')} ( - _id SERIAL NOT NULL, - _tenant INTEGER DEFAULT NULL, + _id BIGSERIAL NOT NULL, + _tenant BIGINT DEFAULT NULL, _type VARCHAR(12) NOT NULL, _permission VARCHAR(255) NOT NULL, _document VARCHAR(255) NOT NULL, diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 7e649919d..5bef6c3e6 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1086,9 +1086,9 @@ public function getMaxVarcharLength(): int public function getMaxIndexLength(): int { /** - * $tenant int = 1 + * $tenant bigint requires 2 index length */ - return $this->sharedTables ? 767 : 768; + return $this->sharedTables ? 766 : 768; } /** diff --git a/src/Database/Validator/Index.php b/src/Database/Validator/Index.php index 2270a6e0f..0357a5abb 100644 --- a/src/Database/Validator/Index.php +++ b/src/Database/Validator/Index.php @@ -185,10 +185,17 @@ public function checkIndexLength(Document $index): bool $attributeSize = $attribute->getAttribute('size', 0); $indexLength = $lengths[$attributePosition] ?? $attributeSize; break; + case Database::VAR_FLOAT: $attributeSize = 2; // 8 bytes / 4 mb4 $indexLength = 2; break; + + case Database::VAR_INTEGER: + $attributeSize = $attribute->getAttribute('size', 0); + $indexLength = $attributeSize = $attributeSize >= 8 ? 2 : 1; // bigint + break; + default: $attributeSize = 1; // 4 bytes / 4 mb4 $indexLength = 1;