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

Dynamic attibute for related primary key #243

Open
wants to merge 10 commits into
base: main
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
3 changes: 3 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str

return "VARCHAR({$size})";

case Database::VAR_ID:
return 'INT UNSIGNED'; // Same as Primary Key Sqlite does not allow INT(11)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this INT(11) and override for SQLite?


case Database::VAR_INTEGER: // We don't support zerofill: https://stackoverflow.com/a/5634147/2299554
$signed = ($signed) ? '' : ' UNSIGNED';

Expand Down
3 changes: 3 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,9 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str

return "VARCHAR({$size})";

case Database::VAR_ID:
return 'INTEGER';

case Database::VAR_INTEGER: // We don't support zerofill: https://stackoverflow.com/a/5634147/2299554

if ($size >= 8) { // INT = 4 bytes, BIGINT = 8 bytes
Expand Down
6 changes: 6 additions & 0 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ public function getAttributeWidth(Document $collection): int
$total += 4; // INT takes 4 bytes
}
break;

case Database::VAR_ID:
$total += 4;
break;

case Database::VAR_FLOAT:
// DOUBLE takes 8 bytes
$total += 8;
Expand All @@ -357,6 +362,7 @@ public function getAttributeWidth(Document $collection): int
case Database::VAR_DATETIME:
$total += 19; // 2022-06-26 14:46:24
break;

default:
throw new Exception('Unknown Type');
}
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Database
public const VAR_FLOAT = 'double';
public const VAR_BOOLEAN = 'boolean';
public const VAR_DATETIME = 'datetime';
public const VAR_ID = 'id';

// Relationships Types
public const VAR_DOCUMENT = 'document';
Expand Down Expand Up @@ -759,6 +760,7 @@ public function createAttribute(string $collection, string $id, string $type, in
case self::VAR_FLOAT:
case self::VAR_BOOLEAN:
case self::VAR_DATETIME:
case self::VAR_ID:
break;
default:
throw new Exception('Unknown attribute type: ' . $type);
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ public function isValid($document): bool
$validator = new Integer();
break;

case Database::VAR_ID:
$validator = new Text(24, min: 0); // Mongo Id length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be 36 for non-mongo adapters 🤔 how can we handle both?

break;

case Database::VAR_FLOAT:
$validator = new FloatValidator();
break;
Expand Down
31 changes: 28 additions & 3 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ public function testCreateCollectionWithSchema(): void
'array' => false,
'filters' => [],
]),
new Document([
'$id' => ID::custom('collection_id'),
'type' => Database::VAR_ID,
'size' => 0,
'required' => true,
'signed' => false,
'array' => false,
'filters' => [],
]),
];

$indexes = [
Expand All @@ -360,6 +369,13 @@ public function testCreateCollectionWithSchema(): void
'lengths' => [],
'orders' => ['DESC', 'ASC'],
]),
new Document([
'$id' => ID::custom('index4'),
'type' => Database::INDEX_KEY,
'attributes' => ['collection_id'],
'lengths' => [],
'orders' => ['DESC', 'ASC'],
]),
];

$collection = static::getDatabase()->createCollection('withSchema', $attributes, $indexes);
Expand All @@ -368,7 +384,7 @@ public function testCreateCollectionWithSchema(): void
$this->assertEquals('withSchema', $collection->getId());

$this->assertIsArray($collection->getAttribute('attributes'));
$this->assertCount(3, $collection->getAttribute('attributes'));
$this->assertCount(4, $collection->getAttribute('attributes'));
$this->assertEquals('attribute1', $collection->getAttribute('attributes')[0]['$id']);
$this->assertEquals(Database::VAR_STRING, $collection->getAttribute('attributes')[0]['type']);
$this->assertEquals('attribute2', $collection->getAttribute('attributes')[1]['$id']);
Expand All @@ -377,14 +393,20 @@ public function testCreateCollectionWithSchema(): void
$this->assertEquals(Database::VAR_BOOLEAN, $collection->getAttribute('attributes')[2]['type']);

$this->assertIsArray($collection->getAttribute('indexes'));
$this->assertCount(3, $collection->getAttribute('indexes'));
$this->assertCount(4, $collection->getAttribute('indexes'));
$this->assertEquals('index1', $collection->getAttribute('indexes')[0]['$id']);
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[0]['type']);
$this->assertEquals('index2', $collection->getAttribute('indexes')[1]['$id']);
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[1]['type']);
$this->assertEquals('index3', $collection->getAttribute('indexes')[2]['$id']);
$this->assertEquals(Database::INDEX_KEY, $collection->getAttribute('indexes')[2]['type']);

$doc = static::getDatabase()->createDocument('withSchema', new Document([
'collection_id' => $collection->getInternalId()
]));

$this->assertIsString($doc->getAttribute('collection_id'));

static::getDatabase()->deleteCollection('withSchema');

// Test collection with dash (+attribute +index)
Expand Down Expand Up @@ -550,7 +572,7 @@ public function testCreateCollectionValidator(): void

public function testCreateDocument(): Document
{
static::getDatabase()->createCollection('documents');
$collection = static::getDatabase()->createCollection('documents');

$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'string', Database::VAR_STRING, 128, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'integer', Database::VAR_INTEGER, 0, true));
Expand All @@ -560,6 +582,7 @@ public function testCreateDocument(): Document
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'colors', Database::VAR_STRING, 32, true, null, true, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'empty', Database::VAR_STRING, 32, false, null, true, true));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'with-dash', Database::VAR_STRING, 128, false, null));
$this->assertEquals(true, static::getDatabase()->createAttribute('documents', 'collection_id', Database::VAR_ID, 0, false));

$document = static::getDatabase()->createDocument('documents', new Document([
'$permissions' => [
Expand All @@ -584,6 +607,7 @@ public function testCreateDocument(): Document
'colors' => ['pink', 'green', 'blue'],
'empty' => [],
'with-dash' => 'Works',
'collection_id' => $collection->getInternalId(),
]));

$this->assertNotEmpty(true, $document->getId());
Expand All @@ -601,6 +625,7 @@ public function testCreateDocument(): Document
$this->assertEquals(['pink', 'green', 'blue'], $document->getAttribute('colors'));
$this->assertEquals([], $document->getAttribute('empty'));
$this->assertEquals('Works', $document->getAttribute('with-dash'));
$this->assertEquals($collection->getInternalId(), $document->getAttribute('collection_id'));

return $document;
}
Expand Down