From d85d579c0640494742cde71f3497eb2b02d718dd Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Wed, 2 Nov 2022 19:55:08 +0800 Subject: [PATCH 1/2] Remove deprecated `Type::getName` usage --- src/DBAL/Models/DBALColumn.php | 30 +---- src/DBAL/RegisterColumnType.php | 129 +++++++--------------- src/DBAL/Types/DoubleType.php | 2 +- src/DBAL/Types/EnumType.php | 2 +- src/DBAL/Types/GeometryCollectionType.php | 2 +- src/DBAL/Types/GeometryType.php | 2 +- src/DBAL/Types/IpAddressType.php | 2 +- src/DBAL/Types/JsonbType.php | 2 +- src/DBAL/Types/LineStringType.php | 2 +- src/DBAL/Types/LongTextType.php | 2 +- src/DBAL/Types/MacAddressType.php | 2 +- src/DBAL/Types/MediumIntegerType.php | 2 +- src/DBAL/Types/MediumTextType.php | 2 +- src/DBAL/Types/MultiLineStringType.php | 2 +- src/DBAL/Types/MultiPointType.php | 2 +- src/DBAL/Types/MultiPolygonType.php | 2 +- src/DBAL/Types/PointType.php | 2 +- src/DBAL/Types/PolygonType.php | 2 +- src/DBAL/Types/SetType.php | 2 +- src/DBAL/Types/TimeTzType.php | 2 +- src/DBAL/Types/TimestampType.php | 2 +- src/DBAL/Types/TimestampTzType.php | 2 +- src/DBAL/Types/TinyIntegerType.php | 2 +- src/DBAL/Types/Types.php | 129 +++++++++++++--------- src/DBAL/Types/UUIDType.php | 2 +- src/DBAL/Types/YearType.php | 2 +- src/Enum/Migrations/Method/ColumnType.php | 22 +++- 27 files changed, 156 insertions(+), 200 deletions(-) diff --git a/src/DBAL/Models/DBALColumn.php b/src/DBAL/Models/DBALColumn.php index 705f1f8d..a63c913a 100644 --- a/src/DBAL/Models/DBALColumn.php +++ b/src/DBAL/Models/DBALColumn.php @@ -3,7 +3,6 @@ namespace KitLoong\MigrationsGenerator\DBAL\Models; use Doctrine\DBAL\Schema\Column as DoctrineDBALColumn; -use KitLoong\MigrationsGenerator\DBAL\Types\Types; use KitLoong\MigrationsGenerator\Enum\Migrations\ColumnName; use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType; use KitLoong\MigrationsGenerator\Schema\Models\Column; @@ -105,7 +104,7 @@ public function __construct(string $table, DoctrineDBALColumn $column) { $this->tableName = $table; $this->name = $column->getName(); - $this->type = $this->mapToColumnType($column->getType()->getName()); + $this->type = ColumnType::fromDBALType($column->getType()); $this->length = $column->getLength(); $this->scale = $column->getScale(); $this->precision = $column->getPrecision(); @@ -272,33 +271,6 @@ public function isRawDefault(): bool return $this->rawDefault; } - /** - * Converts built-in DBALTypes to ColumnType (Laravel column). - * - * @param string $dbalType - * @return \KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType - */ - private function mapToColumnType(string $dbalType): ColumnType - { - $map = [ - Types::BIGINT => ColumnType::BIG_INTEGER(), - Types::BLOB => ColumnType::BINARY(), - Types::DATE_MUTABLE => ColumnType::DATE(), - Types::DATE_IMMUTABLE => ColumnType::DATE(), - Types::DATETIME_MUTABLE => ColumnType::DATETIME(), - Types::DATETIME_IMMUTABLE => ColumnType::DATETIME(), - Types::DATETIMETZ_MUTABLE => ColumnType::DATETIME_TZ(), - Types::DATETIMETZ_IMMUTABLE => ColumnType::DATETIME_TZ(), - Types::SMALLINT => ColumnType::SMALL_INTEGER(), - Types::GUID => ColumnType::UUID(), - Types::TIME_MUTABLE => ColumnType::TIME(), - Types::TIME_IMMUTABLE => ColumnType::TIME(), - ]; - - // $dbalType outside from the map has the same name with ColumnType. - return $map[$dbalType] ?? ColumnType::from($dbalType); - } - /** * Set the column type to "increments" or "*Increments" if the column is auto increment. * If the DB supports unsigned, should check if the column is unsigned. diff --git a/src/DBAL/RegisterColumnType.php b/src/DBAL/RegisterColumnType.php index 543274e3..5d7e3805 100644 --- a/src/DBAL/RegisterColumnType.php +++ b/src/DBAL/RegisterColumnType.php @@ -4,34 +4,13 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Types\Types as DoctrineDBALTypes; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use KitLoong\MigrationsGenerator\DBAL\Types\CustomType; -use KitLoong\MigrationsGenerator\DBAL\Types\DoubleType; -use KitLoong\MigrationsGenerator\DBAL\Types\EnumType; -use KitLoong\MigrationsGenerator\DBAL\Types\GeometryCollectionType; -use KitLoong\MigrationsGenerator\DBAL\Types\GeometryType; -use KitLoong\MigrationsGenerator\DBAL\Types\IpAddressType; -use KitLoong\MigrationsGenerator\DBAL\Types\JsonbType; -use KitLoong\MigrationsGenerator\DBAL\Types\LineStringType; -use KitLoong\MigrationsGenerator\DBAL\Types\LongTextType; -use KitLoong\MigrationsGenerator\DBAL\Types\MacAddressType; -use KitLoong\MigrationsGenerator\DBAL\Types\MediumIntegerType; -use KitLoong\MigrationsGenerator\DBAL\Types\MediumTextType; -use KitLoong\MigrationsGenerator\DBAL\Types\MultiLineStringType; -use KitLoong\MigrationsGenerator\DBAL\Types\MultiPointType; -use KitLoong\MigrationsGenerator\DBAL\Types\MultiPolygonType; -use KitLoong\MigrationsGenerator\DBAL\Types\PointType; -use KitLoong\MigrationsGenerator\DBAL\Types\PolygonType; -use KitLoong\MigrationsGenerator\DBAL\Types\SetType; -use KitLoong\MigrationsGenerator\DBAL\Types\TimestampType; -use KitLoong\MigrationsGenerator\DBAL\Types\TimestampTzType; -use KitLoong\MigrationsGenerator\DBAL\Types\TimeTzType; -use KitLoong\MigrationsGenerator\DBAL\Types\TinyIntegerType; use KitLoong\MigrationsGenerator\DBAL\Types\Types; -use KitLoong\MigrationsGenerator\DBAL\Types\UUIDType; -use KitLoong\MigrationsGenerator\DBAL\Types\YearType; use KitLoong\MigrationsGenerator\Enum\Driver; +use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType; use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository; class RegisterColumnType @@ -53,30 +32,29 @@ public function handle(): void $doctrineTypes = [ Driver::MYSQL()->getValue() => [ - 'bit' => Types::BOOLEAN, - 'geomcollection' => Types::GEOMETRY_COLLECTION, - 'json' => Types::JSON, - 'mediumint' => Types::MEDIUM_INTEGER, - 'tinyint' => Types::TINY_INTEGER, + 'bit' => DoctrineDBALTypes::BOOLEAN, + 'geomcollection' => ColumnType::GEOMETRY_COLLECTION, + 'mediumint' => ColumnType::MEDIUM_INTEGER, + 'tinyint' => ColumnType::TINY_INTEGER, ], Driver::PGSQL()->getValue() => [ - '_int4' => Types::TEXT, - '_int8' => Types::TEXT, - '_numeric' => Types::FLOAT, - '_text' => Types::TEXT, - 'cidr' => Types::STRING, - 'geography' => Types::GEOMETRY, - 'inet' => Types::IP_ADDRESS, - 'macaddr' => Types::MAC_ADDRESS, - 'oid' => Types::STRING, + '_int4' => DoctrineDBALTypes::TEXT, + '_int8' => DoctrineDBALTypes::TEXT, + '_numeric' => DoctrineDBALTypes::FLOAT, + '_text' => DoctrineDBALTypes::TEXT, + 'cidr' => DoctrineDBALTypes::STRING, + 'geography' => ColumnType::GEOMETRY, + 'inet' => ColumnType::IP_ADDRESS, + 'macaddr' => ColumnType::MAC_ADDRESS, + 'oid' => DoctrineDBALTypes::STRING, ], Driver::SQLITE()->getValue() => [], Driver::SQLSRV()->getValue() => [ - 'geography' => Types::GEOMETRY, - 'money' => Types::DECIMAL, - 'smallmoney' => Types::DECIMAL, - 'tinyint' => Types::TINY_INTEGER, - 'xml' => Types::TEXT, + 'geography' => ColumnType::GEOMETRY, + 'money' => DoctrineDBALTypes::DECIMAL, + 'smallmoney' => DoctrineDBALTypes::DECIMAL, + 'tinyint' => ColumnType::TINY_INTEGER, + 'xml' => DoctrineDBALTypes::TEXT, ], ]; @@ -93,38 +71,18 @@ public function handle(): void */ private function registerLaravelColumnType(): void { - /** - * The map of supported doctrine mapping types. - */ - $typeMap = [ - // [$name => $className] - Types::DOUBLE => DoubleType::class, - Types::ENUM => EnumType::class, - Types::GEOMETRY => GeometryType::class, - Types::GEOMETRY_COLLECTION => GeometryCollectionType::class, - Types::IP_ADDRESS => IpAddressType::class, - Types::JSONB => JsonbType::class, - Types::LINE_STRING => LineStringType::class, - Types::LONG_TEXT => LongTextType::class, - Types::MAC_ADDRESS => MacAddressType::class, - Types::MEDIUM_INTEGER => MediumIntegerType::class, - Types::MEDIUM_TEXT => MediumTextType::class, - Types::MULTI_LINE_STRING => MultiLineStringType::class, - Types::MULTI_POINT => MultiPointType::class, - Types::MULTI_POLYGON => MultiPolygonType::class, - Types::POINT => PointType::class, - Types::POLYGON => PolygonType::class, - Types::SET => SetType::class, - Types::TIMESTAMP => TimestampType::class, - Types::TIMESTAMP_TZ => TimestampTzType::class, - Types::TIME_TZ => TimeTzType::class, - Types::TINY_INTEGER => TinyIntegerType::class, - Types::UUID => UUIDType::class, - Types::YEAR => YearType::class, - ]; + /** @var array $typesMap */ + $typesMap = array_flip(Types::ADDITIONAL_TYPES_MAP); + + foreach ($typesMap as $type => $doctrineTypeClassName) { + // Add a new type by providing a `type` name and a `\Doctrine\DBAL\Types\Type` class name. + // eg: `$type = double`, `$doctrineTypeClassName = \KitLoong\MigrationsGenerator\DBAL\Types\DoubleType` + $this->addOrOverrideType($type, $doctrineTypeClassName); - foreach ($typeMap as $dbType => $class) { - $this->overrideDoctrineType($dbType, $class); + // Register type mapping so that Doctrine DBAL can recognize the DB column type. + // eg: `$type = double` + // Now Doctrine DBAL can recognize `column double NOT NULL` and create a column instance with type `\KitLoong\MigrationsGenerator\DBAL\Types\DoubleType`. + $this->registerDoctrineTypeMapping($type, $type); } } @@ -177,34 +135,21 @@ private function getCustomTypes(): Collection return new Collection(); } - /** - * Register custom doctrine type, override if exists. - * - * @param string $dbType - * @param string $class The class name of the custom type. - * @throws \Doctrine\DBAL\Exception - */ - private function overrideDoctrineType(string $dbType, string $class): void - { - $this->addOrOverrideType($dbType, $class); - $this->registerDoctrineTypeMapping($dbType, $dbType); - } - /** * Add or override doctrine type. * - * @param string $dbType - * @param string $class The class name of the custom type. + * @param string $type + * @param string $class The class name which is extends {@see \Doctrine\DBAL\Types\Type}. * @throws \Doctrine\DBAL\Exception */ - private function addOrOverrideType(string $dbType, string $class): void + private function addOrOverrideType(string $type, string $class): void { - if (!Type::hasType($dbType)) { - Type::addType($dbType, $class); + if (!Type::hasType($type)) { + Type::addType($type, $class); return; } - Type::overrideType($dbType, $class); + Type::overrideType($type, $class); } /** diff --git a/src/DBAL/Types/DoubleType.php b/src/DBAL/Types/DoubleType.php index 47c4150c..ad841b80 100644 --- a/src/DBAL/Types/DoubleType.php +++ b/src/DBAL/Types/DoubleType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::DOUBLE; + return ''; } } diff --git a/src/DBAL/Types/EnumType.php b/src/DBAL/Types/EnumType.php index b26c5101..049808f4 100644 --- a/src/DBAL/Types/EnumType.php +++ b/src/DBAL/Types/EnumType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::ENUM; + return ''; } } diff --git a/src/DBAL/Types/GeometryCollectionType.php b/src/DBAL/Types/GeometryCollectionType.php index b9bb7de3..6a5d31bb 100644 --- a/src/DBAL/Types/GeometryCollectionType.php +++ b/src/DBAL/Types/GeometryCollectionType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::GEOMETRY_COLLECTION; + return ''; } } diff --git a/src/DBAL/Types/GeometryType.php b/src/DBAL/Types/GeometryType.php index 6adc0185..b73e6edf 100644 --- a/src/DBAL/Types/GeometryType.php +++ b/src/DBAL/Types/GeometryType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::GEOMETRY; + return ''; } } diff --git a/src/DBAL/Types/IpAddressType.php b/src/DBAL/Types/IpAddressType.php index 17aa0482..d83187fa 100644 --- a/src/DBAL/Types/IpAddressType.php +++ b/src/DBAL/Types/IpAddressType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::IP_ADDRESS; + return ''; } } diff --git a/src/DBAL/Types/JsonbType.php b/src/DBAL/Types/JsonbType.php index 5833349f..4ff05ee3 100644 --- a/src/DBAL/Types/JsonbType.php +++ b/src/DBAL/Types/JsonbType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::JSONB; + return ''; } } diff --git a/src/DBAL/Types/LineStringType.php b/src/DBAL/Types/LineStringType.php index 72a59b1e..70870dd9 100644 --- a/src/DBAL/Types/LineStringType.php +++ b/src/DBAL/Types/LineStringType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::LINE_STRING; + return ''; } } diff --git a/src/DBAL/Types/LongTextType.php b/src/DBAL/Types/LongTextType.php index 6d3b643e..0da6422e 100644 --- a/src/DBAL/Types/LongTextType.php +++ b/src/DBAL/Types/LongTextType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::LONG_TEXT; + return ''; } } diff --git a/src/DBAL/Types/MacAddressType.php b/src/DBAL/Types/MacAddressType.php index 47a19a4d..8f68d153 100644 --- a/src/DBAL/Types/MacAddressType.php +++ b/src/DBAL/Types/MacAddressType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MAC_ADDRESS; + return ''; } } diff --git a/src/DBAL/Types/MediumIntegerType.php b/src/DBAL/Types/MediumIntegerType.php index 7f6f2d4b..926b7a43 100644 --- a/src/DBAL/Types/MediumIntegerType.php +++ b/src/DBAL/Types/MediumIntegerType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MEDIUM_INTEGER; + return ''; } } diff --git a/src/DBAL/Types/MediumTextType.php b/src/DBAL/Types/MediumTextType.php index 7d1c3dd2..aeb8298b 100644 --- a/src/DBAL/Types/MediumTextType.php +++ b/src/DBAL/Types/MediumTextType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MEDIUM_TEXT; + return ''; } } diff --git a/src/DBAL/Types/MultiLineStringType.php b/src/DBAL/Types/MultiLineStringType.php index 6bf4ca69..2c5d8064 100644 --- a/src/DBAL/Types/MultiLineStringType.php +++ b/src/DBAL/Types/MultiLineStringType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MULTI_LINE_STRING; + return ''; } } diff --git a/src/DBAL/Types/MultiPointType.php b/src/DBAL/Types/MultiPointType.php index cb18bf51..719b3d9e 100644 --- a/src/DBAL/Types/MultiPointType.php +++ b/src/DBAL/Types/MultiPointType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MULTI_POINT; + return ''; } } diff --git a/src/DBAL/Types/MultiPolygonType.php b/src/DBAL/Types/MultiPolygonType.php index 752f2021..167c5b71 100644 --- a/src/DBAL/Types/MultiPolygonType.php +++ b/src/DBAL/Types/MultiPolygonType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::MULTI_POLYGON; + return ''; } } diff --git a/src/DBAL/Types/PointType.php b/src/DBAL/Types/PointType.php index fb234bbb..01152c39 100644 --- a/src/DBAL/Types/PointType.php +++ b/src/DBAL/Types/PointType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::POINT; + return ''; } } diff --git a/src/DBAL/Types/PolygonType.php b/src/DBAL/Types/PolygonType.php index 0be97aaa..d284435e 100644 --- a/src/DBAL/Types/PolygonType.php +++ b/src/DBAL/Types/PolygonType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::POLYGON; + return ''; } } diff --git a/src/DBAL/Types/SetType.php b/src/DBAL/Types/SetType.php index 60ca1918..6084f5b7 100644 --- a/src/DBAL/Types/SetType.php +++ b/src/DBAL/Types/SetType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::SET; + return ''; } } diff --git a/src/DBAL/Types/TimeTzType.php b/src/DBAL/Types/TimeTzType.php index 4d44ef07..547473e7 100644 --- a/src/DBAL/Types/TimeTzType.php +++ b/src/DBAL/Types/TimeTzType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::TIME_TZ; + return ''; } } diff --git a/src/DBAL/Types/TimestampType.php b/src/DBAL/Types/TimestampType.php index fd2a4b77..be04ec9d 100644 --- a/src/DBAL/Types/TimestampType.php +++ b/src/DBAL/Types/TimestampType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::TIMESTAMP; + return ''; } } diff --git a/src/DBAL/Types/TimestampTzType.php b/src/DBAL/Types/TimestampTzType.php index 65a3b30e..1672dc0e 100644 --- a/src/DBAL/Types/TimestampTzType.php +++ b/src/DBAL/Types/TimestampTzType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::TIMESTAMP_TZ; + return ''; } } diff --git a/src/DBAL/Types/TinyIntegerType.php b/src/DBAL/Types/TinyIntegerType.php index c75b5daa..793a7247 100644 --- a/src/DBAL/Types/TinyIntegerType.php +++ b/src/DBAL/Types/TinyIntegerType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::TINY_INTEGER; + return ''; } } diff --git a/src/DBAL/Types/Types.php b/src/DBAL/Types/Types.php index 4d5fbd91..c36fadc3 100644 --- a/src/DBAL/Types/Types.php +++ b/src/DBAL/Types/Types.php @@ -2,63 +2,88 @@ namespace KitLoong\MigrationsGenerator\DBAL\Types; -use Doctrine\DBAL\Types\Types as BuiltInTypes; +use Doctrine\DBAL\Types\AsciiStringType; +use Doctrine\DBAL\Types\BigIntType; +use Doctrine\DBAL\Types\BinaryType; +use Doctrine\DBAL\Types\BlobType; +use Doctrine\DBAL\Types\BooleanType; +use Doctrine\DBAL\Types\DateImmutableType; +use Doctrine\DBAL\Types\DateIntervalType; +use Doctrine\DBAL\Types\DateTimeImmutableType; +use Doctrine\DBAL\Types\DateTimeType; +use Doctrine\DBAL\Types\DateTimeTzImmutableType; +use Doctrine\DBAL\Types\DateTimeTzType; +use Doctrine\DBAL\Types\DateType; +use Doctrine\DBAL\Types\DecimalType; +use Doctrine\DBAL\Types\FloatType; +use Doctrine\DBAL\Types\GuidType; +use Doctrine\DBAL\Types\IntegerType; +use Doctrine\DBAL\Types\JsonType; +use Doctrine\DBAL\Types\SimpleArrayType; +use Doctrine\DBAL\Types\SmallIntType; +use Doctrine\DBAL\Types\StringType; +use Doctrine\DBAL\Types\TextType; +use Doctrine\DBAL\Types\TimeImmutableType; +use Doctrine\DBAL\Types\TimeType; use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType; final class Types { - // Default built-in types provided by Doctrine DBAL. - public const ASCII_STRING = BuiltInTypes::ASCII_STRING; - public const BIGINT = BuiltInTypes::BIGINT; - public const BINARY = BuiltInTypes::BINARY; - public const BLOB = BuiltInTypes::BLOB; - public const BOOLEAN = BuiltInTypes::BOOLEAN; - public const DATE_MUTABLE = BuiltInTypes::DATE_MUTABLE; - public const DATE_IMMUTABLE = BuiltInTypes::DATE_IMMUTABLE; - public const DATEINTERVAL = BuiltInTypes::DATEINTERVAL; - public const DATETIME_MUTABLE = BuiltInTypes::DATETIME_MUTABLE; - public const DATETIME_IMMUTABLE = BuiltInTypes::DATETIME_IMMUTABLE; - public const DATETIMETZ_MUTABLE = BuiltInTypes::DATETIMETZ_MUTABLE; - public const DATETIMETZ_IMMUTABLE = BuiltInTypes::DATETIMETZ_IMMUTABLE; - public const DECIMAL = BuiltInTypes::DECIMAL; - public const FLOAT = BuiltInTypes::FLOAT; - public const GUID = BuiltInTypes::GUID; - public const INTEGER = BuiltInTypes::INTEGER; - public const JSON = BuiltInTypes::JSON; - public const SIMPLE_ARRAY = BuiltInTypes::SIMPLE_ARRAY; - public const SMALLINT = BuiltInTypes::SMALLINT; - public const STRING = BuiltInTypes::STRING; - public const TEXT = BuiltInTypes::TEXT; - public const TIME_MUTABLE = BuiltInTypes::TIME_MUTABLE; - public const TIME_IMMUTABLE = BuiltInTypes::TIME_IMMUTABLE; + /** + * Default built-in types provided by Doctrine DBAL. + */ + public const BUILTIN_TYPES_MAP = [ + AsciiStringType::class => ColumnType::STRING, + BigIntType::class => ColumnType::BIG_INTEGER, + BinaryType::class => ColumnType::BINARY, + BlobType::class => ColumnType::BINARY, + BooleanType::class => ColumnType::BOOLEAN, + DateType::class => ColumnType::DATE, + DateImmutableType::class => ColumnType::DATE, + DateIntervalType::class => ColumnType::DATE, + DateTimeType::class => ColumnType::DATETIME, + DateTimeImmutableType::class => ColumnType::DATETIME, + DateTimeTzType::class => ColumnType::DATETIME_TZ, + DateTimeTzImmutableType::class => ColumnType::DATETIME_TZ, + DecimalType::class => ColumnType::DECIMAL, + FloatType::class => ColumnType::FLOAT, + GuidType::class => ColumnType::UUID, + IntegerType::class => ColumnType::INTEGER, + JsonType::class => ColumnType::JSON, + SimpleArrayType::class => ColumnType::STRING, + SmallIntType::class => ColumnType::SMALL_INTEGER, + StringType::class => ColumnType::STRING, + TextType::class => ColumnType::TEXT, + TimeType::class => ColumnType::TIME, + TimeImmutableType::class => ColumnType::TIME, + ]; /** - * Custom types, should identical with CustomDoctrineType name. - * Example, - * - * @see \KitLoong\MigrationsGenerator\DBAL\Types\DoubleType::getName() + * Additional types provided by Migration Generator. */ - public const DOUBLE = ColumnType::DOUBLE; - public const ENUM = ColumnType::ENUM; - public const GEOMETRY = ColumnType::GEOMETRY; - public const GEOMETRY_COLLECTION = ColumnType::GEOMETRY_COLLECTION; - public const IP_ADDRESS = ColumnType::IP_ADDRESS; - public const JSONB = ColumnType::JSONB; - public const LINE_STRING = ColumnType::LINE_STRING; - public const LONG_TEXT = ColumnType::LONG_TEXT; - public const MAC_ADDRESS = ColumnType::MAC_ADDRESS; - public const MEDIUM_INTEGER = ColumnType::MEDIUM_INTEGER; - public const MEDIUM_TEXT = ColumnType::MEDIUM_TEXT; - public const MULTI_LINE_STRING = ColumnType::MULTI_LINE_STRING; - public const MULTI_POINT = ColumnType::MULTI_POINT; - public const MULTI_POLYGON = ColumnType::MULTI_POLYGON; - public const POINT = ColumnType::POINT; - public const POLYGON = ColumnType::POLYGON; - public const SET = ColumnType::SET; - public const TIMESTAMP = ColumnType::TIMESTAMP; - public const TIMESTAMP_TZ = ColumnType::TIMESTAMP_TZ; - public const TIME_TZ = ColumnType::TIME_TZ; - public const TINY_INTEGER = ColumnType::TINY_INTEGER; - public const UUID = ColumnType::UUID; - public const YEAR = ColumnType::YEAR; + public const ADDITIONAL_TYPES_MAP = [ + DoubleType::class => ColumnType::DOUBLE, + EnumType::class => ColumnType::ENUM, + GeometryType::class => ColumnType::GEOMETRY, + GeometryCollectionType::class => ColumnType::GEOMETRY_COLLECTION, + IpAddressType::class => ColumnType::IP_ADDRESS, + JsonbType::class => ColumnType::JSONB, + LineStringType::class => ColumnType::LINE_STRING, + LongTextType::class => ColumnType::LONG_TEXT, + MacAddressType::class => ColumnType::MAC_ADDRESS, + MediumIntegerType::class => ColumnType::MEDIUM_INTEGER, + MediumTextType::class => ColumnType::MEDIUM_TEXT, + MultiLineStringType::class => ColumnType::MULTI_LINE_STRING, + MultiPointType::class => ColumnType::MULTI_POINT, + MultiPolygonType::class => ColumnType::MULTI_POLYGON, + PointType::class => ColumnType::POINT, + PolygonType::class => ColumnType::POLYGON, + SetType::class => ColumnType::SET, + TimestampType::class => ColumnType::TIMESTAMP, + TimestampTzType::class => ColumnType::TIMESTAMP_TZ, + TimeTzType::class => ColumnType::TIME_TZ, + TinyIntegerType::class => ColumnType::TINY_INTEGER, + UUIDType::class => ColumnType::UUID, + YearType::class => ColumnType::YEAR, + ]; } diff --git a/src/DBAL/Types/UUIDType.php b/src/DBAL/Types/UUIDType.php index a08e4fda..4fc87980 100644 --- a/src/DBAL/Types/UUIDType.php +++ b/src/DBAL/Types/UUIDType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::UUID; + return ''; } } diff --git a/src/DBAL/Types/YearType.php b/src/DBAL/Types/YearType.php index a6a1efd3..13aaafd7 100644 --- a/src/DBAL/Types/YearType.php +++ b/src/DBAL/Types/YearType.php @@ -24,6 +24,6 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform) */ public function getName() { - return Types::YEAR; + return ''; } } diff --git a/src/Enum/Migrations/Method/ColumnType.php b/src/Enum/Migrations/Method/ColumnType.php index e74d5cda..6dc3d73a 100644 --- a/src/Enum/Migrations/Method/ColumnType.php +++ b/src/Enum/Migrations/Method/ColumnType.php @@ -2,15 +2,17 @@ namespace KitLoong\MigrationsGenerator\Enum\Migrations\Method; +use Doctrine\DBAL\Types\Type; +use KitLoong\MigrationsGenerator\DBAL\Types\Types; use MyCLabs\Enum\Enum; /** - * Preserved column types of the framework. + * Define column types of the framework. + * Keep const as public to allow used by: + * {@see \KitLoong\MigrationsGenerator\DBAL\RegisterColumnType::registerLaravelColumnType()} + * {@see \KitLoong\MigrationsGenerator\DBAL\Types\Types} * * @link https://laravel.com/docs/master/migrations#available-column-types - * - * Keep cons as public to allow value assign in const property. - * @see \KitLoong\MigrationsGenerator\DBAL\Types\Types * @method static self BIG_INTEGER() * @method static self BIG_INCREMENTS() * @method static self BINARY() @@ -122,4 +124,16 @@ final class ColumnType extends Enum public const UNSIGNED_TINY_INTEGER = 'unsignedTinyInteger'; public const UUID = 'uuid'; public const YEAR = 'year'; + + /** + * Create instance from {@see \Doctrine\DBAL\Types\Type}. + * + * @param \Doctrine\DBAL\Types\Type $dbalType + * @return static + */ + public static function fromDBALType(Type $dbalType): self + { + $map = Types::BUILTIN_TYPES_MAP + Types::ADDITIONAL_TYPES_MAP; + return self::from($map[get_class($dbalType)]); + } } From 24dafdb2a361fb9afcc21003b182d0beb7fcd95b Mon Sep 17 00:00:00 2001 From: Kit Loong Date: Wed, 2 Nov 2022 21:27:50 +0800 Subject: [PATCH 2/2] Fix #133 Generate SQL Server user defined types --- README.md | 9 +++++++-- src/DBAL/Models/DBALCustomColumn.php | 9 ++++++++- src/DBAL/RegisterColumnType.php | 20 ++++++++++++++------ src/Repositories/SQLSrvRepository.php | 19 +++++++++++++++++++ tests/Feature/SQLSrv/CommandTest.php | 4 ++++ tests/Feature/SQLSrv/SQLSrvTestCase.php | 11 +++++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index afe0587f..0abb1bb1 100644 --- a/README.md +++ b/README.md @@ -142,9 +142,9 @@ The generator first generates all tables and then adds foreign keys to existing However, SQLite only supports foreign keys upon creation of the table and not when tables are altered. *_add_foreign_keys_* migrations will still be generated, however will get omitted if migrate to SQLite type database. -## PostgreSQL Custom Column Type +## User Defined Custom Column Type -The generator will register custom data type from `pg_type`, then generate migration as +The generator will register custom data type from the schema, then generate migration as ```php public function up() @@ -158,6 +158,11 @@ public function up() Note that the new `column` is always added at the end of the created `table` which means the ordering of the column generated in migration will differ from what we have from the schema. +Supported DB: + +- [x] PostgreSQL +- [x] SQL Server + ## Thank You Thanks to Bernhard Breytenbach for his great work. This package is cloned from https://github.com/Xethron/migrations-generator. diff --git a/src/DBAL/Models/DBALCustomColumn.php b/src/DBAL/Models/DBALCustomColumn.php index 18d3b20a..3f9d5204 100644 --- a/src/DBAL/Models/DBALCustomColumn.php +++ b/src/DBAL/Models/DBALCustomColumn.php @@ -31,7 +31,14 @@ public function __construct(string $table, DoctrineDBALColumn $column) { $this->name = $column->getName(); $this->tableName = $table; - $this->sqls = DB::getDoctrineConnection()->getDatabasePlatform()->getAlterTableSQL(new TableDiff($this->tableName, [$column])); + + // COLLATE clause cannot be used on user-defined data types. + // Unset collation here. + $platformOptions = $column->getPlatformOptions(); + unset($platformOptions['collation']); + $column->setPlatformOptions($platformOptions); + + $this->sqls = DB::getDoctrineConnection()->getDatabasePlatform()->getAlterTableSQL(new TableDiff($this->tableName, [$column])); } /** diff --git a/src/DBAL/RegisterColumnType.php b/src/DBAL/RegisterColumnType.php index 5d7e3805..ea35db98 100644 --- a/src/DBAL/RegisterColumnType.php +++ b/src/DBAL/RegisterColumnType.php @@ -12,14 +12,17 @@ use KitLoong\MigrationsGenerator\Enum\Driver; use KitLoong\MigrationsGenerator\Enum\Migrations\Method\ColumnType; use KitLoong\MigrationsGenerator\Repositories\PgSQLRepository; +use KitLoong\MigrationsGenerator\Repositories\SQLSrvRepository; class RegisterColumnType { private $pgSQLRepository; + private $sqlSrvRepository; - public function __construct(PgSQLRepository $pgSQLRepository) + public function __construct(PgSQLRepository $pgSQLRepository, SQLSrvRepository $sqlSrvRepository) { - $this->pgSQLRepository = $pgSQLRepository; + $this->pgSQLRepository = $pgSQLRepository; + $this->sqlSrvRepository = $sqlSrvRepository; } /** @@ -128,11 +131,16 @@ public function getName() */ private function getCustomTypes(): Collection { - if (DB::getDriverName() === Driver::PGSQL()->getValue()) { - return $this->pgSQLRepository->getCustomDataTypes(); - } + switch (DB::getDriverName()) { + case Driver::PGSQL(): + return $this->pgSQLRepository->getCustomDataTypes(); + + case Driver::SQLSRV(): + return $this->sqlSrvRepository->getCustomDataTypes(); - return new Collection(); + default: + return new Collection(); + } } /** diff --git a/src/Repositories/SQLSrvRepository.php b/src/Repositories/SQLSrvRepository.php index c0d39cf6..c30cc8cd 100644 --- a/src/Repositories/SQLSrvRepository.php +++ b/src/Repositories/SQLSrvRepository.php @@ -194,4 +194,23 @@ public function getEnumPresetValues(string $table, string $column): Collection return new Collection(array_reverse(explode('\' OR ' . $separator, $value))); } + + /** + * Get a list of custom data types. + * + * @return \Illuminate\Support\Collection + */ + public function getCustomDataTypes(): Collection + { + $rows = DB::select("SELECT * FROM sys.types WHERE is_user_defined = 1"); + $types = new Collection(); + + if (count($rows) > 0) { + foreach ($rows as $row) { + $types->push($row->name); + } + } + + return $types; + } } diff --git a/tests/Feature/SQLSrv/CommandTest.php b/tests/Feature/SQLSrv/CommandTest.php index d7724ad7..f3729025 100644 --- a/tests/Feature/SQLSrv/CommandTest.php +++ b/tests/Feature/SQLSrv/CommandTest.php @@ -34,6 +34,10 @@ public function testRun() { $migrateTemplates = function () { $this->migrateGeneral('sqlsrv'); + + DB::statement( + "ALTER TABLE all_columns_sqlsrv ADD accountnumber accountnumber NOT NULL" + ); }; $generateMigrations = function () { diff --git a/tests/Feature/SQLSrv/SQLSrvTestCase.php b/tests/Feature/SQLSrv/SQLSrvTestCase.php index 7bacadb8..d325fd05 100644 --- a/tests/Feature/SQLSrv/SQLSrvTestCase.php +++ b/tests/Feature/SQLSrv/SQLSrvTestCase.php @@ -31,6 +31,17 @@ protected function getEnvironmentSetUp($app) ]); } + protected function setUp(): void + { + parent::setUp(); + + // Drop first. + DB::statement("DROP TYPE IF EXISTS accountnumber"); + + // Create for custom column type test. + DB::statement("CREATE TYPE accountnumber FROM [nvarchar](15) NULL"); + } + protected function dumpSchemaAs(string $destination): void { $tables = DB::getDoctrineSchemaManager()->listTableNames();