Skip to content

Commit

Permalink
Merge pull request #5476 from Laravel-Backpack/update-database-deps
Browse files Browse the repository at this point in the history
Add some more helper methods to ease Laravel 11 upgrade
  • Loading branch information
pxpm authored Mar 18, 2024
2 parents fc63e2e + f686588 commit 1e14f5c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
58 changes: 46 additions & 12 deletions src/app/Library/Database/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@ final class DatabaseSchema

/**
* Return the schema for the table.
*
* @param string $connection
* @param string $table
*/
public static function getForTable(string $connection, string $table)
{
self::generateDatabaseSchema($connection, $table);
$connection = $connection ?: config('database.default');

self::generateDatabaseSchema($connection);

return self::$schema[$connection][$table] ?? null;
}

public static function getTables(string $connection = null): array
{
$connection = $connection ?: config('database.default');
self::generateDatabaseSchema($connection);

return self::$schema[$connection] ?? [];
}

public function listTableColumnsNames(string $connection, string $table)
{
$table = self::getForTable($connection, $table);
Expand All @@ -34,16 +41,19 @@ public function listTableIndexes(string $connection, string $table)
return self::getIndexColumnNames($connection, $table);
}

public function getManager(string $connection = null)
{
$connection = $connection ?: config('database.default');

return self::getSchemaManager($connection);
}

/**
* Generates and store the database schema.
*
* @param string $connection
* @param string $table
* @return void
*/
private static function generateDatabaseSchema(string $connection, string $table)
private static function generateDatabaseSchema(string $connection)
{
if (! isset(self::$schema[$connection]) || ! isset(self::$schema[$connection][$table])) {
if (! isset(self::$schema[$connection])) {
self::$schema[$connection] = self::mapTables($connection);
}
}
Expand All @@ -64,7 +74,7 @@ private static function mapTables(string $connection)
}

if (is_array($table)) {
$table = new Table(self::mapTableColumns($connection, $tableName));
$table = new Table($tableName, self::mapTableColumns($connection, $tableName));
}

return [$tableName => $table];
Expand Down Expand Up @@ -103,10 +113,34 @@ private static function getCreateSchema(string $connection)
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager;
}

private static function dbalTypes()
{
return [
'enum' => \Doctrine\DBAL\Types\Types::STRING,
'jsonb' => \Doctrine\DBAL\Types\Types::JSON,
'geometry' => \Doctrine\DBAL\Types\Types::STRING,
'point' => \Doctrine\DBAL\Types\Types::STRING,
'lineString' => \Doctrine\DBAL\Types\Types::STRING,
'polygon' => \Doctrine\DBAL\Types\Types::STRING,
'multiPoint' => \Doctrine\DBAL\Types\Types::STRING,
'multiLineString' => \Doctrine\DBAL\Types\Types::STRING,
'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING,
'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING,
];
}

private static function getSchemaManager(string $connection)
{
$connection = DB::connection($connection);

return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder();
if (method_exists($connection, 'getDoctrineSchemaManager')) {
foreach (self::dbalTypes() as $key => $value) {
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value);
}

return $connection->getDoctrineSchemaManager();
}

return $connection->getSchemaBuilder();
}
}
14 changes: 13 additions & 1 deletion src/app/Library/Database/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

final class Table
{
private string $name;
private array $columns;

public function __construct(array $columns = [])
public function __construct(string $name, array $columns = [])
{
$this->name = $name;
foreach ($columns as $column) {
$this->columns[$column['name']] = new class($column)
{
Expand All @@ -30,6 +32,11 @@ public function getDefault()
return $this->column['default'];
}

public function getUnsigned()
{
return in_array('unsigned', explode(' ', $this->column['type']));
}

public function getType()
{
return new class($this->column)
Expand All @@ -48,6 +55,11 @@ public function getName()
}
}

public function getName(): string
{
return $this->name;
}

public function getColumns()
{
return $this->columns;
Expand Down
2 changes: 1 addition & 1 deletion src/app/Library/Database/TableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TableSchema

public function __construct(string $connection, string $table)
{
$this->schema = DatabaseSchema::getForTable($connection, $table);
$this->schema = app('DatabaseSchema')->getForTable($connection, $table);
}

/**
Expand Down

0 comments on commit 1e14f5c

Please sign in to comment.