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
8 changes: 8 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public function createCollection(string $name, array $attributes = [], array $in
/** @var array<string> $indexStrings */
$indexStrings = [];

$hash = [];

foreach ($attributes as $key => $attribute) {
$attrId = $this->filter($attribute->getId());
$hash[$attrId] = $attribute;

$attrType = $this->getSQLType(
$attribute->getAttribute('type'),
Expand Down Expand Up @@ -131,13 +134,18 @@ public function createCollection(string $name, array $attributes = [], array $in
'$updatedAt' => '_updatedAt',
default => $attribute
};

$indexAttribute = $this->filter($indexAttribute);

if ($indexType === Database::INDEX_FULLTEXT) {
$indexOrder = '';
}

$indexAttributes[$nested] = "`{$indexAttribute}`{$indexLength} {$indexOrder}";

if (!empty($hash[$indexAttribute]['array']) && $this->getSupportForCastIndexArray()) {
$indexAttributes[$nested] = '(CAST(`' . $indexAttribute . '` AS char(' . Database::ARRAY_INDEX_LENGTH . ') ARRAY))';
}
}

$indexAttributes = \implode(", ", $indexAttributes);
Expand Down
36 changes: 36 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,42 @@ public function createCollection(string $id, array $attributes = [], array $inde
throw new DuplicateException('Collection ' . $id . ' already exists');
}

/**
* Fix metadata index length & orders
*/
foreach ($indexes as $key => $index) {
$lengths = $index->getAttribute('lengths', []);
$orders = $index->getAttribute('orders', []);

foreach ($index->getAttribute('attributes', []) as $i => $attr) {
foreach ($attributes as $collectionAttribute) {
if ($collectionAttribute->getAttribute('$id') === $attr) {
/**
* mysql does not save length in collection when length = attributes size
*/
if ($collectionAttribute->getAttribute('type') === Database::VAR_STRING) {
if (!empty($lengths[$i]) && $lengths[$i] === $collectionAttribute->getAttribute('size') && $this->adapter->getMaxIndexLength() > 0) {
$lengths[$i] = null;
}
}

$isArray = $collectionAttribute->getAttribute('array', false);
if ($isArray) {
if ($this->adapter->getMaxIndexLength() > 0) {
$lengths[$i] = self::ARRAY_INDEX_LENGTH;
}
$orders[$i] = null;
}
break;
}
}
}

$index->setAttribute('lengths', $lengths);
$index->setAttribute('orders', $orders);
$indexes[$key] = $index;
}

$collection = new Document([
'$id' => ID::custom($id),
'$permissions' => $permissions,
Expand Down
68 changes: 68 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,74 @@ public function testUpdateDeleteCollectionNotFound(): void
}
}

public function testCreateCollectionWithSchemaIndexes(): void
{
$database = static::getDatabase();

$attributes = [
new Document([
'$id' => ID::custom('username'),
'type' => Database::VAR_STRING,
'size' => 100,
'required' => false,
'signed' => true,
'array' => false,
]),
new Document([
'$id' => ID::custom('cards'),
'type' => Database::VAR_STRING,
'size' => 5000,
'required' => false,
'signed' => true,
'array' => true,
]),
];

$indexes = [
new Document([
'$id' => ID::custom('idx_cards'),
'type' => Database::INDEX_KEY,
'attributes' => ['cards'],
'lengths' => [500], // Will be changed to Database::ARRAY_INDEX_LENGTH (255)
'orders' => [Database::ORDER_DESC],
]),
new Document([
'$id' => ID::custom('idx_username'),
'type' => Database::INDEX_KEY,
'attributes' => ['username'],
'lengths' => [100], // Will be removed since equal to attributes size
'orders' => [],
]),
new Document([
'$id' => ID::custom('idx_username_created_at'),
'type' => Database::INDEX_KEY,
'attributes' => ['username'],
'lengths' => [99], // Length not equal to attributes length
'orders' => [Database::ORDER_DESC],
]),
];

$collection = $database->createCollection(
'collection98',
$attributes,
$indexes,
permissions: [
Permission::create(Role::any()),
]
);

$this->assertEquals($collection->getAttribute('indexes')[0]['attributes'][0], 'cards');
$this->assertEquals($collection->getAttribute('indexes')[0]['lengths'][0], Database::ARRAY_INDEX_LENGTH);
$this->assertEquals($collection->getAttribute('indexes')[0]['orders'][0], null);

$this->assertEquals($collection->getAttribute('indexes')[1]['attributes'][0], 'username');
$this->assertEquals($collection->getAttribute('indexes')[1]['lengths'][0], null);

$this->assertEquals($collection->getAttribute('indexes')[2]['attributes'][0], 'username');
$this->assertEquals($collection->getAttribute('indexes')[2]['lengths'][0], 99);
$this->assertEquals($collection->getAttribute('indexes')[2]['orders'][0], Database::ORDER_DESC);
}

public function testGetCollectionId(): void
{
if (!static::getDatabase()->getAdapter()->getSupportForGetConnectionId()) {
Expand Down