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

feat: add support for Schema::getIndexes #856

Merged
merged 9 commits into from
May 25, 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
31 changes: 31 additions & 0 deletions src/Oci8/Query/Processors/OracleProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,35 @@ public function processForeignKeys($results)
];
}, $results);
}

/**
* Process the results of an indexes query.
*
* @param array $results
* @return array
*/
public function processIndexes($results)
{
$collection = array_map(function ($result) {
$result = (object) $result;

return [
'name' => $name = strtolower($result->name),
'columns' => $result->columns,
'type' => strtolower($result->type),
'unique' => (bool) $result->unique,
'primary' => str_contains($name, '_pk'),
];
}, $results);

return collect($collection)->groupBy('name')->map(function ($items) {
return [
'name' => $items->first()['name'],
'columns' => $items->pluck('columns')->map(fn ($item) => strtolower($item))->all(),
'type' => $items->first()['type'],
'unique' => $items->first()['unique'],
'primary' => $items->first()['primary'],
];
})->values()->all();
}
}
20 changes: 20 additions & 0 deletions src/Oci8/Schema/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,4 +956,24 @@ protected function modifyCollate(Blueprint $blueprint, Fluent $column)
return ' collate '.$this->wrapValue($column->collation);
}
}

/**
* Compile the query to determine the indexes.
*
* @param string $database
* @param string $table
* @return string
*/
public function compileIndexes($database, $table)
{
return sprintf(
'select i.index_name as name, i.column_name as columns, '
."a.index_type as type, decode(a.uniqueness, 'UNIQUE', 1, 0) as \"UNIQUE\" "
.'from all_ind_columns i join ALL_INDEXES a on a.index_name = i.index_name '
.'WHERE i.table_name = a.table_name AND i.table_owner = a.table_owner AND '
.'i.TABLE_OWNER = upper(%s) AND i.TABLE_NAME = upper(%s) ',
$this->quoteString($database),
$this->quoteString($table)
);
}
}
17 changes: 17 additions & 0 deletions src/Oci8/Schema/OracleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,21 @@ protected function parseSchemaAndTable($reference)

return [$schema, $parts[0]];
}

/**
* Get the indexes for a given table.
*
* @param string $table
* @return array
*/
public function getIndexes($table)
{
$table = $this->connection->getTablePrefix().$table;

return $this->connection->getPostProcessor()->processIndexes(
$this->connection->selectFromWriteConnection(
$this->grammar->compileIndexes($this->connection->getConfig('username'), $table)
)
);
}
}
21 changes: 20 additions & 1 deletion tests/Functional/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function it_can_get_columns_number_precision()

$this->assertCount(1, $columns);
$this->assertTrue(collect($columns)->contains(
fn ($column) => $column['name'] === 'a_float' && $column['comment'] === 'a float.' && $column['precision'] === 4
fn ($column) => $column['name'] === 'a_float'
&& $column['comment'] === 'a float.'
&& $column['precision'] === 4
));
}

Expand Down Expand Up @@ -212,4 +214,21 @@ public function it_can_get_foreign_keys()
&& in_array('foo_id', $key['columns'])
));
}

#[Test]
public function it_can_add_column_index()
{
if (Schema::hasTable('index_table')) {
Schema::drop('index_table');
}

Schema::create('index_table', function (Blueprint $table) {
$table->integer('id');
$table->string('name')->index();
});

$indexes = array_column(Schema::getIndexes('index_table'), 'name');

$this->assertContains('index_table_name_index', $indexes, 'name');
}
}
Loading