Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Primary key bigint #482

Open
wants to merge 5 commits into
base: 0.53.x
Choose a base branch
from
Open
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
52 changes: 26 additions & 26 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
";
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Database/Validator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading