Skip to content

127 support multidimensional indexes #154

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

Merged
merged 5 commits into from
Aug 5, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace TestSetup\Database\Migrations;

use Illuminate\Database\Migrations\Migration;
use LaravelFreelancerNL\Aranguent\Facades\Schema;
use LaravelFreelancerNL\Aranguent\Schema\Blueprint;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('events', function (Blueprint $collection) {
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('events');
}
};
1 change: 1 addition & 0 deletions TestSetup/Database/Seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public function run()
$this->call(TagsSeeder::class);
$this->call(TaggablesSeeder::class);
$this->call(HousesSeeder::class);
$this->call(EventsSeeder::class);
}
}
56 changes: 56 additions & 0 deletions TestSetup/Database/Seeders/EventsSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use TestSetup\Models\Event;

class EventsSeeder extends Seeder
{
/**
* Run the database Seeds.
*
* @return void
*/
public function run()
{
$events = '[
{
"_key": "first-men-invase-westeros",
"name": "First men invade Westeros",
"type": "invasion",
"age": "The Dawn Age",
"timeline": {
"starts_at": -12000.0,
"ends_at": -10000.0
}
},
{
"_key": "the-long-night",
"name": "The Long Night",
"type": "war",
"age": "The Age of Heroes",
"timeline": {
"starts_at": -8000.0,
"ends_at": -7900.0
}
},
{
"_key": "aegon-the-conqueror-invades-westeros",
"name": "Aegon the Conqueror invades Westeros",
"type": "invasion",
"age": "The Targaryen Conquest",
"timeline": {
"starts_at": -2.0,
"ends_at": 0.0
}
}
]';

$events = json_decode($events, JSON_OBJECT_AS_ARRAY);

foreach ($events as $event) {
Event::insertOrIgnore($event);
}
}
}
18 changes: 18 additions & 0 deletions TestSetup/Models/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TestSetup\Models;

use LaravelFreelancerNL\Aranguent\Eloquent\Model;

class Event extends Model
{
protected $table = 'events';

protected $fillable = [
'id',
'name',
'type',
'age',
'timeline',
];
}
17 changes: 9 additions & 8 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ See the [ArangoDB Documentation for all options](https://docs.arangodb.com/3.3/H
Within the collection blueprint you can create indexes.
This following indexes are supported:

Type | Purpose | Blueprint Method
---------- |-----------------------------| ----------------
Persistent | Ranged matching | `$table->index($columns = null, $name = null, $algorithm = null, $indexOptions = [])`
Primary * | Unique ranged matching | `$table->primary($columns = null, $name = null, $indexOptions = [])`
Unique | Unique ranged matching | `$table->unique($attributes, $indexOptions = [])`
Geo | Location matching | `$table->spatialIndex($columns, $name = null, $indexOptions = [])`
TTL | Auto-expiring documents | `$table->ttlIndex($columns, $expireAfter, $name = null, $indexOptions = [])`
Inverted | Fast full text searching | `$table->invertedIndex($columns = null, $name = null, $indexOptions = [])`
Type | Purpose | Blueprint Method
---------- |--------------------------| ----------------
Persistent | Ranged matching | `$table->index($columns = null, $name = null, $algorithm = null, $indexOptions = [])`
Primary * | Unique ranged matching | `$table->primary($columns = null, $name = null, $indexOptions = [])`
Unique | Unique ranged matching | `$table->unique($attributes, $indexOptions = [])`
Geo | Location matching | `$table->spatialIndex($columns, $name = null, $indexOptions = [])`
TTL | Auto-expiring documents | `$table->ttlIndex($columns, $expireAfter, $name = null, $indexOptions = [])`
Inverted | Fast full text searching | `$table->invertedIndex($columns = null, $name = null, $indexOptions = [])`
multiDimensional | 2D+ numeric search | `$table->multiDimensionalIndex($columns = null, $name = null, $indexOptions = [], $type = 'mdi')`

* the primary method is supported for composite keys. ArangoDB already sets a primary index on the _key property.

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</coverage>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="ARANGODB_VERSION" value="3.11"/>
<env name="ARANGODB_VERSION" value="3.12"/>
<env name="LARAVEL_VERSION" value="11"/>
<env name="RAY_ENABLED" value="(true)"/>
<env name="SEND_CACHE_TO_RAY" value="(false)"/>
Expand Down
19 changes: 18 additions & 1 deletion src/Schema/Concerns/Indexes.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ public function invertedIndex($columns = null, $name = null, $indexOptions = [])
return $this->indexCommand('inverted', $columns, $name, $indexOptions);
}


public function multiDimensionalIndex(array $columns = null, string $name = null, array $indexOptions = [], string $type = 'mdi'): Fluent
{
return $this->indexCommand(
$type,
$columns,
$name,
$indexOptions,
);
}

public function persistentIndex(array $columns = null, string $name = null, array $indexOptions = []): Fluent
{
return $this->indexCommand('persistent', $columns, $name, $indexOptions);
Expand Down Expand Up @@ -228,7 +239,13 @@ public function dropInvertedIndex(string $name): Fluent
return $this->dropIndex($name);
}


/**
* Indicate that the given index should be dropped.
*/
public function dropMultiDimensionalIndex(string $name): Fluent
{
return $this->dropIndex($name);
}

/**
* Drop the index by first getting all the indexes on the table; then selecting the matching one
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/MigrateFreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
])->assertExitCode(0);

$collections = $this->schemaManager->getCollections(true);
expect(count($collections))->toBe(15);
expect(count($collections))->toBe(16);
});

test('migrate:fresh --database=arangodb', function () {
Expand All @@ -46,7 +46,7 @@
])->assertExitCode(0);

$collections = $this->schemaManager->getCollections(true);
expect(count($collections))->toBe(15);
expect(count($collections))->toBe(16);
});

test('migrate:fresh --database=none', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/MigrateRefreshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
])->assertExitCode(0);

$collections = $this->schemaManager->getCollections(true);
expect(count($collections))->toBe(15);
expect(count($collections))->toBe(16);
});

test('migrate:refresh --database=arangodb', function () {
Expand All @@ -45,7 +45,7 @@
])->assertExitCode(0);

$collections = $this->schemaManager->getCollections(true);
expect(count($collections))->toBe(15);
expect(count($collections))->toBe(16);
});

test('migrate:refresh --database=none', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/Migrations/MigrationRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
});

afterEach(function () {
$migrations = $this->databaseMigrationRepository->getMigrations(11);
$migrations = $this->databaseMigrationRepository->getMigrations(12);
foreach($migrations as $migration) {
$this->databaseMigrationRepository->delete($migration);
}
Expand Down Expand Up @@ -42,7 +42,7 @@
$this->databaseMigrationRepository->delete($migration);

$results = $this->databaseMigrationRepository->getRan();
ray('delete getRan', $results);

expect($results)->toBeEmpty();
});

Expand Down
62 changes: 62 additions & 0 deletions tests/Schema/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,68 @@
});
});

test('multiDimensionalIndex && dropMultiDimensionalIndex', function () {
$this->skipTestOn('arangodb', '<', '3.12');

$name = 'events_timeline_mdi';

Schema::table('events', function (Blueprint $table) use ($name) {
$table->multiDimensionalIndex(
columns: [
'timeline.starts_at',
'timeline.ends_at',
],
name: $name,
indexOptions: [
'fieldValueTypes' => 'double',
],
);
});

$index = $this->schemaManager->getIndexByName('events', $name);

expect($index->name)->toEqual($name);
expect($index->type)->toEqual('mdi');

Schema::table('events', function (Blueprint $table) use ($name) {
$table->dropMultiDimensionalIndex($name);
});
});

test('Prefixed multiDimensionalIndex', function () {
$this->skipTestOn('arangodb', '<', '3.12');

$name = 'events_timeline_mdi_prefixed';

Schema::table('events', function (Blueprint $table) use ($name) {
$table->multiDimensionalIndex(
columns: [
'timeline.starts_at',
'timeline.ends_at',
],
name: $name,
indexOptions: [
'fieldValueTypes' => 'double',
'prefixFields' => [
'age',
'type',
],
],
type: 'mdi-prefixed',
);
});

$index = $this->schemaManager->getIndexByName('events', $name);

expect($index->name)->toEqual($name);
expect($index->type)->toEqual('mdi-prefixed');

Schema::table('events', function (Blueprint $table) use ($name) {
$table->dropMultiDimensionalIndex($name);
});
});


test('persistentIndex', function () {
Schema::table('characters', function (Blueprint $table) {
$table->persistentIndex(['name']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

$tables = Schema::getAllTables();

expect(count($initialTables))->toEqual(15);
expect(count($initialTables))->toEqual(16);
expect(count($tables))->toEqual(0);

$this->artisan('migrate:install')->assertExitCode(0);
Expand Down
2 changes: 1 addition & 1 deletion tests/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

$tables = Schema::getAllTables();

expect(count($initialTables))->toEqual(15);
expect(count($initialTables))->toEqual(16);
expect(count($tables))->toEqual(0);

refreshDatabase();
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ protected function skipTestOn(string $software, string $operator = '<', string $
$currentVersion = getenv(strtoupper($software . '_VERSION'));
}

if (!$currentVersion) {
return;
}

if (version_compare($currentVersion, $version, $operator)) {
$this->markTestSkipped('This test does not support ' . ucfirst($software) . ' versions ' . $operator . ' ' . $version);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Testing/DatabaseTruncationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
test('Ensure all tables are present', function () {
$tables = Schema::getAllTables();

expect(count($tables))->toEqual(15);
expect(count($tables))->toEqual(16);
});


Expand Down
Loading