From 03f2d378a8c484fb75d9e657defe4d3c2e0e0b71 Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 5 Aug 2024 16:35:29 +0200 Subject: [PATCH 1/5] Added support for multidimensional indexes --- .../2024_08_05_145621_create_events_table.php | 31 ++++++++++ TestSetup/Database/Seeders/DatabaseSeeder.php | 1 + TestSetup/Database/Seeders/EventsSeeder.php | 56 ++++++++++++++++++ TestSetup/Models/Event.php | 18 ++++++ docs/migrations.md | 17 +++--- src/Schema/Concerns/Indexes.php | 19 +++++- tests/Console/MigrateFreshCommandTest.php | 4 +- tests/Console/MigrateRefreshCommandTest.php | 4 +- tests/Schema/IndexTest.php | 58 +++++++++++++++++++ 9 files changed, 195 insertions(+), 13 deletions(-) create mode 100644 TestSetup/Database/Migrations/2024_08_05_145621_create_events_table.php create mode 100644 TestSetup/Database/Seeders/EventsSeeder.php create mode 100644 TestSetup/Models/Event.php diff --git a/TestSetup/Database/Migrations/2024_08_05_145621_create_events_table.php b/TestSetup/Database/Migrations/2024_08_05_145621_create_events_table.php new file mode 100644 index 0000000..c90a64f --- /dev/null +++ b/TestSetup/Database/Migrations/2024_08_05_145621_create_events_table.php @@ -0,0 +1,31 @@ +call(TagsSeeder::class); $this->call(TaggablesSeeder::class); $this->call(HousesSeeder::class); + $this->call(EventsSeeder::class); } } diff --git a/TestSetup/Database/Seeders/EventsSeeder.php b/TestSetup/Database/Seeders/EventsSeeder.php new file mode 100644 index 0000000..44ca194 --- /dev/null +++ b/TestSetup/Database/Seeders/EventsSeeder.php @@ -0,0 +1,56 @@ +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. diff --git a/src/Schema/Concerns/Indexes.php b/src/Schema/Concerns/Indexes.php index a7ff092..7171c40 100644 --- a/src/Schema/Concerns/Indexes.php +++ b/src/Schema/Concerns/Indexes.php @@ -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); @@ -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 diff --git a/tests/Console/MigrateFreshCommandTest.php b/tests/Console/MigrateFreshCommandTest.php index 52aa689..02cf133 100644 --- a/tests/Console/MigrateFreshCommandTest.php +++ b/tests/Console/MigrateFreshCommandTest.php @@ -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 () { @@ -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 () { diff --git a/tests/Console/MigrateRefreshCommandTest.php b/tests/Console/MigrateRefreshCommandTest.php index a61fda2..63bdc87 100644 --- a/tests/Console/MigrateRefreshCommandTest.php +++ b/tests/Console/MigrateRefreshCommandTest.php @@ -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 () { @@ -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 () { diff --git a/tests/Schema/IndexTest.php b/tests/Schema/IndexTest.php index aff397c..32764b9 100644 --- a/tests/Schema/IndexTest.php +++ b/tests/Schema/IndexTest.php @@ -147,6 +147,64 @@ }); }); +test('multiDimensionalIndex && dropMultiDimensionalIndex', function () { + $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 () { + $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']); From 836b89c022f3dd1535ca12fbd2e5d8274b8d2bee Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 5 Aug 2024 16:45:40 +0200 Subject: [PATCH 2/5] Skip mdi index tests on ArangoDB versions < 3.12 --- phpunit.xml | 2 +- tests/Schema/IndexTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 146617a..72599c4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -29,7 +29,7 @@ - + diff --git a/tests/Schema/IndexTest.php b/tests/Schema/IndexTest.php index 32764b9..6e3630d 100644 --- a/tests/Schema/IndexTest.php +++ b/tests/Schema/IndexTest.php @@ -148,6 +148,8 @@ }); test('multiDimensionalIndex && dropMultiDimensionalIndex', function () { + $this->skipTestOn('arangodb', '<', '3.12'); + $name = 'events_timeline_mdi'; Schema::table('events', function (Blueprint $table) use ($name) { From fab08c738b3f8fff7df8b98b873780a9837d1c23 Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 5 Aug 2024 16:45:57 +0200 Subject: [PATCH 3/5] Bumped up expected collection count --- tests/Migrations/MigrationRepositoryTest.php | 4 ++-- tests/Schema/SchemaBuilderTest.php | 2 +- tests/Schema/TableTest.php | 2 +- tests/Testing/DatabaseTruncationTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Migrations/MigrationRepositoryTest.php b/tests/Migrations/MigrationRepositoryTest.php index 5818911..e548f95 100644 --- a/tests/Migrations/MigrationRepositoryTest.php +++ b/tests/Migrations/MigrationRepositoryTest.php @@ -10,7 +10,7 @@ }); afterEach(function () { - $migrations = $this->databaseMigrationRepository->getMigrations(11); + $migrations = $this->databaseMigrationRepository->getMigrations(12); foreach($migrations as $migration) { $this->databaseMigrationRepository->delete($migration); } @@ -42,7 +42,7 @@ $this->databaseMigrationRepository->delete($migration); $results = $this->databaseMigrationRepository->getRan(); - ray('delete getRan', $results); + expect($results)->toBeEmpty(); }); diff --git a/tests/Schema/SchemaBuilderTest.php b/tests/Schema/SchemaBuilderTest.php index 2e18ba5..04eeb4c 100644 --- a/tests/Schema/SchemaBuilderTest.php +++ b/tests/Schema/SchemaBuilderTest.php @@ -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); diff --git a/tests/Schema/TableTest.php b/tests/Schema/TableTest.php index 694db2e..b21f092 100644 --- a/tests/Schema/TableTest.php +++ b/tests/Schema/TableTest.php @@ -101,7 +101,7 @@ $tables = Schema::getAllTables(); - expect(count($initialTables))->toEqual(15); + expect(count($initialTables))->toEqual(16); expect(count($tables))->toEqual(0); refreshDatabase(); diff --git a/tests/Testing/DatabaseTruncationTest.php b/tests/Testing/DatabaseTruncationTest.php index 1de61c3..2fc0439 100644 --- a/tests/Testing/DatabaseTruncationTest.php +++ b/tests/Testing/DatabaseTruncationTest.php @@ -11,7 +11,7 @@ test('Ensure all tables are present', function () { $tables = Schema::getAllTables(); - expect(count($tables))->toEqual(15); + expect(count($tables))->toEqual(16); }); From 9dfc02e86174f8e911a6cc1b05aa70cabf1b2fb3 Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 5 Aug 2024 16:46:34 +0200 Subject: [PATCH 4/5] Run test if currentVersion isn't set --- tests/TestCase.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 25219c2..3ea51bd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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); } From a5be2a144f1050e10ef9b520e8973c7d5aa1a393 Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 5 Aug 2024 16:56:15 +0200 Subject: [PATCH 5/5] Exclude prefixed mdi from tests on ArangoDB < 3.12 --- tests/Schema/IndexTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Schema/IndexTest.php b/tests/Schema/IndexTest.php index 6e3630d..843b04e 100644 --- a/tests/Schema/IndexTest.php +++ b/tests/Schema/IndexTest.php @@ -176,6 +176,8 @@ }); test('Prefixed multiDimensionalIndex', function () { + $this->skipTestOn('arangodb', '<', '3.12'); + $name = 'events_timeline_mdi_prefixed'; Schema::table('events', function (Blueprint $table) use ($name) {