Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Prevent error in db/model commands when using unsupported columns #43635

Merged
merged 8 commits into from Aug 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Illuminate/Database/Console/DatabaseInspectionCommand.php
Expand Up @@ -16,6 +16,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 @@ -193,4 +212,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
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
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
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