Skip to content

Commit

Permalink
[9.x] Prevent error in db/model commands when using unsupported colum…
Browse files Browse the repository at this point in the history
…ns (#43635)

* Prevent exception logging

* Register mappings

* Apply fixes from StyleCI

* Typo

* Apply fixes from StyleCI

* Additional type mappings

* Update ShowCommand.php

* Update DatabaseInspectionCommand.php

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people authored Aug 11, 2022
1 parent 2d834af commit ddc99ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Console/DatabaseInspectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@

abstract class DatabaseInspectionCommand extends Command
{
/**
* A map of database column types.
*
* @var array
*/
protected $typeMappings = [
'bit' => 'string',
'enum' => 'string',
'geometry' => 'string',
'geomcollection' => 'string',
'linestring' => 'string',
'multilinestring' => 'string',
'multipoint' => 'string',
'multipolygon' => 'string',
'point' => 'string',
'polygon' => 'string',
'sysname' => 'string',
];

/**
* The Composer instance.
*
Expand Down Expand Up @@ -206,4 +225,17 @@ protected function installDependencies()
}
}
}

/**
* Register custom Doctrine type mappings.
*
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
* @return void
*/
protected function registerTypeMapping(AbstractPlatform $platform)
{
foreach ($this->typeMappings as $type => $value) {
$platform->registerDoctrineTypeMapping($type, $value);
}
}
}
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Console/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function handle(ConnectionResolverInterface $connections)

$schema = $connection->getDoctrineSchemaManager();

$this->registerTypeMapping($schema->getDatabasePlatform());

$data = [
'platform' => [
'config' => $this->getConfigFromDatabase($database),
Expand Down Expand Up @@ -75,7 +77,7 @@ protected function tables(ConnectionInterface $connection, AbstractSchemaManager
'table' => $table->getName(),
'size' => $this->getTableSize($connection, $table->getName()),
'rows' => $this->option('counts') ? $connection->table($table->getName())->count() : null,
'engine' => rescue(fn () => $table->getOption('engine')),
'engine' => rescue(fn () => $table->getOption('engine'), null, false),
'comment' => $table->getComment(),
]);
}
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Database/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function handle(ConnectionResolverInterface $connections)
$connection = $connections->connection($this->input->getOption('database'));

$schema = $connection->getDoctrineSchemaManager();
$this->registerTypeMapping($schema->getDatabasePlatform());

$table = $this->argument('table') ?: $this->components->choice(
'Which table would you like to inspect?',
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function handle()
protected function getAttributes($model)
{
$schema = $model->getConnection()->getDoctrineSchemaManager();
$this->registerTypeMapping($schema->getDatabasePlatform());
$table = $model->getConnection()->getTablePrefix().$model->getTable();
$columns = $schema->listTableColumns($table);
$indexes = $schema->listTableIndexes($table);
Expand Down

0 comments on commit ddc99ff

Please sign in to comment.