Skip to content
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
24 changes: 24 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,28 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)

return $command;
}

/**
* @inheritDoc
* @param list<string> $columns
*/
protected function createIndexName($type, array $columns)
{
[$schema, $table] = $this->connection
->getSchemaBuilder()
->parseSchemaAndTable($this->table);

if ($this->connection->getConfig('prefix_indexes')) {
$table = $this->connection->getTablePrefix() . $table;
}

$index = strtolower($table . '_' . implode('_', $columns) . '_' . $type);

if ($type !== 'foreign' && $schema !== null) {
$index = $schema . '.' . $index;
}

return str_replace('-', '_', $index);
}

}
30 changes: 23 additions & 7 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ public function setDatabaseOptions(array $options): void
$connection->statement("ALTER DATABASE `{$name}` SET OPTIONS ({$line})");
}

/**
* Create a named schema with the given name.
*
* @param string $name
* @return void
*/
public function createNamedSchema(string $name): void
{
$this->connection->statement("CREATE SCHEMA {$this->grammar->wrap($name)}");
}

/**
* @deprecated Use Blueprint::dropIndex() instead. Will be removed in v10.0.
*
Expand Down Expand Up @@ -127,7 +138,7 @@ public function dropAllTables()

// add parents counter
foreach ($tables as $table) {
$sortedTables[$table['name']] = ['parents' => 0, ...$table];
$sortedTables[$table['schema_qualified_name']] = ['parents' => 0, ...$table];
}

// loop through all tables and count how many parents they have
Expand All @@ -150,9 +161,9 @@ public function dropAllTables()
// drop foreign keys first (otherwise index queries will include them)
$queries = [];
foreach ($sortedTables as $tableData) {
$tableName = $tableData['name'];
$foreigns = $this->getForeignKeys($tableName);
$blueprint = $this->createBlueprint($tableName);
$sqn = $tableData['schema_qualified_name'];
$foreigns = $this->getForeignKeys($sqn);
$blueprint = $this->createBlueprint($sqn);
foreach ($foreigns as $foreign) {
$blueprint->dropForeign($foreign['name']);
}
Expand All @@ -164,18 +175,23 @@ public function dropAllTables()
// drop indexes and tables
$queries = [];
foreach ($sortedTables as $tableData) {
$tableName = $tableData['name'];
$indexes = $this->getIndexListing($tableName);
$blueprint = $this->createBlueprint($tableName);
$schema = $tableData['schema'] ?? null;
$sqn = $tableData['schema_qualified_name'];
$indexes = $this->getIndexListing($sqn);
$blueprint = $this->createBlueprint($sqn);
foreach ($indexes as $index) {
if ($index === 'PRIMARY_KEY') {
continue;
}
if ($schema !== null) {
$index = $schema . '.' . $index;
}
$blueprint->dropIndex($index);
}
$blueprint->drop();
array_push($queries, ...$blueprint->toSql());
}

$connection->runDdlBatch($queries);
}
}
53 changes: 23 additions & 30 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,30 @@ class Grammar extends BaseGrammar
protected $modifiers = ['Nullable', 'Default', 'GeneratedAs', 'Invisible', 'Increment', 'UseSequence'];

/**
* Compile the query to determine the tables.
*
* @param $schema
* @return string
* @inheritDoc
*/
public function compileTables($schema)
{
return implode(' ', [
'select',
implode(', ', [
'table_name as name',
'table_name as `name`',
'table_schema as `schema`',
'parent_table_name as parent',
'parent_table_name as `parent`',
]),
'from information_schema.tables',
'where table_type = \'BASE TABLE\'',
'and table_schema = \'\'',
'from `information_schema`.`tables`',
'where `table_type` = \'BASE TABLE\'',
(match (true) {
is_array($schema) => 'and `table_schema` in (' . $this->quoteString($schema) . ')',
!is_null($schema) => 'and `table_schema` = ' . $this->quoteString($schema),
default => '',
}),
'order by `table_schema`, `table_name`'
]);
}

/**
* Compile the query to determine the columns.
*
* @param string $table
* @return string
* @inheritDoc
*/
public function compileColumns($schema, $table)
{
Expand All @@ -76,17 +75,15 @@ public function compileColumns($schema, $table)
'is_nullable as `nullable`',
'column_default as `default`',
]),
'from information_schema.columns',
'where table_name = ' . $this->quoteString($table),
'from `information_schema`.`columns`',
'where `table_name` = ' . $this->quoteString($table),
'and table_schema = ' . $this->quoteString($schema ?? ''),
'order by `ordinal_position` asc',
]);
}

/**
* Compile the query to determine the list of indexes.
*
* @param string|null $schema
* @param $table
* @return string
* @inheritDoc
*/
public function compileIndexes($schema, $table)
{
Expand All @@ -100,18 +97,14 @@ public function compileIndexes($schema, $table)
]),
'from information_schema.indexes as i',
'join information_schema.index_columns as c on i.table_schema = c.table_schema and i.table_name = c.table_name and i.index_name = c.index_name',
'where i.table_schema = ' . $this->quoteString(''),
'and i.table_name = ' . $this->quoteString($table),
'group by i.index_name, i.index_type, i.is_unique',
'where i.table_name = ' . $this->quoteString($table),
'and i.table_schema = ' . $this->quoteString($schema ?? ''),
'group by i.index_name, i.index_type, i.is_unique, i.table_schema',
]);
}

/**
* Compile the query to determine the list of foreign keys.
*
* @param string|null $schema
* @param $table
* @return string
* @inheritDoc
*/
public function compileForeignKeys($schema, $table)
{
Expand All @@ -129,8 +122,8 @@ public function compileForeignKeys($schema, $table)
'from information_schema.key_column_usage kc',
'join information_schema.referential_constraints rc on kc.constraint_name = rc.constraint_name',
'join information_schema.constraint_column_usage cc on kc.constraint_name = cc.constraint_name',
'where kc.table_schema = ""',
'and kc.table_name = ' . $this->quoteString($table),
'where kc.table_name = ' . $this->quoteString($table),
'and kc.table_schema = ' . $this->quoteString($schema ?? ''),
'group by kc.constraint_name, cc.table_schema, cc.table_name, rc.update_rule, rc.delete_rule',
]);
}
Expand Down
Loading