diff --git a/composer.json b/composer.json index aeb48066..d29d35cb 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Tracer.php b/src/Tracer.php index 6b02aea7..d954625a 100644 --- a/src/Tracer.php +++ b/src/Tracer.php @@ -4,7 +4,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Filesystem\Filesystem; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\Str; class Tracer @@ -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 @@ -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; } @@ -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