Skip to content

Commit e01b70e

Browse files
authored
feat: named schema support (#288)
1 parent 37772cf commit e01b70e

File tree

5 files changed

+322
-44
lines changed

5 files changed

+322
-44
lines changed

src/Schema/Blueprint.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,28 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)
487487

488488
return $command;
489489
}
490+
491+
/**
492+
* @inheritDoc
493+
* @param list<string> $columns
494+
*/
495+
protected function createIndexName($type, array $columns)
496+
{
497+
[$schema, $table] = $this->connection
498+
->getSchemaBuilder()
499+
->parseSchemaAndTable($this->table);
500+
501+
if ($this->connection->getConfig('prefix_indexes')) {
502+
$table = $this->connection->getTablePrefix() . $table;
503+
}
504+
505+
$index = strtolower($table . '_' . implode('_', $columns) . '_' . $type);
506+
507+
if ($type !== 'foreign' && $schema !== null) {
508+
$index = $schema . '.' . $index;
509+
}
510+
511+
return str_replace('-', '_', $index);
512+
}
513+
490514
}

src/Schema/Builder.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public function setDatabaseOptions(array $options): void
6060
$connection->statement("ALTER DATABASE `{$name}` SET OPTIONS ({$line})");
6161
}
6262

63+
/**
64+
* Create a named schema with the given name.
65+
*
66+
* @param string $name
67+
* @return void
68+
*/
69+
public function createNamedSchema(string $name): void
70+
{
71+
$this->connection->statement("CREATE SCHEMA {$this->grammar->wrap($name)}");
72+
}
73+
6374
/**
6475
* @deprecated Use Blueprint::dropIndex() instead. Will be removed in v10.0.
6576
*
@@ -127,7 +138,7 @@ public function dropAllTables()
127138

128139
// add parents counter
129140
foreach ($tables as $table) {
130-
$sortedTables[$table['name']] = ['parents' => 0, ...$table];
141+
$sortedTables[$table['schema_qualified_name']] = ['parents' => 0, ...$table];
131142
}
132143

133144
// loop through all tables and count how many parents they have
@@ -150,9 +161,9 @@ public function dropAllTables()
150161
// drop foreign keys first (otherwise index queries will include them)
151162
$queries = [];
152163
foreach ($sortedTables as $tableData) {
153-
$tableName = $tableData['name'];
154-
$foreigns = $this->getForeignKeys($tableName);
155-
$blueprint = $this->createBlueprint($tableName);
164+
$sqn = $tableData['schema_qualified_name'];
165+
$foreigns = $this->getForeignKeys($sqn);
166+
$blueprint = $this->createBlueprint($sqn);
156167
foreach ($foreigns as $foreign) {
157168
$blueprint->dropForeign($foreign['name']);
158169
}
@@ -164,18 +175,23 @@ public function dropAllTables()
164175
// drop indexes and tables
165176
$queries = [];
166177
foreach ($sortedTables as $tableData) {
167-
$tableName = $tableData['name'];
168-
$indexes = $this->getIndexListing($tableName);
169-
$blueprint = $this->createBlueprint($tableName);
178+
$schema = $tableData['schema'] ?? null;
179+
$sqn = $tableData['schema_qualified_name'];
180+
$indexes = $this->getIndexListing($sqn);
181+
$blueprint = $this->createBlueprint($sqn);
170182
foreach ($indexes as $index) {
171183
if ($index === 'PRIMARY_KEY') {
172184
continue;
173185
}
186+
if ($schema !== null) {
187+
$index = $schema . '.' . $index;
188+
}
174189
$blueprint->dropIndex($index);
175190
}
176191
$blueprint->drop();
177192
array_push($queries, ...$blueprint->toSql());
178193
}
194+
179195
$connection->runDdlBatch($queries);
180196
}
181197
}

src/Schema/Grammar.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,30 @@ class Grammar extends BaseGrammar
4040
protected $modifiers = ['Nullable', 'Default', 'GeneratedAs', 'Invisible', 'Increment', 'UseSequence'];
4141

4242
/**
43-
* Compile the query to determine the tables.
44-
*
45-
* @param $schema
46-
* @return string
43+
* @inheritDoc
4744
*/
4845
public function compileTables($schema)
4946
{
5047
return implode(' ', [
5148
'select',
5249
implode(', ', [
53-
'table_name as name',
50+
'table_name as `name`',
5451
'table_schema as `schema`',
55-
'parent_table_name as parent',
52+
'parent_table_name as `parent`',
5653
]),
57-
'from information_schema.tables',
58-
'where table_type = \'BASE TABLE\'',
59-
'and table_schema = \'\'',
54+
'from `information_schema`.`tables`',
55+
'where `table_type` = \'BASE TABLE\'',
56+
(match (true) {
57+
is_array($schema) => 'and `table_schema` in (' . $this->quoteString($schema) . ')',
58+
!is_null($schema) => 'and `table_schema` = ' . $this->quoteString($schema),
59+
default => '',
60+
}),
61+
'order by `table_schema`, `table_name`'
6062
]);
6163
}
6264

6365
/**
64-
* Compile the query to determine the columns.
65-
*
66-
* @param string $table
67-
* @return string
66+
* @inheritDoc
6867
*/
6968
public function compileColumns($schema, $table)
7069
{
@@ -76,17 +75,15 @@ public function compileColumns($schema, $table)
7675
'is_nullable as `nullable`',
7776
'column_default as `default`',
7877
]),
79-
'from information_schema.columns',
80-
'where table_name = ' . $this->quoteString($table),
78+
'from `information_schema`.`columns`',
79+
'where `table_name` = ' . $this->quoteString($table),
80+
'and table_schema = ' . $this->quoteString($schema ?? ''),
81+
'order by `ordinal_position` asc',
8182
]);
8283
}
8384

8485
/**
85-
* Compile the query to determine the list of indexes.
86-
*
87-
* @param string|null $schema
88-
* @param $table
89-
* @return string
86+
* @inheritDoc
9087
*/
9188
public function compileIndexes($schema, $table)
9289
{
@@ -100,18 +97,14 @@ public function compileIndexes($schema, $table)
10097
]),
10198
'from information_schema.indexes as i',
10299
'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',
103-
'where i.table_schema = ' . $this->quoteString(''),
104-
'and i.table_name = ' . $this->quoteString($table),
105-
'group by i.index_name, i.index_type, i.is_unique',
100+
'where i.table_name = ' . $this->quoteString($table),
101+
'and i.table_schema = ' . $this->quoteString($schema ?? ''),
102+
'group by i.index_name, i.index_type, i.is_unique, i.table_schema',
106103
]);
107104
}
108105

109106
/**
110-
* Compile the query to determine the list of foreign keys.
111-
*
112-
* @param string|null $schema
113-
* @param $table
114-
* @return string
107+
* @inheritDoc
115108
*/
116109
public function compileForeignKeys($schema, $table)
117110
{
@@ -129,8 +122,8 @@ public function compileForeignKeys($schema, $table)
129122
'from information_schema.key_column_usage kc',
130123
'join information_schema.referential_constraints rc on kc.constraint_name = rc.constraint_name',
131124
'join information_schema.constraint_column_usage cc on kc.constraint_name = cc.constraint_name',
132-
'where kc.table_schema = ""',
133-
'and kc.table_name = ' . $this->quoteString($table),
125+
'where kc.table_name = ' . $this->quoteString($table),
126+
'and kc.table_schema = ' . $this->quoteString($schema ?? ''),
134127
'group by kc.constraint_name, cc.table_schema, cc.table_name, rc.update_rule, rc.delete_rule',
135128
]);
136129
}

0 commit comments

Comments
 (0)