Skip to content

Commit

Permalink
schema manager - better schema handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Mar 12, 2024
1 parent dabaa68 commit 784f890
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 46 deletions.
22 changes: 11 additions & 11 deletions src/Platform/Schema/MySQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function listSchemas(string $database): array
}

#[\Override]
public function listTables(string $database, string $schema = 'public'): array
protected function doListTables(string $database, string $schema): array
{
if ('public' !== $schema) {
return [];
Expand All @@ -92,7 +92,7 @@ public function listTables(string $database, string $schema = 'public'): array
}

#[\Override]
public function tableExists(string $database, string $name, string $schema = 'public'): bool
protected function doTableExists(string $database, string $schema, string $name): bool
{
if ('public' !== $schema) {
return false;
Expand All @@ -118,7 +118,7 @@ public function tableExists(string $database, string $name, string $schema = 'pu
}

#[\Override]
protected function getTableComment(string $database, string $name, string $schema = 'public'): ?string
protected function getTableComment(string $database, string $schema, string $name): ?string
{
return $this
->queryExecutor
Expand All @@ -139,7 +139,7 @@ protected function getTableComment(string $database, string $name, string $schem
}

#[\Override]
protected function getTableColumns(string $database, string $name, string $schema = 'public'): array
protected function getTableColumns(string $database, string $schema, string $name): array
{
/*
$defaultCollation = $this
Expand Down Expand Up @@ -204,7 +204,7 @@ protected function getTableColumns(string $database, string $name, string $schem
}

#[\Override]
protected function getTablePrimaryKey(string $database, string $name, string $schema = 'public'): ?Key
protected function getTablePrimaryKey(string $database, string $schema, string $name): ?Key
{
$primaryKeyColumns = $this
->queryExecutor
Expand Down Expand Up @@ -240,19 +240,19 @@ protected function getTablePrimaryKey(string $database, string $name, string $sc
}

#[\Override]
protected function getTableForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableForeignKeys(string $database, string $schema, string $name): array
{
return \array_values(\array_filter(
$this->getAllTableKeysInfo($database, $name, $schema),
$this->getAllTableKeysInfo($database, $schema, $name),
fn (ForeignKey $key) => ($key->getTable() === $name && $key->getSchema() === $schema),
));
}

#[\Override]
protected function getTableReverseForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableReverseForeignKeys(string $database, string $schema, string $name): array
{
return \array_values(\array_filter(
$this->getAllTableKeysInfo($database, $name, $schema),
$this->getAllTableKeysInfo($database, $schema, $name),
fn (ForeignKey $key) => ($key->getForeignTable() === $name && $key->getForeignSchema() === $schema),
));
}
Expand All @@ -268,7 +268,7 @@ protected function getTableReverseForeignKeys(string $database, string $name, st
*
* @return ForeignKey[]
*/
private function getAllTableKeysInfo(string $database, string $name, string $schema = 'public'): array
private function getAllTableKeysInfo(string $database, string $schema, string $name): array
{
$ret = [];

Expand Down Expand Up @@ -333,7 +333,7 @@ protected function doWriteColumnCollation(string $collation): Expression
if ('binary' === $collation) {
$characterSet = 'binary';
} else if (\str_contains($collation, '_')) {
list ($characterSet,) = \explode('_', $collation, 2);
list($characterSet,) = \explode('_', $collation, 2);
} else {
$characterSet = $collation;
$collation .= '_general_ci';
Expand Down
22 changes: 11 additions & 11 deletions src/Platform/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function listSchemas(string $database): array
}

#[\Override]
public function listTables(string $database, string $schema = 'public'): array
protected function doListTables(string $database, string $schema): array
{
return $this
->queryExecutor
Expand All @@ -89,7 +89,7 @@ public function listTables(string $database, string $schema = 'public'): array
}

#[\Override]
public function tableExists(string $database, string $name, string $schema = 'public'): bool
protected function doTableExists(string $database, string $schema, string $name): bool
{
return (bool) $this
->queryExecutor
Expand All @@ -111,7 +111,7 @@ public function tableExists(string $database, string $name, string $schema = 'pu
}

#[\Override]
protected function getTableComment(string $database, string $name, string $schema = 'public'): ?string
protected function getTableComment(string $database, string $schema, string $name): ?string
{
return $this
->queryExecutor
Expand All @@ -134,7 +134,7 @@ protected function getTableComment(string $database, string $name, string $schem
}

#[\Override]
protected function getTableColumns(string $database, string $name, string $schema = 'public'): array
protected function getTableColumns(string $database, string $schema, string $name): array
{
$defaultCollation = $this
->queryExecutor
Expand Down Expand Up @@ -191,9 +191,9 @@ protected function getTableColumns(string $database, string $name, string $schem
}

#[\Override]
protected function getTablePrimaryKey(string $database, string $name, string $schema = 'public'): ?Key
protected function getTablePrimaryKey(string $database, string $schema, string $name): ?Key
{
$result = $this->getAllTableKeysInfo($database, $name, $schema);
$result = $this->getAllTableKeysInfo($database, $schema, $name);

while ($row = $result->fetchRow()) {
if ($row->get('type') === 'p') {
Expand All @@ -213,10 +213,10 @@ protected function getTablePrimaryKey(string $database, string $name, string $sc
}

#[\Override]
protected function getTableForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableForeignKeys(string $database, string $schema, string $name): array
{
$ret = [];
$result = $this->getAllTableKeysInfo($database, $name, $schema);
$result = $this->getAllTableKeysInfo($database, $schema, $name);

while ($row = $result->fetchRow()) {
if ($row->get('type') === 'f' && $row->get('table_source', 'string') === $name) {
Expand All @@ -239,10 +239,10 @@ protected function getTableForeignKeys(string $database, string $name, string $s
}

#[\Override]
protected function getTableReverseForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableReverseForeignKeys(string $database, string $schema, string $name): array
{
$ret = [];
$result = $this->getAllTableKeysInfo($database, $name, $schema);
$result = $this->getAllTableKeysInfo($database, $schema, $name);

while ($row = $result->fetchRow()) {
if ($row->get('type') === 'f' && $row->get('table_source', 'string') !== $name) {
Expand Down Expand Up @@ -273,7 +273,7 @@ protected function getTableReverseForeignKeys(string $database, string $name, st
* Since this is querying the catalog, it will be fast no matter how
* much result this yields.
*/
private function getAllTableKeysInfo(string $database, string $name, string $schema = 'public'): Result
private function getAllTableKeysInfo(string $database, string $schema, string $name): Result
{
return $this
->queryExecutor
Expand Down
14 changes: 7 additions & 7 deletions src/Platform/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function listSchemas(string $database): array
}

#[\Override]
public function listTables(string $database, string $schema = 'public'): array
protected function doListTables(string $database, string $schema): array
{
if ('public' !== $schema) {
return [];
Expand Down Expand Up @@ -92,13 +92,13 @@ public function listTables(string $database, string $schema = 'public'): array
}

#[\Override]
public function tableExists(string $database, string $name, string $schema = 'public'): bool
protected function doTableExists(string $database, string $schema, string $name): bool
{
return \in_array($name, $this->listTables($database, $schema));
}

#[\Override]
protected function getTableComment(string $database, string $name, string $schema = 'public'): ?string
protected function getTableComment(string $database, string $schema, string $name): ?string
{
if ('public' !== $schema) {
return null;
Expand All @@ -109,7 +109,7 @@ protected function getTableComment(string $database, string $name, string $schem
}

#[\Override]
protected function getTableColumns(string $database, string $name, string $schema = 'public'): array
protected function getTableColumns(string $database, string $schema, string $name): array
{
if ('public' !== $schema) {
return [];
Expand Down Expand Up @@ -175,7 +175,7 @@ protected function getTableColumns(string $database, string $name, string $schem
}

#[\Override]
protected function getTablePrimaryKey(string $database, string $name, string $schema = 'public'): ?Key
protected function getTablePrimaryKey(string $database, string $schema, string $name): ?Key
{
if ('public' !== $schema) {
return null;
Expand Down Expand Up @@ -231,7 +231,7 @@ protected function getTablePrimaryKey(string $database, string $name, string $sc
}

#[\Override]
protected function getTableForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableForeignKeys(string $database, string $schema, string $name): array
{
if ('public' !== $schema) {
return [];
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function getTableForeignKeys(string $database, string $name, string $s
}

#[\Override]
protected function getTableReverseForeignKeys(string $database, string $name, string $schema = 'public'): array
protected function getTableReverseForeignKeys(string $database, string $schema, string $name): array
{
if ('public' !== $schema) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Read/ObjectId.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public function __construct(
private readonly string $database,
private readonly string $type,
private readonly string $name,
private readonly string $schema,
/** For a column, for example, the table name. */
private readonly ?string $namespace = null,
private readonly ?string $schema = 'public',
) {}

/**
Expand Down
Loading

0 comments on commit 784f890

Please sign in to comment.