Skip to content

Remove DBAL for Laravel 11: Fix type mappings #669

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

Merged
merged 5 commits into from
Feb 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"require": {
"illuminate/console": "^10.0|^11.0",
"illuminate/database": "^10.0|^11.0",
"illuminate/database": "^10.38|^11.0",
"illuminate/filesystem": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"laravel-shift/faker-registry": "^0.3.0",
Expand Down
85 changes: 52 additions & 33 deletions src/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class Tracer
Expand Down Expand Up @@ -93,7 +92,7 @@ private function loadModel(string $class)

private function extractColumns(Model $model): array
{
return Schema::getColumns($model->getTable());
return $model->getConnection()->getSchemaBuilder()->getColumns($model->getTable());
}

private function mapColumns(array $columns): array
Expand All @@ -104,23 +103,24 @@ private function mapColumns(array $columns): array
->toArray();
}

public static function columnAttributes($column): string
public static function columnAttributes(array $column): string
{
$attributes = [];

$type = self::translations($column['type_name']);
$type = self::translations($column);

if (in_array($type, ['decimal', 'float']) && str_contains($column['type'], '(')) {
if (in_array($type, ['decimal', 'float', 'time', 'timetz', 'datetime', 'datetimetz', 'timestamp', 'timestamptz', 'geography', 'geometry'])
&& str_contains($column['type'], '(')) {
$options = Str::between($column['type'], '(', ')');
if ($options) {
$type .= ':' . $options;
}
} elseif ($type === 'string' && str_contains($column['type'], '(')) {
} elseif (in_array($type, ['string', 'char']) && str_contains($column['type'], '(')) {
$length = Str::between($column['type'], '(', ')');
if ($length != 255) {
$type .= ':' . $length;
}
} elseif ($type === 'enum') {
} elseif (in_array($type, ['enum', 'set'])) {
$options = Str::between($column['type'], '(', ')');
$type .= ':' . $options;
}
Expand Down Expand Up @@ -148,38 +148,57 @@ public static function columnAttributes($column): string
return implode(' ', $attributes);
}

private static function translations(string $type): string
private static function translations(array $column): string
{
static $mappings = [
'array' => 'string',
'bigint' => 'biginteger',
'binary' => 'binary',
'blob' => 'binary',
'boolean' => 'boolean',
$type = match ($column['type']) {
'tinyint(1)', 'bit' => 'boolean',
'nvarchar(max)' => 'text',
default => null,
};

$type ??= match ($column['type_name']) {
'bigint', 'int8' => 'biginteger',
'binary', 'varbinary', 'bytea', 'image', 'blob', 'tinyblob', 'mediumblob', 'longblob' => 'binary',
// 'bit', 'varbit' => 'bit',
'boolean', 'bool' => 'boolean',
'char', 'bpchar', 'nchar' => 'char',
'date' => 'date',
'date_immutable' => 'date',
'dateinterval' => 'date',
'datetime' => 'datetime',
'datetime_immutable' => 'datetime',
'datetimetz' => 'datetimetz',
'datetimetz_immutable' => 'datetimetz',
'decimal' => 'decimal',
'datetime', 'datetime2' => 'datetime',
'datetimeoffset' => 'datetimetz',
'decimal', 'numeric' => 'decimal',
'double', 'float8' => 'double',
'enum' => 'enum',
'float' => 'float',
'guid' => 'string',
'integer' => 'integer',
'float', 'real', 'float4' => 'float',
'geography' => 'geography',
'geometry', 'geometrycollection', 'linestring', 'multilinestring', 'multipoint', 'multipolygon', 'point', 'polygon' => 'geometry',
// 'box', 'circle', 'line', 'lseg', 'path' => 'geometry',
'integer', 'int', 'int4' => 'integer',
'inet', 'cidr' => 'ipaddress',
// 'interval' => 'interval',
'json' => 'json',
'jsonb' => 'jsonb',
'longtext' => 'longtext',
'object' => 'string',
'simple_array' => 'string',
'smallint' => 'smallinteger',
'string' => 'string',
'text' => 'text',
'macaddr', 'macaddr8' => 'macadress',
'mediumint' => 'mediuminteger',
'mediumtext' => 'mediumtext',
// 'money', 'smallmoney' => 'money',
'set' => 'set',
'smallint', 'int2' => 'smallinteger',
'text', 'ntext' => 'text',
'time' => 'time',
'time_immutable' => 'time',
];

return $mappings[$type] ?? 'string';
'timestamp' => 'timestamp',
'timestamptz' => 'timestamptz',
'timetz' => 'timetz',
'tinyint' => 'tinyinteger',
'tinytext' => 'tinytext',
'uuid', 'uniqueidentifier' => 'uuid',
'varchar', 'nvarchar' => 'string',
// 'xml' => 'xml',
'year' => 'year',
default => null,
};

return $type ?? 'string';
}

private function translateColumns(array $columns): array
Expand Down