Skip to content

Commit

Permalink
Added Schema::hasIndex (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaravelFreelancerNL authored Aug 3, 2024
1 parent b6341ad commit af2be73
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ class Builder extends \Illuminate\Database\Schema\Builder
*/
public $grammar;


/**
* index prefixes?
*/
public ?bool $prefixIndexes;

/**
* The table prefix.
*/
public string $prefix;

/**
* Create a new database Schema manager.
*
Expand All @@ -49,6 +60,13 @@ public function __construct(Connection $connection)
$this->grammar = $connection->getSchemaGrammar();

$this->schemaManager = $connection->getArangoClient()->schema();

$this->prefixIndexes = $this->connection->getConfig('prefix_indexes');

$this->prefix = $this->prefixIndexes
? $this->connection->getConfig('prefix')
: '';

}

/**
Expand Down Expand Up @@ -171,6 +189,49 @@ public function hasColumns($table, $columns)
return $this->connection->select($compilation['aqb'])[0];
}

/**
* Create a default index name for the table.
*
* @param string $type
*/
public function createIndexName(string $table, string $type, array $columns, array $options = []): string
{
$nameParts = [];
$nameParts[] = $this->prefix . $table;
$nameParts = array_merge($nameParts, $columns);
$nameParts[] = $type;
$nameParts = array_merge($nameParts, array_keys($options));
array_filter($nameParts);

$index = strtolower(implode('_', $nameParts));
$index = preg_replace("/\[\*+\]+/", '_array', $index);

return preg_replace('/[^A-Za-z0-9]+/', '_', $index);
}

/**
* Determine if the given table has a given index.
*
* @param string $table
* @param string|array<string> $index
* @param string|null $type
* @return bool
*/
public function hasIndex($table, $index, $type = null, array $options = [])
{
$name = $index;

if ($type === null) {
$type = 'persistent';
}

if (is_array($index)) {
$name = $this->createIndexName($table, $type, $index, $options = []);
}

return !!$this->schemaManager->getIndexByName($table, $name);
}

/**
* Create a database in the schema.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Schema/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@
expect($searchResult)->toBeFalse();
});


test('Schema::hasIndex', function () {
Schema::table('characters', function (Blueprint $table) {
$table->index(['name']);
});
$indexName = 'characters_name_persistent';

expect(Schema::hasIndex('characters', $indexName))->toBeTrue();
expect(Schema::hasIndex('characters', 'notAnIndex'))->toBeFalse();
});

test('Schema::hasIndex by columns', function () {
Schema::table('characters', function (Blueprint $table) {
$table->index(['name']);
});

expect(Schema::hasIndex('characters', ['name']))->toBeTrue();
expect(Schema::hasIndex('characters', ['name', 'lastName']))->toBeFalse();
});

test('index names only contains alpha numeric characters', function () {
Schema::table('characters', function (Blueprint $table) {
$indexName = $table->createIndexName('persistent', ['addresses[*]']);
Expand Down

0 comments on commit af2be73

Please sign in to comment.