From e3617739829a9f7008467e1cfe16764cd58573ad Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:00:30 -0400 Subject: [PATCH 01/31] Refactor `ColumnSchemaBuilder::class`, add shortcuts for generator columns, add new type column `auto_increment`, add more test. --- src/db/ColumnSchemaBuilder.php | 361 ++++++++++-------- src/db/QueryBuilder.php | 40 +- src/db/Schema.php | 228 +++++++---- src/db/mssql/ColumnSchemaBuilder.php | 179 ++++++++- src/db/mssql/QueryBuilder.php | 60 ++- src/db/mssql/Schema.php | 35 +- src/db/mysql/ColumnSchemaBuilder.php | 168 ++++++-- src/db/mysql/QueryBuilder.php | 15 +- src/db/mysql/Schema.php | 31 +- src/db/oci/ColumnSchemaBuilder.php | 30 +- src/db/oci/Schema.php | 22 +- src/db/pgsql/Schema.php | 31 +- src/db/sqlite/ColumnSchemaBuilder.php | 27 +- src/db/sqlite/Schema.php | 26 +- .../db/mssql/provider/ColumnTypeProvider.php | 283 ++++++++++++++ .../db/mssql/querybuilder/ColumnTypeTest.php | 120 ++++++ .../db/mssql/querybuilder/CommentOnTest.php | 113 ++++++ .../db/mysql/provider/ColumnTypeProvider.php | 230 +++++++++++ .../db/mysql/querybuilder/ColumnTypeTest.php | 168 ++++++++ .../provider/AbstractColumnTypeProvider.php | 94 +++++ .../db/querybuilder/AbstractColumnType.php | 57 +++ tests/support/TestHelper.php | 29 ++ 22 files changed, 1927 insertions(+), 420 deletions(-) create mode 100644 tests/framework/db/mssql/provider/ColumnTypeProvider.php create mode 100644 tests/framework/db/mssql/querybuilder/ColumnTypeTest.php create mode 100644 tests/framework/db/mssql/querybuilder/CommentOnTest.php create mode 100644 tests/framework/db/mysql/provider/ColumnTypeProvider.php create mode 100644 tests/framework/db/mysql/querybuilder/ColumnTypeTest.php create mode 100644 tests/framework/db/provider/AbstractColumnTypeProvider.php create mode 100644 tests/framework/db/querybuilder/AbstractColumnType.php create mode 100644 tests/support/TestHelper.php diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index d041d457..81042bc4 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -1,9 +1,6 @@ - * @since 2.0.6 */ -class ColumnSchemaBuilder extends BaseObject +class ColumnSchemaBuilder extends BaseObject implements \Stringable { - // Internally used constants representing categories that abstract column types fall under. - // See [[$categoryMap]] for mappings of abstract column types to category. - // @since 2.0.8 - const CATEGORY_PK = 'pk'; - const CATEGORY_STRING = 'string'; - const CATEGORY_NUMERIC = 'numeric'; - const CATEGORY_TIME = 'time'; - const CATEGORY_OTHER = 'other'; + /** + * Abstract column type for `INTEGER AUTO_INCREMENT`. + */ + public const CATEGORY_AUTO = 'auto'; + + /** + * Abstract column type for `BIGINT AUTO_INCREMENT`. + */ + public const CATEGORY_BIGAUTO = 'bigauto'; /** - * @var string the column type definition such as INTEGER, VARCHAR, DATETIME, etc. + * Abstract column type for `INTEGER AUTO_INCREMENT PRIMARY KEY`. */ - protected $type; + public const CATEGORY_PK = 'pk'; + /** - * @var int|string|array column size or precision definition. This is what goes into the parenthesis after + * Abstract column type for `BIGINT AUTO_INCREMENT PRIMARY KEY`. + */ + public const CATEGORY_BIGPK = 'bigpk'; + + /** + * Abstract column type for `STRING` columns. + */ + public const CATEGORY_STRING = 'string'; + + /** + * Abstract column type for `NUMERIC` columns. + */ + public const CATEGORY_NUMERIC = 'numeric'; + + /** + * Abstract column type for `TIME` columns. + */ + public const CATEGORY_TIME = 'time'; + + /** + * Abstract column type for `OTHER` columns. + */ + public const CATEGORY_OTHER = 'other'; + + /** + * @var string|null the column type definition such as INTEGER, VARCHAR, DATETIME, etc. + */ + protected string|null $type = null; + + /** + * @var array|int|string|null column size or precision definition. This is what goes into the parenthesis after * the column type. This can be either a string, an integer or an array. If it is an array, the array values will * be joined into a string separated by comma. */ - protected $length; + protected array|int|string|null $length = null; + /** * @var bool|null whether the column is or not nullable. If this is `true`, a `NOT NULL` constraint will be added. * If this is `false`, a `NULL` constraint will be added. */ - protected $isNotNull; + protected bool|null $isNotNull = null; + /** * @var bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added. */ - protected $isUnique = false; + protected bool $isUnique = false; + /** * @var string the `CHECK` constraint for the column. */ - protected $check; + protected string $check = ''; + /** * @var mixed default value of the column. */ - protected $default; + protected mixed $default = null; + /** * @var mixed SQL string to be appended to column schema definition. - * @since 2.0.9 */ - protected $append; + protected mixed $append = null; + /** * @var bool whether the column values should be unsigned. If this is `true`, an `UNSIGNED` keyword will be added. - * @since 2.0.7 */ - protected $isUnsigned = false; + protected bool $isUnsigned = false; + /** * @var string the column after which this column will be added. - * @since 2.0.8 */ - protected $after; + protected string $after = ''; + /** * @var bool whether this column is to be inserted at the beginning of the table. - * @since 2.0.8 */ - protected $isFirst; + protected bool $isFirst = false; + /** + * @var Connection the database connection. This is used mainly to escape table and column names. + */ + protected Connection $db; /** * @var array mapping of abstract column types (keys) to type categories (values). - * @since 2.0.43 */ - public static $typeCategoryMap = [ + public array $typeCategoryMap = [ + Schema::TYPE_AUTO => self::CATEGORY_AUTO, + Schema::TYPE_BIGAUTO => self::CATEGORY_BIGAUTO, Schema::TYPE_PK => self::CATEGORY_PK, Schema::TYPE_UPK => self::CATEGORY_PK, Schema::TYPE_BIGPK => self::CATEGORY_PK, @@ -107,216 +142,235 @@ class ColumnSchemaBuilder extends BaseObject Schema::TYPE_BOOLEAN => self::CATEGORY_NUMERIC, Schema::TYPE_MONEY => self::CATEGORY_NUMERIC, ]; - /** - * @var \yii\db\Connection the current database connection. It is used mainly to escape strings - * safely when building the final column schema string. - * @since 2.0.8 - */ - public $db; + /** * @var string comment value of the column. - * @since 2.0.8 */ - public $comment; + public string $comment = ''; /** * Create a column schema builder instance giving the type and value precision. * * @param string $type type of the column. See [[$type]]. - * @param int|string|array|null $length length or precision of the column. See [[$length]]. - * @param \yii\db\Connection|null $db the current database connection. See [[$db]]. + * @param array|int|string|null $length length or precision of the column. See [[$length]]. + * @param Connection|null $db the current database connection. See [[$db]]. * @param array $config name-value pairs that will be used to initialize the object properties */ - public function __construct($type, $length = null, $db = null, $config = []) - { + public function __construct( + Connection $db, + string|null $type = null, + array|int|string|null $length = null, + array $config = [] + ) { + $this->db = $db; $this->type = $type; $this->length = $length; - $this->db = $db; + parent::__construct($config); } /** * Adds a `NOT NULL` constraint to the column. - * @return $this + * + * @return static Instance of the column schema builder. */ - public function notNull() + public function notNull(): static { $this->isNotNull = true; + return $this; } /** * Adds a `NULL` constraint to the column. - * @return $this - * @since 2.0.9 + * + * @return static Instance of the column schema builder. */ - public function null() + public function null(): static { $this->isNotNull = false; + return $this; } /** * Adds a `UNIQUE` constraint to the column. - * @return $this + * + * @return static Instance of the column schema builder. */ - public function unique() + public function unique(): static { $this->isUnique = true; + return $this; } /** * Sets a `CHECK` constraint for the column. + * * @param string $check the SQL of the `CHECK` constraint to be added. - * @return $this + * + * @return static Instance of the column schema builder. */ - public function check($check) + public function check(string $check): static { $this->check = $check; + return $this; } /** * Specify the default value for the column. + * * @param mixed $default the default value. - * @return $this + * + * @return static Instance of the column schema builder. */ - public function defaultValue($default) + public function defaultValue(mixed $default): static { - if ($default === null) { - $this->null(); - } + $this->default = $default ?? $this->null(); - $this->default = $default; return $this; } /** * Specifies the comment for column. - * @param string $comment the comment - * @return $this - * @since 2.0.8 + * + * @param string $comment the comment. + * @param Connection|null $db the database connection. If db is not null, the comment will be quoted using db. + * + * @return static Instance of the column schema builder. */ - public function comment($comment) + public function comment(string $comment): static { + $comment = $this->db->quoteValue($comment); + $this->comment = $comment; + return $this; } /** * Marks column as unsigned. - * @return $this - * @since 2.0.7 + * + * @return static Instance of the column schema builder. */ - public function unsigned() + public function unsigned(): static { - switch ($this->type) { - case Schema::TYPE_PK: - $this->type = Schema::TYPE_UPK; - break; - case Schema::TYPE_BIGPK: - $this->type = Schema::TYPE_UBIGPK; - break; - } + $this->type = match ($this->type) { + Schema::TYPE_PK => Schema::TYPE_UPK, + Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, + }; + $this->isUnsigned = true; + return $this; } /** * Adds an `AFTER` constraint to the column. * Note: MySQL and Oracle support only. + * * @param string $after the column after which $this column will be added. - * @return $this - * @since 2.0.8 + * + * @return static Instance of the column schema builder. */ - public function after($after) + public function after(string $after): static { - $this->after = $after; + $this->after = $this->db->quoteColumnName($after); + return $this; } /** * Adds an `FIRST` constraint to the column. * Note: MySQL and Oracle support only. - * @return $this - * @since 2.0.8 + * + * @return static Instance of the column schema builder. */ - public function first() + public function first(): static { $this->isFirst = true; + return $this; } /** * Specify the default SQL expression for the column. + * * @param string $default the default value expression. - * @return $this - * @since 2.0.7 + * + * @return static Instance of the column schema builder. */ - public function defaultExpression($default) + public function defaultExpression(string $default): static { $this->default = new Expression($default); + return $this; } /** * Specify additional SQL to be appended to column definition. * Position modifiers will be appended after column definition in databases that support them. + * * @param string $sql the SQL string to be appended. - * @return $this - * @since 2.0.9 + * @param Connection|null $db the database connection. If db is not null, the SQL will be quoted using db. + * + * @return static Instance of the column schema builder. */ - public function append($sql) + public function append(string $sql, Connection|null $db = null): static { + if ($db !== null) { + $sql = $db->quoteSql($sql); + } + $this->append = $sql; + return $this; } /** * Builds the full string for the column's schema. + * * @return string */ - public function __toString() + public function __toString(): string { - switch ($this->getTypeCategory()) { - case self::CATEGORY_PK: - $format = '{type}{check}{comment}{append}'; - break; - default: - $format = '{type}{length}{notnull}{unique}{default}{check}{comment}{append}'; - } + $format = match ($this->getTypeCategory()) { + self::CATEGORY_PK => '{type}{check}{comment}{append}', + default => '{type}{length}{notnull}{unique}{default}{check}{comment}{append}', + }; return $this->buildCompleteString($format); } /** * @return array mapping of abstract column types (keys) to type categories (values). - * @since 2.0.43 */ - public function getCategoryMap() + public function getCategoryMap(): array { - return static::$typeCategoryMap; + return $this->typeCategoryMap; } /** * @param array $categoryMap mapping of abstract column types (keys) to type categories (values). - * @since 2.0.43 */ - public function setCategoryMap($categoryMap) + public function setCategoryMap(array $categoryMap): void { - static::$typeCategoryMap = $categoryMap; + $this->typeCategoryMap = $categoryMap; } /** * Builds the length/precision part of the column. - * @return string + * + * @return string a string containing the length/precision of the column. */ protected function buildLengthString() { if ($this->length === null || $this->length === []) { return ''; } + if (is_array($this->length)) { $this->length = implode(',', $this->length); } @@ -326,65 +380,55 @@ protected function buildLengthString() /** * Builds the not null constraint for the column. - * @return string returns 'NOT NULL' if [[isNotNull]] is true, - * 'NULL' if [[isNotNull]] is false or an empty string otherwise. + * + * @return string returns 'NOT NULL' if [[isNotNull]] is true, 'NULL' if [[isNotNull]] is false or an empty string + * otherwise. */ - protected function buildNotNullString() + protected function buildNotNullString(): string { - if ($this->isNotNull === true) { - return ' NOT NULL'; - } elseif ($this->isNotNull === false) { - return ' NULL'; - } - - return ''; + return match ($this->isNotNull) { + true => ' NOT NULL', + false => ' NULL', + default => '', + }; } /** * Builds the unique constraint for the column. + * * @return string returns string 'UNIQUE' if [[isUnique]] is true, otherwise it returns an empty string. */ - protected function buildUniqueString() + protected function buildUniqueString(): string { return $this->isUnique ? ' UNIQUE' : ''; } /** * Return the default value for the column. + * * @return string|null string with default value of column. */ - protected function buildDefaultValue() + protected function buildDefaultValue(): string|null { - if ($this->default === null) { - return $this->isNotNull === false ? 'NULL' : null; - } - - switch (gettype($this->default)) { - case 'double': - // ensure type cast always has . as decimal separator in all locales - $defaultValue = StringHelper::floatToString($this->default); - break; - case 'boolean': - $defaultValue = $this->default ? 'TRUE' : 'FALSE'; - break; - case 'integer': - case 'object': - $defaultValue = (string) $this->default; - break; - default: - $defaultValue = "'{$this->default}'"; - } - - return $defaultValue; + return match ($this->default) { + // ensure type cast always has . as decimal separator in all locales + 'double' => StringHelper::floatToString($this->default), + 'boolean' => $this->default ? 'TRUE' : 'FALSE', + 'integer', 'object' => (string) $this->default, + null => $this->isNotNull === false ? 'NULL' : null, + default => "'{$this->default}'", + }; } /** * Builds the default value specification for the column. + * * @return string string with default value of column. */ - protected function buildDefaultString() + protected function buildDefaultString(): string { $defaultValue = $this->buildDefaultValue(); + if ($defaultValue === null) { return ''; } @@ -394,80 +438,82 @@ protected function buildDefaultString() /** * Builds the check constraint for the column. + * * @return string a string containing the CHECK constraint. */ - protected function buildCheckString() + protected function buildCheckString(): string { - return $this->check !== null ? " CHECK ({$this->check})" : ''; + return $this->check !== '' ? " CHECK ({$this->check})" : ''; } /** * Builds the unsigned string for column. Defaults to unsupported. + * * @return string a string containing UNSIGNED keyword. - * @since 2.0.7 */ - protected function buildUnsignedString() + protected function buildUnsignedString(): string { return ''; } /** * Builds the after constraint for the column. Defaults to unsupported. + * * @return string a string containing the AFTER constraint. - * @since 2.0.8 */ - protected function buildAfterString() + protected function buildAfterString(): string { return ''; } /** * Builds the first constraint for the column. Defaults to unsupported. + * * @return string a string containing the FIRST constraint. - * @since 2.0.8 */ - protected function buildFirstString() + protected function buildFirstString(): string { return ''; } /** * Builds the custom string that's appended to column definition. + * * @return string custom string to append. - * @since 2.0.9 */ - protected function buildAppendString() + protected function buildAppendString(): string { return $this->append !== null ? ' ' . $this->append : ''; } /** * Returns the category of the column type. + * * @return string a string containing the column type category name. - * @since 2.0.8 */ - protected function getTypeCategory() + protected function getTypeCategory(): string { return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : null; } /** * Builds the comment specification for the column. - * @return string a string containing the COMMENT keyword and the comment itself - * @since 2.0.8 + * + * @return string a string containing the COMMENT keyword and the comment itself. */ - protected function buildCommentString() + protected function buildCommentString(): string { return ''; } /** * Returns the complete column definition from input format. + * * @param string $format the format of the definition. + * * @return string a string containing the complete column definition. - * @since 2.0.8 */ - protected function buildCompleteString($format) + protected function buildCompleteString(string $format): string { $placeholderValues = [ '{type}' => $this->type, @@ -481,6 +527,7 @@ protected function buildCompleteString($format) '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), '{append}' => $this->buildAppendString(), ]; + return strtr($format, $placeholderValues); } } diff --git a/src/db/QueryBuilder.php b/src/db/QueryBuilder.php index e2b1df55..b45a42f1 100644 --- a/src/db/QueryBuilder.php +++ b/src/db/QueryBuilder.php @@ -1264,9 +1264,12 @@ public function dropView($viewName) * The following abstract column types are supported (using MySQL as an example to explain the corresponding * physical types): * - * - `pk`: an auto-incremental primary key type, will be converted into "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" - * - `bigpk`: an auto-incremental primary key type, will be converted into "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" - * - `upk`: an unsigned auto-incremental primary key type, will be converted into "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" + * - `pk`: an auto-incremental primary key type, will be converted into + * "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY" + * - `bigpk`: an auto-incremental primary key type, will be converted into + * "bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY" + * - `upk`: an unsigned auto-incremental primary key type, will be converted into + * "int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY" * - `char`: char type, will be converted into "char(1)" * - `string`: string type, will be converted into "varchar(255)" * - `text`: a long string type, will be converted into "text" @@ -1283,31 +1286,42 @@ public function dropView($viewName) * - `money`: money type, will be converted into "decimal(19,4)" * - `binary`: binary data type, will be converted into "blob" * - * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only - * the first part will be converted, and the rest of the parts will be appended to the converted result. + * If the abstract type contains two or more parts separated by spaces (e.g. "string NOT NULL"), then only the first + * part will be converted, and the rest of the parts will be appended to the converted result. + * * For example, 'string NOT NULL' is converted to 'varchar(255) NOT NULL'. * - * For some of the abstract types you can also specify a length or precision constraint - * by appending it in round brackets directly to the type. + * For some of the abstract types you can also specify a length or precision constraint by appending it in round + * brackets directly to the type. + * * For example `string(32)` will be converted into "varchar(32)" on a MySQL database. + * * If the underlying DBMS does not support these kind of constraints for a type it will * be ignored. * * If a type cannot be found in [[typeMap]], it will be returned without any change. - * @param string|ColumnSchemaBuilder $type abstract column type + * + * @param string|ColumnSchemaBuilder $type abstract column type. + * * @return string physical column type. */ - public function getColumnType($type) + public function getColumnType(string|ColumnSchemaBuilder $type): string { - if ($type instanceof ColumnSchemaBuilder) { - $type = $type->__toString(); + if (is_string($type) === false) { + $type = (string) $type; } if (isset($this->typeMap[$type])) { return $this->typeMap[$type]; - } elseif (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) { + } + + if (preg_match('/^(\w+)\((.+?)\)(.*)$/', $type, $matches)) { if (isset($this->typeMap[$matches[1]])) { - return preg_replace('/\(.+\)/', '(' . $matches[2] . ')', $this->typeMap[$matches[1]]) . $matches[3]; + return preg_replace( + '/\(.+\)/', + '(' . $matches[2] . ')', + $this->typeMap[$matches[1]] + ) . $matches[3]; } } elseif (preg_match('/^(\w+)\s+/', $type, $matches)) { if (isset($this->typeMap[$matches[1]])) { diff --git a/src/db/Schema.php b/src/db/Schema.php index 1f90c803..5a5da5c7 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -20,108 +20,202 @@ * * Schema represents the database schema information that is DBMS specific. * - * @property-read string $lastInsertID The row ID of the last row inserted, or the last value retrieved from - * the sequence object. + * @property-read string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the + * sequence object. * @property-read QueryBuilder $queryBuilder The query builder for this connection. * @property-read string[] $schemaNames All schema names in the database, except system schemas. * @property-read string $serverVersion Server version as a string. * @property-read string[] $tableNames All table names in the database. - * @property-read TableSchema[] $tableSchemas The metadata for all tables in the database. Each array element - * is an instance of [[TableSchema]] or its child class. + * @property-read TableSchema[] $tableSchemas The metadata for all tables in the database. Each array element is an + * instance of [[TableSchema]] or its child class. * @property-write string $transactionIsolationLevel The transaction isolation level to use for this * transaction. This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], * [[Transaction::REPEATABLE_READ]] and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific * syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. - * - * @author Qiang Xue - * @author Sergey Makinen - * @since 2.0 */ abstract class Schema extends BaseObject { - // The following are the supported abstract column data types. - const TYPE_PK = 'pk'; - const TYPE_UPK = 'upk'; - const TYPE_BIGPK = 'bigpk'; - const TYPE_UBIGPK = 'ubigpk'; - const TYPE_CHAR = 'char'; - const TYPE_STRING = 'string'; - const TYPE_TEXT = 'text'; - const TYPE_TINYINT = 'tinyint'; - const TYPE_SMALLINT = 'smallint'; - const TYPE_INTEGER = 'integer'; - const TYPE_BIGINT = 'bigint'; - const TYPE_FLOAT = 'float'; - const TYPE_DOUBLE = 'double'; - const TYPE_DECIMAL = 'decimal'; - const TYPE_DATETIME = 'datetime'; - const TYPE_TIMESTAMP = 'timestamp'; - const TYPE_TIME = 'time'; - const TYPE_DATE = 'date'; - const TYPE_BINARY = 'binary'; - const TYPE_BOOLEAN = 'boolean'; - const TYPE_MONEY = 'money'; - const TYPE_JSON = 'json'; - /** - * Schema cache version, to detect incompatibilities in cached values when the - * data format of the cache changes. - */ - const SCHEMA_CACHE_VERSION = 1; - - /** - * @var Connection the database connection - */ - public $db; + /** + * Define the abstract column type as an `integer` auto-incremental. + */ + public const TYPE_AUTO = 'auto'; + + /** + * Define the abstract column type as an `bigint` auto-incremental. + */ + public const TYPE_BIGAUTO = 'bigauto'; + + /** + * Define the abstract column type as an `bigint` primary key. + */ + public const TYPE_BIGPK = 'bigpk'; + + /** + * Define the abstract column type as an `integer` primary key. + */ + public const TYPE_PK = 'pk'; + + /** + * Define the abstract column type as an `unsigned bigint` primary key. + */ + public const TYPE_UBIGPK = 'ubigpk'; + + /** + * Define the abstract column type as an `unsigned integer` primary key. + */ + public const TYPE_UPK = 'upk'; + + /** + * Define the abstract column type as `char`. + */ + public const TYPE_CHAR = 'char'; + + /** + * Define the abstract column type as `string`. + */ + public const TYPE_STRING = 'string'; + + /** + * Define the abstract column type as `text`. + */ + public const TYPE_TEXT = 'text'; + + /** + * Define the abstract column type as `smallint`. + */ + public const TYPE_TINYINT = 'tinyint'; + + /** + * Define the abstract column type as `smallint`. + */ + public const TYPE_SMALLINT = 'smallint'; + + /** + * Define the abstract column type as `integer`. + */ + public const TYPE_INTEGER = 'integer'; + + /** + * Define the abstract column type as `bigint`. + */ + public const TYPE_BIGINT = 'bigint'; + + /** + * Define the abstract column type as `float`. + */ + public const TYPE_FLOAT = 'float'; + + /** + * Define the abstract column type as `double`. + */ + public const TYPE_DOUBLE = 'double'; + + /** + * Define the abstract column type as `decimal`. + */ + public const TYPE_DECIMAL = 'decimal'; + + /** + * Define the abstract column type as `datetime`. + */ + public const TYPE_DATETIME = 'datetime'; + + /** + * Define the abstract column type as `timestamp`. + */ + public const TYPE_TIMESTAMP = 'timestamp'; + + /** + * Define the abstract column type as `time`. + */ + public const TYPE_TIME = 'time'; + + /** + * Define the abstract column type as `date`. + */ + public const TYPE_DATE = 'date'; + + /** + * Define the abstract column type as `binary`. + */ + public const TYPE_BINARY = 'binary'; + + /** + * Define the abstract column type as a `boolean`. + */ + public const TYPE_BOOLEAN = 'boolean'; + + /** + * Define the abstract column type as a `money`. + */ + public const TYPE_MONEY = 'money'; + + /** + * Define the abstract column type as `json` data. + */ + public const TYPE_JSON = 'json'; + + /** + * Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes. + */ + public const SCHEMA_CACHE_VERSION = 1; + + /** + * @var Connection|null the database connection. + */ + public Connection|null $db = null; + /** * @var string the default schema name used for the current session. */ - public $defaultSchema; + public string $defaultSchema = ''; + /** - * @var array map of DB errors and corresponding exceptions + * @var array map of DB errors and corresponding exceptions. * If left part is found in DB error message exception class from the right part is used. */ - public $exceptionMap = [ + public array $exceptionMap = [ 'SQLSTATE[23' => 'yii\db\IntegrityException', ]; + /** - * @var string|array column schema class or class config - * @since 2.0.11 + * @var string|array column schema class or class config. */ - public $columnSchemaClass = 'yii\db\ColumnSchema'; + public array|string $columnSchemaClass = 'yii\db\ColumnSchema'; /** * @var string|string[] character used to quote schema, table, etc. names. * An array of 2 characters can be used in case starting and ending characters are different. - * @since 2.0.14 */ protected array|string $tableQuoteCharacter = "'"; + /** * @var string|string[] character used to quote column names. * An array of 2 characters can be used in case starting and ending characters are different. - * @since 2.0.14 */ protected array|string $columnQuoteCharacter = '"'; /** - * @var array list of ALL schema names in the database, except system schemas + * @var array list of ALL schema names in the database, except system schemas. */ - private $_schemaNames; + private array $_schemaNames = []; + /** - * @var array list of ALL table names in the database + * @var array list of ALL table names in the database. */ - private $_tableNames = []; + private array $_tableNames = []; /** * @var array list of loaded table metadata (table name => metadata type => metadata). */ - private $_tableMetadata = []; + private array $_tableMetadata = []; /** - * @var QueryBuilder the query builder for this database + * @var QueryBuilder|null the query builder for this database */ - private $_builder; + private QueryBuilder|null $_builder = null; /** * @var string server version as a string. */ - private $_serverVersion; + private string|null $_serverVersion = null; /** @@ -206,14 +300,15 @@ public function getTableSchemas($schema = '', $refresh = false) /** * Returns all schema names in the database, except system schemas. - * @param bool $refresh whether to fetch the latest available schema names. If this is false, - * schema names fetched previously (if available) will be returned. + * + * @param bool $refresh whether to fetch the latest available schema names. If this is false, schema names fetched + * previously (if available) will be returned. + * * @return string[] all schema names in the database, except system schemas. - * @since 2.0.4 */ public function getSchemaNames($refresh = false) { - if ($this->_schemaNames === null || $refresh) { + if ($this->_schemaNames === [] || $refresh) { $this->_schemaNames = $this->findSchemaNames(); } @@ -320,14 +415,15 @@ public function createQueryBuilder() * * This method may be overridden by child classes to create a DBMS-specific column schema builder. * - * @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. + * @param string|null $type type of the column. See [[ColumnSchemaBuilder::$type]]. If null, returning instance may + * not be configured yet. * @param int|string|array|null $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. - * @return ColumnSchemaBuilder column schema builder instance - * @since 2.0.6 + * + * @return ColumnSchemaBuilder the column schema builder instance. */ - public function createColumnSchemaBuilder($type, $length = null) + public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::class, [$type, $length]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length, $this->db]); } /** diff --git a/src/db/mssql/ColumnSchemaBuilder.php b/src/db/mssql/ColumnSchemaBuilder.php index 4523f332..21a9aa00 100644 --- a/src/db/mssql/ColumnSchemaBuilder.php +++ b/src/db/mssql/ColumnSchemaBuilder.php @@ -1,13 +1,10 @@ - * @since 2.0.42 */ -class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder +class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder { + /** + * @var string The format string for the column's schema. + */ protected $format = '{type}{length}{notnull}{unique}{default}{check}{append}'; + /** + * The column types that are auto-incremental or primary key. + */ + private const CATEGORY_GROUP_AUTO_PK = [ + self::CATEGORY_AUTO, + self::CATEGORY_BIGAUTO, + self::CATEGORY_PK, + self::CATEGORY_BIGPK, + ]; + + /** + * @var array The parameters for the IDENTITY column. + */ + private array $identityParams = []; + + public function __construct(Connection $db, string|null $type = null, $length = null, $config = []) + { + if ((in_array($type, self::CATEGORY_GROUP_AUTO_PK)) && is_array($length) && count($length) === 2) { + // start, increment. If increment is 0, it will be set to 1. + if ($length[1] === 0) { + $length[1] = 1; + } + + $this->identityParams = $length; + } + + parent::__construct($db, $type, $length, $config); + } /** * Builds the full string for the column's schema. + * * @return string */ - public function __toString() + public function __toString(): string { - if ($this->getTypeCategory() === self::CATEGORY_PK) { - $format = '{type}{check}{comment}{append}'; - } else { - $format = $this->format; + $format = $this->format; + + if (in_array($this->getTypeCategory(), self::CATEGORY_GROUP_AUTO_PK)) { + $format = '{type}{identity}{check}{comment}{append}'; } return $this->buildCompleteString($format); } + /** + * Creates an `IDENTITY` column. + * + * @param int|null $start The start value for the identity column. if null, it will be set to 1. + * @param int|null $increment The increment value for the identity column. if null, it will be set to 1. + * + * @return self The column schema builder instance. + */ + public function autoIncrement(int $start = null, int $increment = null): self + { + $this->type = self::CATEGORY_AUTO; + + if ($start !== null && $increment !== null) { + $this->identityParams = [$start, $increment === 0 ? 1 : $increment]; + } + + return $this; + } + + /** + * Creates a `BIGINT IDENTITY` column. + * + * @param int|null $start The start value for the identity column. if null, it will be set to 1. + * @param int|null $increment The increment value for the identity column. if null, it will be set to 1. + * + * @return self The column schema builder instance. + */ + public function bigAutoIncrement(int $start = null, int $increment = null): self + { + $this->type = self::CATEGORY_BIGAUTO; + + if ($start !== null && $increment !== null) { + $this->identityParams = [$start, $increment === 0 ? 1 : $increment]; + } + + return $this; + } + + /** + * Creates a `BIGINT IDENTITY PRIMARY KEY` column. + * + * @param int|null $start The start value for the identity column. if null, it will be set to 1. + * @param int|null $increment The increment value for the identity column. if null, it will be set to 1. + * + * @return self The column schema builder instance. + */ + public function bigPrimaryKey(int $start = null, int $increment = null): self + { + $this->type = self::CATEGORY_BIGPK; + + if ($start !== null && $increment !== null) { + $this->identityParams = [$start, $increment === 0 ? 1 : $increment]; + } + + return $this; + } + + /** + * Creates an `INTEGER IDENTITY PRIMARY KEY` column. + * + * @param int|null $start The start value for the identity column. if null, it will be set to 1. + * @param int|null $increment The increment value for the identity column. if null, it will be set to 1. + * + * @return self The column schema builder instance. + */ + public function primaryKey(int $start = null, int $increment = null): self + { + $this->type = self::CATEGORY_PK; + + if ($start !== null && $increment !== null) { + $this->identityParams = [$start, $increment === 0 ? 1 : $increment]; + } + + return $this; + } + /** * Changes default format string to MSSQL ALTER COMMAND. */ - public function setAlterColumnFormat() + public function setAlterColumnFormat(): void { $this->format = '{type}{length}{notnull}{append}'; } /** - * Getting the `Default` value for constraint + * Getting the `Default` value for constraint. + * * @return string|Expression|null default value of the column. */ - public function getDefaultValue() + public function getDefaultValue(): string|Expression|null { if ($this->default instanceof Expression) { return $this->default; @@ -61,10 +164,11 @@ public function getDefaultValue() } /** - * Get the `Check` value for constraint + * Get the `Check` value for constraint. + * * @return string|null the `CHECK` constraint for the column. */ - public function getCheckValue() + public function getCheckValue(): string|null { return $this->check !== null ? (string) $this->check : null; } @@ -72,8 +176,41 @@ public function getCheckValue() /** * @return bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added. */ - public function isUnique() + public function isUnique(): bool { return $this->isUnique; } + + /** + * {@inheritdoc} + */ + protected function buildCompleteString($format): string + { + $placeholderValues = [ + '{type}' => $this->type, + '{length}' => $this->buildLengthString(), + '{unsigned}' => $this->buildUnsignedString(), + '{notnull}' => $this->buildNotNullString(), + '{unique}' => $this->buildUniqueString(), + '{default}' => $this->buildDefaultString(), + '{check}' => $this->buildCheckString(), + '{comment}' => $this->buildCommentString(), + '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), + '{append}' => $this->buildAppendString(), + '{identity}' => $this->buildIdentityString(), + ]; + return strtr($format, $placeholderValues); + } + + /** + * {@inheritdoc} + */ + protected function buildIdentityString(): string + { + if (in_array($this->type, self::CATEGORY_GROUP_AUTO_PK) && $this->identityParams !== []) { + return "({$this->identityParams[0]},{$this->identityParams[1]})"; + } + + return ''; + } } diff --git a/src/db/mssql/QueryBuilder.php b/src/db/mssql/QueryBuilder.php index 4804ad60..8aa65b49 100644 --- a/src/db/mssql/QueryBuilder.php +++ b/src/db/mssql/QueryBuilder.php @@ -6,12 +6,13 @@ use yii\base\InvalidArgumentException; use yii\base\NotSupportedException; +use yii\db\ColumnSchemaBuilder; use yii\db\Expression; use yii\db\ExpressionInterface; use yii\db\QueryInterface; /** - * QueryBuilder is the query builder for MS SQL Server databases (version 2008 and above). + * QueryBuilder is the query builder for MS SQL Server databases (version 2012 and above). */ class QueryBuilder extends \yii\db\QueryBuilder { @@ -19,26 +20,45 @@ class QueryBuilder extends \yii\db\QueryBuilder * @var array mapping from abstract column types (keys) to physical column types (values). */ public $typeMap = [ + // auto-incremental + Schema::TYPE_AUTO => 'int IDENTITY', + Schema::TYPE_BIGAUTO => 'bigint IDENTITY', + + // auto-incremental primary key Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY', Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', + + // string types Schema::TYPE_CHAR => 'nchar(1)', Schema::TYPE_STRING => 'nvarchar(255)', Schema::TYPE_TEXT => 'nvarchar(max)', + + // integers Schema::TYPE_TINYINT => 'tinyint', Schema::TYPE_SMALLINT => 'smallint', Schema::TYPE_INTEGER => 'int', Schema::TYPE_BIGINT => 'bigint', + + // decimals Schema::TYPE_FLOAT => 'float', Schema::TYPE_DOUBLE => 'float', Schema::TYPE_DECIMAL => 'decimal(18,0)', + + // dates Schema::TYPE_DATETIME => 'datetime', Schema::TYPE_TIMESTAMP => 'datetime', Schema::TYPE_TIME => 'time', Schema::TYPE_DATE => 'date', + + // binary Schema::TYPE_BINARY => 'varbinary(max)', + + // boolean Schema::TYPE_BOOLEAN => 'bit', + + // money Schema::TYPE_MONEY => 'decimal(19,4)', ]; @@ -580,14 +600,13 @@ public function update($table, $columns, $condition, &$params) /** * {@inheritdoc} */ - public function getColumnType($type) + public function getColumnType(string|ColumnSchemaBuilder $type): string { $columnType = parent::getColumnType($type); - // remove unsupported keywords - $columnType = preg_replace("/\s*comment '.*'/i", '', $columnType); - $columnType = preg_replace('/ first$/i', '', $columnType); - return $columnType; + $columnType = $this->handleIdentityColumn($columnType, (string) $type); + + return preg_replace(["/\s*comment '.*'/i", '/ first$/i', '/\s+unsigned/i'], '', $columnType); } /** @@ -646,4 +665,33 @@ public function dropColumn($table, $column) return $this->dropConstraintsForColumn($table, $column) . "\nALTER TABLE " . $this->db->quoteTableName($table) . ' DROP COLUMN ' . $this->db->quoteColumnName($column); } + + private function handleIdentityColumn(string $columnType, string $type): string + { + if (preg_match('/^(big)?(auto|pk)\s*\((\d+),(\d+)\)$/i', $type, $matches)) { + $isBig = !empty($matches[1]); + $isPk = strtolower($matches[2]) === 'pk'; + $start = $matches[3]; + $increment = ($matches[4] == '0') ? '1' : $matches[4]; + $dataType = $isBig ? 'bigint' : 'int'; + + return "$dataType IDENTITY($start,$increment)" . ($isPk ? ' PRIMARY KEY' : ''); + } + + if (preg_match('/^(big)?int IDENTITY\((\d+)\)(\s+PRIMARY KEY)?$/i', $columnType, $matches)) { + $dataType = !empty($matches[1]) ? 'bigint' : 'int'; + $isPk = !empty($matches[3]); + + return "$dataType IDENTITY" . ($isPk ? ' PRIMARY KEY' : ''); + } + + if (preg_match('/IDENTITY\((\d+),(\d+)\)/i', $columnType, $matches)) { + $start = $matches[1]; + $increment = ($matches[2] == '0') ? '1' : $matches[2]; + + return preg_replace('/IDENTITY\(\d+,\d+\)/i', "IDENTITY($start,$increment)", $columnType); + } + + return $columnType; + } } diff --git a/src/db/mssql/Schema.php b/src/db/mssql/Schema.php index 8f3969d2..838a1d30 100644 --- a/src/db/mssql/Schema.php +++ b/src/db/mssql/Schema.php @@ -1,9 +1,6 @@ - * @since 2.0 + * Schema is the class for retrieving metadata from MS SQL Server databases (version 2012 and above). */ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { @@ -32,15 +26,17 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface /** * {@inheritdoc} */ - public $columnSchemaClass = 'yii\db\mssql\ColumnSchema'; + public array|string $columnSchemaClass = 'yii\db\mssql\ColumnSchema'; + /** * @var string the default schema used for the current session. */ - public $defaultSchema = 'dbo'; + public string $defaultSchema = 'dbo'; + /** * @var array mapping from physical column types (keys) to abstract column types (values) */ - public $typeMap = [ + public array $typeMap = [ // exact numbers 'bigint' => self::TYPE_BIGINT, 'numeric' => self::TYPE_DECIMAL, @@ -51,10 +47,12 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface 'int' => self::TYPE_INTEGER, 'tinyint' => self::TYPE_TINYINT, 'money' => self::TYPE_MONEY, + // approximate numbers 'float' => self::TYPE_FLOAT, 'double' => self::TYPE_DOUBLE, 'real' => self::TYPE_FLOAT, + // date and time 'date' => self::TYPE_DATE, 'datetimeoffset' => self::TYPE_DATETIME, @@ -62,20 +60,23 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface 'smalldatetime' => self::TYPE_DATETIME, 'datetime' => self::TYPE_DATETIME, 'time' => self::TYPE_TIME, + // character strings 'char' => self::TYPE_CHAR, 'varchar' => self::TYPE_STRING, 'text' => self::TYPE_TEXT, + // unicode character strings 'nchar' => self::TYPE_CHAR, 'nvarchar' => self::TYPE_STRING, 'ntext' => self::TYPE_TEXT, + // binary strings 'binary' => self::TYPE_BINARY, 'varbinary' => self::TYPE_BINARY, 'image' => self::TYPE_BINARY, - // other data types - // 'cursor' type cannot be used with tables + + // other data types, 'cursor' type cannot be used with tables 'timestamp' => self::TYPE_TIMESTAMP, 'hierarchyid' => self::TYPE_STRING, 'uniqueidentifier' => self::TYPE_STRING, @@ -332,7 +333,7 @@ public function rollBackSavepoint($name) */ public function createQueryBuilder() { - return Yii::createObject(QueryBuilder::className(), [$this->db]); + return Yii::createObject(QueryBuilder::class, [$this->db]); } /** @@ -778,8 +779,8 @@ public function quoteColumnName(string $name): string /** * {@inheritdoc} */ - public function createColumnSchemaBuilder($type, $length = null) + public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length]); } } diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index dbf507e0..ef7c2e8f 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -1,26 +1,138 @@ - * @since 2.0.8 */ -class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder +class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder { + /** + * Abstract column type for `UNSIGNED BIGINT AUTO_INCREMENT PRIMARY KEY` columns. + */ + public const CATEGORY_UBIGPK = 'ubigpk'; + + /** + * Abstract column type for `UNSIGNED AUTO_INCREMENT PRIMARY KEY` columns. + */ + public const CATEGORY_UPK = 'upk'; + + /** + * Creates an `INTEGER AUTO_INCREMENT` column. + * + * @param int|null $length column size or precision definition. + * + * @return self The column schema builder instance. + */ + public function autoIncrement(int|null $length = null): self + { + $this->type = self::CATEGORY_AUTO; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + + /** + * Creates a `BIGINT AUTO_INCREMENT` column. + * + * @param int|null $length column size or precision definition. Defaults to 20. + * + * @return self The column schema builder instance. + */ + public function bigAutoIncrement(int|null $length = null): self + { + $this->type = self::CATEGORY_BIGAUTO; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + + /** + * Creates a `BIGINT AUTO_INCREMENT PRIMARY KEY` column. + * + * @param int|null $length column size or precision definition. Defaults to 20. + * + * @return self The column schema builder instance. + */ + public function bigPrimaryKey(int|null $length = null): self + { + $this->type = self::CATEGORY_BIGPK; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + + /** + * Creates a `INTEGER AUTO_INCREMENT PRIMARY KEY` column. + * + * @param int|null $length column size or precision definition. Defaults to 11. + * + * @return self The column schema builder instance. + */ + public function primaryKey(int|null $length = null): self + { + $this->type = self::CATEGORY_PK; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + + /** + * Creates a `UNSIGNED BIGINT AUTO_INCREMENT PRIMARY KEY` column. + * + * @param int|null $length column size or precision definition. Defaults to 20. + * + * @return self The column schema builder instance. + */ + public function unsignedBigPrimaryKey(int|null $length = null): self + { + $this->type = self::CATEGORY_UBIGPK; + $this->isUnsigned = true; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + + /** + * Creates a `UNSIGNED INTEGER AUTO_INCREMENT PRIMARY KEY` column. + * + * @param int|null $length column size or precision definition. Defaults to 11. + * + * @return self The column schema builder instance. + */ + public function unsignedPrimaryKey(int|null $length = null): self + { + $this->type = self::CATEGORY_UPK; + $this->isUnsigned = true; + + if ($length !== null) { + $this->length = $length; + } + + return $this; + } + /** * {@inheritdoc} */ - protected function buildUnsignedString() + protected function buildUnsignedString(): string { return $this->isUnsigned ? ' UNSIGNED' : ''; } @@ -28,17 +140,17 @@ protected function buildUnsignedString() /** * {@inheritdoc} */ - protected function buildAfterString() + protected function buildAfterString(): string { - return $this->after !== null ? - ' AFTER ' . $this->db->quoteColumnName($this->after) : + return $this->after !== '' ? + ' AFTER ' . $this->after : ''; } /** * {@inheritdoc} */ - protected function buildFirstString() + protected function buildFirstString(): string { return $this->isFirst ? ' FIRST' : ''; } @@ -46,26 +158,24 @@ protected function buildFirstString() /** * {@inheritdoc} */ - protected function buildCommentString() + protected function buildCommentString(): string { - return $this->comment !== null ? ' COMMENT ' . $this->db->quoteValue($this->comment) : ''; + return $this->comment !== '' ? ' COMMENT ' . $this->comment : ''; } /** * {@inheritdoc} */ - public function __toString() + public function __toString(): string { - switch ($this->getTypeCategory()) { - case self::CATEGORY_PK: - $format = '{type}{length}{comment}{check}{append}{pos}'; - break; - case self::CATEGORY_NUMERIC: - $format = '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}'; - break; - default: - $format = '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}'; - } + $format = match ($this->getTypeCategory()) { + self::CATEGORY_AUTO, + self::CATEGORY_BIGAUTO, + self::CATEGORY_BIGPK, + self::CATEGORY_PK => '{type}{length}{comment}{check}{append}{pos}', + self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}', + default => '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}', + }; return $this->buildCompleteString($format); } diff --git a/src/db/mysql/QueryBuilder.php b/src/db/mysql/QueryBuilder.php index 4cdf298f..d84d6260 100644 --- a/src/db/mysql/QueryBuilder.php +++ b/src/db/mysql/QueryBuilder.php @@ -15,9 +15,6 @@ /** * QueryBuilder is the query builder for MySQL databases. - * - * @author Qiang Xue - * @since 2.0 */ class QueryBuilder extends \yii\db\QueryBuilder { @@ -25,10 +22,18 @@ class QueryBuilder extends \yii\db\QueryBuilder * @var array mapping from abstract column types (keys) to physical column types (values). */ public $typeMap = [ - Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + // auto-incremental + Schema::TYPE_AUTO => 'int(11) AUTO_INCREMENT', + Schema::TYPE_BIGAUTO => 'bigint(20) AUTO_INCREMENT', + + // auto-incremental primary key Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + + // auto-incremental unsigned primary key Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + Schema::TYPE_CHAR => 'char(1)', Schema::TYPE_STRING => 'varchar(255)', Schema::TYPE_TEXT => 'text', diff --git a/src/db/mysql/Schema.php b/src/db/mysql/Schema.php index cec8aea9..18f73445 100644 --- a/src/db/mysql/Schema.php +++ b/src/db/mysql/Schema.php @@ -1,9 +1,6 @@ - * @since 2.0 + * Schema is the class for retrieving metadata from a MySQL database (5.6.x, 5.7.x and 8.x). */ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { @@ -34,17 +28,12 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface /** * {@inheritdoc} */ - public $columnSchemaClass = 'yii\db\mysql\ColumnSchema'; - /** - * @var bool whether MySQL used is older than 5.1. - */ - private $_oldMysql; - + public array|string $columnSchemaClass = 'yii\db\mysql\ColumnSchema'; /** * @var array mapping from physical column types (keys) to abstract column types (values) */ - public $typeMap = [ + public array $typeMap = [ 'tinyint' => self::TYPE_TINYINT, 'bool' => self::TYPE_TINYINT, 'boolean' => self::TYPE_TINYINT, @@ -87,11 +76,17 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface * {@inheritdoc} */ protected array|string $tableQuoteCharacter = '`'; + /** * {@inheritdoc} */ protected array|string $columnQuoteCharacter = '`'; + /** + * @var bool|null whether MySQL used is older than 5.1. + */ + private bool|null $_oldMysql = null; + /** * {@inheritdoc} */ @@ -517,9 +512,9 @@ public function findUniqueIndexes($table) /** * {@inheritdoc} */ - public function createColumnSchemaBuilder($type, $length = null) + public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length, $this->db]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length]); } /** diff --git a/src/db/oci/ColumnSchemaBuilder.php b/src/db/oci/ColumnSchemaBuilder.php index dcb5ff21..ba9cdf11 100644 --- a/src/db/oci/ColumnSchemaBuilder.php +++ b/src/db/oci/ColumnSchemaBuilder.php @@ -1,9 +1,6 @@ - * @author Chris Harris - * @since 2.0.6 */ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder { /** * {@inheritdoc} */ - protected function buildUnsignedString() + protected function buildUnsignedString(): string { return $this->isUnsigned ? ' UNSIGNED' : ''; } @@ -29,18 +22,13 @@ protected function buildUnsignedString() /** * {@inheritdoc} */ - public function __toString() + public function __toString(): string { - switch ($this->getTypeCategory()) { - case self::CATEGORY_PK: - $format = '{type}{length}{check}{append}'; - break; - case self::CATEGORY_NUMERIC: - $format = '{type}{length}{unsigned}{default}{notnull}{check}{append}'; - break; - default: - $format = '{type}{length}{default}{notnull}{check}{append}'; - } + $format = match ($this->getTypeCategory()) { + self::CATEGORY_PK => '{type}{length}{check}{append}', + self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{default}{notnull}{check}{append}', + default => '{type}{length}{default}{notnull}{check}{append}', + }; return $this->buildCompleteString($format); } diff --git a/src/db/oci/Schema.php b/src/db/oci/Schema.php index 44f277b4..02732d92 100644 --- a/src/db/oci/Schema.php +++ b/src/db/oci/Schema.php @@ -1,9 +1,6 @@ - * @since 2.0 */ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { @@ -38,12 +32,13 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface /** * {@inheritdoc} */ - public $columnSchemaClass = 'yii\db\oci\ColumnSchema'; + public array|string $columnSchemaClass = 'yii\db\oci\ColumnSchema'; + /** * @var array map of DB errors and corresponding exceptions * If left part is found in DB error message exception class from the right part is used. */ - public $exceptionMap = [ + public array $exceptionMap = [ 'ORA-00001: unique constraint' => 'yii\db\IntegrityException', ]; @@ -52,7 +47,6 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface */ protected array|string $tableQuoteCharacter = '"'; - /** * {@inheritdoc} */ @@ -272,9 +266,9 @@ public function createQueryBuilder() /** * {@inheritdoc} */ - public function createColumnSchemaBuilder($type, $length = null) + public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length]); } /** diff --git a/src/db/pgsql/Schema.php b/src/db/pgsql/Schema.php index c51528c8..441b02ac 100644 --- a/src/db/pgsql/Schema.php +++ b/src/db/pgsql/Schema.php @@ -1,9 +1,6 @@ - * @since 2.0 + * Schema is the class for retrieving metadata from a PostgreSQL database (version 9.x and above). */ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { use ViewFinderTrait; use ConstraintFinderTrait; - const TYPE_JSONB = 'jsonb'; + /** + * Define the abstract column type as `jsonb`. + */ + public const TYPE_JSONB = 'jsonb'; /** * @var string the default schema used for the current session. */ - public $defaultSchema = 'public'; + public string $defaultSchema = 'public'; + /** * {@inheritdoc} */ - public $columnSchemaClass = 'yii\db\pgsql\ColumnSchema'; + public array|string $columnSchemaClass = 'yii\db\pgsql\ColumnSchema'; + /** - * @var array mapping from physical column types (keys) to abstract - * column types (values) + * @var array mapping from physical column types (keys) to abstract column types (values). + * * @see https://www.postgresql.org/docs/current/datatype.html#DATATYPE-TABLE */ - public $typeMap = [ + public array $typeMap = [ 'bit' => self::TYPE_INTEGER, 'bit varying' => self::TYPE_INTEGER, 'varbit' => self::TYPE_INTEGER, @@ -131,7 +129,6 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface */ protected array|string $tableQuoteCharacter = '"'; - /** * {@inheritdoc} */ diff --git a/src/db/sqlite/ColumnSchemaBuilder.php b/src/db/sqlite/ColumnSchemaBuilder.php index d9853b08..63a6cb6e 100644 --- a/src/db/sqlite/ColumnSchemaBuilder.php +++ b/src/db/sqlite/ColumnSchemaBuilder.php @@ -1,9 +1,6 @@ - * @since 2.0.8 */ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder { /** * {@inheritdoc} */ - protected function buildUnsignedString() + protected function buildUnsignedString(): string { return $this->isUnsigned ? ' UNSIGNED' : ''; } @@ -30,16 +24,11 @@ protected function buildUnsignedString() */ public function __toString() { - switch ($this->getTypeCategory()) { - case self::CATEGORY_PK: - $format = '{type}{check}{append}'; - break; - case self::CATEGORY_NUMERIC: - $format = '{type}{length}{unsigned}{notnull}{unique}{check}{default}{append}'; - break; - default: - $format = '{type}{length}{notnull}{unique}{check}{default}{append}'; - } + $format = match ($this->getTypeCategory()) { + self::CATEGORY_PK => '{type}{check}{append}', + self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{unique}{check}{default}{append}', + default => '{type}{length}{notnull}{unique}{check}{default}{append}', + }; return $this->buildCompleteString($format); } diff --git a/src/db/sqlite/Schema.php b/src/db/sqlite/Schema.php index df087c41..0be128f2 100644 --- a/src/db/sqlite/Schema.php +++ b/src/db/sqlite/Schema.php @@ -1,9 +1,6 @@ - * @since 2.0 + * @property-write string $transactionIsolationLevel The transaction isolation level to use for this transaction. + * This can be either [[Transaction::READ_UNCOMMITTED]] or [[Transaction::SERIALIZABLE]]. */ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { use ConstraintFinderTrait; /** - * @var array mapping from physical column types (keys) to abstract column types (values) + * @var array mapping from physical column types (keys) to abstract column types (values). */ - public $typeMap = [ + public array $typeMap = [ 'tinyint' => self::TYPE_TINYINT, 'bit' => self::TYPE_SMALLINT, 'boolean' => self::TYPE_BOOLEAN, @@ -78,7 +72,6 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface */ protected array|string $columnQuoteCharacter = '`'; - /** * {@inheritdoc} */ @@ -212,11 +205,10 @@ public function createQueryBuilder() /** * {@inheritdoc} - * @return ColumnSchemaBuilder column schema builder instance */ - public function createColumnSchemaBuilder($type, $length = null) + public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::className(), [$type, $length]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length]); } /** diff --git a/tests/framework/db/mssql/provider/ColumnTypeProvider.php b/tests/framework/db/mssql/provider/ColumnTypeProvider.php new file mode 100644 index 00000000..302d3af9 --- /dev/null +++ b/tests/framework/db/mssql/provider/ColumnTypeProvider.php @@ -0,0 +1,283 @@ + [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int IDENTITY', + ], + 'auto(1)' => [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int IDENTITY', + ], + 'auto(0,0)' => [ + 1 => 'auto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), + 3 => 'int IDENTITY(0,1)', + ], + 'auto(1,1)' => [ + 1 => 'auto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + 3 => 'int IDENTITY(1,1)', + ], + 'auto(2,3)' => [ + 1 => 'auto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + 3 => 'int IDENTITY(2,3)', + ], + ]; + + $types = parent::autoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public static function autoIncrementWithRaw(): array + { + return [ + [ + 'int IDENTITY', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + ], + [ + 'int IDENTITY(1)', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 'int IDENTITY', + ], + [ + 'int IDENTITY(0,0)', + 'auto(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), + 'int IDENTITY(0,1)', + ], + [ + 'int IDENTITY(1,1)', + 'auto(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + ], + [ + 'int IDENTITY(2,3)', + 'auto(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + ], + ]; + } + + public static function bigAutoIncrement(): array + { + $expected = [ + 'bigauto' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint IDENTITY', + ], + 'bigauto(1)' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint IDENTITY', + ], + 'bigauto(0,0)' => [ + 1 => 'bigauto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), + 3 => 'bigint IDENTITY(0,1)', + ], + 'bigauto(1,1)' => [ + 1 => 'bigauto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + 3 => 'bigint IDENTITY(1,1)', + ], + 'bigauto(2,3)' => [ + 1 => 'bigauto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + 3 => 'bigint IDENTITY(2,3)', + ], + ]; + + $types = parent::bigAutoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public static function bigAutoIncrementWithRaw(): array + { + return [ + [ + 'bigint IDENTITY', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + ], + [ + 'bigint IDENTITY(1)', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 'bigint IDENTITY', + ], + [ + 'bigint IDENTITY(0,0)', + 'bigauto(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), + 'bigint IDENTITY(0,1)', + ], + [ + 'bigint IDENTITY(1,1)', + 'bigauto(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + ], + [ + 'bigint IDENTITY(2,3)', + 'bigauto(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + ], + ]; + } + + public static function bigPrimaryKey(): array + { + $expected = [ + 'bigpk' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint IDENTITY PRIMARY KEY', + ], + 'bigpk(1)' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint IDENTITY PRIMARY KEY', + ], + 'bigpk(0,0)' => [ + 1 => 'bigpk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), + 3 => 'bigint IDENTITY(0,1) PRIMARY KEY', + ], + 'bigpk(1,1)' => [ + 1 => 'bigpk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + 3 => 'bigint IDENTITY(1,1) PRIMARY KEY', + ], + 'bigpk(2,3)' => [ + 1 => 'bigpk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', + ], + ]; + + $types = parent::bigPrimaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public static function bigPrimaryKeyWithRaw(): array + { + return [ + [ + 'bigint IDENTITY PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + ], + [ + 'bigint IDENTITY(1) PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 'bigint IDENTITY PRIMARY KEY', + ], + [ + 'bigint IDENTITY(0,0) PRIMARY KEY', + 'bigpk(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), + 'bigint IDENTITY(0,1) PRIMARY KEY', + ], + [ + 'bigint IDENTITY(1,1) PRIMARY KEY', + 'bigpk(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + ], + [ + 'bigint IDENTITY(2,3) PRIMARY KEY', + 'bigpk(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + ], + ]; + } + + public static function primaryKey(): array + { + $expected = [ + 'pk' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int IDENTITY PRIMARY KEY', + ], + 'pk(1)' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int IDENTITY PRIMARY KEY', + ], + 'pk(0,0)' => [ + 1 => 'pk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), + 3 => 'int IDENTITY(0,1) PRIMARY KEY', + ], + 'pk(1,1)' => [ + 1 => 'pk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + 3 => 'int IDENTITY(1,1) PRIMARY KEY', + ], + 'pk(2,3)' => [ + 1 => 'pk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + 3 => 'int IDENTITY(2,3) PRIMARY KEY', + ], + ]; + + $types = parent::primaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public static function primaryKeyWithRaw(): array + { + return [ + [ + 'int IDENTITY PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + ], + [ + 'int IDENTITY(1) PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 'int IDENTITY PRIMARY KEY', + ], + [ + 'int IDENTITY(0,0) PRIMARY KEY', + 'pk(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), + 'int IDENTITY(0,1) PRIMARY KEY', + ], + [ + 'int IDENTITY(1,1) PRIMARY KEY', + 'pk(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + ], + [ + 'int IDENTITY(2,3) PRIMARY KEY', + 'pk(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + ], + ]; + } +} diff --git a/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php new file mode 100644 index 00000000..eba71a37 --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php @@ -0,0 +1,120 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::autoIncrement + */ + public function testAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::autoIncrementWithRaw + */ + public function testAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigAutoIncrement + */ + public function testBigAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigAutoIncrementWithRaw + */ + public function testBigAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigPrimaryKey + */ + public function testBigPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigPrimaryKeyWithRaw + */ + public function testBigPrimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::primaryKey + */ + public function testPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::primaryKeyWithRaw + */ + public function testprimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mssql/querybuilder/CommentOnTest.php b/tests/framework/db/mssql/querybuilder/CommentOnTest.php new file mode 100644 index 00000000..9a5cc28e --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/CommentOnTest.php @@ -0,0 +1,113 @@ +db = MssqlConnection::getConnection(); + } + + public function testAddCommentOnColumn(): void + { + TableGenerator::ensureTable($this->db, Customer::class); + + $qb = $this->db->getQueryBuilder(); + + $expectedSQL = <<assertEquals($expectedSQL, $qb->addCommentOnColumn('customer', 'id', 'Primary key.')); + + TableGenerator::ensureNoTable($this->db, 'customer'); + } + + public function testAddCommentOnColumnWithException(): void + { + $qb = $this->db->getQueryBuilder(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Table not found: noExist'); + + $qb->addCommentOnColumn('noExist', 'id', 'Primary key.'); + } + + public function testAddCommentOnTable(): void + { + TableGenerator::ensureTable($this->db, Customer::class); + + $qb = $this->db->getQueryBuilder(); + + $expectedSQL = <<assertSame($expectedSQL, $qb->addCommentOnTable('customer', 'Customer table.')); + + TableGenerator::ensureNoTable($this->db, 'customer'); + } + + public function testAddCommentOnTableWithException(): void + { + $qb = $this->db->getQueryBuilder(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Table not found: noExist'); + + $qb->addCommentOnTable('noExist', 'Customer table.'); + } +} diff --git a/tests/framework/db/mysql/provider/ColumnTypeProvider.php b/tests/framework/db/mysql/provider/ColumnTypeProvider.php new file mode 100644 index 00000000..e2e70d1a --- /dev/null +++ b/tests/framework/db/mysql/provider/ColumnTypeProvider.php @@ -0,0 +1,230 @@ + [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int(11) AUTO_INCREMENT', + ], + 'auto(1)' => [ + 1 => 'auto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int(1) AUTO_INCREMENT', + ], + ]; + + $types = parent::autoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public function autoIncrementWithRaw(): array + { + return [ + [ + 'int(11) AUTO_INCREMENT', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + ], + [ + 'int(1) AUTO_INCREMENT', + 'auto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 'int(1) AUTO_INCREMENT', + ], + ]; + } + + public static function bigAutoIncrement(): array + { + $expected = [ + 'bigauto' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint(20) AUTO_INCREMENT', + ], + 'bigauto(1)' => [ + 1 => 'bigauto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint(1) AUTO_INCREMENT', + ], + ]; + + $types = parent::bigAutoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public function bigAutoIncrementWithRaw(): array + { + return [ + [ + 'bigint(20) AUTO_INCREMENT', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + ], + [ + 'bigint(1) AUTO_INCREMENT', + 'bigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 'bigint(1) AUTO_INCREMENT', + ], + ]; + } + + public static function bigPrimaryKey(): array + { + $expected = [ + 'bigpk' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + 'bigpk(1)' => [ + 1 => 'bigpk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + + $types = parent::bigPrimaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public function bigPrimaryKeyWithRaw(): array + { + return [ + [ + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + ], + [ + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public static function primaryKey(): array + { + $expected = [ + 'pk' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + 'pk(1)' => [ + 1 => 'pk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + + $types = parent::primaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public function primaryKeyWithRaw(): array + { + return [ + [ + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + ], + [ + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'pk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function unsignedBigPrimaryKey(): array + { + return [ + [ + Schema::TYPE_UBIGPK, + 'ubigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + [ + Schema::TYPE_UBIGPK . '(1)', + 'ubigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(1), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function unsignedBigPrimaryKeyWithRaw(): array + { + return [ + [ + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'ubigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(), + ], + [ + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'ubigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(1), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function unsignedPrimaryKey(): array + { + return [ + [ + Schema::TYPE_UPK, + 'upk', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + [ + Schema::TYPE_UPK . '(1)', + 'upk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(1), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function unsignedPrimaryKeyWithRaw(): array + { + return [ + [ + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'upk', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(), + ], + [ + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'upk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(1), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } +} diff --git a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php new file mode 100644 index 00000000..9ea29592 --- /dev/null +++ b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php @@ -0,0 +1,168 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::autoIncrement + */ + public function testAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::autoIncrementWithRaw + */ + public function testAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigAutoIncrement + */ + public function testBigAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigAutoIncrementWithRaw + */ + public function testBigAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigPrimaryKey + */ + public function testBigPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigPrimaryKeyWithRaw + */ + public function testBigPrimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::primaryKey + */ + public function testPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::primaryKeyWithRaw + */ + public function testprimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigPrimaryKey + */ + public function testUnsignedBigPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigPrimaryKeyWithRaw + */ + public function testUnsignedBigPrimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedPrimaryKey + */ + public function testUnsignedPrimaryKey( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedPrimaryKeyWithRaw + */ + public function testUnsignedPrimaryKeyWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/provider/AbstractColumnTypeProvider.php b/tests/framework/db/provider/AbstractColumnTypeProvider.php new file mode 100644 index 00000000..e24245a2 --- /dev/null +++ b/tests/framework/db/provider/AbstractColumnTypeProvider.php @@ -0,0 +1,94 @@ + [ + Schema::TYPE_AUTO, + ], + 'auto(1)' => [ + Schema::TYPE_AUTO . '(1)', + ], + 'auto(0,0)' => [ + Schema::TYPE_AUTO . '(0,0)', + ], + 'auto(1,1)' => [ + Schema::TYPE_AUTO . '(1,1)', + ], + 'auto(2,3)' => [ + Schema::TYPE_AUTO . '(2,3)', + ], + ]; + } + + public static function bigAutoIncrement(): array + { + return [ + 'bigauto' => [ + Schema::TYPE_BIGAUTO, + ], + 'bigauto(1)' => [ + Schema::TYPE_BIGAUTO . '(1)', + ], + 'bigauto(0,0)' => [ + Schema::TYPE_BIGAUTO . '(0,0)', + ], + 'bigauto(1,1)' => [ + Schema::TYPE_BIGAUTO . '(1,1)', + ], + 'bigauto(2,3)' => [ + Schema::TYPE_BIGAUTO . '(2,3)', + ], + ]; + } + + public static function bigPrimaryKey(): array + { + return [ + 'bigpk' => [ + Schema::TYPE_BIGPK, + ], + 'bigpk(1)' => [ + Schema::TYPE_BIGPK . '(1)', + ], + 'bigpk(0,0)' => [ + Schema::TYPE_BIGPK . '(0,0)', + ], + 'bigpk(1,1)' => [ + Schema::TYPE_BIGPK . '(1,1)', + ], + 'bigpk(2,3)' => [ + Schema::TYPE_BIGPK . '(2,3)', + ], + ]; + } + + public static function primaryKey(): array + { + return [ + 'pk' => [ + Schema::TYPE_PK, + ], + 'pk(1)' => [ + Schema::TYPE_PK . '(1)', + ], + 'pk(0,0)' => [ + Schema::TYPE_PK . '(0,0)', + ], + 'pk(1,1)' => [ + Schema::TYPE_PK . '(1,1)', + ], + 'pk(2,3)' => [ + Schema::TYPE_PK . '(2,3)', + ], + ]; + } +} diff --git a/tests/framework/db/querybuilder/AbstractColumnType.php b/tests/framework/db/querybuilder/AbstractColumnType.php new file mode 100644 index 00000000..37b4ec27 --- /dev/null +++ b/tests/framework/db/querybuilder/AbstractColumnType.php @@ -0,0 +1,57 @@ +db; + } + + public function tearDown(): void + { + $this->db->close(); + $this->db = null; + + parent::tearDown(); + } + + public function getColumnType( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $columnSchemaBuilder = $this->db->schema->createColumnSchemaBuilder(); + $qb = $this->db->getQueryBuilder(); + + $this->assertSame($expectedColumn, $builder($columnSchemaBuilder)->__toString()); + $this->assertSame($expectedBuilder, $qb->getColumnType($column)); + $this->assertSame($expectedBuilder, $qb->getColumnType($builder($columnSchemaBuilder))); + } + + public function getColumnTypeRaw(string $sql, string $column, Closure $builder, string $expectedColumn): void + { + $columnSchemaBuilder = $this->db->schema->createColumnSchemaBuilder(); + $qb = $this->db->getQueryBuilder(); + + if ($expectedColumn === '') { + $expectedColumn = $sql; + } + + $this->assertSame($column, $builder($columnSchemaBuilder)->__toString()); + $this->assertSame($expectedColumn, $qb->getColumnType($sql)); + $this->assertSame($expectedColumn, $qb->getColumnType($builder($columnSchemaBuilder))); + } +} diff --git a/tests/support/TestHelper.php b/tests/support/TestHelper.php new file mode 100644 index 00000000..0e54eb4c --- /dev/null +++ b/tests/support/TestHelper.php @@ -0,0 +1,29 @@ + $data) { + if (isset($dataProvider[$testName])) { + $result[$testName] = array_replace($dataProvider[$testName], $data); + } + } + + return $result; + } +} From 5b3f6e49bf09ff19d582feff14405d643e10263d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:05:39 -0400 Subject: [PATCH 02/31] Remove unsed tests. --- .../db/mssql/querybuilder/CommentOnTest.php | 113 ------------------ 1 file changed, 113 deletions(-) delete mode 100644 tests/framework/db/mssql/querybuilder/CommentOnTest.php diff --git a/tests/framework/db/mssql/querybuilder/CommentOnTest.php b/tests/framework/db/mssql/querybuilder/CommentOnTest.php deleted file mode 100644 index 9a5cc28e..00000000 --- a/tests/framework/db/mssql/querybuilder/CommentOnTest.php +++ /dev/null @@ -1,113 +0,0 @@ -db = MssqlConnection::getConnection(); - } - - public function testAddCommentOnColumn(): void - { - TableGenerator::ensureTable($this->db, Customer::class); - - $qb = $this->db->getQueryBuilder(); - - $expectedSQL = <<assertEquals($expectedSQL, $qb->addCommentOnColumn('customer', 'id', 'Primary key.')); - - TableGenerator::ensureNoTable($this->db, 'customer'); - } - - public function testAddCommentOnColumnWithException(): void - { - $qb = $this->db->getQueryBuilder(); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Table not found: noExist'); - - $qb->addCommentOnColumn('noExist', 'id', 'Primary key.'); - } - - public function testAddCommentOnTable(): void - { - TableGenerator::ensureTable($this->db, Customer::class); - - $qb = $this->db->getQueryBuilder(); - - $expectedSQL = <<assertSame($expectedSQL, $qb->addCommentOnTable('customer', 'Customer table.')); - - TableGenerator::ensureNoTable($this->db, 'customer'); - } - - public function testAddCommentOnTableWithException(): void - { - $qb = $this->db->getQueryBuilder(); - - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Table not found: noExist'); - - $qb->addCommentOnTable('noExist', 'Customer table.'); - } -} From d2b4c0eb4bcbbbbeddeed652330ab583653bfb57 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:10:04 -0400 Subject: [PATCH 03/31] Refactor `getTypeCategory()` method in `ColumnSchemaBuilder.php` to return nullable string. --- src/db/ColumnSchemaBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index 81042bc4..d4367454 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -489,9 +489,9 @@ protected function buildAppendString(): string /** * Returns the category of the column type. * - * @return string a string containing the column type category name. + * @return string|null a string containing the column type category name. */ - protected function getTypeCategory(): string + protected function getTypeCategory(): string|null { return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : null; } From f642c09eb2a508b102b8db55fabc5ccacd845294 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:13:38 -0400 Subject: [PATCH 04/31] Refactor `ColumnSchemaBuilder.php` to use static property for `typeCategoryMap`. --- src/db/ColumnSchemaBuilder.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index d4367454..c24870f5 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -114,10 +114,15 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable */ protected Connection $db; + /** + * @var string comment value of the column. + */ + public string $comment = ''; + /** * @var array mapping of abstract column types (keys) to type categories (values). */ - public array $typeCategoryMap = [ + public static array $typeCategoryMap = [ Schema::TYPE_AUTO => self::CATEGORY_AUTO, Schema::TYPE_BIGAUTO => self::CATEGORY_BIGAUTO, Schema::TYPE_PK => self::CATEGORY_PK, @@ -143,11 +148,6 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable Schema::TYPE_MONEY => self::CATEGORY_NUMERIC, ]; - /** - * @var string comment value of the column. - */ - public string $comment = ''; - /** * Create a column schema builder instance giving the type and value precision. * @@ -349,7 +349,7 @@ public function __toString(): string */ public function getCategoryMap(): array { - return $this->typeCategoryMap; + return static::$typeCategoryMap; } /** @@ -357,7 +357,7 @@ public function getCategoryMap(): array */ public function setCategoryMap(array $categoryMap): void { - $this->typeCategoryMap = $categoryMap; + static::$typeCategoryMap = $categoryMap; } /** From 92846357b762b4c62b3978a0f72ecb181d84bbbf Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:24:26 -0400 Subject: [PATCH 05/31] Fix tests `ColumnSchemaBuilderTest.php`. --- .../framework/db/ColumnSchemaBuilderTest.php | 2 +- .../db/mssql/ColumnSchemaBuilderTest.php | 20 +++++++------------ .../db/mysql/ColumnSchemaBuilderTest.php | 18 ++++++----------- .../db/oci/ColumnSchemaBuilderTest.php | 18 ++++++----------- .../db/pgsql/ColumnSchemaBuilderTest.php | 20 +++++++------------ .../db/sqlite/ColumnSchemaBuilderTest.php | 18 ++++++----------- 6 files changed, 33 insertions(+), 63 deletions(-) diff --git a/tests/framework/db/ColumnSchemaBuilderTest.php b/tests/framework/db/ColumnSchemaBuilderTest.php index 43cec278..eb0ecd52 100644 --- a/tests/framework/db/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/ColumnSchemaBuilderTest.php @@ -20,7 +20,7 @@ abstract class ColumnSchemaBuilderTest extends DatabaseTestCase */ public function getColumnSchemaBuilder($type, $length = null) { - return new ColumnSchemaBuilder($type, $length, $this->getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } /** diff --git a/tests/framework/db/mssql/ColumnSchemaBuilderTest.php b/tests/framework/db/mssql/ColumnSchemaBuilderTest.php index 783d4aa1..0f3c32ed 100644 --- a/tests/framework/db/mssql/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/mssql/ColumnSchemaBuilderTest.php @@ -1,30 +1,24 @@ getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } } diff --git a/tests/framework/db/mysql/ColumnSchemaBuilderTest.php b/tests/framework/db/mysql/ColumnSchemaBuilderTest.php index e930deef..e5c61356 100644 --- a/tests/framework/db/mysql/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/mysql/ColumnSchemaBuilderTest.php @@ -1,9 +1,6 @@ getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } /** diff --git a/tests/framework/db/oci/ColumnSchemaBuilderTest.php b/tests/framework/db/oci/ColumnSchemaBuilderTest.php index 8e0a8e96..3e6fa366 100644 --- a/tests/framework/db/oci/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/oci/ColumnSchemaBuilderTest.php @@ -1,9 +1,6 @@ getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } /** diff --git a/tests/framework/db/pgsql/ColumnSchemaBuilderTest.php b/tests/framework/db/pgsql/ColumnSchemaBuilderTest.php index 32d4f0eb..c5c0a57b 100644 --- a/tests/framework/db/pgsql/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/pgsql/ColumnSchemaBuilderTest.php @@ -1,30 +1,24 @@ getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } } diff --git a/tests/framework/db/sqlite/ColumnSchemaBuilderTest.php b/tests/framework/db/sqlite/ColumnSchemaBuilderTest.php index c8e7d5b0..56288962 100644 --- a/tests/framework/db/sqlite/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/sqlite/ColumnSchemaBuilderTest.php @@ -1,9 +1,6 @@ getConnection()); + return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } /** From ec143eb89bd7017ad9d704e8b60a678f754c6b07 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:27:47 -0400 Subject: [PATCH 06/31] Refactor `ColumnSchemaBuilder.php` to handle default type in switch statement. --- src/db/ColumnSchemaBuilder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index c24870f5..d1a51d2e 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -260,6 +260,7 @@ public function unsigned(): static $this->type = match ($this->type) { Schema::TYPE_PK => Schema::TYPE_UPK, Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, + default => $this->type, }; $this->isUnsigned = true; From b43182f8e1088762f34f42b8b0b5a19cfa821aa0 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:34:17 -0400 Subject: [PATCH 07/31] Refactor `createColumnSchemaBuilder()` method in `Schema.php` to remove unnecessary parameter. --- src/db/Schema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/Schema.php b/src/db/Schema.php index 5a5da5c7..0b7fc86a 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -423,7 +423,7 @@ public function createQueryBuilder() */ public function createColumnSchemaBuilder(string|null $type = null, $length = null): ColumnSchemaBuilder { - return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length, $this->db]); + return Yii::createObject(ColumnSchemaBuilder::class, [$this->db, $type, $length]); } /** From e8fc328c4663e233df739b70baafe1339e4accac Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 09:58:41 -0400 Subject: [PATCH 08/31] Refactor `ColumnSchemaBuilder.php` to handle default type in switch statement. --- src/db/ColumnSchemaBuilder.php | 25 ++++++++++++++----------- src/db/sqlite/ColumnSchemaBuilder.php | 4 +--- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index d1a51d2e..736f7311 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -115,9 +115,9 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable protected Connection $db; /** - * @var string comment value of the column. + * @var string|null comment value of the column. */ - public string $comment = ''; + public string|null $comment = null; /** * @var array mapping of abstract column types (keys) to type categories (values). @@ -228,7 +228,11 @@ public function check(string $check): static */ public function defaultValue(mixed $default): static { - $this->default = $default ?? $this->null(); + if ($default === null) { + $this->null(); + } + + $this->default = $default; return $this; } @@ -319,13 +323,9 @@ public function defaultExpression(string $default): static * * @return static Instance of the column schema builder. */ - public function append(string $sql, Connection|null $db = null): static + public function append(string $sql): static { - if ($db !== null) { - $sql = $db->quoteSql($sql); - } - - $this->append = $sql; + $this->append = $this->db->quoteSql($sql); return $this; } @@ -411,12 +411,15 @@ protected function buildUniqueString(): string */ protected function buildDefaultValue(): string|null { - return match ($this->default) { + if ($this->default === null) { + return $this->isNotNull === false ? 'NULL' : null; + } + + return match (gettype($this->default)) { // ensure type cast always has . as decimal separator in all locales 'double' => StringHelper::floatToString($this->default), 'boolean' => $this->default ? 'TRUE' : 'FALSE', 'integer', 'object' => (string) $this->default, - null => $this->isNotNull === false ? 'NULL' : null, default => "'{$this->default}'", }; } diff --git a/src/db/sqlite/ColumnSchemaBuilder.php b/src/db/sqlite/ColumnSchemaBuilder.php index 63a6cb6e..5e7d68b3 100644 --- a/src/db/sqlite/ColumnSchemaBuilder.php +++ b/src/db/sqlite/ColumnSchemaBuilder.php @@ -4,12 +4,10 @@ namespace yii\db\sqlite; -use yii\db\ColumnSchemaBuilder as AbstractColumnSchemaBuilder; - /** * ColumnSchemaBuilder is the schema builder for Sqlite databases. */ -class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder +class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder { /** * {@inheritdoc} From 4e71a89ae779bdc44b81f9672badb20ad88a0183 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 10:11:24 -0400 Subject: [PATCH 09/31] Refactor `ColumnSchemaBuilder.php` to handle nullable `CHECK` constraint. --- src/db/ColumnSchemaBuilder.php | 7 +++---- src/db/mssql/ColumnSchemaBuilder.php | 1 + tests/framework/db/mssql/querybuilder/ColumnTypeTest.php | 1 + tests/framework/db/mysql/querybuilder/ColumnTypeTest.php | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index 736f7311..41be06ef 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -80,9 +80,9 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable protected bool $isUnique = false; /** - * @var string the `CHECK` constraint for the column. + * @var string|null the `CHECK` constraint for the column. */ - protected string $check = ''; + protected string|null $check = null; /** * @var mixed default value of the column. @@ -241,7 +241,6 @@ public function defaultValue(mixed $default): static * Specifies the comment for column. * * @param string $comment the comment. - * @param Connection|null $db the database connection. If db is not null, the comment will be quoted using db. * * @return static Instance of the column schema builder. */ @@ -447,7 +446,7 @@ protected function buildDefaultString(): string */ protected function buildCheckString(): string { - return $this->check !== '' ? " CHECK ({$this->check})" : ''; + return $this->check !== null ? " CHECK ({$this->check})" : ''; } /** diff --git a/src/db/mssql/ColumnSchemaBuilder.php b/src/db/mssql/ColumnSchemaBuilder.php index 21a9aa00..57b29b7a 100644 --- a/src/db/mssql/ColumnSchemaBuilder.php +++ b/src/db/mssql/ColumnSchemaBuilder.php @@ -199,6 +199,7 @@ protected function buildCompleteString($format): string '{append}' => $this->buildAppendString(), '{identity}' => $this->buildIdentityString(), ]; + return strtr($format, $placeholderValues); } diff --git a/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php index eba71a37..75d477a0 100644 --- a/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php +++ b/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php @@ -11,6 +11,7 @@ * @group db * @group mssql * @group querybuilder + * @group column-schema-builder * @group column-type */ final class ColumnTypeTest extends \yiiunit\framework\db\querybuilder\AbstractColumnType diff --git a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php index 9ea29592..6cda7377 100644 --- a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php +++ b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php @@ -11,6 +11,7 @@ * @group db * @group mysql * @group querybuilder + * @group column-schema-builder * @group column-type */ final class ColumnTypeTest extends \yiiunit\framework\db\querybuilder\AbstractColumnType From b503024cd3e4edc6148fa16a6bb1a94d9d075a69 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 10:18:18 -0400 Subject: [PATCH 10/31] Refactor `ColumnSchemaBuilder.php` to handle nullable `COMMENT` in buildCommentString() method. --- src/db/mysql/ColumnSchemaBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index ef7c2e8f..d1e07545 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -160,7 +160,7 @@ protected function buildFirstString(): string */ protected function buildCommentString(): string { - return $this->comment !== '' ? ' COMMENT ' . $this->comment : ''; + return $this->comment !== null ? ' COMMENT ' . $this->comment : ''; } /** From f22e36d328c451432cf3322b2a44593fc98e7549 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 10:24:16 -0400 Subject: [PATCH 11/31] Refactor `ColumnSchemaBuilder.php` to handle nullable `after` property in `buildAfterString()` method. --- src/db/ColumnSchemaBuilder.php | 4 ++-- src/db/mysql/ColumnSchemaBuilder.php | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index 41be06ef..c1058bdd 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -100,9 +100,9 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable protected bool $isUnsigned = false; /** - * @var string the column after which this column will be added. + * @var string|null the column after which this column will be added. */ - protected string $after = ''; + protected string|null $after = null; /** * @var bool whether this column is to be inserted at the beginning of the table. diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index d1e07545..fed35548 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -142,9 +142,7 @@ protected function buildUnsignedString(): string */ protected function buildAfterString(): string { - return $this->after !== '' ? - ' AFTER ' . $this->after : - ''; + return $this->after !== null ? ' AFTER ' . $this->after : ''; } /** @@ -160,7 +158,7 @@ protected function buildFirstString(): string */ protected function buildCommentString(): string { - return $this->comment !== null ? ' COMMENT ' . $this->comment : ''; + return $this->comment !== null ? ' COMMENT ' . $this->db->quoteValue($this->comment) : ''; } /** From 3ccca1ab8fca6b7e400abb2649bef960f58da0f7 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 11:12:59 -0400 Subject: [PATCH 12/31] Refactor `Schema.php` to use nullable type for defaultSchema property. --- src/db/Schema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db/Schema.php b/src/db/Schema.php index 0b7fc86a..84cef5e0 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -166,9 +166,9 @@ abstract class Schema extends BaseObject public Connection|null $db = null; /** - * @var string the default schema name used for the current session. + * @var string|null the default schema name used for the current session. */ - public string $defaultSchema = ''; + public string|null $defaultSchema = null; /** * @var array map of DB errors and corresponding exceptions. From d7683582ca099d86423994e7c528dd13f31b8b7a Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 11:16:33 -0400 Subject: [PATCH 13/31] Refactor `Schema.php` to use nullable type for defaultSchema property. --- src/db/mssql/Schema.php | 4 ++-- src/db/pgsql/Schema.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db/mssql/Schema.php b/src/db/mssql/Schema.php index 838a1d30..f6eb842d 100644 --- a/src/db/mssql/Schema.php +++ b/src/db/mssql/Schema.php @@ -29,9 +29,9 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface public array|string $columnSchemaClass = 'yii\db\mssql\ColumnSchema'; /** - * @var string the default schema used for the current session. + * @var string|null the default schema used for the current session. */ - public string $defaultSchema = 'dbo'; + public string|null $defaultSchema = 'dbo'; /** * @var array mapping from physical column types (keys) to abstract column types (values) diff --git a/src/db/pgsql/Schema.php b/src/db/pgsql/Schema.php index 441b02ac..8dd97fc3 100644 --- a/src/db/pgsql/Schema.php +++ b/src/db/pgsql/Schema.php @@ -31,9 +31,9 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface public const TYPE_JSONB = 'jsonb'; /** - * @var string the default schema used for the current session. + * @var string|null the default schema used for the current session. */ - public string $defaultSchema = 'public'; + public string|null $defaultSchema = 'public'; /** * {@inheritdoc} From 0d938e7dd92a5c8e15cea8e1e7e335f92734ba20 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 11:38:11 -0400 Subject: [PATCH 14/31] Refactor `ColumnSchemaBuilder.php` to handle nullable `after` property in `buildAfterString()` method. --- src/db/ColumnSchemaBuilder.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index c1058bdd..d359d1ff 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -154,7 +154,7 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable * @param string $type type of the column. See [[$type]]. * @param array|int|string|null $length length or precision of the column. See [[$length]]. * @param Connection|null $db the current database connection. See [[$db]]. - * @param array $config name-value pairs that will be used to initialize the object properties + * @param array $config name-value pairs that will be used to initialize the object properties. */ public function __construct( Connection $db, @@ -246,8 +246,6 @@ public function defaultValue(mixed $default): static */ public function comment(string $comment): static { - $comment = $this->db->quoteValue($comment); - $this->comment = $comment; return $this; @@ -281,7 +279,7 @@ public function unsigned(): static */ public function after(string $after): static { - $this->after = $this->db->quoteColumnName($after); + $this->after = $after; return $this; } @@ -324,7 +322,7 @@ public function defaultExpression(string $default): static */ public function append(string $sql): static { - $this->append = $this->db->quoteSql($sql); + $this->append = $sql; return $this; } From d17fb7631f10c3979fb6458ff79e5c853cfc30c4 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 20 Aug 2024 11:49:12 -0400 Subject: [PATCH 15/31] Refactor `ColumnSchemaBuilder.php` to handle nullable `after` property in `buildAfterString()` method. --- src/db/mysql/ColumnSchemaBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index fed35548..aca0e089 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -142,7 +142,7 @@ protected function buildUnsignedString(): string */ protected function buildAfterString(): string { - return $this->after !== null ? ' AFTER ' . $this->after : ''; + return $this->after !== null ? ' AFTER ' . $this->db->quoteColumnName($this->after) : ''; } /** From e8e0ecd59c1ea5b200fd6c54f35952aa84248bfd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 04:30:15 -0400 Subject: [PATCH 16/31] Refactor ColumnSchemaBuilder.php to handle nullable after property in buildAfterString() method. --- src/db/ColumnSchemaBuilder.php | 365 +++++++----------- src/db/mssql/ColumnSchemaBuilder.php | 34 +- src/db/mysql/ColumnSchemaBuilder.php | 113 ++++-- .../db/mysql/provider/ColumnTypeProvider.php | 16 +- 4 files changed, 234 insertions(+), 294 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index d359d1ff..882cb205 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -57,27 +57,24 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable public const CATEGORY_OTHER = 'other'; /** - * @var string|null the column type definition such as INTEGER, VARCHAR, DATETIME, etc. - */ - protected string|null $type = null; - - /** - * @var array|int|string|null column size or precision definition. This is what goes into the parenthesis after - * the column type. This can be either a string, an integer or an array. If it is an array, the array values will - * be joined into a string separated by comma. + * The column types that are auto-incremental or primary key. */ - protected array|int|string|null $length = null; + public const CATEGORY_GROUP_AUTO_PK = [ + self::CATEGORY_AUTO, + self::CATEGORY_BIGAUTO, + self::CATEGORY_PK, + self::CATEGORY_BIGPK, + ]; /** - * @var bool|null whether the column is or not nullable. If this is `true`, a `NOT NULL` constraint will be added. - * If this is `false`, a `NULL` constraint will be added. + * @var mixed SQL string to be appended to column schema definition. */ - protected bool|null $isNotNull = null; + protected mixed $append = null; /** - * @var bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added. + * @var string|null comment value of the column. */ - protected bool $isUnique = false; + protected string|null $comment = null; /** * @var string|null the `CHECK` constraint for the column. @@ -85,50 +82,54 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable protected string|null $check = null; /** - * @var mixed default value of the column. - */ - protected mixed $default = null; - - /** - * @var mixed SQL string to be appended to column schema definition. + * @var Connection the database connection. This is used mainly to escape table and column names. */ - protected mixed $append = null; + protected Connection $db; /** - * @var bool whether the column values should be unsigned. If this is `true`, an `UNSIGNED` keyword will be added. + * @var mixed default value of the column. */ - protected bool $isUnsigned = false; + protected mixed $default = null; /** - * @var string|null the column after which this column will be added. + * @var bool|null whether the column is or not nullable. If this is `true`, a `NOT NULL` constraint will be added. + * If this is `false`, a `NULL` constraint will be added. */ - protected string|null $after = null; + protected bool|null $isNotNull = null; /** - * @var bool whether this column is to be inserted at the beginning of the table. + * @var bool whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added. */ - protected bool $isFirst = false; + protected bool $isUnique = false; /** - * @var Connection the database connection. This is used mainly to escape table and column names. + * @var array|int|string|null column size or precision definition. This is what goes into the parenthesis after + * the column type. This can be either a string, an integer or an array. If it is an array, the array values will + * be joined into a string separated by comma. */ - protected Connection $db; + protected array|int|string|null $length = null; /** - * @var string|null comment value of the column. + * @var string|null the column type definition such as INTEGER, VARCHAR, DATETIME, etc. */ - public string|null $comment = null; + protected string|null $type = null; /** * @var array mapping of abstract column types (keys) to type categories (values). */ public static array $typeCategoryMap = [ + // auto-incremental column types Schema::TYPE_AUTO => self::CATEGORY_AUTO, Schema::TYPE_BIGAUTO => self::CATEGORY_BIGAUTO, + + // integer auto-incremental primary key column types Schema::TYPE_PK => self::CATEGORY_PK, Schema::TYPE_UPK => self::CATEGORY_PK, - Schema::TYPE_BIGPK => self::CATEGORY_PK, - Schema::TYPE_UBIGPK => self::CATEGORY_PK, + + // big integer auto-incremental primary key column types + Schema::TYPE_BIGPK => self::CATEGORY_BIGPK, + Schema::TYPE_UBIGPK => self::CATEGORY_BIGPK, + Schema::TYPE_CHAR => self::CATEGORY_STRING, Schema::TYPE_STRING => self::CATEGORY_STRING, Schema::TYPE_TEXT => self::CATEGORY_STRING, @@ -170,37 +171,31 @@ public function __construct( } /** - * Adds a `NOT NULL` constraint to the column. + * Builds the full string for the column's schema. * - * @return static Instance of the column schema builder. + * @return string */ - public function notNull(): static + public function __toString(): string { - $this->isNotNull = true; + $format = match ($this->getTypeCategory()) { + self::CATEGORY_PK => '{type}{check}{comment}{append}', + default => '{type}{length}{notnull}{unique}{default}{check}{comment}{append}', + }; - return $this; + return $this->buildCompleteString($format); } /** - * Adds a `NULL` constraint to the column. + * Specify additional SQL to be appended to column definition. + * Position modifiers will be appended after column definition in databases that support them. * - * @return static Instance of the column schema builder. - */ - public function null(): static - { - $this->isNotNull = false; - - return $this; - } - - /** - * Adds a `UNIQUE` constraint to the column. + * @param string $sql the SQL string to be appended. * * @return static Instance of the column schema builder. */ - public function unique(): static + public function append(string $sql): static { - $this->isUnique = true; + $this->append = $sql; return $this; } @@ -220,185 +215,173 @@ public function check(string $check): static } /** - * Specify the default value for the column. + * Specifies the comment for column. * - * @param mixed $default the default value. + * @param string $comment the comment. * * @return static Instance of the column schema builder. */ - public function defaultValue(mixed $default): static + public function comment(string $comment): static { - if ($default === null) { - $this->null(); - } - - $this->default = $default; + $this->comment = $comment; return $this; } /** - * Specifies the comment for column. + * Specify the default SQL expression for the column. * - * @param string $comment the comment. + * @param string $default the default value expression. * * @return static Instance of the column schema builder. */ - public function comment(string $comment): static + public function defaultExpression(string $default): static { - $this->comment = $comment; + $this->default = new Expression($default); return $this; } /** - * Marks column as unsigned. + * Specify the default value for the column. + * + * @param mixed $default the default value. * * @return static Instance of the column schema builder. */ - public function unsigned(): static + public function defaultValue(mixed $default): static { - $this->type = match ($this->type) { - Schema::TYPE_PK => Schema::TYPE_UPK, - Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, - default => $this->type, - }; + if ($default === null) { + $this->null(); + } - $this->isUnsigned = true; + $this->default = $default; return $this; } /** - * Adds an `AFTER` constraint to the column. - * Note: MySQL and Oracle support only. - * - * @param string $after the column after which $this column will be added. - * - * @return static Instance of the column schema builder. + * @return array mapping of abstract column types (keys) to type categories (values). */ - public function after(string $after): static + public function getCategoryMap(): array { - $this->after = $after; - - return $this; + return static::$typeCategoryMap; } /** - * Adds an `FIRST` constraint to the column. - * Note: MySQL and Oracle support only. + * Adds a `NOT NULL` constraint to the column. * * @return static Instance of the column schema builder. */ - public function first(): static + public function notNull(): static { - $this->isFirst = true; + $this->isNotNull = true; return $this; } /** - * Specify the default SQL expression for the column. - * - * @param string $default the default value expression. + * Adds a `NULL` constraint to the column. * * @return static Instance of the column schema builder. */ - public function defaultExpression(string $default): static + public function null(): static { - $this->default = new Expression($default); + $this->isNotNull = false; return $this; } /** - * Specify additional SQL to be appended to column definition. - * Position modifiers will be appended after column definition in databases that support them. - * - * @param string $sql the SQL string to be appended. - * @param Connection|null $db the database connection. If db is not null, the SQL will be quoted using db. - * - * @return static Instance of the column schema builder. + * @param array $categoryMap mapping of abstract column types (keys) to type categories (values). */ - public function append(string $sql): static + public function setCategoryMap(array $categoryMap): void { - $this->append = $sql; - - return $this; + static::$typeCategoryMap = $categoryMap; } /** - * Builds the full string for the column's schema. + * Adds a `UNIQUE` constraint to the column. * - * @return string + * @return static Instance of the column schema builder. */ - public function __toString(): string + public function unique(): static { - $format = match ($this->getTypeCategory()) { - self::CATEGORY_PK => '{type}{check}{comment}{append}', - default => '{type}{length}{notnull}{unique}{default}{check}{comment}{append}', - }; + $this->isUnique = true; - return $this->buildCompleteString($format); + return $this; } /** - * @return array mapping of abstract column types (keys) to type categories (values). + * Builds the custom string that's appended to column definition. + * + * @return string custom string to append. */ - public function getCategoryMap(): array + protected function buildAppendString(): string { - return static::$typeCategoryMap; + return $this->append !== null ? ' ' . $this->append : ''; } /** - * @param array $categoryMap mapping of abstract column types (keys) to type categories (values). + * Builds the check constraint for the column. + * + * @return string a string containing the CHECK constraint. */ - public function setCategoryMap(array $categoryMap): void + protected function buildCheckString(): string { - static::$typeCategoryMap = $categoryMap; + return $this->check !== null ? " CHECK ({$this->check})" : ''; } /** - * Builds the length/precision part of the column. + * Builds the comment specification for the column. * - * @return string a string containing the length/precision of the column. + * @return string a string containing the COMMENT keyword and the comment itself. */ - protected function buildLengthString() + protected function buildCommentString(): string { - if ($this->length === null || $this->length === []) { - return ''; - } - - if (is_array($this->length)) { - $this->length = implode(',', $this->length); - } - - return "({$this->length})"; + return ''; } /** - * Builds the not null constraint for the column. + * Returns the complete column definition from input format. * - * @return string returns 'NOT NULL' if [[isNotNull]] is true, 'NULL' if [[isNotNull]] is false or an empty string - * otherwise. + * @param string $format the format of the definition. + * @param array $customPlaceHolder custom placeholder values. + * + * @return string a string containing the complete column definition. */ - protected function buildNotNullString(): string + protected function buildCompleteString(string $format, array $customPlaceHolder = []): string { - return match ($this->isNotNull) { - true => ' NOT NULL', - false => ' NULL', - default => '', - }; + $placeholderValues = [ + '{type}' => $this->type, + '{length}' => $this->buildLengthString(), + '{notnull}' => $this->buildNotNullString(), + '{unique}' => $this->buildUniqueString(), + '{default}' => $this->buildDefaultString(), + '{check}' => $this->buildCheckString(), + '{comment}' => $this->buildCommentString(), + '{append}' => $this->buildAppendString(), + ]; + + $placeholderValues += $customPlaceHolder; + + return strtr($format, $placeholderValues); } /** - * Builds the unique constraint for the column. + * Builds the default value specification for the column. * - * @return string returns string 'UNIQUE' if [[isUnique]] is true, otherwise it returns an empty string. + * @return string string with default value of column. */ - protected function buildUniqueString(): string + protected function buildDefaultString(): string { - return $this->isUnique ? ' UNIQUE' : ''; + $defaultValue = $this->buildDefaultValue(); + + if ($defaultValue === null) { + return ''; + } + + return ' DEFAULT ' . $defaultValue; } /** @@ -422,69 +405,46 @@ protected function buildDefaultValue(): string|null } /** - * Builds the default value specification for the column. + * Builds the length/precision part of the column. * - * @return string string with default value of column. + * @return string a string containing the length/precision of the column. */ - protected function buildDefaultString(): string + protected function buildLengthString() { - $defaultValue = $this->buildDefaultValue(); - - if ($defaultValue === null) { + if ($this->length === null || $this->length === []) { return ''; } - return ' DEFAULT ' . $defaultValue; - } - - /** - * Builds the check constraint for the column. - * - * @return string a string containing the CHECK constraint. - */ - protected function buildCheckString(): string - { - return $this->check !== null ? " CHECK ({$this->check})" : ''; - } - - /** - * Builds the unsigned string for column. Defaults to unsupported. - * - * @return string a string containing UNSIGNED keyword. - */ - protected function buildUnsignedString(): string - { - return ''; - } + if (is_array($this->length)) { + $this->length = implode(',', $this->length); + } - /** - * Builds the after constraint for the column. Defaults to unsupported. - * - * @return string a string containing the AFTER constraint. - */ - protected function buildAfterString(): string - { - return ''; + return "({$this->length})"; } /** - * Builds the first constraint for the column. Defaults to unsupported. + * Builds the not null constraint for the column. * - * @return string a string containing the FIRST constraint. + * @return string returns 'NOT NULL' if [[isNotNull]] is true, 'NULL' if [[isNotNull]] is false or an empty string + * otherwise. */ - protected function buildFirstString(): string + protected function buildNotNullString(): string { - return ''; + return match ($this->isNotNull) { + true => ' NOT NULL', + false => ' NULL', + default => '', + }; } /** - * Builds the custom string that's appended to column definition. + * Builds the unique constraint for the column. * - * @return string custom string to append. + * @return string returns string 'UNIQUE' if [[isUnique]] is true, otherwise it returns an empty string. */ - protected function buildAppendString(): string + protected function buildUniqueString(): string { - return $this->append !== null ? ' ' . $this->append : ''; + return $this->isUnique ? ' UNIQUE' : ''; } /** @@ -496,39 +456,4 @@ protected function getTypeCategory(): string|null { return isset($this->categoryMap[$this->type]) ? $this->categoryMap[$this->type] : null; } - - /** - * Builds the comment specification for the column. - * - * @return string a string containing the COMMENT keyword and the comment itself. - */ - protected function buildCommentString(): string - { - return ''; - } - - /** - * Returns the complete column definition from input format. - * - * @param string $format the format of the definition. - * - * @return string a string containing the complete column definition. - */ - protected function buildCompleteString(string $format): string - { - $placeholderValues = [ - '{type}' => $this->type, - '{length}' => $this->buildLengthString(), - '{unsigned}' => $this->buildUnsignedString(), - '{notnull}' => $this->buildNotNullString(), - '{unique}' => $this->buildUniqueString(), - '{default}' => $this->buildDefaultString(), - '{check}' => $this->buildCheckString(), - '{comment}' => $this->buildCommentString(), - '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), - '{append}' => $this->buildAppendString(), - ]; - - return strtr($format, $placeholderValues); - } } diff --git a/src/db/mssql/ColumnSchemaBuilder.php b/src/db/mssql/ColumnSchemaBuilder.php index 57b29b7a..e23ff97a 100644 --- a/src/db/mssql/ColumnSchemaBuilder.php +++ b/src/db/mssql/ColumnSchemaBuilder.php @@ -20,16 +20,6 @@ class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder */ protected $format = '{type}{length}{notnull}{unique}{default}{check}{append}'; - /** - * The column types that are auto-incremental or primary key. - */ - private const CATEGORY_GROUP_AUTO_PK = [ - self::CATEGORY_AUTO, - self::CATEGORY_BIGAUTO, - self::CATEGORY_PK, - self::CATEGORY_BIGPK, - ]; - /** * @var array The parameters for the IDENTITY column. */ @@ -37,7 +27,7 @@ class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder public function __construct(Connection $db, string|null $type = null, $length = null, $config = []) { - if ((in_array($type, self::CATEGORY_GROUP_AUTO_PK)) && is_array($length) && count($length) === 2) { + if (in_array($type, self::CATEGORY_GROUP_AUTO_PK, true) && is_array($length) && count($length) === 2) { // start, increment. If increment is 0, it will be set to 1. if ($length[1] === 0) { $length[1] = 1; @@ -58,7 +48,7 @@ public function __toString(): string { $format = $this->format; - if (in_array($this->getTypeCategory(), self::CATEGORY_GROUP_AUTO_PK)) { + if (in_array($this->getTypeCategory(), self::CATEGORY_GROUP_AUTO_PK, true)) { $format = '{type}{identity}{check}{comment}{append}'; } @@ -184,23 +174,9 @@ public function isUnique(): bool /** * {@inheritdoc} */ - protected function buildCompleteString($format): string + protected function buildCompleteString(string $format, array $customPlaceHolder = []): string { - $placeholderValues = [ - '{type}' => $this->type, - '{length}' => $this->buildLengthString(), - '{unsigned}' => $this->buildUnsignedString(), - '{notnull}' => $this->buildNotNullString(), - '{unique}' => $this->buildUniqueString(), - '{default}' => $this->buildDefaultString(), - '{check}' => $this->buildCheckString(), - '{comment}' => $this->buildCommentString(), - '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), - '{append}' => $this->buildAppendString(), - '{identity}' => $this->buildIdentityString(), - ]; - - return strtr($format, $placeholderValues); + return parent::buildCompleteString($format, ['{identity}' => $this->buildIdentityString()]); } /** @@ -208,7 +184,7 @@ protected function buildCompleteString($format): string */ protected function buildIdentityString(): string { - if (in_array($this->type, self::CATEGORY_GROUP_AUTO_PK) && $this->identityParams !== []) { + if (in_array($this->getTypeCategory(), self::CATEGORY_GROUP_AUTO_PK, true) && $this->identityParams !== []) { return "({$this->identityParams[0]},{$this->identityParams[1]})"; } diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index aca0e089..4bc0d468 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -19,6 +19,52 @@ class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder */ public const CATEGORY_UPK = 'upk'; + /** + * @var string|null the column after which this column will be added. + */ + protected string|null $after = null; + + /** + * @var bool whether this column is to be inserted at the beginning of the table. + */ + protected bool $isFirst = false; + + /** + * @var bool whether the column values should be unsigned. If this is `true`, an `UNSIGNED` keyword will be added. + */ + protected bool $isUnsigned = false; + + /** + * {@inheritdoc} + */ + public function __toString(): string + { + $format = match ($this->getTypeCategory()) { + self::CATEGORY_AUTO, + self::CATEGORY_BIGAUTO, + self::CATEGORY_BIGPK, + self::CATEGORY_PK => '{type}{length}{comment}{check}{append}{pos}', + self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}', + default => '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}', + }; + + return $this->buildCompleteString($format); + } + + /** + * Adds an `AFTER` constraint to the column. + * + * @param string $after the column after which $this column will be added. + * + * @return static Instance of the column schema builder. + */ + public function after(string $after): static + { + $this->after = $after; + + return $this; + } + /** * Creates an `INTEGER AUTO_INCREMENT` column. * @@ -74,34 +120,27 @@ public function bigPrimaryKey(int|null $length = null): self } /** - * Creates a `INTEGER AUTO_INCREMENT PRIMARY KEY` column. - * - * @param int|null $length column size or precision definition. Defaults to 11. + * Adds an `FIRST` constraint to the column. * - * @return self The column schema builder instance. + * @return static Instance of the column schema builder. */ - public function primaryKey(int|null $length = null): self + public function first(): static { - $this->type = self::CATEGORY_PK; - - if ($length !== null) { - $this->length = $length; - } + $this->isFirst = true; return $this; } /** - * Creates a `UNSIGNED BIGINT AUTO_INCREMENT PRIMARY KEY` column. + * Creates a `INTEGER AUTO_INCREMENT PRIMARY KEY` column. * - * @param int|null $length column size or precision definition. Defaults to 20. + * @param int|null $length column size or precision definition. Defaults to 11. * * @return self The column schema builder instance. */ - public function unsignedBigPrimaryKey(int|null $length = null): self + public function primaryKey(int|null $length = null): self { - $this->type = self::CATEGORY_UBIGPK; - $this->isUnsigned = true; + $this->type = self::CATEGORY_PK; if ($length !== null) { $this->length = $length; @@ -111,20 +150,19 @@ public function unsignedBigPrimaryKey(int|null $length = null): self } /** - * Creates a `UNSIGNED INTEGER AUTO_INCREMENT PRIMARY KEY` column. + * Marks column as unsigned. * - * @param int|null $length column size or precision definition. Defaults to 11. - * - * @return self The column schema builder instance. + * @return static Instance of the column schema builder. */ - public function unsignedPrimaryKey(int|null $length = null): self + public function unsigned(): static { - $this->type = self::CATEGORY_UPK; - $this->isUnsigned = true; + $this->type = match ($this->type) { + Schema::TYPE_PK => Schema::TYPE_UPK, + Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, + default => $this->type, + }; - if ($length !== null) { - $this->length = $length; - } + $this->isUnsigned = true; return $this; } @@ -162,19 +200,20 @@ protected function buildCommentString(): string } /** - * {@inheritdoc} + * Returns the complete column definition from input format. + * + * @param string $format the format of the definition. + * + * @return string a string containing the complete column definition. */ - public function __toString(): string + protected function buildCompleteString(string $format, array $customPlaceHolder = []): string { - $format = match ($this->getTypeCategory()) { - self::CATEGORY_AUTO, - self::CATEGORY_BIGAUTO, - self::CATEGORY_BIGPK, - self::CATEGORY_PK => '{type}{length}{comment}{check}{append}{pos}', - self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}', - default => '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}', - }; - - return $this->buildCompleteString($format); + return parent::buildCompleteString( + $format, + [ + '{unsigned}' => $this->buildUnsignedString(), + '{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), + ], + ); } } diff --git a/tests/framework/db/mysql/provider/ColumnTypeProvider.php b/tests/framework/db/mysql/provider/ColumnTypeProvider.php index e2e70d1a..3b4b490f 100644 --- a/tests/framework/db/mysql/provider/ColumnTypeProvider.php +++ b/tests/framework/db/mysql/provider/ColumnTypeProvider.php @@ -164,13 +164,13 @@ public function unsignedBigPrimaryKey(): array [ Schema::TYPE_UBIGPK, 'ubigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(), + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ Schema::TYPE_UBIGPK . '(1)', 'ubigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(1), + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; @@ -182,12 +182,12 @@ public function unsignedBigPrimaryKeyWithRaw(): array [ 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'ubigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(), + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), ], [ 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'ubigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedBigPrimaryKey(1), + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; @@ -199,13 +199,13 @@ public function unsignedPrimaryKey(): array [ Schema::TYPE_UPK, 'upk', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(), + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ Schema::TYPE_UPK . '(1)', 'upk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(1), + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; @@ -217,12 +217,12 @@ public function unsignedPrimaryKeyWithRaw(): array [ 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'upk', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(), + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), ], [ 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'upk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->unsignedPrimaryKey(1), + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; From 72e757d137a7f7fe42ad0918061de1becea0b501 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 05:31:31 -0400 Subject: [PATCH 17/31] Rever change visibility comment property in `ColumnSchemaBuilder::class`. --- src/db/ColumnSchemaBuilder.php | 10 +++--- src/db/oci/ColumnSchemaBuilder.php | 10 +----- src/db/sqlite/ColumnSchemaBuilder.php | 28 +++++++++++++++- tests/framework/db/QueryBuilderTest.php | 33 +++++++++++-------- tests/framework/db/pgsql/QueryBuilderTest.php | 10 ------ .../framework/db/sqlite/QueryBuilderTest.php | 11 ------- 6 files changed, 52 insertions(+), 50 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index 882cb205..ad282d6e 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -71,11 +71,6 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable */ protected mixed $append = null; - /** - * @var string|null comment value of the column. - */ - protected string|null $comment = null; - /** * @var string|null the `CHECK` constraint for the column. */ @@ -114,6 +109,11 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable */ protected string|null $type = null; + /** + * @var string|null comment value of the column. + */ + public string|null $comment = null; + /** * @var array mapping of abstract column types (keys) to type categories (values). */ diff --git a/src/db/oci/ColumnSchemaBuilder.php b/src/db/oci/ColumnSchemaBuilder.php index ba9cdf11..a8c737ea 100644 --- a/src/db/oci/ColumnSchemaBuilder.php +++ b/src/db/oci/ColumnSchemaBuilder.php @@ -11,14 +11,6 @@ */ class ColumnSchemaBuilder extends AbstractColumnSchemaBuilder { - /** - * {@inheritdoc} - */ - protected function buildUnsignedString(): string - { - return $this->isUnsigned ? ' UNSIGNED' : ''; - } - /** * {@inheritdoc} */ @@ -26,7 +18,7 @@ public function __toString(): string { $format = match ($this->getTypeCategory()) { self::CATEGORY_PK => '{type}{length}{check}{append}', - self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{default}{notnull}{check}{append}', + self::CATEGORY_NUMERIC => '{type}{length}{default}{notnull}{check}{append}', default => '{type}{length}{default}{notnull}{check}{append}', }; diff --git a/src/db/sqlite/ColumnSchemaBuilder.php b/src/db/sqlite/ColumnSchemaBuilder.php index 5e7d68b3..fd5ef61a 100644 --- a/src/db/sqlite/ColumnSchemaBuilder.php +++ b/src/db/sqlite/ColumnSchemaBuilder.php @@ -9,6 +9,29 @@ */ class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder { + /** + * @var bool whether the column values should be unsigned. If this is `true`, an `UNSIGNED` keyword will be added. + */ + protected bool $isUnsigned = false; + + /** + * Marks column as unsigned. + * + * @return static Instance of the column schema builder. + */ + public function unsigned(): static + { + $this->type = match ($this->type) { + Schema::TYPE_PK => Schema::TYPE_UPK, + Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, + default => $this->type, + }; + + $this->isUnsigned = true; + + return $this; + } + /** * {@inheritdoc} */ @@ -28,6 +51,9 @@ public function __toString() default => '{type}{length}{notnull}{unique}{check}{default}{append}', }; - return $this->buildCompleteString($format); + return $this->buildCompleteString( + $format, + ['{unsigned}' => $this->buildUnsignedString()], + ); } } diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index 3a897f8d..62f71a18 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -603,7 +603,8 @@ public function columnTypes() ], [ Schema::TYPE_TINYINT . ' UNSIGNED', - $this->tinyInteger()->unsigned(), + in_array($this->driverName, ['mysql', 'sqlite'], true) + ? $this->tinyInteger()->unsigned() : $this->tinyInteger(), [ 'mysql' => 'tinyint(3) UNSIGNED', 'sqlite' => 'tinyint UNSIGNED', @@ -867,21 +868,29 @@ public function columnTypes() ], [ Schema::TYPE_UPK, - $this->primaryKey()->unsigned(), + in_array($this->driverName, ['mysql', 'sqlite'], true) + ? $this->primaryKey()->unsigned() : $this->primaryKey(), [ 'mysql' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pgsql' => 'serial NOT NULL PRIMARY KEY', 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', ], + [ + 'pgsql' => 'pk', + ] ], [ Schema::TYPE_UBIGPK, - $this->bigPrimaryKey()->unsigned(), + in_array($this->driverName, ['mysql', 'sqlite'], true) + ? $this->bigPrimaryKey()->unsigned() : $this->bigPrimaryKey(), [ 'mysql' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pgsql' => 'bigserial NOT NULL PRIMARY KEY', 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', ], + [ + 'pgsql' => 'bigpk', + ] ], [ Schema::TYPE_INTEGER . " COMMENT 'test comment'", @@ -899,7 +908,6 @@ public function columnTypes() $this->primaryKey()->comment('test comment'), [ 'mysql' => "int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'test comment'", - 'sqlsrv' => 'int IDENTITY PRIMARY KEY', ], [ 'sqlsrv' => 'pk', @@ -907,10 +915,9 @@ public function columnTypes() ], [ Schema::TYPE_PK . ' FIRST', - $this->primaryKey()->first(), + in_array($this->driverName, ['mysql'], true) ? $this->primaryKey()->first() : $this->primaryKey(), [ 'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST', - 'sqlsrv' => 'int IDENTITY PRIMARY KEY', ], [ 'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY', @@ -919,10 +926,9 @@ public function columnTypes() ], [ Schema::TYPE_INTEGER . ' FIRST', - $this->integer()->first(), + in_array($this->driverName, ['mysql'], true) ? $this->integer()->first() : $this->integer(), [ 'mysql' => 'int(11) FIRST', - 'sqlsrv' => 'int', ], [ 'oci' => 'NUMBER(10)', @@ -932,10 +938,9 @@ public function columnTypes() ], [ Schema::TYPE_STRING . ' FIRST', - $this->string()->first(), + in_array($this->driverName, ['mysql'], true) ? $this->string()->first() : $this->string(), [ 'mysql' => 'varchar(255) FIRST', - 'sqlsrv' => 'nvarchar(255)', ], [ 'oci' => 'VARCHAR2(255)', @@ -944,10 +949,10 @@ public function columnTypes() ], [ Schema::TYPE_INTEGER . ' NOT NULL FIRST', - $this->integer()->append('NOT NULL')->first(), + in_array($this->driverName, ['mysql'], true) + ? $this->integer()->append('NOT NULL')->first() : $this->integer()->append('NOT NULL'), [ 'mysql' => 'int(11) NOT NULL FIRST', - 'sqlsrv' => 'int NOT NULL', ], [ 'oci' => 'NUMBER(10) NOT NULL', @@ -956,10 +961,10 @@ public function columnTypes() ], [ Schema::TYPE_STRING . ' NOT NULL FIRST', - $this->string()->append('NOT NULL')->first(), + in_array($this->driverName, ['mysql'], true) + ? $this->string()->append('NOT NULL')->first() : $this->string()->append('NOT NULL'), [ 'mysql' => 'varchar(255) NOT NULL FIRST', - 'sqlsrv' => 'nvarchar(255) NOT NULL', ], [ 'oci' => 'VARCHAR2(255) NOT NULL', diff --git a/tests/framework/db/pgsql/QueryBuilderTest.php b/tests/framework/db/pgsql/QueryBuilderTest.php index 918f3021..eeb7492f 100644 --- a/tests/framework/db/pgsql/QueryBuilderTest.php +++ b/tests/framework/db/pgsql/QueryBuilderTest.php @@ -41,16 +41,6 @@ public function columnTypes() $this->char(6)->check('value LIKE \'test%\''), 'char(6) CHECK (value LIKE \'test%\')', ], - [ - Schema::TYPE_CHAR . '(6)', - $this->char(6)->unsigned(), - 'char(6)', - ], - [ - Schema::TYPE_INTEGER . '(8)', - $this->integer(8)->unsigned(), - 'integer', - ], [ Schema::TYPE_TIMESTAMP . '(4)', $this->timestamp(4), diff --git a/tests/framework/db/sqlite/QueryBuilderTest.php b/tests/framework/db/sqlite/QueryBuilderTest.php index fa3619ac..645b6564 100644 --- a/tests/framework/db/sqlite/QueryBuilderTest.php +++ b/tests/framework/db/sqlite/QueryBuilderTest.php @@ -22,17 +22,6 @@ class QueryBuilderTest extends \yiiunit\framework\db\QueryBuilderTest protected $likeEscapeCharSql = " ESCAPE '\\'"; - public function columnTypes() - { - return array_merge(parent::columnTypes(), [ - [ - Schema::TYPE_PK, - $this->primaryKey()->first()->after('col_before'), - 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - ], - ]); - } - public function conditionProvider() { return array_merge(parent::conditionProvider(), [ From 23598927b07fd68ee81757076fe4ff4792e21793 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 05:47:18 -0400 Subject: [PATCH 18/31] Refactor Oracle query builder to remove unnecessary primary key type mappings. --- src/db/oci/QueryBuilder.php | 2 -- tests/framework/db/ColumnSchemaBuilderTest.php | 7 +------ .../framework/db/oci/ColumnSchemaBuilderTest.php | 15 --------------- 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/db/oci/QueryBuilder.php b/src/db/oci/QueryBuilder.php index 475d0f6b..fd75608f 100644 --- a/src/db/oci/QueryBuilder.php +++ b/src/db/oci/QueryBuilder.php @@ -24,9 +24,7 @@ class QueryBuilder extends \yii\db\QueryBuilder */ public $typeMap = [ Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', - Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY', Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', - Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY', Schema::TYPE_CHAR => 'CHAR(1)', Schema::TYPE_STRING => 'VARCHAR2(255)', Schema::TYPE_TEXT => 'CLOB', diff --git a/tests/framework/db/ColumnSchemaBuilderTest.php b/tests/framework/db/ColumnSchemaBuilderTest.php index eb0ecd52..33913fd0 100644 --- a/tests/framework/db/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/ColumnSchemaBuilderTest.php @@ -29,12 +29,6 @@ public function getColumnSchemaBuilder($type, $length = null) public function typesProvider() { return [ - ['integer NULL DEFAULT NULL', Schema::TYPE_INTEGER, null, [ - ['unsigned'], ['null'], - ]], - ['integer(10)', Schema::TYPE_INTEGER, 10, [ - ['unsigned'], - ]], ['timestamp() WITH TIME ZONE NOT NULL', 'timestamp() WITH TIME ZONE', null, [ ['notNull'], ]], @@ -68,6 +62,7 @@ public function testCustomTypes($expected, $type, $length, $calls) public function checkBuildString($expected, $type, $length, $calls) { $builder = $this->getColumnSchemaBuilder($type, $length); + foreach ($calls as $call) { $method = array_shift($call); \call_user_func_array([$builder, $method], $call); diff --git a/tests/framework/db/oci/ColumnSchemaBuilderTest.php b/tests/framework/db/oci/ColumnSchemaBuilderTest.php index 3e6fa366..35f126c8 100644 --- a/tests/framework/db/oci/ColumnSchemaBuilderTest.php +++ b/tests/framework/db/oci/ColumnSchemaBuilderTest.php @@ -22,19 +22,4 @@ public function getColumnSchemaBuilder($type, $length = null): ColumnSchemaBuild { return new ColumnSchemaBuilder($this->getConnection(), $type, $length); } - - /** - * @return array - */ - public function typesProvider() - { - return [ - ['integer UNSIGNED', Schema::TYPE_INTEGER, null, [ - ['unsigned'], - ]], - ['integer(10) UNSIGNED', Schema::TYPE_INTEGER, 10, [ - ['unsigned'], - ]], - ]; - } } From 4f0d0bbffcddf85eb8f7a073ce785d538d55d288 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 08:43:50 -0400 Subject: [PATCH 19/31] Refactor column type mappings in QueryBuilder classes for primary keys. --- src/db/Schema.php | 14 +++++++------- src/db/mssql/QueryBuilder.php | 6 ++++-- src/db/mysql/QueryBuilder.php | 4 ++-- src/db/oci/QueryBuilder.php | 6 ++++++ src/db/pgsql/QueryBuilder.php | 6 +++++- src/db/sqlite/QueryBuilder.php | 8 ++++++-- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/db/Schema.php b/src/db/Schema.php index 84cef5e0..dd1fa9b1 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -45,26 +45,26 @@ abstract class Schema extends BaseObject */ public const TYPE_BIGAUTO = 'bigauto'; - /** - * Define the abstract column type as an `bigint` primary key. - */ - public const TYPE_BIGPK = 'bigpk'; - /** * Define the abstract column type as an `integer` primary key. */ public const TYPE_PK = 'pk'; /** - * Define the abstract column type as an `unsigned bigint` primary key. + * Define the abstract column type as an `bigint` primary key. */ - public const TYPE_UBIGPK = 'ubigpk'; + public const TYPE_BIGPK = 'bigpk'; /** * Define the abstract column type as an `unsigned integer` primary key. */ public const TYPE_UPK = 'upk'; + /** + * Define the abstract column type as an `unsigned bigint` primary key. + */ + public const TYPE_UBIGPK = 'ubigpk'; + /** * Define the abstract column type as `char`. */ diff --git a/src/db/mssql/QueryBuilder.php b/src/db/mssql/QueryBuilder.php index 8aa65b49..7adf6ae7 100644 --- a/src/db/mssql/QueryBuilder.php +++ b/src/db/mssql/QueryBuilder.php @@ -24,10 +24,12 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_AUTO => 'int IDENTITY', Schema::TYPE_BIGAUTO => 'bigint IDENTITY', - // auto-incremental primary key + // primary key Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY', - Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', + + // unsigned primary key (MSSQL does not support unsigned columns) + Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', // string types diff --git a/src/db/mysql/QueryBuilder.php b/src/db/mysql/QueryBuilder.php index d84d6260..26c11114 100644 --- a/src/db/mysql/QueryBuilder.php +++ b/src/db/mysql/QueryBuilder.php @@ -26,11 +26,11 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_AUTO => 'int(11) AUTO_INCREMENT', Schema::TYPE_BIGAUTO => 'bigint(20) AUTO_INCREMENT', - // auto-incremental primary key + // primary key Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - // auto-incremental unsigned primary key + // primary key unsigned Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', diff --git a/src/db/oci/QueryBuilder.php b/src/db/oci/QueryBuilder.php index fd75608f..d6fc66ec 100644 --- a/src/db/oci/QueryBuilder.php +++ b/src/db/oci/QueryBuilder.php @@ -23,8 +23,14 @@ class QueryBuilder extends \yii\db\QueryBuilder * @var array mapping from abstract column types (keys) to physical column types (values). */ public $typeMap = [ + // primary key Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', + + // unsigned primary key (Oracle does not support unsigned integer) + Schema::TYPE_UPK => 'NUMBER(10) NOT NULL PRIMARY KEY', + Schema::TYPE_UBIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', + Schema::TYPE_CHAR => 'CHAR(1)', Schema::TYPE_STRING => 'VARCHAR2(255)', Schema::TYPE_TEXT => 'CLOB', diff --git a/src/db/pgsql/QueryBuilder.php b/src/db/pgsql/QueryBuilder.php index eb055461..8591359d 100644 --- a/src/db/pgsql/QueryBuilder.php +++ b/src/db/pgsql/QueryBuilder.php @@ -48,10 +48,14 @@ class QueryBuilder extends \yii\db\QueryBuilder * @var array mapping from abstract column types (keys) to physical column types (values). */ public $typeMap = [ + // primary key Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY', - Schema::TYPE_UPK => 'serial NOT NULL PRIMARY KEY', Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY', + + // unsigned primary key (Oracle does not support unsigned integer) + Schema::TYPE_UPK => 'serial NOT NULL PRIMARY KEY', Schema::TYPE_UBIGPK => 'bigserial NOT NULL PRIMARY KEY', + Schema::TYPE_CHAR => 'char(1)', Schema::TYPE_STRING => 'varchar(255)', Schema::TYPE_TEXT => 'text', diff --git a/src/db/sqlite/QueryBuilder.php b/src/db/sqlite/QueryBuilder.php index 53071acf..a6646e5f 100644 --- a/src/db/sqlite/QueryBuilder.php +++ b/src/db/sqlite/QueryBuilder.php @@ -29,10 +29,14 @@ class QueryBuilder extends \yii\db\QueryBuilder * @var array mapping from abstract column types (keys) to physical column types (values). */ public $typeMap = [ + // primary key Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - Schema::TYPE_UPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', Schema::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', - Schema::TYPE_UBIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + + // unsigned primary + Schema::TYPE_UPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', + Schema::TYPE_UBIGPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', + Schema::TYPE_CHAR => 'char(1)', Schema::TYPE_STRING => 'varchar(255)', Schema::TYPE_TEXT => 'text', From e8d0dbb807c89350bdab7ca2fc06d9e9ee0d3174 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 08:46:22 -0400 Subject: [PATCH 20/31] Refactor SQLite primary key type mapping in QueryBuilderTest.php. --- tests/framework/db/QueryBuilderTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index 62f71a18..4fe0632e 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -873,7 +873,7 @@ public function columnTypes() [ 'mysql' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pgsql' => 'serial NOT NULL PRIMARY KEY', - 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + 'sqlite' => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', ], [ 'pgsql' => 'pk', @@ -886,7 +886,7 @@ public function columnTypes() [ 'mysql' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', 'pgsql' => 'bigserial NOT NULL PRIMARY KEY', - 'sqlite' => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', + 'sqlite' => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', ], [ 'pgsql' => 'bigpk', From 8360d733867587474442f9c8930c4f6b5f8aa662 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 09:07:07 -0400 Subject: [PATCH 21/31] Refactor column type mappings for auto-incremental primary keys in MySQL. --- src/db/Schema.php | 4 +- src/db/mysql/ColumnSchemaBuilder.php | 5 +- src/db/mysql/QueryBuilder.php | 4 ++ src/db/mysql/Schema.php | 10 +++ .../db/mysql/provider/ColumnTypeProvider.php | 70 +++++++++++++++++++ .../db/mysql/querybuilder/ColumnTypeTest.php | 48 +++++++++++++ 6 files changed, 136 insertions(+), 5 deletions(-) diff --git a/src/db/Schema.php b/src/db/Schema.php index dd1fa9b1..b6040581 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -56,12 +56,12 @@ abstract class Schema extends BaseObject public const TYPE_BIGPK = 'bigpk'; /** - * Define the abstract column type as an `unsigned integer` primary key. + * Define the abstract column type as an `integer UNSIGNED` primary key. */ public const TYPE_UPK = 'upk'; /** - * Define the abstract column type as an `unsigned bigint` primary key. + * Define the abstract column type as an `bigint UNSIGNED` primary key. */ public const TYPE_UBIGPK = 'ubigpk'; diff --git a/src/db/mysql/ColumnSchemaBuilder.php b/src/db/mysql/ColumnSchemaBuilder.php index 4bc0d468..34ac311e 100644 --- a/src/db/mysql/ColumnSchemaBuilder.php +++ b/src/db/mysql/ColumnSchemaBuilder.php @@ -40,9 +40,6 @@ class ColumnSchemaBuilder extends \yii\db\ColumnSchemaBuilder public function __toString(): string { $format = match ($this->getTypeCategory()) { - self::CATEGORY_AUTO, - self::CATEGORY_BIGAUTO, - self::CATEGORY_BIGPK, self::CATEGORY_PK => '{type}{length}{comment}{check}{append}{pos}', self::CATEGORY_NUMERIC => '{type}{length}{unsigned}{notnull}{default}{unique}{comment}{append}{pos}{check}', default => '{type}{length}{notnull}{default}{unique}{comment}{append}{pos}{check}', @@ -157,6 +154,8 @@ public function primaryKey(int|null $length = null): self public function unsigned(): static { $this->type = match ($this->type) { + Schema::TYPE_AUTO => Schema::TYPE_UAUTO, + Schema::TYPE_BIGAUTO => Schema::TYPE_UBIGAUTO, Schema::TYPE_PK => Schema::TYPE_UPK, Schema::TYPE_BIGPK => Schema::TYPE_UBIGPK, default => $this->type, diff --git a/src/db/mysql/QueryBuilder.php b/src/db/mysql/QueryBuilder.php index 26c11114..dd8bdaaf 100644 --- a/src/db/mysql/QueryBuilder.php +++ b/src/db/mysql/QueryBuilder.php @@ -26,6 +26,10 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_AUTO => 'int(11) AUTO_INCREMENT', Schema::TYPE_BIGAUTO => 'bigint(20) AUTO_INCREMENT', + // auto-incremental unsigned + Schema::TYPE_UAUTO => 'int(10) UNSIGNED AUTO_INCREMENT', + Schema::TYPE_UBIGAUTO => 'bigint(20) UNSIGNED AUTO_INCREMENT', + // primary key Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', diff --git a/src/db/mysql/Schema.php b/src/db/mysql/Schema.php index 18f73445..e9afdce0 100644 --- a/src/db/mysql/Schema.php +++ b/src/db/mysql/Schema.php @@ -25,6 +25,16 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { use ConstraintFinderTrait; + /** + * Define the abstract column type as an `integer UNSIGNED` auto-incremental. + */ + public const TYPE_UAUTO = 'uauto'; + + /** + * Define the abstract column type as an `bigint UNSIGNED` auto-incremental. + */ + public const TYPE_UBIGAUTO = 'ubigauto'; + /** * {@inheritdoc} */ diff --git a/tests/framework/db/mysql/provider/ColumnTypeProvider.php b/tests/framework/db/mysql/provider/ColumnTypeProvider.php index 3b4b490f..1263eb75 100644 --- a/tests/framework/db/mysql/provider/ColumnTypeProvider.php +++ b/tests/framework/db/mysql/provider/ColumnTypeProvider.php @@ -158,6 +158,76 @@ public function primaryKeyWithRaw(): array ]; } + public function unsignedAutoIncrement(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UAUTO, + 'uauto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), + 'int(10) UNSIGNED AUTO_INCREMENT', + ], + [ + \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', + 'uauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + + public function unsignedAutoIncrementWithRaw(): array + { + return [ + [ + 'int(10) UNSIGNED AUTO_INCREMENT', + 'uauto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), + ], + [ + 'int(1) UNSIGNED AUTO_INCREMENT', + 'uauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + + public function unsignedBigAutoIncrement(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UBIGAUTO, + 'ubigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), + 'bigint(20) UNSIGNED AUTO_INCREMENT', + ], + [ + \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', + 'ubigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + + public function unsignedBigAutoIncrementWithRaw(): array + { + return [ + [ + 'bigint(20) UNSIGNED AUTO_INCREMENT', + 'ubigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), + ], + [ + 'bigint(1) UNSIGNED AUTO_INCREMENT', + 'ubigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + public function unsignedBigPrimaryKey(): array { return [ diff --git a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php index 6cda7377..4e205f08 100644 --- a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php +++ b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php @@ -119,6 +119,54 @@ public function testprimaryKeyWithRaw( $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); } + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedAutoIncrement + */ + public function testUnsignedAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedAutoIncrementWithRaw + */ + public function testUnsignedAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigAutoIncrement + */ + public function testUnsignedBigAutoIncrement( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigAutoIncrementWithRaw + */ + public function testUnsignedBigAutoIncrementWithRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + /** * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigPrimaryKey */ From 7d40efe33392dba07f0d18451fc406282c6e1cc0 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 21 Aug 2024 09:37:20 -0400 Subject: [PATCH 22/31] Remove const for unsigned, only use in `mysql`, `sqlite`. --- src/db/ColumnSchemaBuilder.php | 2 -- src/db/Schema.php | 10 ------- src/db/mssql/QueryBuilder.php | 4 --- src/db/mysql/Schema.php | 10 +++++++ src/db/oci/QueryBuilder.php | 4 --- src/db/pgsql/QueryBuilder.php | 4 --- src/db/sqlite/Schema.php | 20 +++++++++++++ tests/framework/db/QueryBuilderTest.php | 28 ------------------- .../db/mysql/provider/ColumnTypeProvider.php | 8 +++--- 9 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index ad282d6e..2a715a72 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -124,11 +124,9 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable // integer auto-incremental primary key column types Schema::TYPE_PK => self::CATEGORY_PK, - Schema::TYPE_UPK => self::CATEGORY_PK, // big integer auto-incremental primary key column types Schema::TYPE_BIGPK => self::CATEGORY_BIGPK, - Schema::TYPE_UBIGPK => self::CATEGORY_BIGPK, Schema::TYPE_CHAR => self::CATEGORY_STRING, Schema::TYPE_STRING => self::CATEGORY_STRING, diff --git a/src/db/Schema.php b/src/db/Schema.php index b6040581..b9b84db2 100644 --- a/src/db/Schema.php +++ b/src/db/Schema.php @@ -55,16 +55,6 @@ abstract class Schema extends BaseObject */ public const TYPE_BIGPK = 'bigpk'; - /** - * Define the abstract column type as an `integer UNSIGNED` primary key. - */ - public const TYPE_UPK = 'upk'; - - /** - * Define the abstract column type as an `bigint UNSIGNED` primary key. - */ - public const TYPE_UBIGPK = 'ubigpk'; - /** * Define the abstract column type as `char`. */ diff --git a/src/db/mssql/QueryBuilder.php b/src/db/mssql/QueryBuilder.php index 7adf6ae7..03df5eed 100644 --- a/src/db/mssql/QueryBuilder.php +++ b/src/db/mssql/QueryBuilder.php @@ -28,10 +28,6 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_PK => 'int IDENTITY PRIMARY KEY', Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', - // unsigned primary key (MSSQL does not support unsigned columns) - Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', - Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', - // string types Schema::TYPE_CHAR => 'nchar(1)', Schema::TYPE_STRING => 'nvarchar(255)', diff --git a/src/db/mysql/Schema.php b/src/db/mysql/Schema.php index e9afdce0..bba0e2ef 100644 --- a/src/db/mysql/Schema.php +++ b/src/db/mysql/Schema.php @@ -35,6 +35,16 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface */ public const TYPE_UBIGAUTO = 'ubigauto'; + /** + * Define the abstract column type as an `integer UNSIGNED` primary key. + */ + public const TYPE_UPK = 'upk'; + + /** + * Define the abstract column type as an `bigint UNSIGNED` primary key. + */ + public const TYPE_UBIGPK = 'ubigpk'; + /** * {@inheritdoc} */ diff --git a/src/db/oci/QueryBuilder.php b/src/db/oci/QueryBuilder.php index d6fc66ec..11c0a47f 100644 --- a/src/db/oci/QueryBuilder.php +++ b/src/db/oci/QueryBuilder.php @@ -27,10 +27,6 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', - // unsigned primary key (Oracle does not support unsigned integer) - Schema::TYPE_UPK => 'NUMBER(10) NOT NULL PRIMARY KEY', - Schema::TYPE_UBIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', - Schema::TYPE_CHAR => 'CHAR(1)', Schema::TYPE_STRING => 'VARCHAR2(255)', Schema::TYPE_TEXT => 'CLOB', diff --git a/src/db/pgsql/QueryBuilder.php b/src/db/pgsql/QueryBuilder.php index 8591359d..f6666788 100644 --- a/src/db/pgsql/QueryBuilder.php +++ b/src/db/pgsql/QueryBuilder.php @@ -52,10 +52,6 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY', Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY', - // unsigned primary key (Oracle does not support unsigned integer) - Schema::TYPE_UPK => 'serial NOT NULL PRIMARY KEY', - Schema::TYPE_UBIGPK => 'bigserial NOT NULL PRIMARY KEY', - Schema::TYPE_CHAR => 'char(1)', Schema::TYPE_STRING => 'varchar(255)', Schema::TYPE_TEXT => 'text', diff --git a/src/db/sqlite/Schema.php b/src/db/sqlite/Schema.php index 0be128f2..93131079 100644 --- a/src/db/sqlite/Schema.php +++ b/src/db/sqlite/Schema.php @@ -29,6 +29,26 @@ class Schema extends \yii\db\Schema implements ConstraintFinderInterface { use ConstraintFinderTrait; + /** + * Define the abstract column type as an `integer UNSIGNED` auto-incremental. + */ + public const TYPE_UAUTO = 'uauto'; + + /** + * Define the abstract column type as an `bigint UNSIGNED` auto-incremental. + */ + public const TYPE_UBIGAUTO = 'ubigauto'; + + /** + * Define the abstract column type as an `integer UNSIGNED` primary key. + */ + public const TYPE_UPK = 'upk'; + + /** + * Define the abstract column type as an `bigint UNSIGNED` primary key. + */ + public const TYPE_UBIGPK = 'ubigpk'; + /** * @var array mapping from physical column types (keys) to abstract column types (values). */ diff --git a/tests/framework/db/QueryBuilderTest.php b/tests/framework/db/QueryBuilderTest.php index 4fe0632e..13ea72b6 100644 --- a/tests/framework/db/QueryBuilderTest.php +++ b/tests/framework/db/QueryBuilderTest.php @@ -866,32 +866,6 @@ public function columnTypes() 'sqlsrv' => 'datetime NULL DEFAULT NULL', ], ], - [ - Schema::TYPE_UPK, - in_array($this->driverName, ['mysql', 'sqlite'], true) - ? $this->primaryKey()->unsigned() : $this->primaryKey(), - [ - 'mysql' => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pgsql' => 'serial NOT NULL PRIMARY KEY', - 'sqlite' => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', - ], - [ - 'pgsql' => 'pk', - ] - ], - [ - Schema::TYPE_UBIGPK, - in_array($this->driverName, ['mysql', 'sqlite'], true) - ? $this->bigPrimaryKey()->unsigned() : $this->bigPrimaryKey(), - [ - 'mysql' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pgsql' => 'bigserial NOT NULL PRIMARY KEY', - 'sqlite' => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', - ], - [ - 'pgsql' => 'bigpk', - ] - ], [ Schema::TYPE_INTEGER . " COMMENT 'test comment'", $this->integer()->comment('test comment'), @@ -1017,9 +991,7 @@ public function testCreateTableColumnTypes() foreach ($this->columnTypes() as $item) { list($column, $builder, $expected) = $item; if (!(strncmp($column, Schema::TYPE_PK, 2) === 0 || - strncmp($column, Schema::TYPE_UPK, 3) === 0 || strncmp($column, Schema::TYPE_BIGPK, 5) === 0 || - strncmp($column, Schema::TYPE_UBIGPK, 6) === 0 || strncmp(substr($column, -5), 'FIRST', 5) === 0 )) { $columns['col' . ++$i] = str_replace('CHECK (value', 'CHECK ([[col' . $i . ']]', $column); diff --git a/tests/framework/db/mysql/provider/ColumnTypeProvider.php b/tests/framework/db/mysql/provider/ColumnTypeProvider.php index 1263eb75..00946970 100644 --- a/tests/framework/db/mysql/provider/ColumnTypeProvider.php +++ b/tests/framework/db/mysql/provider/ColumnTypeProvider.php @@ -232,13 +232,13 @@ public function unsignedBigPrimaryKey(): array { return [ [ - Schema::TYPE_UBIGPK, + \yii\db\mysql\Schema::TYPE_UBIGPK, 'ubigpk', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ - Schema::TYPE_UBIGPK . '(1)', + \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', 'ubigpk(1)', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', @@ -267,13 +267,13 @@ public function unsignedPrimaryKey(): array { return [ [ - Schema::TYPE_UPK, + \yii\db\mysql\Schema::TYPE_UPK, 'upk', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ - Schema::TYPE_UPK . '(1)', + \yii\db\mysql\Schema::TYPE_UPK . '(1)', 'upk(1)', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', From 54d13f2e8cba81a02cc6358d7ff109f847b9198b Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 23 Aug 2024 05:48:54 -0400 Subject: [PATCH 23/31] Refactor column type mappings for auto-incremental and primary keys in MSSQL. --- src/db/mssql/QueryBuilder.php | 12 +- .../db/mssql/command/ColumnTypesTest.php | 340 ++++++++++++++++++ .../db/mssql/provider/ColumnTypeProvider.php | 7 + tests/support/TableGenerator.php | 52 +++ 4 files changed, 405 insertions(+), 6 deletions(-) create mode 100644 tests/framework/db/mssql/command/ColumnTypesTest.php create mode 100644 tests/support/TableGenerator.php diff --git a/src/db/mssql/QueryBuilder.php b/src/db/mssql/QueryBuilder.php index 03df5eed..b5f9a523 100644 --- a/src/db/mssql/QueryBuilder.php +++ b/src/db/mssql/QueryBuilder.php @@ -666,28 +666,28 @@ public function dropColumn($table, $column) private function handleIdentityColumn(string $columnType, string $type): string { - if (preg_match('/^(big)?(auto|pk)\s*\((\d+),(\d+)\)$/i', $type, $matches)) { + if (preg_match('/^(big)?(auto|pk)\s*\(([-]?\d+),(\d+)\)$/i', $type, $matches)) { $isBig = !empty($matches[1]); $isPk = strtolower($matches[2]) === 'pk'; - $start = $matches[3]; + $start = $matches[3]; // Ahora puede ser negativo $increment = ($matches[4] == '0') ? '1' : $matches[4]; $dataType = $isBig ? 'bigint' : 'int'; return "$dataType IDENTITY($start,$increment)" . ($isPk ? ' PRIMARY KEY' : ''); } - if (preg_match('/^(big)?int IDENTITY\((\d+)\)(\s+PRIMARY KEY)?$/i', $columnType, $matches)) { + if (preg_match('/^(big)?int IDENTITY\(([-]?\d+)\)(\s+PRIMARY KEY)?$/i', $columnType, $matches)) { $dataType = !empty($matches[1]) ? 'bigint' : 'int'; $isPk = !empty($matches[3]); return "$dataType IDENTITY" . ($isPk ? ' PRIMARY KEY' : ''); } - if (preg_match('/IDENTITY\((\d+),(\d+)\)/i', $columnType, $matches)) { - $start = $matches[1]; + if (preg_match('/IDENTITY\(([-]?\d+),(\d+)\)/i', $columnType, $matches)) { + $start = $matches[1]; // Ahora puede ser negativo $increment = ($matches[2] == '0') ? '1' : $matches[2]; - return preg_replace('/IDENTITY\(\d+,\d+\)/i', "IDENTITY($start,$increment)", $columnType); + return preg_replace('/IDENTITY\(([-]?\d+),\d+\)/i', "IDENTITY($start,$increment)", $columnType); } return $columnType; diff --git a/tests/framework/db/mssql/command/ColumnTypesTest.php b/tests/framework/db/mssql/command/ColumnTypesTest.php new file mode 100644 index 00000000..7e21187b --- /dev/null +++ b/tests/framework/db/mssql/command/ColumnTypesTest.php @@ -0,0 +1,340 @@ +db = MssqlConnection::getConnection(); + } + + public function testAutoincrement(): void + { + $table = 'autoincrement'; + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('auto', $autoColumn->__toString()); + $this->assertSame('int IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($table)->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('2', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $table); + } + + public function testAutoincrementWithLength(): void + { + $table = 'autoincrement'; + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 2]); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('auto(-10,2)', $autoColumn->__toString()); + $this->assertSame('int IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($table)->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('-8', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $table); + } + + public function testBigAutoincrement(): void + { + $table = 'bigautoincrement'; + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigauto', $autoColumn->__toString()); + $this->assertSame('bigint IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema('bigautoincrement')->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert( + 'bigautoincrement', + ['name'], + [ + ['test1'], + ['test2'], + ], + )->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('2', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, 'bigautoincrement'); + } + + public function testBigAutoincrementWithLength(): void + { + $table = 'bigautoincrement'; + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 2]); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigauto(-10,2)', $autoColumn->__toString()); + $this->assertSame('bigint IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($table)->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('-8', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $table); + } + + public function testBigPrimaryKey(): void + { + $table = 'bigprimarykey'; + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigpk', $primaryKeyColumn->__toString()); + $this->assertSame('bigint IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($table)->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('2', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $table); + } + + public function testBigPrimaryKeyWithLength(): void + { + $table = 'bigautoincrement'; + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 2]); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigpk(-10,2)', $primaryKeyColumn->__toString()); + $this->assertSame( + 'bigint IDENTITY(-10,2) PRIMARY KEY', + $this->db->queryBuilder->getColumnType($primaryKeyColumn), + ); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($table)->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('-8', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $table); + } + + public function testPrimaryKey(): void + { + $table = 'primarykey'; + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('pk', $primaryKeyColumn->__toString()); + $this->assertSame('int IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema('primarykey')->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('2', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, 'primarykey'); + } + + public function testPrimaryKeyWithLength(): void + { + $table = 'primarykey'; + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 2]); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('pk(-10,2)', $primaryKeyColumn->__toString()); + $this->assertSame('int IDENTITY(-10,2) PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); + + $result = $this->ensureTable($table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema('primarykey')->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('-8', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, 'primarykey'); + } + + private function ensureTable(string $tableName, array $columns): int + { + return TableGenerator::ensureTable($this->db, $tableName, $columns); + } +} diff --git a/tests/framework/db/mssql/provider/ColumnTypeProvider.php b/tests/framework/db/mssql/provider/ColumnTypeProvider.php index 302d3af9..23695515 100644 --- a/tests/framework/db/mssql/provider/ColumnTypeProvider.php +++ b/tests/framework/db/mssql/provider/ColumnTypeProvider.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\mssql\provider; use yii\db\mssql\ColumnSchemaBuilder; +use yii\db\Schema; use yiiunit\support\TestHelper; final class ColumnTypeProvider extends \yiiunit\framework\db\provider\AbstractColumnTypeProvider @@ -37,6 +38,12 @@ public static function autoIncrement(): array 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), 3 => 'int IDENTITY(2,3)', ], + 'auto(-10,1)' => [ + Schema::TYPE_AUTO . '(-10,1)', + 'auto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), + 'int IDENTITY(-10,1)', + ], ]; $types = parent::autoIncrement(); diff --git a/tests/support/TableGenerator.php b/tests/support/TableGenerator.php new file mode 100644 index 00000000..d89c5efc --- /dev/null +++ b/tests/support/TableGenerator.php @@ -0,0 +1,52 @@ +getTableSchema($tableName, true) !== null) { + self::ensureNoTable($db, $tableName); + } + + return $db->createCommand()->createTable($tableName, $columns, $options)->execute(); + } + + /** + * @psalm-param array|class-string $fixtureClass + */ + public static function ensureTableWithFixture(Connection $db, array|string $fixtureClass): void + { + $fixture = new $fixtureClass($db); + + $tableName = $fixture->getName(); + + if ($db->getTableSchema($tableName, true) !== null) { + self::ensureNoTable($db, $tableName); + } + + $db->createCommand()->createTable( + $tableName, + $fixture->getColumns(), + $fixture->getOptions(), + )->execute(); + + foreach ($fixture->getPrimaryKeys() as $primaryKey) { + $db->createCommand()->addPrimaryKey("PK_{$tableName}", $tableName, $primaryKey)->execute(); + } + + foreach ($fixture->getData() as $data) { + $db->createCommand()->insert($tableName, $data)->execute(); + } + } + + public static function ensureNoTable(Connection $db, string $tableName): void + { + $db->createCommand()->dropTable($tableName)->execute(); + } +} From 245e935d1978875c92fa0ee6cf3406b68da57608 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 23 Aug 2024 05:53:54 -0400 Subject: [PATCH 24/31] Add more tests. --- .../db/mssql/provider/ColumnTypeProvider.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/framework/db/mssql/provider/ColumnTypeProvider.php b/tests/framework/db/mssql/provider/ColumnTypeProvider.php index 23695515..7219d91e 100644 --- a/tests/framework/db/mssql/provider/ColumnTypeProvider.php +++ b/tests/framework/db/mssql/provider/ColumnTypeProvider.php @@ -81,6 +81,12 @@ public static function autoIncrementWithRaw(): array 'auto(2,3)', static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), ], + [ + 'int IDENTITY(-10,1)', + 'auto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), + 'int IDENTITY(-10,1)', + ], ]; } @@ -112,6 +118,12 @@ public static function bigAutoIncrement(): array 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), 3 => 'bigint IDENTITY(2,3)', ], + 'bigauto(-10,1)' => [ + Schema::TYPE_BIGAUTO . '(-10,1)', + 'bigauto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), + 'bigint IDENTITY(-10,1)', + ], ]; $types = parent::bigAutoIncrement(); @@ -149,6 +161,12 @@ public static function bigAutoIncrementWithRaw(): array 'bigauto(2,3)', static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), ], + [ + 'bigint IDENTITY(-10,1)', + 'bigauto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), + 'bigint IDENTITY(-10,1)', + ], ]; } @@ -180,6 +198,12 @@ public static function bigPrimaryKey(): array 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', ], + 'bigpk(-10,1)' => [ + Schema::TYPE_BIGPK . '(-10,1)', + 'bigpk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + ], ]; $types = parent::bigPrimaryKey(); @@ -217,6 +241,12 @@ public static function bigPrimaryKeyWithRaw(): array 'bigpk(2,3)', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), ], + [ + 'bigint IDENTITY(-10,1) PRIMARY KEY', + 'bigpk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + ], ]; } @@ -248,6 +278,12 @@ public static function primaryKey(): array 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), 3 => 'int IDENTITY(2,3) PRIMARY KEY', ], + 'pk(-10,1)' => [ + Schema::TYPE_PK . '(-10,1)', + 'pk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), + 'int IDENTITY(-10,1) PRIMARY KEY', + ], ]; $types = parent::primaryKey(); @@ -285,6 +321,12 @@ public static function primaryKeyWithRaw(): array 'pk(2,3)', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), ], + [ + 'int IDENTITY(-10,1) PRIMARY KEY', + 'pk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), + 'int IDENTITY(-10,1) PRIMARY KEY', + ], ]; } } From c72f314fb8db561113ead9e78fd624c3889c1312 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 23 Aug 2024 08:05:28 -0400 Subject: [PATCH 25/31] Raise code coverage `MSSQL` 100%. --- .../db/mssql/command/ColumnTypesTest.php | 226 ++++++++++++++---- 1 file changed, 177 insertions(+), 49 deletions(-) diff --git a/tests/framework/db/mssql/command/ColumnTypesTest.php b/tests/framework/db/mssql/command/ColumnTypesTest.php index 7e21187b..34630a35 100644 --- a/tests/framework/db/mssql/command/ColumnTypesTest.php +++ b/tests/framework/db/mssql/command/ColumnTypesTest.php @@ -19,6 +19,7 @@ final class ColumnTypes extends \yiiunit\TestCase { protected Connection $db; + protected string $table = 'column_types'; protected function setUp(): void { @@ -29,7 +30,6 @@ protected function setUp(): void public function testAutoincrement(): void { - $table = 'autoincrement'; $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO); $columns = [ 'id' => $autoColumn, @@ -40,12 +40,12 @@ public function testAutoincrement(): void $this->assertSame('auto', $autoColumn->__toString()); $this->assertSame('int IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema($table)->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertNull($column->isPrimaryKey); @@ -53,7 +53,7 @@ public function testAutoincrement(): void $this->assertSame('integer', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -61,12 +61,11 @@ public function testAutoincrement(): void // Ensure last insert ID $this->assertSame('2', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, $table); + TableGenerator::ensureNoTable($this->db, $this->table); } public function testAutoincrementWithLength(): void { - $table = 'autoincrement'; $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 2]); $columns = [ 'id' => $autoColumn, @@ -77,12 +76,12 @@ public function testAutoincrementWithLength(): void $this->assertSame('auto(-10,2)', $autoColumn->__toString()); $this->assertSame('int IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema($table)->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertNull($column->isPrimaryKey); @@ -90,7 +89,7 @@ public function testAutoincrementWithLength(): void $this->assertSame('integer', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -98,12 +97,47 @@ public function testAutoincrementWithLength(): void // Ensure last insert ID $this->assertSame('-8', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, $table); + TableGenerator::ensureNoTable($this->db, $this->table); + } + + public function testAutoincrementWithLengthZero(): void + { + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [0, 0]); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('auto(0,1)', $autoColumn->__toString()); + $this->assertSame('int IDENTITY(0,1)', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($this->table)->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('1', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $this->table); } public function testBigAutoincrement(): void { - $table = 'bigautoincrement'; $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO); $columns = [ 'id' => $autoColumn, @@ -114,12 +148,12 @@ public function testBigAutoincrement(): void $this->assertSame('bigauto', $autoColumn->__toString()); $this->assertSame('bigint IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema('bigautoincrement')->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertNull($column->isPrimaryKey); @@ -127,14 +161,7 @@ public function testBigAutoincrement(): void $this->assertSame('bigint', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert( - 'bigautoincrement', - ['name'], - [ - ['test1'], - ['test2'], - ], - )->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -142,12 +169,11 @@ public function testBigAutoincrement(): void // Ensure last insert ID $this->assertSame('2', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, 'bigautoincrement'); + TableGenerator::ensureNoTable($this->db, $this->table); } public function testBigAutoincrementWithLength(): void { - $table = 'bigautoincrement'; $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 2]); $columns = [ 'id' => $autoColumn, @@ -158,12 +184,12 @@ public function testBigAutoincrementWithLength(): void $this->assertSame('bigauto(-10,2)', $autoColumn->__toString()); $this->assertSame('bigint IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema($table)->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertNull($column->isPrimaryKey); @@ -171,7 +197,7 @@ public function testBigAutoincrementWithLength(): void $this->assertSame('bigint', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -179,12 +205,47 @@ public function testBigAutoincrementWithLength(): void // Ensure last insert ID $this->assertSame('-8', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, $table); + TableGenerator::ensureNoTable($this->db, $this->table); + } + + public function testBigAutoincrementWithLengthZero(): void + { + $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [0, 0]); + $columns = [ + 'id' => $autoColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigauto(0,1)', $autoColumn->__toString()); + $this->assertSame('bigint IDENTITY(0,1)', $this->db->queryBuilder->getColumnType($autoColumn)); + + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($this->table)->getColumn('id'); + + // Ensure column was created + $this->assertNull($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('1', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $this->table); } public function testBigPrimaryKey(): void { - $table = 'bigprimarykey'; $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK); $columns = [ 'id' => $primaryKeyColumn, @@ -195,12 +256,12 @@ public function testBigPrimaryKey(): void $this->assertSame('bigpk', $primaryKeyColumn->__toString()); $this->assertSame('bigint IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema($table)->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertTrue($column->isPrimaryKey); @@ -208,7 +269,7 @@ public function testBigPrimaryKey(): void $this->assertSame('bigint', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -216,12 +277,11 @@ public function testBigPrimaryKey(): void // Ensure last insert ID $this->assertSame('2', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, $table); + TableGenerator::ensureNoTable($this->db, $this->table); } public function testBigPrimaryKeyWithLength(): void { - $table = 'bigautoincrement'; $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 2]); $columns = [ 'id' => $primaryKeyColumn, @@ -235,12 +295,12 @@ public function testBigPrimaryKeyWithLength(): void $this->db->queryBuilder->getColumnType($primaryKeyColumn), ); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema($table)->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertTrue($column->isPrimaryKey); @@ -248,7 +308,7 @@ public function testBigPrimaryKeyWithLength(): void $this->assertSame('bigint', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -256,12 +316,50 @@ public function testBigPrimaryKeyWithLength(): void // Ensure last insert ID $this->assertSame('-8', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, $table); + TableGenerator::ensureNoTable($this->db, $this->table); + } + + public function testBigPrimaryKeyWithLengthZero(): void + { + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [0, 0]); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('bigpk(0,1)', $primaryKeyColumn->__toString()); + $this->assertSame( + 'bigint IDENTITY(0,1) PRIMARY KEY', + $this->db->queryBuilder->getColumnType($primaryKeyColumn), + ); + + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($this->table)->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('bigint', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('1', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $this->table); } public function testPrimaryKey(): void { - $table = 'primarykey'; $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK); $columns = [ 'id' => $primaryKeyColumn, @@ -272,12 +370,12 @@ public function testPrimaryKey(): void $this->assertSame('pk', $primaryKeyColumn->__toString()); $this->assertSame('int IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema('primarykey')->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertTrue($column->isPrimaryKey); @@ -285,7 +383,7 @@ public function testPrimaryKey(): void $this->assertSame('integer', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -293,12 +391,11 @@ public function testPrimaryKey(): void // Ensure last insert ID $this->assertSame('2', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, 'primarykey'); + TableGenerator::ensureNoTable($this->db, $this->table); } public function testPrimaryKeyWithLength(): void { - $table = 'primarykey'; $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 2]); $columns = [ 'id' => $primaryKeyColumn, @@ -309,12 +406,12 @@ public function testPrimaryKeyWithLength(): void $this->assertSame('pk(-10,2)', $primaryKeyColumn->__toString()); $this->assertSame('int IDENTITY(-10,2) PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - $result = $this->ensureTable($table, $columns); + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); // Ensure table was created $this->assertSame(0, $result); - $column = $this->db->getTableSchema('primarykey')->getColumn('id'); + $column = $this->db->getTableSchema($this->table)->getColumn('id'); // Ensure column was created $this->assertTrue($column->isPrimaryKey); @@ -322,7 +419,7 @@ public function testPrimaryKeyWithLength(): void $this->assertSame('integer', $column->type); $this->assertFalse($column->allowNull); - $result = $this->db->createCommand()->batchInsert($table, ['name'], [['test1'], ['test2']])->execute(); + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); // Ensure data was inserted $this->assertSame(2, $result); @@ -330,11 +427,42 @@ public function testPrimaryKeyWithLength(): void // Ensure last insert ID $this->assertSame('-8', $this->db->getLastInsertID()); - TableGenerator::ensureNoTable($this->db, 'primarykey'); + TableGenerator::ensureNoTable($this->db, $this->table); } - private function ensureTable(string $tableName, array $columns): int + public function testPrimaryKeyWithLengthZero(): void { - return TableGenerator::ensureTable($this->db, $tableName, $columns); + $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK, [0, 0]); + $columns = [ + 'id' => $primaryKeyColumn, + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), + ]; + + // Ensure column type + $this->assertSame('pk(0,1)', $primaryKeyColumn->__toString()); + $this->assertSame('int IDENTITY(0,1) PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); + + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($this->table)->getColumn('id'); + + // Ensure column was created + $this->assertTrue($column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame('integer', $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame('1', $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $this->table); } } From 0330ae51c4cd96bf6a89f100b283eb11bb7e341f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 23 Aug 2024 08:10:29 -0400 Subject: [PATCH 26/31] Refactor column type mappings for auto-incremental and primary keys. --- src/db/ColumnSchemaBuilder.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index 2a715a72..f7a52dbb 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -122,10 +122,8 @@ class ColumnSchemaBuilder extends BaseObject implements \Stringable Schema::TYPE_AUTO => self::CATEGORY_AUTO, Schema::TYPE_BIGAUTO => self::CATEGORY_BIGAUTO, - // integer auto-incremental primary key column types + // primary key column types Schema::TYPE_PK => self::CATEGORY_PK, - - // big integer auto-incremental primary key column types Schema::TYPE_BIGPK => self::CATEGORY_BIGPK, Schema::TYPE_CHAR => self::CATEGORY_STRING, From 66b6fb27edf2c2164987a2141711adcce49210dd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 23 Aug 2024 12:44:47 -0400 Subject: [PATCH 27/31] Better structure tests. --- .../types/AbstractExecuteColumnTypesTest.php | 55 ++ .../db/mssql/command/ColumnTypesTest.php | 468 ------------------ .../mssql/command/types/AutoIncrementTest.php | 44 ++ .../command/types/BigAutoIncrementTest.php | 44 ++ .../mssql/command/types/BigPrimaryKeyTest.php | 44 ++ .../db/mssql/command/types/PrimaryKeyTest.php | 44 ++ .../db/mssql/provider/ColumnTypeProvider.php | 332 ------------- .../provider/types/AutoIncrementProvider.php | 126 +++++ .../types/BigAutoIncrementProvider.php | 126 +++++ .../provider/types/BigPrimaryKeyProvider.php | 126 +++++ .../provider/types/PrimaryKeyProvider.php | 126 +++++ .../db/mssql/querybuilder/ColumnTypeTest.php | 121 ----- .../querybuilder/types/AutoIncrementTest.php | 49 ++ .../types/BigAutoIncrementTest.php | 49 ++ .../querybuilder/types/BigPrimaryKeyTest.php | 49 ++ .../querybuilder/types/PrimaryKeyTest.php | 49 ++ .../db/mysql/provider/ColumnTypeProvider.php | 300 ----------- .../provider/types/AutoIncrementProvider.php | 83 ++++ .../types/BigAutoIncrementProvider.php | 83 ++++ .../provider/types/BigPrimaryKeyProvider.php | 83 ++++ .../provider/types/PrimaryKeyProvider.php | 83 ++++ .../db/mysql/querybuilder/ColumnTypeTest.php | 217 -------- .../querybuilder/types/AutoIncrementTest.php | 73 +++ .../types/BigAutoIncrementTest.php | 73 +++ .../querybuilder/types/BigPrimaryKeyTest.php | 73 +++ .../querybuilder/types/PrimaryKeyTest.php | 73 +++ .../provider/AbstractColumnTypeProvider.php | 94 ---- .../types/AbstractAutoIncrementProvider.php | 31 ++ .../AbstractBigAutoIncrementProvider.php | 31 ++ .../types/AbstractBigPrimaryKeyProvider.php | 31 ++ .../types/AbstractPrimaryKeyProvider.php | 31 ++ .../{ => types}/AbstractColumnType.php | 2 +- 32 files changed, 1680 insertions(+), 1533 deletions(-) create mode 100644 tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php delete mode 100644 tests/framework/db/mssql/command/ColumnTypesTest.php create mode 100644 tests/framework/db/mssql/command/types/AutoIncrementTest.php create mode 100644 tests/framework/db/mssql/command/types/BigAutoIncrementTest.php create mode 100644 tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php create mode 100644 tests/framework/db/mssql/command/types/PrimaryKeyTest.php delete mode 100644 tests/framework/db/mssql/provider/ColumnTypeProvider.php create mode 100644 tests/framework/db/mssql/provider/types/AutoIncrementProvider.php create mode 100644 tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php create mode 100644 tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php create mode 100644 tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php delete mode 100644 tests/framework/db/mssql/querybuilder/ColumnTypeTest.php create mode 100644 tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php create mode 100644 tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php create mode 100644 tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php create mode 100644 tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php delete mode 100644 tests/framework/db/mysql/provider/ColumnTypeProvider.php create mode 100644 tests/framework/db/mysql/provider/types/AutoIncrementProvider.php create mode 100644 tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php create mode 100644 tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php create mode 100644 tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php delete mode 100644 tests/framework/db/mysql/querybuilder/ColumnTypeTest.php create mode 100644 tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php create mode 100644 tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php create mode 100644 tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php create mode 100644 tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php delete mode 100644 tests/framework/db/provider/AbstractColumnTypeProvider.php create mode 100644 tests/framework/db/provider/types/AbstractAutoIncrementProvider.php create mode 100644 tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php create mode 100644 tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php create mode 100644 tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php rename tests/framework/db/querybuilder/{ => types}/AbstractColumnType.php (96%) diff --git a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php new file mode 100644 index 00000000..0f602c7d --- /dev/null +++ b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php @@ -0,0 +1,55 @@ + $abstractColumn($this->db->schema), + 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING) + ]; + + // Ensure column schema type + $this->assertSame($expectedColumnSchemaType, $this->db->queryBuilder->getColumnType($columns['id'])); + + $result = TableGenerator::ensureTable($this->db, $this->table, $columns); + + // Ensure table was created + $this->assertSame(0, $result); + + $column = $this->db->getTableSchema($this->table)->getColumn('id'); + + // Ensure column was created + $this->assertSame($isPrimaryKey, $column->isPrimaryKey); + $this->assertTrue($column->autoIncrement); + $this->assertSame($expectedColumnType, $column->type); + $this->assertFalse($column->allowNull); + + $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); + + // Ensure data was inserted + $this->assertSame(2, $result); + + // Ensure last insert ID + $this->assertSame($expectedLastInsertID, $this->db->getLastInsertID()); + + TableGenerator::ensureNoTable($this->db, $this->table); + } +} diff --git a/tests/framework/db/mssql/command/ColumnTypesTest.php b/tests/framework/db/mssql/command/ColumnTypesTest.php deleted file mode 100644 index 34630a35..00000000 --- a/tests/framework/db/mssql/command/ColumnTypesTest.php +++ /dev/null @@ -1,468 +0,0 @@ -db = MssqlConnection::getConnection(); - } - - public function testAutoincrement(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('auto', $autoColumn->__toString()); - $this->assertSame('int IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('2', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testAutoincrementWithLength(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 2]); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('auto(-10,2)', $autoColumn->__toString()); - $this->assertSame('int IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('-8', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testAutoincrementWithLengthZero(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [0, 0]); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('auto(0,1)', $autoColumn->__toString()); - $this->assertSame('int IDENTITY(0,1)', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('1', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigAutoincrement(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigauto', $autoColumn->__toString()); - $this->assertSame('bigint IDENTITY', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('2', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigAutoincrementWithLength(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 2]); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigauto(-10,2)', $autoColumn->__toString()); - $this->assertSame('bigint IDENTITY(-10,2)', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('-8', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigAutoincrementWithLengthZero(): void - { - $autoColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [0, 0]); - $columns = [ - 'id' => $autoColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigauto(0,1)', $autoColumn->__toString()); - $this->assertSame('bigint IDENTITY(0,1)', $this->db->queryBuilder->getColumnType($autoColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertNull($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('1', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigPrimaryKey(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigpk', $primaryKeyColumn->__toString()); - $this->assertSame('bigint IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('2', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigPrimaryKeyWithLength(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 2]); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigpk(-10,2)', $primaryKeyColumn->__toString()); - $this->assertSame( - 'bigint IDENTITY(-10,2) PRIMARY KEY', - $this->db->queryBuilder->getColumnType($primaryKeyColumn), - ); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('-8', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testBigPrimaryKeyWithLengthZero(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [0, 0]); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('bigpk(0,1)', $primaryKeyColumn->__toString()); - $this->assertSame( - 'bigint IDENTITY(0,1) PRIMARY KEY', - $this->db->queryBuilder->getColumnType($primaryKeyColumn), - ); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('bigint', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('1', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testPrimaryKey(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('pk', $primaryKeyColumn->__toString()); - $this->assertSame('int IDENTITY PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('2', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testPrimaryKeyWithLength(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 2]); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('pk(-10,2)', $primaryKeyColumn->__toString()); - $this->assertSame('int IDENTITY(-10,2) PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('-8', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } - - public function testPrimaryKeyWithLengthZero(): void - { - $primaryKeyColumn = $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_PK, [0, 0]); - $columns = [ - 'id' => $primaryKeyColumn, - 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING)->notNull(), - ]; - - // Ensure column type - $this->assertSame('pk(0,1)', $primaryKeyColumn->__toString()); - $this->assertSame('int IDENTITY(0,1) PRIMARY KEY', $this->db->queryBuilder->getColumnType($primaryKeyColumn)); - - $result = TableGenerator::ensureTable($this->db, $this->table, $columns); - - // Ensure table was created - $this->assertSame(0, $result); - - $column = $this->db->getTableSchema($this->table)->getColumn('id'); - - // Ensure column was created - $this->assertTrue($column->isPrimaryKey); - $this->assertTrue($column->autoIncrement); - $this->assertSame('integer', $column->type); - $this->assertFalse($column->allowNull); - - $result = $this->db->createCommand()->batchInsert($this->table, ['name'], [['test1'], ['test2']])->execute(); - - // Ensure data was inserted - $this->assertSame(2, $result); - - // Ensure last insert ID - $this->assertSame('1', $this->db->getLastInsertID()); - - TableGenerator::ensureNoTable($this->db, $this->table); - } -} diff --git a/tests/framework/db/mssql/command/types/AutoIncrementTest.php b/tests/framework/db/mssql/command/types/AutoIncrementTest.php new file mode 100644 index 00000000..624aaa0a --- /dev/null +++ b/tests/framework/db/mssql/command/types/AutoIncrementTest.php @@ -0,0 +1,44 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php new file mode 100644 index 00000000..1e97ce61 --- /dev/null +++ b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php @@ -0,0 +1,44 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php new file mode 100644 index 00000000..488c394b --- /dev/null +++ b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php @@ -0,0 +1,44 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mssql/command/types/PrimaryKeyTest.php b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php new file mode 100644 index 00000000..b7f51d62 --- /dev/null +++ b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php @@ -0,0 +1,44 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mssql/provider/ColumnTypeProvider.php b/tests/framework/db/mssql/provider/ColumnTypeProvider.php deleted file mode 100644 index 7219d91e..00000000 --- a/tests/framework/db/mssql/provider/ColumnTypeProvider.php +++ /dev/null @@ -1,332 +0,0 @@ - [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - 3 => 'int IDENTITY', - ], - 'auto(1)' => [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 3 => 'int IDENTITY', - ], - 'auto(0,0)' => [ - 1 => 'auto(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), - 3 => 'int IDENTITY(0,1)', - ], - 'auto(1,1)' => [ - 1 => 'auto(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), - 3 => 'int IDENTITY(1,1)', - ], - 'auto(2,3)' => [ - 1 => 'auto(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), - 3 => 'int IDENTITY(2,3)', - ], - 'auto(-10,1)' => [ - Schema::TYPE_AUTO . '(-10,1)', - 'auto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), - 'int IDENTITY(-10,1)', - ], - ]; - - $types = parent::autoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public static function autoIncrementWithRaw(): array - { - return [ - [ - 'int IDENTITY', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - ], - [ - 'int IDENTITY(1)', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 'int IDENTITY', - ], - [ - 'int IDENTITY(0,0)', - 'auto(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), - 'int IDENTITY(0,1)', - ], - [ - 'int IDENTITY(1,1)', - 'auto(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), - ], - [ - 'int IDENTITY(2,3)', - 'auto(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), - ], - [ - 'int IDENTITY(-10,1)', - 'auto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), - 'int IDENTITY(-10,1)', - ], - ]; - } - - public static function bigAutoIncrement(): array - { - $expected = [ - 'bigauto' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - 3 => 'bigint IDENTITY', - ], - 'bigauto(1)' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 3 => 'bigint IDENTITY', - ], - 'bigauto(0,0)' => [ - 1 => 'bigauto(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), - 3 => 'bigint IDENTITY(0,1)', - ], - 'bigauto(1,1)' => [ - 1 => 'bigauto(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), - 3 => 'bigint IDENTITY(1,1)', - ], - 'bigauto(2,3)' => [ - 1 => 'bigauto(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), - 3 => 'bigint IDENTITY(2,3)', - ], - 'bigauto(-10,1)' => [ - Schema::TYPE_BIGAUTO . '(-10,1)', - 'bigauto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), - 'bigint IDENTITY(-10,1)', - ], - ]; - - $types = parent::bigAutoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public static function bigAutoIncrementWithRaw(): array - { - return [ - [ - 'bigint IDENTITY', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - ], - [ - 'bigint IDENTITY(1)', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 'bigint IDENTITY', - ], - [ - 'bigint IDENTITY(0,0)', - 'bigauto(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), - 'bigint IDENTITY(0,1)', - ], - [ - 'bigint IDENTITY(1,1)', - 'bigauto(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), - ], - [ - 'bigint IDENTITY(2,3)', - 'bigauto(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), - ], - [ - 'bigint IDENTITY(-10,1)', - 'bigauto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), - 'bigint IDENTITY(-10,1)', - ], - ]; - } - - public static function bigPrimaryKey(): array - { - $expected = [ - 'bigpk' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - 3 => 'bigint IDENTITY PRIMARY KEY', - ], - 'bigpk(1)' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 3 => 'bigint IDENTITY PRIMARY KEY', - ], - 'bigpk(0,0)' => [ - 1 => 'bigpk(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), - 3 => 'bigint IDENTITY(0,1) PRIMARY KEY', - ], - 'bigpk(1,1)' => [ - 1 => 'bigpk(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), - 3 => 'bigint IDENTITY(1,1) PRIMARY KEY', - ], - 'bigpk(2,3)' => [ - 1 => 'bigpk(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), - 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', - ], - 'bigpk(-10,1)' => [ - Schema::TYPE_BIGPK . '(-10,1)', - 'bigpk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), - 'bigint IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - - $types = parent::bigPrimaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public static function bigPrimaryKeyWithRaw(): array - { - return [ - [ - 'bigint IDENTITY PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - ], - [ - 'bigint IDENTITY(1) PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 'bigint IDENTITY PRIMARY KEY', - ], - [ - 'bigint IDENTITY(0,0) PRIMARY KEY', - 'bigpk(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), - 'bigint IDENTITY(0,1) PRIMARY KEY', - ], - [ - 'bigint IDENTITY(1,1) PRIMARY KEY', - 'bigpk(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), - ], - [ - 'bigint IDENTITY(2,3) PRIMARY KEY', - 'bigpk(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), - ], - [ - 'bigint IDENTITY(-10,1) PRIMARY KEY', - 'bigpk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), - 'bigint IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - } - - public static function primaryKey(): array - { - $expected = [ - 'pk' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - 3 => 'int IDENTITY PRIMARY KEY', - ], - 'pk(1)' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 3 => 'int IDENTITY PRIMARY KEY', - ], - 'pk(0,0)' => [ - 1 => 'pk(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), - 3 => 'int IDENTITY(0,1) PRIMARY KEY', - ], - 'pk(1,1)' => [ - 1 => 'pk(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), - 3 => 'int IDENTITY(1,1) PRIMARY KEY', - ], - 'pk(2,3)' => [ - 1 => 'pk(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), - 3 => 'int IDENTITY(2,3) PRIMARY KEY', - ], - 'pk(-10,1)' => [ - Schema::TYPE_PK . '(-10,1)', - 'pk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), - 'int IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - - $types = parent::primaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public static function primaryKeyWithRaw(): array - { - return [ - [ - 'int IDENTITY PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - ], - [ - 'int IDENTITY(1) PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 'int IDENTITY PRIMARY KEY', - ], - [ - 'int IDENTITY(0,0) PRIMARY KEY', - 'pk(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), - 'int IDENTITY(0,1) PRIMARY KEY', - ], - [ - 'int IDENTITY(1,1) PRIMARY KEY', - 'pk(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), - ], - [ - 'int IDENTITY(2,3) PRIMARY KEY', - 'pk(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), - ], - [ - 'int IDENTITY(-10,1) PRIMARY KEY', - 'pk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), - 'int IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - } -} diff --git a/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php new file mode 100644 index 00000000..efe62833 --- /dev/null +++ b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php @@ -0,0 +1,126 @@ + [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int IDENTITY', + ], + 'auto(1)' => [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int IDENTITY', + ], + 'auto(0,0)' => [ + 1 => 'auto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), + 3 => 'int IDENTITY(0,1)', + ], + 'auto(1,1)' => [ + 1 => 'auto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + 3 => 'int IDENTITY(1,1)', + ], + 'auto(2,3)' => [ + 1 => 'auto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + 3 => 'int IDENTITY(2,3)', + ], + 'auto(-10,1)' => [ + Schema::TYPE_AUTO . '(-10,1)', + 'auto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), + 'int IDENTITY(-10,1)', + ], + ]; + + $types = parent::autoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), + 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [0, 0]), + 'int IDENTITY(0,1)', + null, + 'integer', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 2]), + 'int IDENTITY(-10,2)', + null, + 'integer', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [2, 3]), + 'int IDENTITY(2,3)', + null, + 'integer', + '5', + ], + ]; + } + + public static function raw(): array + { + return [ + [ + 'int IDENTITY', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + ], + [ + 'int IDENTITY(1)', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 'int IDENTITY', + ], + [ + 'int IDENTITY(0,0)', + 'auto(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), + 'int IDENTITY(0,1)', + ], + [ + 'int IDENTITY(1,1)', + 'auto(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + ], + [ + 'int IDENTITY(2,3)', + 'auto(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + ], + [ + 'int IDENTITY(-10,1)', + 'auto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), + 'int IDENTITY(-10,1)', + ], + ]; + } +} diff --git a/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php new file mode 100644 index 00000000..d91bb619 --- /dev/null +++ b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php @@ -0,0 +1,126 @@ + [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint IDENTITY', + ], + 'bigauto(1)' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint IDENTITY', + ], + 'bigauto(0,0)' => [ + 1 => 'bigauto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), + 3 => 'bigint IDENTITY(0,1)', + ], + 'bigauto(1,1)' => [ + 1 => 'bigauto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + 3 => 'bigint IDENTITY(1,1)', + ], + 'bigauto(2,3)' => [ + 1 => 'bigauto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + 3 => 'bigint IDENTITY(2,3)', + ], + 'bigauto(-10,1)' => [ + Schema::TYPE_BIGAUTO . '(-10,1)', + 'bigauto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), + 'bigint IDENTITY(-10,1)', + ], + ]; + + $types = parent::bigAutoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), + 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [0, 0]), + 'bigint IDENTITY(0,1)', + null, + 'bigint', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 2]), + 'bigint IDENTITY(-10,2)', + null, + 'bigint', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [2, 3]), + 'bigint IDENTITY(2,3)', + null, + 'bigint', + '5', + ], + ]; + } + + public static function raw(): array + { + return [ + [ + 'bigint IDENTITY', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + ], + [ + 'bigint IDENTITY(1)', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 'bigint IDENTITY', + ], + [ + 'bigint IDENTITY(0,0)', + 'bigauto(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), + 'bigint IDENTITY(0,1)', + ], + [ + 'bigint IDENTITY(1,1)', + 'bigauto(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + ], + [ + 'bigint IDENTITY(2,3)', + 'bigauto(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + ], + [ + 'bigint IDENTITY(-10,1)', + 'bigauto(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), + 'bigint IDENTITY(-10,1)', + ], + ]; + } +} diff --git a/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php new file mode 100644 index 00000000..dd495bad --- /dev/null +++ b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php @@ -0,0 +1,126 @@ + [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint IDENTITY PRIMARY KEY', + ], + 'bigpk(1)' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint IDENTITY PRIMARY KEY', + ], + 'bigpk(0,0)' => [ + 1 => 'bigpk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), + 3 => 'bigint IDENTITY(0,1) PRIMARY KEY', + ], + 'bigpk(1,1)' => [ + 1 => 'bigpk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + 3 => 'bigint IDENTITY(1,1) PRIMARY KEY', + ], + 'bigpk(2,3)' => [ + 1 => 'bigpk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', + ], + 'bigpk(-10,1)' => [ + Schema::TYPE_BIGPK . '(-10,1)', + 'bigpk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + ], + ]; + + $types = parent::bigPrimaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), + 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [0, 0]), + 'bigint IDENTITY(0,1) PRIMARY KEY', + true, + 'bigint', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 2]), + 'bigint IDENTITY(-10,2) PRIMARY KEY', + true, + 'bigint', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [2, 3]), + 'bigint IDENTITY(2,3) PRIMARY KEY', + true, + 'bigint', + '5', + ], + ]; + } + + public static function raw(): array + { + return [ + [ + 'bigint IDENTITY PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + ], + [ + 'bigint IDENTITY(1) PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 'bigint IDENTITY PRIMARY KEY', + ], + [ + 'bigint IDENTITY(0,0) PRIMARY KEY', + 'bigpk(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), + 'bigint IDENTITY(0,1) PRIMARY KEY', + ], + [ + 'bigint IDENTITY(1,1) PRIMARY KEY', + 'bigpk(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + ], + [ + 'bigint IDENTITY(2,3) PRIMARY KEY', + 'bigpk(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + ], + [ + 'bigint IDENTITY(-10,1) PRIMARY KEY', + 'bigpk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + ], + ]; + } +} diff --git a/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php new file mode 100644 index 00000000..6d15e35e --- /dev/null +++ b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php @@ -0,0 +1,126 @@ + [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int IDENTITY PRIMARY KEY', + ], + 'pk(1)' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int IDENTITY PRIMARY KEY', + ], + 'pk(0,0)' => [ + 1 => 'pk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), + 3 => 'int IDENTITY(0,1) PRIMARY KEY', + ], + 'pk(1,1)' => [ + 1 => 'pk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + 3 => 'int IDENTITY(1,1) PRIMARY KEY', + ], + 'pk(2,3)' => [ + 1 => 'pk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + 3 => 'int IDENTITY(2,3) PRIMARY KEY', + ], + 'pk(-10,1)' => [ + Schema::TYPE_PK . '(-10,1)', + 'pk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), + 'int IDENTITY(-10,1) PRIMARY KEY', + ], + ]; + + $types = parent::primaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), + 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [0, 0]), + 'int IDENTITY(0,1) PRIMARY KEY', + true, + 'integer', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 2]), + 'int IDENTITY(-10,2) PRIMARY KEY', + true, + 'integer', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [2, 3]), + 'int IDENTITY(2,3) PRIMARY KEY', + true, + 'integer', + '5', + ], + ]; + } + + public static function raw(): array + { + return [ + [ + 'int IDENTITY PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + ], + [ + 'int IDENTITY(1) PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 'int IDENTITY PRIMARY KEY', + ], + [ + 'int IDENTITY(0,0) PRIMARY KEY', + 'pk(0,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), + 'int IDENTITY(0,1) PRIMARY KEY', + ], + [ + 'int IDENTITY(1,1) PRIMARY KEY', + 'pk(1,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + ], + [ + 'int IDENTITY(2,3) PRIMARY KEY', + 'pk(2,3)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + ], + [ + 'int IDENTITY(-10,1) PRIMARY KEY', + 'pk(-10,1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), + 'int IDENTITY(-10,1) PRIMARY KEY', + ], + ]; + } +} diff --git a/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php deleted file mode 100644 index 75d477a0..00000000 --- a/tests/framework/db/mssql/querybuilder/ColumnTypeTest.php +++ /dev/null @@ -1,121 +0,0 @@ -db = MssqlConnection::getConnection(); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::autoIncrement - */ - public function testAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::autoIncrementWithRaw - */ - public function testAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigAutoIncrement - */ - public function testBigAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigAutoIncrementWithRaw - */ - public function testBigAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigPrimaryKey - */ - public function testBigPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::bigPrimaryKeyWithRaw - */ - public function testBigPrimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::primaryKey - */ - public function testPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\ColumnTypeProvider::primaryKeyWithRaw - */ - public function testprimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } -} diff --git a/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php b/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php new file mode 100644 index 00000000..b7fcc063 --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php @@ -0,0 +1,49 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php b/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php new file mode 100644 index 00000000..54a1ed16 --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php @@ -0,0 +1,49 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php b/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php new file mode 100644 index 00000000..94a54dfe --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php @@ -0,0 +1,49 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php b/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php new file mode 100644 index 00000000..c44ef03f --- /dev/null +++ b/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php @@ -0,0 +1,49 @@ +db = MssqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mysql/provider/ColumnTypeProvider.php b/tests/framework/db/mysql/provider/ColumnTypeProvider.php deleted file mode 100644 index 00946970..00000000 --- a/tests/framework/db/mysql/provider/ColumnTypeProvider.php +++ /dev/null @@ -1,300 +0,0 @@ - [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - 3 => 'int(11) AUTO_INCREMENT', - ], - 'auto(1)' => [ - 1 => 'auto(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 3 => 'int(1) AUTO_INCREMENT', - ], - ]; - - $types = parent::autoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public function autoIncrementWithRaw(): array - { - return [ - [ - 'int(11) AUTO_INCREMENT', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - ], - [ - 'int(1) AUTO_INCREMENT', - 'auto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 'int(1) AUTO_INCREMENT', - ], - ]; - } - - public static function bigAutoIncrement(): array - { - $expected = [ - 'bigauto' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - 3 => 'bigint(20) AUTO_INCREMENT', - ], - 'bigauto(1)' => [ - 1 => 'bigauto(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 3 => 'bigint(1) AUTO_INCREMENT', - ], - ]; - - $types = parent::bigAutoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public function bigAutoIncrementWithRaw(): array - { - return [ - [ - 'bigint(20) AUTO_INCREMENT', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - ], - [ - 'bigint(1) AUTO_INCREMENT', - 'bigauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 'bigint(1) AUTO_INCREMENT', - ], - ]; - } - - public static function bigPrimaryKey(): array - { - $expected = [ - 'bigpk' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - 3 => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - 'bigpk(1)' => [ - 1 => 'bigpk(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 3 => 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - - $types = parent::bigPrimaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public function bigPrimaryKeyWithRaw(): array - { - return [ - [ - 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - ], - [ - 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'bigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public static function primaryKey(): array - { - $expected = [ - 'pk' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - 3 => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - 'pk(1)' => [ - 1 => 'pk(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 3 => 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - - $types = parent::primaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public function primaryKeyWithRaw(): array - { - return [ - [ - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - ], - [ - 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public function unsignedAutoIncrement(): array - { - return [ - [ - \yii\db\mysql\Schema::TYPE_UAUTO, - 'uauto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), - 'int(10) UNSIGNED AUTO_INCREMENT', - ], - [ - \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', - 'uauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), - 'int(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public function unsignedAutoIncrementWithRaw(): array - { - return [ - [ - 'int(10) UNSIGNED AUTO_INCREMENT', - 'uauto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), - ], - [ - 'int(1) UNSIGNED AUTO_INCREMENT', - 'uauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), - 'int(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public function unsignedBigAutoIncrement(): array - { - return [ - [ - \yii\db\mysql\Schema::TYPE_UBIGAUTO, - 'ubigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), - 'bigint(20) UNSIGNED AUTO_INCREMENT', - ], - [ - \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', - 'ubigauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), - 'bigint(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public function unsignedBigAutoIncrementWithRaw(): array - { - return [ - [ - 'bigint(20) UNSIGNED AUTO_INCREMENT', - 'ubigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), - ], - [ - 'bigint(1) UNSIGNED AUTO_INCREMENT', - 'ubigauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), - 'bigint(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public function unsignedBigPrimaryKey(): array - { - return [ - [ - \yii\db\mysql\Schema::TYPE_UBIGPK, - 'ubigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - [ - \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', - 'ubigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public function unsignedBigPrimaryKeyWithRaw(): array - { - return [ - [ - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'ubigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), - ], - [ - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'ubigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public function unsignedPrimaryKey(): array - { - return [ - [ - \yii\db\mysql\Schema::TYPE_UPK, - 'upk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - [ - \yii\db\mysql\Schema::TYPE_UPK . '(1)', - 'upk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public function unsignedPrimaryKeyWithRaw(): array - { - return [ - [ - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'upk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), - ], - [ - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'upk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } -} diff --git a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php new file mode 100644 index 00000000..53430eb6 --- /dev/null +++ b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php @@ -0,0 +1,83 @@ + [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int(11) AUTO_INCREMENT', + ], + 'auto(1)' => [ + 1 => 'auto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int(1) AUTO_INCREMENT', + ], + ]; + + $types = parent::autoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public function builderWithUnsigned(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UAUTO, + 'uauto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), + 'int(10) UNSIGNED AUTO_INCREMENT', + ], + [ + \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', + 'uauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + + public function raw(): array + { + return [ + [ + 'int(11) AUTO_INCREMENT', + 'auto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + ], + [ + 'int(1) AUTO_INCREMENT', + 'auto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 'int(1) AUTO_INCREMENT', + ], + ]; + } + + public function rawWithUnsigned(): array + { + return [ + [ + 'int(10) UNSIGNED AUTO_INCREMENT', + 'uauto', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), + ], + [ + 'int(1) UNSIGNED AUTO_INCREMENT', + 'uauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } +} diff --git a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php new file mode 100644 index 00000000..e16c3c59 --- /dev/null +++ b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php @@ -0,0 +1,83 @@ + [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint(20) AUTO_INCREMENT', + ], + 'bigauto(1)' => [ + 1 => 'bigauto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint(1) AUTO_INCREMENT', + ], + ]; + + $types = parent::bigAutoIncrement(); + + return TestHelper::addExpected($expected, $types); + } + + public function builderWithUnsigned(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UBIGAUTO, + 'ubigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), + 'bigint(20) UNSIGNED AUTO_INCREMENT', + ], + [ + \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', + 'ubigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } + + public function raw(): array + { + return [ + [ + 'bigint(20) AUTO_INCREMENT', + 'bigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + ], + [ + 'bigint(1) AUTO_INCREMENT', + 'bigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 'bigint(1) AUTO_INCREMENT', + ], + ]; + } + + public function rawWithUnsigned(): array + { + return [ + [ + 'bigint(20) UNSIGNED AUTO_INCREMENT', + 'ubigauto', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), + ], + [ + 'bigint(1) UNSIGNED AUTO_INCREMENT', + 'ubigauto(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + ], + ]; + } +} diff --git a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php new file mode 100644 index 00000000..0f19159c --- /dev/null +++ b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php @@ -0,0 +1,83 @@ + [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + 'bigpk(1)' => [ + 1 => 'bigpk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + + $types = parent::bigPrimaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public function builderWithUnsigned(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UBIGPK, + 'ubigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + [ + \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', + 'ubigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function raw(): array + { + return [ + [ + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + ], + [ + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function rawWithUnsigned(): array + { + return [ + [ + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'ubigpk', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), + ], + [ + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'ubigpk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } +} diff --git a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php new file mode 100644 index 00000000..693b282a --- /dev/null +++ b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php @@ -0,0 +1,83 @@ + [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + 'pk(1)' => [ + 1 => 'pk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + + $types = parent::primaryKey(); + + return TestHelper::addExpected($expected, $types); + } + + public function builderWithUnsigned(): array + { + return [ + [ + \yii\db\mysql\Schema::TYPE_UPK, + 'upk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + [ + \yii\db\mysql\Schema::TYPE_UPK . '(1)', + 'upk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function raw(): array + { + return [ + [ + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'pk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + ], + [ + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'pk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } + + public function rawWithUnsigned(): array + { + return [ + [ + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'upk', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), + ], + [ + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'upk(1)', + static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + ], + ]; + } +} diff --git a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php b/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php deleted file mode 100644 index 4e205f08..00000000 --- a/tests/framework/db/mysql/querybuilder/ColumnTypeTest.php +++ /dev/null @@ -1,217 +0,0 @@ -db = MysqlConnection::getConnection(); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::autoIncrement - */ - public function testAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::autoIncrementWithRaw - */ - public function testAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigAutoIncrement - */ - public function testBigAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigAutoIncrementWithRaw - */ - public function testBigAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigPrimaryKey - */ - public function testBigPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::bigPrimaryKeyWithRaw - */ - public function testBigPrimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::primaryKey - */ - public function testPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::primaryKeyWithRaw - */ - public function testprimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedAutoIncrement - */ - public function testUnsignedAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedAutoIncrementWithRaw - */ - public function testUnsignedAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigAutoIncrement - */ - public function testUnsignedBigAutoIncrement( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigAutoIncrementWithRaw - */ - public function testUnsignedBigAutoIncrementWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigPrimaryKey - */ - public function testUnsignedBigPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedBigPrimaryKeyWithRaw - */ - public function testUnsignedBigPrimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedPrimaryKey - */ - public function testUnsignedPrimaryKey( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\ColumnTypeProvider::unsignedPrimaryKeyWithRaw - */ - public function testUnsignedPrimaryKeyWithRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } -} diff --git a/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php b/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php new file mode 100644 index 00000000..92a6964c --- /dev/null +++ b/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php @@ -0,0 +1,73 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::builderWithUnsigned + */ + public function testBuilderWithUnsigned( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::rawWithUnsigned + */ + public function testRawWithUnsigned( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php b/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php new file mode 100644 index 00000000..6627f28a --- /dev/null +++ b/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php @@ -0,0 +1,73 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::builderWithUnsigned + */ + public function testBuilderWithUnsigned( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::rawWithUnsigned + */ + public function testRawWithUnsigned( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php b/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php new file mode 100644 index 00000000..37524e0c --- /dev/null +++ b/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php @@ -0,0 +1,73 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::builderWithUnsigned + */ + public function testBuilderWithUnsigned( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::rawWithUnsigned + */ + public function testRawWithUnsigned( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php b/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php new file mode 100644 index 00000000..dd1bc3ec --- /dev/null +++ b/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php @@ -0,0 +1,73 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::builder + */ + public function testBuilder( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::builderWithUnsigned + */ + public function testBuilderWithUnsigned( + string $column, + string $expectedColumn, + Closure $builder, + string $expectedBuilder, + ): void { + $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::raw + */ + public function testRaw( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::rawWithUnsigned + */ + public function testRawWithUnsigned( + string $columnRaw, + string $column, + Closure $builder, + string $expectColumn = '', + ): void { + $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); + } +} diff --git a/tests/framework/db/provider/AbstractColumnTypeProvider.php b/tests/framework/db/provider/AbstractColumnTypeProvider.php deleted file mode 100644 index e24245a2..00000000 --- a/tests/framework/db/provider/AbstractColumnTypeProvider.php +++ /dev/null @@ -1,94 +0,0 @@ - [ - Schema::TYPE_AUTO, - ], - 'auto(1)' => [ - Schema::TYPE_AUTO . '(1)', - ], - 'auto(0,0)' => [ - Schema::TYPE_AUTO . '(0,0)', - ], - 'auto(1,1)' => [ - Schema::TYPE_AUTO . '(1,1)', - ], - 'auto(2,3)' => [ - Schema::TYPE_AUTO . '(2,3)', - ], - ]; - } - - public static function bigAutoIncrement(): array - { - return [ - 'bigauto' => [ - Schema::TYPE_BIGAUTO, - ], - 'bigauto(1)' => [ - Schema::TYPE_BIGAUTO . '(1)', - ], - 'bigauto(0,0)' => [ - Schema::TYPE_BIGAUTO . '(0,0)', - ], - 'bigauto(1,1)' => [ - Schema::TYPE_BIGAUTO . '(1,1)', - ], - 'bigauto(2,3)' => [ - Schema::TYPE_BIGAUTO . '(2,3)', - ], - ]; - } - - public static function bigPrimaryKey(): array - { - return [ - 'bigpk' => [ - Schema::TYPE_BIGPK, - ], - 'bigpk(1)' => [ - Schema::TYPE_BIGPK . '(1)', - ], - 'bigpk(0,0)' => [ - Schema::TYPE_BIGPK . '(0,0)', - ], - 'bigpk(1,1)' => [ - Schema::TYPE_BIGPK . '(1,1)', - ], - 'bigpk(2,3)' => [ - Schema::TYPE_BIGPK . '(2,3)', - ], - ]; - } - - public static function primaryKey(): array - { - return [ - 'pk' => [ - Schema::TYPE_PK, - ], - 'pk(1)' => [ - Schema::TYPE_PK . '(1)', - ], - 'pk(0,0)' => [ - Schema::TYPE_PK . '(0,0)', - ], - 'pk(1,1)' => [ - Schema::TYPE_PK . '(1,1)', - ], - 'pk(2,3)' => [ - Schema::TYPE_PK . '(2,3)', - ], - ]; - } -} diff --git a/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php b/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php new file mode 100644 index 00000000..829c45d7 --- /dev/null +++ b/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php @@ -0,0 +1,31 @@ + [ + Schema::TYPE_AUTO, + ], + 'auto(1)' => [ + Schema::TYPE_AUTO . '(1)', + ], + 'auto(0,0)' => [ + Schema::TYPE_AUTO . '(0,0)', + ], + 'auto(1,1)' => [ + Schema::TYPE_AUTO . '(1,1)', + ], + 'auto(2,3)' => [ + Schema::TYPE_AUTO . '(2,3)', + ], + ]; + } +} diff --git a/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php b/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php new file mode 100644 index 00000000..62708772 --- /dev/null +++ b/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php @@ -0,0 +1,31 @@ + [ + Schema::TYPE_BIGAUTO, + ], + 'bigauto(1)' => [ + Schema::TYPE_BIGAUTO . '(1)', + ], + 'bigauto(0,0)' => [ + Schema::TYPE_BIGAUTO . '(0,0)', + ], + 'bigauto(1,1)' => [ + Schema::TYPE_BIGAUTO . '(1,1)', + ], + 'bigauto(2,3)' => [ + Schema::TYPE_BIGAUTO . '(2,3)', + ], + ]; + } +} diff --git a/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php b/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php new file mode 100644 index 00000000..9a108732 --- /dev/null +++ b/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php @@ -0,0 +1,31 @@ + [ + Schema::TYPE_BIGPK, + ], + 'bigpk(1)' => [ + Schema::TYPE_BIGPK . '(1)', + ], + 'bigpk(0,0)' => [ + Schema::TYPE_BIGPK . '(0,0)', + ], + 'bigpk(1,1)' => [ + Schema::TYPE_BIGPK . '(1,1)', + ], + 'bigpk(2,3)' => [ + Schema::TYPE_BIGPK . '(2,3)', + ], + ]; + } +} diff --git a/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php b/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php new file mode 100644 index 00000000..783c3aa7 --- /dev/null +++ b/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php @@ -0,0 +1,31 @@ + [ + Schema::TYPE_PK, + ], + 'pk(1)' => [ + Schema::TYPE_PK . '(1)', + ], + 'pk(0,0)' => [ + Schema::TYPE_PK . '(0,0)', + ], + 'pk(1,1)' => [ + Schema::TYPE_PK . '(1,1)', + ], + 'pk(2,3)' => [ + Schema::TYPE_PK . '(2,3)', + ], + ]; + } +} diff --git a/tests/framework/db/querybuilder/AbstractColumnType.php b/tests/framework/db/querybuilder/types/AbstractColumnType.php similarity index 96% rename from tests/framework/db/querybuilder/AbstractColumnType.php rename to tests/framework/db/querybuilder/types/AbstractColumnType.php index 37b4ec27..9eba2753 100644 --- a/tests/framework/db/querybuilder/AbstractColumnType.php +++ b/tests/framework/db/querybuilder/types/AbstractColumnType.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace yiiunit\framework\db\querybuilder; +namespace yiiunit\framework\db\querybuilder\types; use Closure; use yii\db\Connection; From 78926481d0ef8abdbb2ac3f7786996594055fc63 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sat, 24 Aug 2024 16:04:49 -0400 Subject: [PATCH 28/31] Refactor column type mappings for auto-incremental and primary keys for `MySQL`. --- src/db/ColumnSchemaBuilder.php | 10 +++ src/db/Command.php | 36 ++++---- src/db/QueryBuilder.php | 31 ++++--- src/db/mysql/QueryBuilder.php | 89 ++++++++++++++++++- .../types/AbstractExecuteColumnTypesTest.php | 10 ++- .../mysql/command/types/AutoIncrementTest.php | 63 +++++++++++++ .../command/types/BigAutoIncrementTest.php | 63 +++++++++++++ .../mysql/command/types/BigPrimaryKeyTest.php | 63 +++++++++++++ .../db/mysql/command/types/PrimaryKeyTest.php | 63 +++++++++++++ .../provider/types/AutoIncrementProvider.php | 57 ++++++++++++ .../types/BigAutoIncrementProvider.php | 58 ++++++++++++ .../provider/types/BigPrimaryKeyProvider.php | 55 ++++++++++++ .../provider/types/PrimaryKeyProvider.php | 55 ++++++++++++ 13 files changed, 622 insertions(+), 31 deletions(-) create mode 100644 tests/framework/db/mysql/command/types/AutoIncrementTest.php create mode 100644 tests/framework/db/mysql/command/types/BigAutoIncrementTest.php create mode 100644 tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php create mode 100644 tests/framework/db/mysql/command/types/PrimaryKeyTest.php diff --git a/src/db/ColumnSchemaBuilder.php b/src/db/ColumnSchemaBuilder.php index f7a52dbb..bda85a9d 100644 --- a/src/db/ColumnSchemaBuilder.php +++ b/src/db/ColumnSchemaBuilder.php @@ -264,6 +264,16 @@ public function getCategoryMap(): array return static::$typeCategoryMap; } + /** + * Returns the column type. + * + * @return string|null the column type. + */ + public function getType(): string|null + { + return $this->type; + } + /** * Adds a `NOT NULL` constraint to the column. * diff --git a/src/db/Command.php b/src/db/Command.php index 5cb61a40..c93a2332 100644 --- a/src/db/Command.php +++ b/src/db/Command.php @@ -651,33 +651,37 @@ public function delete($table, $condition = '', $params = []) /** * Creates a SQL command for creating a new DB table. * - * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), - * where name stands for a column name which will be properly quoted by the method, and definition - * stands for the column type which must contain an abstract DB type. + * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name + * stands for a column name which will be properly quoted by the method, and definition stands for the column type + * which must contain an abstract DB type. * - * The method [[QueryBuilder::getColumnType()]] will be called - * to convert the abstract column types to physical ones. For example, `string` will be converted - * as `varchar(255)`, and `string not null` becomes `varchar(255) not null`. + * The method [[QueryBuilder::getColumnType()]] will be called to convert the abstract column types to physical + * ones. For example, `string` will be converted as `varchar(255)`, and `string not null` becomes + * `varchar(255) not null`. * - * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly - * inserted into the generated SQL. + * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly inserted + * into the generated SQL. * * Example usage: * ```php - * Yii::$app->db->createCommand()->createTable('post', [ - * 'id' => 'pk', - * 'title' => 'string', - * 'text' => 'text', - * 'column_name double precision null default null', - * ]); + * Yii::$app->db->createCommand()->createTable( + * 'post', + * [ + * 'id' => 'pk', + * 'title' => 'string', + * 'text' => 'text', + * 'column_name double precision null default null', + * ], + * ); * ``` * * @param string $table the name of the table to be created. The name will be properly quoted by the method. * @param array $columns the columns (name => definition) in the new table. * @param string|null $options additional SQL fragment that will be appended to the generated SQL. - * @return $this the command object itself + * + * @return static the command object itself. */ - public function createTable($table, $columns, $options = null) + public function createTable(string $table, array $columns, string|null $options = null): static { $sql = $this->db->getQueryBuilder()->createTable($table, $columns, $options); diff --git a/src/db/QueryBuilder.php b/src/db/QueryBuilder.php index b45a42f1..aac56914 100644 --- a/src/db/QueryBuilder.php +++ b/src/db/QueryBuilder.php @@ -783,33 +783,39 @@ public function delete($table, $condition, &$params) /** * Builds a SQL statement for creating a new DB table. * - * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), - * where name stands for a column name which will be properly quoted by the method, and definition - * stands for the column type which must contain an abstract DB type. + * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name + * stands for a column name which will be properly quoted by the method, and definition stands for the column type + * which must contain an abstract DB type. + * * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. * - * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly - * inserted into the generated SQL. + * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly inserted + * into the generated SQL. * * For example, * * ```php - * $sql = $queryBuilder->createTable('user', [ - * 'id' => 'pk', - * 'name' => 'string', - * 'age' => 'integer', - * 'column_name double precision null default null', # definition only example - * ]); + * $sql = $queryBuilder->createTable( + * 'user', + * [ + * 'id' => 'pk', + * 'name' => 'string', + * 'age' => 'integer', + * 'column_name double precision null default null', # definition only example + * ], + * ); * ``` * * @param string $table the name of the table to be created. The name will be properly quoted by the method. * @param array $columns the columns (name => definition) in the new table. * @param string|null $options additional SQL fragment that will be appended to the generated SQL. + * * @return string the SQL statement for creating a new DB table. */ - public function createTable($table, $columns, $options = null) + public function createTable(string $table, array $columns, string|null $options = null): string { $cols = []; + foreach ($columns as $name => $type) { if (is_string($name)) { $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type); @@ -817,6 +823,7 @@ public function createTable($table, $columns, $options = null) $cols[] = "\t" . $type; } } + $sql = 'CREATE TABLE ' . $this->db->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; return $options === null ? $sql : $sql . ' ' . $options; diff --git a/src/db/mysql/QueryBuilder.php b/src/db/mysql/QueryBuilder.php index dd8bdaaf..48a312aa 100644 --- a/src/db/mysql/QueryBuilder.php +++ b/src/db/mysql/QueryBuilder.php @@ -55,7 +55,6 @@ class QueryBuilder extends \yii\db\QueryBuilder Schema::TYPE_JSON => 'json' ]; - /** * {@inheritdoc} */ @@ -76,6 +75,94 @@ protected function defaultExpressionBuilders() ]); } + /** + * Builds a SQL statement for creating a new DB table. + * + * The columns in the new table should be specified as name-definition pairs (e.g. 'name' => 'string'), where name + * stands for a column name which will be properly quoted by the method, and definition stands for the column type + * which must contain an abstract DB type. + * + * The [[getColumnType()]] method will be invoked to convert any abstract type into a physical one. + * + * If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly inserted + * into the generated SQL. + * + * For example, + * + * ```php + * $sql = $queryBuilder->createTable( + * 'user', + * [ + * 'id' => 'pk', + * 'name' => 'string', + * 'age' => 'integer', + * 'column_name double precision null default null', # definition only example + * ], + * ); + * ``` + * + * Note: When column definition is specified as auto-incremental, the column will be set as key automatically. + * + * @param string $table the name of the table to be created. The name will be properly quoted by the method. + * @param array $columns the columns (name => definition) in the new table. + * @param string|null $options additional SQL fragment that will be appended to the generated SQL. + * + * @return string the SQL statement for creating a new DB table. + */ + public function createTable(string $table, array $columns, string|null $options = null): string + { + $autoIncrementColumn = null; + $hasPrimaryKey = false; + + $cols = []; + + foreach ($columns as $name => $type) { + if (is_string($name)) { + if ($type instanceof ColumnSchemaBuilder) { + if ( + in_array( + $type->getType(), + [Schema::TYPE_AUTO, Schema::TYPE_UAUTO, Schema::TYPE_BIGAUTO, Schema::TYPE_UBIGAUTO], + true + ) + ) { + $autoIncrementColumn = $name; + } + + if ( + in_array( + $type->getType(), + [Schema::TYPE_PK, Schema::TYPE_UPK, Schema::TYPE_BIGPK, Schema::TYPE_UBIGPK], + true + ) + ) { + $hasPrimaryKey = true; + } + } else { + if (stripos($type, 'AUTO_INCREMENT') !== false) { + $autoIncrementColumn = $name; + } + + if (stripos($type, 'PRIMARY KEY') !== false) { + $hasPrimaryKey = true; + } + } + + $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type); + } else { + $cols[] = "\t" . $type; + } + } + + if ($autoIncrementColumn !== null && $hasPrimaryKey === false) { + $cols[] = 'PRIMARY KEY (' . $this->db->quoteColumnName($autoIncrementColumn) . ')'; + } + + $sql = 'CREATE TABLE ' . $this->db->quoteTableName($table) . " (\n" . implode(",\n", $cols) . "\n)"; + + return $options === null ? $sql : $sql . ' ' . $options; + } + /** * Builds a SQL statement for renaming a column. * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. diff --git a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php index 0f602c7d..295419be 100644 --- a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php +++ b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php @@ -47,8 +47,14 @@ public function executeColumnTypes( // Ensure data was inserted $this->assertSame(2, $result); - // Ensure last insert ID - $this->assertSame($expectedLastInsertID, $this->db->getLastInsertID()); + // Ensure last insert ID. + // MySQL not return last insert ID for batch insert. + $lastInsertID = match ($this->db->getDriverName()) { + 'mysql' => $this->db->createCommand("SELECT MAX(id) FROM {$this->table}")->queryScalar(), + default => $this->db->getLastInsertID(), + }; + + $this->assertSame($expectedLastInsertID, $lastInsertID); TableGenerator::ensureNoTable($this->db, $this->table); } diff --git a/tests/framework/db/mysql/command/types/AutoIncrementTest.php b/tests/framework/db/mysql/command/types/AutoIncrementTest.php new file mode 100644 index 00000000..28384d3d --- /dev/null +++ b/tests/framework/db/mysql/command/types/AutoIncrementTest.php @@ -0,0 +1,63 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::schemaWithUnsigned + */ + public function testExecuteWithUsigned( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php new file mode 100644 index 00000000..75571fc5 --- /dev/null +++ b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php @@ -0,0 +1,63 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::schemaWithUnsigned + */ + public function testExecuteWithUnsigned( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php new file mode 100644 index 00000000..7d17f356 --- /dev/null +++ b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php @@ -0,0 +1,63 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::schemaWithUnsigned + */ + public function testExecuteWithUnsigned( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mysql/command/types/PrimaryKeyTest.php b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php new file mode 100644 index 00000000..b12b458d --- /dev/null +++ b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php @@ -0,0 +1,63 @@ +db = MysqlConnection::getConnection(); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::schema + */ + public function testExecute( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } + + /** + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::schemaWithUnsigned + */ + public function testExecuteWithUnsigned( + Closure $abstractColumn, + string $expectedColumnSchemaType, + bool|null $isPrimaryKey, + string $expectedColumnType, + int|string $expectedLastInsertID, + ): void { + parent::executeColumnTypes( + $abstractColumn, + $expectedColumnSchemaType, + $isPrimaryKey, + $expectedColumnType, + $expectedLastInsertID, + ); + } +} diff --git a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php index 53430eb6..e55cd56f 100644 --- a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\mysql\provider\types; use yii\db\mysql\ColumnSchemaBuilder; +use yii\db\Schema; use yiiunit\support\TestHelper; final class AutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractAutoIncrementProvider @@ -47,6 +48,62 @@ public function builderWithUnsigned(): array ]; } + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), + 'int(11) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, 1), + 'int(1) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + ]; + } + + public static function schemaWithUnsigned(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO)->unsigned(), + 'int(10) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder(Schema::TYPE_AUTO, 1) + ->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO), + 'int(10) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO, 1), + 'int(1) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + ]; + } + public function raw(): array { return [ diff --git a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php index e16c3c59..d56f0814 100644 --- a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\mysql\provider\types; use yii\db\mysql\ColumnSchemaBuilder; +use yii\db\Schema; use yiiunit\support\TestHelper; final class BigAutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractBigAutoIncrementProvider @@ -47,6 +48,63 @@ public function builderWithUnsigned(): array ]; } + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), + 'bigint(20) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, 1), + 'bigint(1) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + ]; + } + + public static function schemaWithUnsigned(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO)->unsigned(), + 'bigint(20) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, 1) + ->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO), + 'bigint(20) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO, 1), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + ]; + } + public function raw(): array { return [ diff --git a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php index 0f19159c..9bc269ba 100644 --- a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\mysql\provider\types; use yii\db\mysql\ColumnSchemaBuilder; +use yii\db\Schema; use yiiunit\support\TestHelper; final class BigPrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractBigPrimaryKeyProvider @@ -47,6 +48,60 @@ public function builderWithUnsigned(): array ]; } + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, 1), + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + ]; + } + + public static function schemaWithUnsigned(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK)->unsigned(), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, 1)->unsigned(), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK, 1), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + ]; + } + public function raw(): array { return [ diff --git a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php index 693b282a..a2492646 100644 --- a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php @@ -5,6 +5,7 @@ namespace yiiunit\framework\db\mysql\provider\types; use yii\db\mysql\ColumnSchemaBuilder; +use yii\db\Schema; use yiiunit\support\TestHelper; final class PrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractPrimaryKeyProvider @@ -47,6 +48,60 @@ public function builderWithUnsigned(): array ]; } + public static function schema(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, 1), + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + ]; + } + + public static function schemaWithUnsigned(): array + { + return [ + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK)->unsigned(), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, 1)->unsigned(), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK, 1), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + ]; + } + public function raw(): array { return [ From 6eaf0959c8145c134776672787ee1b6149b02872 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 25 Aug 2024 05:50:21 -0400 Subject: [PATCH 29/31] Refactor column type mappings for auto-incremental and primary keys in `MSSQL`, `MYSQL`. --- .../types/AbstractExecuteColumnTypesTest.php | 8 ++- .../mssql/command/types/AutoIncrementTest.php | 2 +- .../command/types/BigAutoIncrementTest.php | 2 +- .../mssql/command/types/BigPrimaryKeyTest.php | 2 +- .../db/mssql/command/types/PrimaryKeyTest.php | 2 +- .../provider/types/AutoIncrementProvider.php | 60 ++++++++++++++++ .../types/BigAutoIncrementProvider.php | 59 +++++++++++++++ .../provider/types/BigPrimaryKeyProvider.php | 59 +++++++++++++++ .../provider/types/PrimaryKeyProvider.php | 59 +++++++++++++++ .../mysql/command/types/AutoIncrementTest.php | 21 +----- .../command/types/BigAutoIncrementTest.php | 21 +----- .../mysql/command/types/BigPrimaryKeyTest.php | 21 +----- .../db/mysql/command/types/PrimaryKeyTest.php | 21 +----- .../provider/types/AutoIncrementProvider.php | 71 +++++++++++++++++-- .../types/BigAutoIncrementProvider.php | 71 +++++++++++++++++-- .../provider/types/BigPrimaryKeyProvider.php | 68 ++++++++++++++++-- .../provider/types/PrimaryKeyProvider.php | 68 ++++++++++++++++-- 17 files changed, 505 insertions(+), 110 deletions(-) diff --git a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php index 295419be..d042b529 100644 --- a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php +++ b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php @@ -15,14 +15,18 @@ abstract class AbstractExecuteColumnTypes extends \yiiunit\TestCase protected string $table = 'column_types'; public function executeColumnTypes( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, int|string $expectedLastInsertID, ): void { + if (is_callable($abstractColumn)) { + $abstractColumn = $abstractColumn($this->db->schema); + } + $columns = [ - 'id' => $abstractColumn($this->db->schema), + 'id' => $abstractColumn, 'name' => $this->db->schema->createColumnSchemaBuilder(Schema::TYPE_STRING) ]; diff --git a/tests/framework/db/mssql/command/types/AutoIncrementTest.php b/tests/framework/db/mssql/command/types/AutoIncrementTest.php index 624aaa0a..30f87aeb 100644 --- a/tests/framework/db/mssql/command/types/AutoIncrementTest.php +++ b/tests/framework/db/mssql/command/types/AutoIncrementTest.php @@ -27,7 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::schema */ public function testExecute( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php index 1e97ce61..9ba86350 100644 --- a/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php @@ -27,7 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::schema */ public function testExecute( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php index 488c394b..fafc0b96 100644 --- a/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php @@ -27,7 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::schema */ public function testExecute( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mssql/command/types/PrimaryKeyTest.php b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php index b7f51d62..4c11a356 100644 --- a/tests/framework/db/mssql/command/types/PrimaryKeyTest.php +++ b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php @@ -27,7 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::schema */ public function testExecute( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php index efe62833..b347d6c3 100644 --- a/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php +++ b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php @@ -54,6 +54,7 @@ public static function builder(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), 'int IDENTITY', @@ -82,6 +83,65 @@ public static function schema(): array 'integer', '5', ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), + 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(0, 0), + 'int IDENTITY(0,1)', + null, + 'integer', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(-10, 2), + 'int IDENTITY(-10,2)', + null, + 'integer', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(2, 3), + 'int IDENTITY(2,3)', + null, + 'integer', + '5', + ], + // raw sql + [ + 'int IDENTITY', + 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + 'int IDENTITY(0,0)', + 'int IDENTITY(0,1)', + null, + 'integer', + '1', + ], + [ + 'int IDENTITY(-10,2)', + 'int IDENTITY(-10,2)', + null, + 'integer', + '-8', + ], + [ + 'int IDENTITY(2,3)', + 'int IDENTITY(2,3)', + null, + 'integer', + '5', + ], + ]; } diff --git a/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php index d91bb619..8e5122b5 100644 --- a/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php +++ b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php @@ -54,6 +54,7 @@ public static function builder(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), 'bigint IDENTITY', @@ -82,6 +83,64 @@ public static function schema(): array 'bigint', '5', ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), + 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(0, 0), + 'bigint IDENTITY(0,1)', + null, + 'bigint', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(-10, 2), + 'bigint IDENTITY(-10,2)', + null, + 'bigint', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(2, 3), + 'bigint IDENTITY(2,3)', + null, + 'bigint', + '5', + ], + // raw sql + [ + 'bigint IDENTITY', + 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + 'bigint IDENTITY(0,0)', + 'bigint IDENTITY(0,1)', + null, + 'bigint', + '1', + ], + [ + 'bigint IDENTITY(-10,2)', + 'bigint IDENTITY(-10,2)', + null, + 'bigint', + '-8', + ], + [ + 'bigint IDENTITY(2,3)', + 'bigint IDENTITY(2,3)', + null, + 'bigint', + '5', + ], ]; } diff --git a/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php index dd495bad..a52e879d 100644 --- a/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php +++ b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php @@ -54,6 +54,7 @@ public static function builder(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), 'bigint IDENTITY PRIMARY KEY', @@ -82,6 +83,64 @@ public static function schema(): array 'bigint', '5', ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), + 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(0, 0), + 'bigint IDENTITY(0,1) PRIMARY KEY', + true, + 'bigint', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(-10, 2), + 'bigint IDENTITY(-10,2) PRIMARY KEY', + true, + 'bigint', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(2, 3), + 'bigint IDENTITY(2,3) PRIMARY KEY', + true, + 'bigint', + '5', + ], + // raw sql + [ + 'bigint IDENTITY PRIMARY KEY', + 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + 'bigint IDENTITY(0,0) PRIMARY KEY', + 'bigint IDENTITY(0,1) PRIMARY KEY', + true, + 'bigint', + '1', + ], + [ + 'bigint IDENTITY(-10,2) PRIMARY KEY', + 'bigint IDENTITY(-10,2) PRIMARY KEY', + true, + 'bigint', + '-8', + ], + [ + 'bigint IDENTITY(2,3) PRIMARY KEY', + 'bigint IDENTITY(2,3) PRIMARY KEY', + true, + 'bigint', + '5', + ], ]; } diff --git a/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php index 6d15e35e..fbe10ec7 100644 --- a/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php +++ b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php @@ -54,6 +54,7 @@ public static function builder(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), 'int IDENTITY PRIMARY KEY', @@ -82,6 +83,64 @@ public static function schema(): array 'integer', '5', ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), + 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(0, 0), + 'int IDENTITY(0,1) PRIMARY KEY', + true, + 'integer', + '1', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(-10, 2), + 'int IDENTITY(-10,2) PRIMARY KEY', + true, + 'integer', + '-8', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(2, 3), + 'int IDENTITY(2,3) PRIMARY KEY', + true, + 'integer', + '5', + ], + // raw sql + [ + 'int IDENTITY PRIMARY KEY', + 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + 'int IDENTITY(0,0) PRIMARY KEY', + 'int IDENTITY(0,1) PRIMARY KEY', + true, + 'integer', + '1', + ], + [ + 'int IDENTITY(-10,2) PRIMARY KEY', + 'int IDENTITY(-10,2) PRIMARY KEY', + true, + 'integer', + '-8', + ], + [ + 'int IDENTITY(2,3) PRIMARY KEY', + 'int IDENTITY(2,3) PRIMARY KEY', + true, + 'integer', + '5', + ], ]; } diff --git a/tests/framework/db/mysql/command/types/AutoIncrementTest.php b/tests/framework/db/mysql/command/types/AutoIncrementTest.php index 28384d3d..a7eaff23 100644 --- a/tests/framework/db/mysql/command/types/AutoIncrementTest.php +++ b/tests/framework/db/mysql/command/types/AutoIncrementTest.php @@ -27,26 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::schema */ public function testExecute( - Closure $abstractColumn, - string $expectedColumnSchemaType, - bool|null $isPrimaryKey, - string $expectedColumnType, - int|string $expectedLastInsertID, - ): void { - parent::executeColumnTypes( - $abstractColumn, - $expectedColumnSchemaType, - $isPrimaryKey, - $expectedColumnType, - $expectedLastInsertID, - ); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::schemaWithUnsigned - */ - public function testExecuteWithUsigned( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php index 75571fc5..4bf122da 100644 --- a/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php @@ -27,26 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::schema */ public function testExecute( - Closure $abstractColumn, - string $expectedColumnSchemaType, - bool|null $isPrimaryKey, - string $expectedColumnType, - int|string $expectedLastInsertID, - ): void { - parent::executeColumnTypes( - $abstractColumn, - $expectedColumnSchemaType, - $isPrimaryKey, - $expectedColumnType, - $expectedLastInsertID, - ); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::schemaWithUnsigned - */ - public function testExecuteWithUnsigned( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php index 7d17f356..7e2b62f7 100644 --- a/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php @@ -27,26 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::schema */ public function testExecute( - Closure $abstractColumn, - string $expectedColumnSchemaType, - bool|null $isPrimaryKey, - string $expectedColumnType, - int|string $expectedLastInsertID, - ): void { - parent::executeColumnTypes( - $abstractColumn, - $expectedColumnSchemaType, - $isPrimaryKey, - $expectedColumnType, - $expectedLastInsertID, - ); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::schemaWithUnsigned - */ - public function testExecuteWithUnsigned( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mysql/command/types/PrimaryKeyTest.php b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php index b12b458d..e467d266 100644 --- a/tests/framework/db/mysql/command/types/PrimaryKeyTest.php +++ b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php @@ -27,26 +27,7 @@ protected function setUp(): void * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::schema */ public function testExecute( - Closure $abstractColumn, - string $expectedColumnSchemaType, - bool|null $isPrimaryKey, - string $expectedColumnType, - int|string $expectedLastInsertID, - ): void { - parent::executeColumnTypes( - $abstractColumn, - $expectedColumnSchemaType, - $isPrimaryKey, - $expectedColumnType, - $expectedLastInsertID, - ); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::schemaWithUnsigned - */ - public function testExecuteWithUnsigned( - Closure $abstractColumn, + Closure|string $abstractColumn, string $expectedColumnSchemaType, bool|null $isPrimaryKey, string $expectedColumnType, diff --git a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php index e55cd56f..cbde3e7f 100644 --- a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php @@ -51,6 +51,7 @@ public function builderWithUnsigned(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), 'int(11) AUTO_INCREMENT', @@ -65,12 +66,7 @@ public static function schema(): array 'integer', 2, ], - ]; - } - - public static function schemaWithUnsigned(): array - { - return [ + // schema with unsigned [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO)->unsigned(), 'int(10) UNSIGNED AUTO_INCREMENT', @@ -101,6 +97,69 @@ public static function schemaWithUnsigned(): array 'integer', 2, ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), + 'int(11) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(1), + 'int(1) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + // builder generator wiht unsigned + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement()->unsigned(), + 'int(10) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder() + ->autoIncrement(1) + ->unsigned(), + 'int(1) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + // raw sql + [ + 'int(11) AUTO_INCREMENT', + 'int(11) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'int(1) AUTO_INCREMENT', + 'int(1) AUTO_INCREMENT', + true, + 'integer', + 2, + ], + // raw sql with unsigned + [ + 'int(10) UNSIGNED AUTO_INCREMENT', + 'int(10) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], + [ + 'int(1) UNSIGNED AUTO_INCREMENT', + 'int(1) UNSIGNED AUTO_INCREMENT', + true, + 'integer', + 2, + ], ]; } diff --git a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php index d56f0814..ada39f5c 100644 --- a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php @@ -51,6 +51,7 @@ public function builderWithUnsigned(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), 'bigint(20) AUTO_INCREMENT', @@ -65,12 +66,7 @@ public static function schema(): array 'bigint', 2, ], - ]; - } - - public static function schemaWithUnsigned(): array - { - return [ + // schema with unsigned [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO)->unsigned(), 'bigint(20) UNSIGNED AUTO_INCREMENT', @@ -102,6 +98,69 @@ public static function schemaWithUnsigned(): array 'bigint', 2, ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), + 'bigint(20) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(1), + 'bigint(1) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + // builder generator with unsigned + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement()->unsigned(), + 'bigint(20) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder() + ->bigAutoIncrement(1) + ->unsigned(), + 'bigint(1) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + // raw sql + [ + 'bigint(20) AUTO_INCREMENT', + 'bigint(20) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'bigint(1) AUTO_INCREMENT', + 'bigint(1) AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + // raw sql with unsigned + [ + 'bigint(20) UNSIGNED AUTO_INCREMENT', + 'bigint(20) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], + [ + 'bigint(1) UNSIGNED AUTO_INCREMENT', + 'bigint(1) UNSIGNED AUTO_INCREMENT', + true, + 'bigint', + 2, + ], ]; } diff --git a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php index 9bc269ba..1125b1a3 100644 --- a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php @@ -51,6 +51,7 @@ public function builderWithUnsigned(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', @@ -65,12 +66,7 @@ public static function schema(): array 'bigint', 2, ], - ]; - } - - public static function schemaWithUnsigned(): array - { - return [ + // schema with unsigned [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK)->unsigned(), 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', @@ -99,6 +95,66 @@ public static function schemaWithUnsigned(): array 'bigint', 2, ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1), + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + // builder generator with unsigned + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey()->unsigned(), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1)->unsigned(), + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + // raw sql + [ + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + // raw sql with unsigned + [ + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ], + [ + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'bigint', + 2, + ] ]; } diff --git a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php index a2492646..9b15e51f 100644 --- a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php @@ -51,6 +51,7 @@ public function builderWithUnsigned(): array public static function schema(): array { return [ + // schema [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', @@ -65,12 +66,7 @@ public static function schema(): array 'integer', 2, ], - ]; - } - - public static function schemaWithUnsigned(): array - { - return [ + // schema with unsigned [ static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK)->unsigned(), 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', @@ -99,6 +95,66 @@ public static function schemaWithUnsigned(): array 'integer', 2, ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1), + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + // builder generator with unsigned + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey()->unsigned(), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1)->unsigned(), + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + // raw sql + [ + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + // raw sql with usigned + [ + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], + [ + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + true, + 'integer', + 2, + ], ]; } From 06ee89f53e6feb5eb3c165d278a9b9ca8c4a36a8 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sun, 25 Aug 2024 05:59:58 -0400 Subject: [PATCH 30/31] Refactor tearDown method in test classes to close database connection. --- .../command/types/AbstractExecuteColumnTypesTest.php | 10 +++++++++- .../db/querybuilder/types/AbstractColumnType.php | 8 -------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php index d042b529..ed5d41fb 100644 --- a/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php +++ b/tests/framework/db/command/types/AbstractExecuteColumnTypesTest.php @@ -11,9 +11,17 @@ abstract class AbstractExecuteColumnTypes extends \yiiunit\TestCase { - protected Connection $db; + protected Connection|null $db = null; protected string $table = 'column_types'; + public function tearDown(): void + { + $this->db->close(); + $this->db = null; + + parent::tearDown(); + } + public function executeColumnTypes( Closure|string $abstractColumn, string $expectedColumnSchemaType, diff --git a/tests/framework/db/querybuilder/types/AbstractColumnType.php b/tests/framework/db/querybuilder/types/AbstractColumnType.php index 9eba2753..4f25e6d8 100644 --- a/tests/framework/db/querybuilder/types/AbstractColumnType.php +++ b/tests/framework/db/querybuilder/types/AbstractColumnType.php @@ -6,19 +6,11 @@ use Closure; use yii\db\Connection; -use yii\db\SchemaBuilderTrait; abstract class AbstractColumnType extends \yiiunit\TestCase { - use SchemaBuilderTrait; - protected Connection|null $db = null; - protected function getDb(): Connection - { - return $this->db; - } - public function tearDown(): void { $this->db->close(); From 259b2e824440ab303a8ee5d84c2396ab7807623d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Aug 2024 12:21:19 -0400 Subject: [PATCH 31/31] Refactor column type mappings for auto-incremental and primary keys in test classes. --- src/db/mysql/QueryBuilder.php | 35 +--- .../mssql/command/types/AutoIncrementTest.php | 2 +- .../command/types/BigAutoIncrementTest.php | 2 +- .../mssql/command/types/BigPrimaryKeyTest.php | 2 +- .../db/mssql/command/types/PrimaryKeyTest.php | 2 +- .../provider/types/AutoIncrementProvider.php | 180 +++++++++--------- .../types/BigAutoIncrementProvider.php | 179 ++++++++--------- .../provider/types/BigPrimaryKeyProvider.php | 179 ++++++++--------- .../provider/types/PrimaryKeyProvider.php | 179 ++++++++--------- .../querybuilder/types/AutoIncrementTest.php | 16 +- .../types/BigAutoIncrementTest.php | 16 +- .../querybuilder/types/BigPrimaryKeyTest.php | 16 +- .../querybuilder/types/PrimaryKeyTest.php | 16 +- .../mysql/command/types/AutoIncrementTest.php | 2 +- .../command/types/BigAutoIncrementTest.php | 2 +- .../mysql/command/types/BigPrimaryKeyTest.php | 2 +- .../db/mysql/command/types/PrimaryKeyTest.php | 2 +- .../provider/types/AutoIncrementProvider.php | 131 ++++--------- .../types/BigAutoIncrementProvider.php | 134 ++++--------- .../provider/types/BigPrimaryKeyProvider.php | 125 ++++-------- .../provider/types/PrimaryKeyProvider.php | 123 ++++-------- .../querybuilder/types/AutoIncrementTest.php | 40 +--- .../types/BigAutoIncrementTest.php | 40 +--- .../querybuilder/types/BigPrimaryKeyTest.php | 40 +--- .../querybuilder/types/PrimaryKeyTest.php | 40 +--- .../types/AbstractAutoIncrementProvider.php | 2 +- .../AbstractBigAutoIncrementProvider.php | 2 +- .../types/AbstractBigPrimaryKeyProvider.php | 2 +- .../types/AbstractPrimaryKeyProvider.php | 2 +- 29 files changed, 541 insertions(+), 972 deletions(-) diff --git a/src/db/mysql/QueryBuilder.php b/src/db/mysql/QueryBuilder.php index 48a312aa..c3b7a063 100644 --- a/src/db/mysql/QueryBuilder.php +++ b/src/db/mysql/QueryBuilder.php @@ -118,37 +118,16 @@ public function createTable(string $table, array $columns, string|null $options foreach ($columns as $name => $type) { if (is_string($name)) { - if ($type instanceof ColumnSchemaBuilder) { - if ( - in_array( - $type->getType(), - [Schema::TYPE_AUTO, Schema::TYPE_UAUTO, Schema::TYPE_BIGAUTO, Schema::TYPE_UBIGAUTO], - true - ) - ) { - $autoIncrementColumn = $name; - } - - if ( - in_array( - $type->getType(), - [Schema::TYPE_PK, Schema::TYPE_UPK, Schema::TYPE_BIGPK, Schema::TYPE_UBIGPK], - true - ) - ) { - $hasPrimaryKey = true; - } - } else { - if (stripos($type, 'AUTO_INCREMENT') !== false) { - $autoIncrementColumn = $name; - } + $columnType = $this->getColumnType($type); + $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $columnType; - if (stripos($type, 'PRIMARY KEY') !== false) { - $hasPrimaryKey = true; - } + if (stripos($columnType, 'AUTO_INCREMENT') !== false) { + $autoIncrementColumn = $name; } - $cols[] = "\t" . $this->db->quoteColumnName($name) . ' ' . $this->getColumnType($type); + if (stripos($columnType, 'PRIMARY KEY') !== false) { + $hasPrimaryKey = true; + } } else { $cols[] = "\t" . $type; } diff --git a/tests/framework/db/mssql/command/types/AutoIncrementTest.php b/tests/framework/db/mssql/command/types/AutoIncrementTest.php index 30f87aeb..14ff52a1 100644 --- a/tests/framework/db/mssql/command/types/AutoIncrementTest.php +++ b/tests/framework/db/mssql/command/types/AutoIncrementTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::schema + * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php index 9ba86350..1b979e08 100644 --- a/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mssql/command/types/BigAutoIncrementTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::schema + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php index fafc0b96..ff11e3b1 100644 --- a/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mssql/command/types/BigPrimaryKeyTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::schema + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mssql/command/types/PrimaryKeyTest.php b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php index 4c11a356..321d14bf 100644 --- a/tests/framework/db/mssql/command/types/PrimaryKeyTest.php +++ b/tests/framework/db/mssql/command/types/PrimaryKeyTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::schema + * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php index b347d6c3..dd9a0a81 100644 --- a/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php +++ b/tests/framework/db/mssql/provider/types/AutoIncrementProvider.php @@ -10,177 +10,179 @@ final class AutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractAutoIncrementProvider { - public static function builder(): array - { - $expected = [ - 'auto' => [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - 3 => 'int IDENTITY', - ], - 'auto(1)' => [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 3 => 'int IDENTITY', - ], - 'auto(0,0)' => [ - 1 => 'auto(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), - 3 => 'int IDENTITY(0,1)', - ], - 'auto(1,1)' => [ - 1 => 'auto(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), - 3 => 'int IDENTITY(1,1)', - ], - 'auto(2,3)' => [ - 1 => 'auto(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), - 3 => 'int IDENTITY(2,3)', - ], - 'auto(-10,1)' => [ - Schema::TYPE_AUTO . '(-10,1)', - 'auto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), - 'int IDENTITY(-10,1)', - ], - ]; - - $types = parent::autoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public static function schema(): array + public static function command(): array { return [ - // schema + // default [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), + Schema::TYPE_AUTO, 'int IDENTITY', null, 'integer', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [0, 0]), + Schema::TYPE_AUTO . '(1)', + 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + Schema::TYPE_AUTO . '(0,0)', 'int IDENTITY(0,1)', null, 'integer', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 2]), - 'int IDENTITY(-10,2)', + Schema::TYPE_AUTO . '(1,1)', + 'int IDENTITY(1,1)', null, 'integer', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [2, 3]), + Schema::TYPE_AUTO . '(2,3)', 'int IDENTITY(2,3)', null, 'integer', '5', ], - // builder generator [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), + Schema::TYPE_AUTO . '(-10,1)', + 'int IDENTITY(-10,1)', + null, + 'integer', + '-9', + ], + // builder + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), 'int IDENTITY', null, 'integer', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(0, 0), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [1]), + 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [0, 0]), 'int IDENTITY(0,1)', null, 'integer', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(-10, 2), - 'int IDENTITY(-10,2)', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [1, 1]), + 'int IDENTITY(1,1)', null, 'integer', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(2, 3), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [2, 3]), 'int IDENTITY(2,3)', null, 'integer', '5', ], - // raw sql [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, [-10, 1]), + 'int IDENTITY(-10,1)', + null, + 'integer', + '-9', + ], + // builder shortcut + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), 'int IDENTITY', + null, + 'integer', + '2', + ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(1), 'int IDENTITY', null, 'integer', '2', ], [ - 'int IDENTITY(0,0)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(0, 0), 'int IDENTITY(0,1)', null, 'integer', '1', ], [ - 'int IDENTITY(-10,2)', - 'int IDENTITY(-10,2)', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(1, 1), + 'int IDENTITY(1,1)', null, 'integer', - '-8', + '2', ], [ - 'int IDENTITY(2,3)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(2, 3), 'int IDENTITY(2,3)', null, 'integer', '5', ], - + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(-10, 1), + 'int IDENTITY(-10,1)', + null, + 'integer', + '-9', + ], ]; } - public static function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'int IDENTITY', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + $expected = [ + 'auto' => [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int IDENTITY', ], - [ - 'int IDENTITY(1)', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 'int IDENTITY', + 'auto(1)' => [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int IDENTITY', ], - [ - 'int IDENTITY(0,0)', - 'auto(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), - 'int IDENTITY(0,1)', + 'auto(0,0)' => [ + 1 => 'auto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(0, 0), + 3 => 'int IDENTITY(0,1)', ], - [ - 'int IDENTITY(1,1)', - 'auto(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + 'auto(1,1)' => [ + 1 => 'auto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1, 1), + 3 => 'int IDENTITY(1,1)', ], - [ - 'int IDENTITY(2,3)', - 'auto(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + 'auto(2,3)' => [ + 1 => 'auto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(2, 3), + 3 => 'int IDENTITY(2,3)', ], - [ - 'int IDENTITY(-10,1)', + 'auto(-10,1)' => [ + Schema::TYPE_AUTO . '(-10,1)', 'auto(-10,1)', static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(-10, 1), 'int IDENTITY(-10,1)', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php index 8e5122b5..cef157a5 100644 --- a/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php +++ b/tests/framework/db/mssql/provider/types/BigAutoIncrementProvider.php @@ -10,176 +10,179 @@ final class BigAutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractBigAutoIncrementProvider { - public static function builder(): array - { - $expected = [ - 'bigauto' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - 3 => 'bigint IDENTITY', - ], - 'bigauto(1)' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 3 => 'bigint IDENTITY', - ], - 'bigauto(0,0)' => [ - 1 => 'bigauto(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), - 3 => 'bigint IDENTITY(0,1)', - ], - 'bigauto(1,1)' => [ - 1 => 'bigauto(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), - 3 => 'bigint IDENTITY(1,1)', - ], - 'bigauto(2,3)' => [ - 1 => 'bigauto(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), - 3 => 'bigint IDENTITY(2,3)', - ], - 'bigauto(-10,1)' => [ - Schema::TYPE_BIGAUTO . '(-10,1)', - 'bigauto(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), - 'bigint IDENTITY(-10,1)', - ], - ]; - - $types = parent::bigAutoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public static function schema(): array + public static function command(): array { return [ - // schema + // default [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), + Schema::TYPE_BIGAUTO, 'bigint IDENTITY', null, 'bigint', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [0, 0]), + Schema::TYPE_BIGAUTO . '(1)', + 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + Schema::TYPE_BIGAUTO . '(0,0)', 'bigint IDENTITY(0,1)', null, 'bigint', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 2]), - 'bigint IDENTITY(-10,2)', + Schema::TYPE_BIGAUTO . '(1,1)', + 'bigint IDENTITY(1,1)', null, 'bigint', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [2, 3]), + Schema::TYPE_BIGAUTO . '(2,3)', 'bigint IDENTITY(2,3)', null, 'bigint', '5', ], - // builder generator [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), + Schema::TYPE_BIGAUTO . '(-10,1)', + 'bigint IDENTITY(-10,1)', + null, + 'bigint', + '-9', + ], + // builder + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), 'bigint IDENTITY', null, 'bigint', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(0, 0), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [1]), + 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [0, 0]), 'bigint IDENTITY(0,1)', null, 'bigint', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(-10, 2), - 'bigint IDENTITY(-10,2)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [1, 1]), + 'bigint IDENTITY(1,1)', null, 'bigint', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(2, 3), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [2, 3]), 'bigint IDENTITY(2,3)', null, 'bigint', '5', ], - // raw sql [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, [-10, 1]), + 'bigint IDENTITY(-10,1)', + null, + 'bigint', + '-9', + ], + // builder generator + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), 'bigint IDENTITY', + null, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(1), 'bigint IDENTITY', null, 'bigint', '2', ], [ - 'bigint IDENTITY(0,0)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(0, 0), 'bigint IDENTITY(0,1)', null, 'bigint', '1', ], [ - 'bigint IDENTITY(-10,2)', - 'bigint IDENTITY(-10,2)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(1, 1), + 'bigint IDENTITY(1,1)', null, 'bigint', - '-8', + '2', ], [ - 'bigint IDENTITY(2,3)', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(2, 3), 'bigint IDENTITY(2,3)', null, 'bigint', '5', ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(-10, 1), + 'bigint IDENTITY(-10,1)', + null, + 'bigint', + '-9', + ], ]; } - public static function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'bigint IDENTITY', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + $expected = [ + 'bigauto' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint IDENTITY', ], - [ - 'bigint IDENTITY(1)', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 'bigint IDENTITY', + 'bigauto(1)' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint IDENTITY', ], - [ - 'bigint IDENTITY(0,0)', - 'bigauto(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), - 'bigint IDENTITY(0,1)', + 'bigauto(0,0)' => [ + 1 => 'bigauto(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(0, 0), + 3 => 'bigint IDENTITY(0,1)', ], - [ - 'bigint IDENTITY(1,1)', - 'bigauto(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + 'bigauto(1,1)' => [ + 1 => 'bigauto(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1, 1), + 3 => 'bigint IDENTITY(1,1)', ], - [ - 'bigint IDENTITY(2,3)', - 'bigauto(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + 'bigauto(2,3)' => [ + 1 => 'bigauto(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(2, 3), + 3 => 'bigint IDENTITY(2,3)', ], - [ - 'bigint IDENTITY(-10,1)', + 'bigauto(-10,1)' => [ + Schema::TYPE_BIGAUTO . '(-10,1)', 'bigauto(-10,1)', static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(-10, 1), 'bigint IDENTITY(-10,1)', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php index a52e879d..6cec51d7 100644 --- a/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php +++ b/tests/framework/db/mssql/provider/types/BigPrimaryKeyProvider.php @@ -10,176 +10,179 @@ final class BigPrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractBigPrimaryKeyProvider { - public static function builder(): array - { - $expected = [ - 'bigpk' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - 3 => 'bigint IDENTITY PRIMARY KEY', - ], - 'bigpk(1)' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 3 => 'bigint IDENTITY PRIMARY KEY', - ], - 'bigpk(0,0)' => [ - 1 => 'bigpk(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), - 3 => 'bigint IDENTITY(0,1) PRIMARY KEY', - ], - 'bigpk(1,1)' => [ - 1 => 'bigpk(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), - 3 => 'bigint IDENTITY(1,1) PRIMARY KEY', - ], - 'bigpk(2,3)' => [ - 1 => 'bigpk(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), - 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', - ], - 'bigpk(-10,1)' => [ - Schema::TYPE_BIGPK . '(-10,1)', - 'bigpk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), - 'bigint IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - - $types = parent::bigPrimaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public static function schema(): array + public static function command(): array { return [ - // schema + // default [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), + Schema::TYPE_BIGPK, 'bigint IDENTITY PRIMARY KEY', true, 'bigint', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [0, 0]), + Schema::TYPE_BIGPK . '(1)', + 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + Schema::TYPE_BIGPK . '(0,0)', 'bigint IDENTITY(0,1) PRIMARY KEY', true, 'bigint', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 2]), - 'bigint IDENTITY(-10,2) PRIMARY KEY', + Schema::TYPE_BIGPK . '(1,1)', + 'bigint IDENTITY(1,1) PRIMARY KEY', true, 'bigint', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [2, 3]), + Schema::TYPE_BIGPK . '(2,3)', 'bigint IDENTITY(2,3) PRIMARY KEY', true, 'bigint', '5', ], - // builder generator [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), + Schema::TYPE_BIGPK . '(-10,1)', + 'bigint IDENTITY(-10,1) PRIMARY KEY', + true, + 'bigint', + '-9', + ], + // builder + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), 'bigint IDENTITY PRIMARY KEY', true, 'bigint', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(0, 0), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [1]), + 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [0, 0]), 'bigint IDENTITY(0,1) PRIMARY KEY', true, 'bigint', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(-10, 2), - 'bigint IDENTITY(-10,2) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [1, 1]), + 'bigint IDENTITY(1,1) PRIMARY KEY', true, 'bigint', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(2, 3), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [2, 3]), 'bigint IDENTITY(2,3) PRIMARY KEY', true, 'bigint', '5', ], - // raw sql [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, [-10, 1]), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + true, + 'bigint', + '-9', + ], + // builder shortcut + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), 'bigint IDENTITY PRIMARY KEY', + true, + 'bigint', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1), 'bigint IDENTITY PRIMARY KEY', true, 'bigint', '2', ], [ - 'bigint IDENTITY(0,0) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(0, 0), 'bigint IDENTITY(0,1) PRIMARY KEY', true, 'bigint', '1', ], [ - 'bigint IDENTITY(-10,2) PRIMARY KEY', - 'bigint IDENTITY(-10,2) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1, 1), + 'bigint IDENTITY(1,1) PRIMARY KEY', true, 'bigint', - '-8', + '2', ], [ - 'bigint IDENTITY(2,3) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(2, 3), 'bigint IDENTITY(2,3) PRIMARY KEY', true, 'bigint', '5', ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(-10, 1), + 'bigint IDENTITY(-10,1) PRIMARY KEY', + true, + 'bigint', + '-9', + ], ]; } - public static function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'bigint IDENTITY PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + $expected = [ + 'bigpk' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint IDENTITY PRIMARY KEY', ], - [ - 'bigint IDENTITY(1) PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 'bigint IDENTITY PRIMARY KEY', + 'bigpk(1)' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint IDENTITY PRIMARY KEY', ], - [ - 'bigint IDENTITY(0,0) PRIMARY KEY', - 'bigpk(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), - 'bigint IDENTITY(0,1) PRIMARY KEY', + 'bigpk(0,0)' => [ + 1 => 'bigpk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(0, 0), + 3 => 'bigint IDENTITY(0,1) PRIMARY KEY', ], - [ - 'bigint IDENTITY(1,1) PRIMARY KEY', - 'bigpk(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + 'bigpk(1,1)' => [ + 1 => 'bigpk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1, 1), + 3 => 'bigint IDENTITY(1,1) PRIMARY KEY', ], - [ - 'bigint IDENTITY(2,3) PRIMARY KEY', - 'bigpk(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + 'bigpk(2,3)' => [ + 1 => 'bigpk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(2, 3), + 3 => 'bigint IDENTITY(2,3) PRIMARY KEY', ], - [ - 'bigint IDENTITY(-10,1) PRIMARY KEY', + 'bigpk(-10,1)' => [ + Schema::TYPE_BIGPK . '(-10,1)', 'bigpk(-10,1)', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(-10, 1), 'bigint IDENTITY(-10,1) PRIMARY KEY', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php index fbe10ec7..63fafbfa 100644 --- a/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php +++ b/tests/framework/db/mssql/provider/types/PrimaryKeyProvider.php @@ -10,176 +10,179 @@ final class PrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractPrimaryKeyProvider { - public static function builder(): array - { - $expected = [ - 'pk' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - 3 => 'int IDENTITY PRIMARY KEY', - ], - 'pk(1)' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 3 => 'int IDENTITY PRIMARY KEY', - ], - 'pk(0,0)' => [ - 1 => 'pk(0,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), - 3 => 'int IDENTITY(0,1) PRIMARY KEY', - ], - 'pk(1,1)' => [ - 1 => 'pk(1,1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), - 3 => 'int IDENTITY(1,1) PRIMARY KEY', - ], - 'pk(2,3)' => [ - 1 => 'pk(2,3)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), - 3 => 'int IDENTITY(2,3) PRIMARY KEY', - ], - 'pk(-10,1)' => [ - Schema::TYPE_PK . '(-10,1)', - 'pk(-10,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), - 'int IDENTITY(-10,1) PRIMARY KEY', - ], - ]; - - $types = parent::primaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public static function schema(): array + public static function command(): array { return [ - // schema + // default [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), + Schema::TYPE_PK, 'int IDENTITY PRIMARY KEY', true, 'integer', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [0, 0]), + Schema::TYPE_PK . '(1)', + 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + Schema::TYPE_PK . '(0,0)', 'int IDENTITY(0,1) PRIMARY KEY', true, 'integer', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 2]), - 'int IDENTITY(-10,2) PRIMARY KEY', + Schema::TYPE_PK . '(1,1)', + 'int IDENTITY(1,1) PRIMARY KEY', true, 'integer', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [2, 3]), + Schema::TYPE_PK . '(2,3)', 'int IDENTITY(2,3) PRIMARY KEY', true, 'integer', '5', ], - // builder generator [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), + Schema::TYPE_PK . '(-10,1)', + 'int IDENTITY(-10,1) PRIMARY KEY', + true, + 'integer', + '-9', + ], + // builder + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), 'int IDENTITY PRIMARY KEY', true, 'integer', '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(0, 0), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [1]), + 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [0, 0]), 'int IDENTITY(0,1) PRIMARY KEY', true, 'integer', '1', ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(-10, 2), - 'int IDENTITY(-10,2) PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [1, 1]), + 'int IDENTITY(1,1) PRIMARY KEY', true, 'integer', - '-8', + '2', ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(2, 3), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [2, 3]), 'int IDENTITY(2,3) PRIMARY KEY', true, 'integer', '5', ], - // raw sql [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, [-10, 1]), + 'int IDENTITY(-10,1) PRIMARY KEY', + true, + 'integer', + '-9', + ], + // builder shortcut + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), 'int IDENTITY PRIMARY KEY', + true, + 'integer', + '2', + ], + [ + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1), 'int IDENTITY PRIMARY KEY', true, 'integer', '2', ], [ - 'int IDENTITY(0,0) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(0, 0), 'int IDENTITY(0,1) PRIMARY KEY', true, 'integer', '1', ], [ - 'int IDENTITY(-10,2) PRIMARY KEY', - 'int IDENTITY(-10,2) PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1, 1), + 'int IDENTITY(1,1) PRIMARY KEY', true, 'integer', - '-8', + '2', ], [ - 'int IDENTITY(2,3) PRIMARY KEY', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(2, 3), 'int IDENTITY(2,3) PRIMARY KEY', true, 'integer', '5', ], + [ + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(-10, 1), + 'int IDENTITY(-10,1) PRIMARY KEY', + true, + 'integer', + '-9', + ], ]; } - public static function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'int IDENTITY PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + $expected = [ + 'pk' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int IDENTITY PRIMARY KEY', ], - [ - 'int IDENTITY(1) PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 'int IDENTITY PRIMARY KEY', + 'pk(1)' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int IDENTITY PRIMARY KEY', ], - [ - 'int IDENTITY(0,0) PRIMARY KEY', - 'pk(0,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), - 'int IDENTITY(0,1) PRIMARY KEY', + 'pk(0,0)' => [ + 1 => 'pk(0,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(0, 0), + 3 => 'int IDENTITY(0,1) PRIMARY KEY', ], - [ - 'int IDENTITY(1,1) PRIMARY KEY', - 'pk(1,1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + 'pk(1,1)' => [ + 1 => 'pk(1,1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1, 1), + 3 => 'int IDENTITY(1,1) PRIMARY KEY', ], - [ - 'int IDENTITY(2,3) PRIMARY KEY', - 'pk(2,3)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + 'pk(2,3)' => [ + 1 => 'pk(2,3)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(2, 3), + 3 => 'int IDENTITY(2,3) PRIMARY KEY', ], - [ - 'int IDENTITY(-10,1) PRIMARY KEY', + 'pk(-10,1)' => [ + Schema::TYPE_PK . '(-10,1)', 'pk(-10,1)', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(-10, 1), 'int IDENTITY(-10,1) PRIMARY KEY', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php b/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php index b7fcc063..8dd3ac14 100644 --- a/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php +++ b/tests/framework/db/mssql/querybuilder/types/AutoIncrementTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::builder + * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,16 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\AutoIncrementProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php b/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php index 54a1ed16..282e0168 100644 --- a/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mssql/querybuilder/types/BigAutoIncrementTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::builder + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,16 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigAutoIncrementProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php b/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php index 94a54dfe..ec81b019 100644 --- a/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mssql/querybuilder/types/BigPrimaryKeyTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::builder + * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,16 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\BigPrimaryKeyProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php b/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php index c44ef03f..712694a1 100644 --- a/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php +++ b/tests/framework/db/mssql/querybuilder/types/PrimaryKeyTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::builder + * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,16 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mssql\provider\types\PrimaryKeyProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mysql/command/types/AutoIncrementTest.php b/tests/framework/db/mysql/command/types/AutoIncrementTest.php index a7eaff23..15a28dfd 100644 --- a/tests/framework/db/mysql/command/types/AutoIncrementTest.php +++ b/tests/framework/db/mysql/command/types/AutoIncrementTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::schema + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php index 4bf122da..08beb854 100644 --- a/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mysql/command/types/BigAutoIncrementTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::schema + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php index 7e2b62f7..d24ac1e2 100644 --- a/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mysql/command/types/BigPrimaryKeyTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::schema + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mysql/command/types/PrimaryKeyTest.php b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php index e467d266..af9c7d91 100644 --- a/tests/framework/db/mysql/command/types/PrimaryKeyTest.php +++ b/tests/framework/db/mysql/command/types/PrimaryKeyTest.php @@ -24,7 +24,7 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::schema + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::command */ public function testExecute( Closure|string $abstractColumn, diff --git a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php index cbde3e7f..536ab022 100644 --- a/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/AutoIncrementProvider.php @@ -10,151 +10,94 @@ final class AutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractAutoIncrementProvider { - public static function builder(): array - { - $expected = [ - 'auto' => [ - 1 => 'auto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), - 3 => 'int(11) AUTO_INCREMENT', - ], - 'auto(1)' => [ - 1 => 'auto(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 3 => 'int(1) AUTO_INCREMENT', - ], - ]; - - $types = parent::autoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public function builderWithUnsigned(): array + public static function command(): array { return [ + // default [ - \yii\db\mysql\Schema::TYPE_UAUTO, - 'uauto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), - 'int(10) UNSIGNED AUTO_INCREMENT', - ], - [ - \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', - 'uauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), - 'int(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public static function schema(): array - { - return [ - // schema - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), + Schema::TYPE_AUTO, 'int(11) AUTO_INCREMENT', true, 'integer', 2, ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, 1), + Schema::TYPE_AUTO . '(1)', 'int(1) AUTO_INCREMENT', true, 'integer', 2, ], - // schema with unsigned - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO)->unsigned(), - 'int(10) UNSIGNED AUTO_INCREMENT', - true, - 'integer', - 2, - ], [ - 'id' => static fn (Schema $schema) => $schema - ->createColumnSchemaBuilder(Schema::TYPE_AUTO, 1) - ->unsigned(), - 'int(1) UNSIGNED AUTO_INCREMENT', - true, - 'integer', - 2, - ], - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO), + \yii\db\mysql\Schema::TYPE_UAUTO, 'int(10) UNSIGNED AUTO_INCREMENT', true, 'integer', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO, 1), + \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', 'int(1) UNSIGNED AUTO_INCREMENT', true, 'integer', 2, ], - // builder generator + // builder [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO), 'int(11) AUTO_INCREMENT', true, 'integer', 2, ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(1), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_AUTO, 1), 'int(1) AUTO_INCREMENT', true, 'integer', 2, ], - // builder generator wiht unsigned [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement()->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO), 'int(10) UNSIGNED AUTO_INCREMENT', true, 'integer', 2, ], [ - 'id' => static fn (Schema $schema) => $schema - ->createColumnSchemaBuilder() - ->autoIncrement(1) - ->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UAUTO, 1), 'int(1) UNSIGNED AUTO_INCREMENT', true, 'integer', 2, ], - // raw sql + // builder shortcut [ - 'int(11) AUTO_INCREMENT', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(), 'int(11) AUTO_INCREMENT', true, 'integer', 2, ], [ - 'int(1) AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement(1), 'int(1) AUTO_INCREMENT', true, 'integer', 2, ], - // raw sql with unsigned [ - 'int(10) UNSIGNED AUTO_INCREMENT', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->autoIncrement()->unsigned(), 'int(10) UNSIGNED AUTO_INCREMENT', true, 'integer', 2, ], [ - 'int(1) UNSIGNED AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder() + ->autoIncrement(1) + ->unsigned(), 'int(1) UNSIGNED AUTO_INCREMENT', true, 'integer', @@ -163,37 +106,35 @@ public static function schema(): array ]; } - public function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'int(11) AUTO_INCREMENT', - 'auto', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + $expected = [ + 'auto' => [ + 1 => 'auto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(), + 3 => 'int(11) AUTO_INCREMENT', ], - [ - 'int(1) AUTO_INCREMENT', - 'auto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), - 'int(1) AUTO_INCREMENT', + 'auto(1)' => [ + 1 => 'auto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1), + 3 => 'int(1) AUTO_INCREMENT', ], - ]; - } - - public function rawWithUnsigned(): array - { - return [ [ - 'int(10) UNSIGNED AUTO_INCREMENT', + \yii\db\mysql\Schema::TYPE_UAUTO, 'uauto', static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement()->unsigned(), + 'int(10) UNSIGNED AUTO_INCREMENT', ], [ - 'int(1) UNSIGNED AUTO_INCREMENT', + \yii\db\mysql\Schema::TYPE_UAUTO . '(1)', 'uauto(1)', static fn (ColumnSchemaBuilder $builder) => $builder->autoIncrement(1)->unsigned(), 'int(1) UNSIGNED AUTO_INCREMENT', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php index ada39f5c..b36682b5 100644 --- a/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php +++ b/tests/framework/db/mysql/provider/types/BigAutoIncrementProvider.php @@ -10,112 +10,55 @@ final class BigAutoIncrementProvider extends \yiiunit\framework\db\provider\types\AbstractBigAutoIncrementProvider { - public static function builder(): array - { - $expected = [ - 'bigauto' => [ - 1 => 'bigauto', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), - 3 => 'bigint(20) AUTO_INCREMENT', - ], - 'bigauto(1)' => [ - 1 => 'bigauto(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 3 => 'bigint(1) AUTO_INCREMENT', - ], - ]; - - $types = parent::bigAutoIncrement(); - - return TestHelper::addExpected($expected, $types); - } - - public function builderWithUnsigned(): array - { - return [ - [ - \yii\db\mysql\Schema::TYPE_UBIGAUTO, - 'ubigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), - 'bigint(20) UNSIGNED AUTO_INCREMENT', - ], - [ - \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', - 'ubigauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), - 'bigint(1) UNSIGNED AUTO_INCREMENT', - ], - ]; - } - - public static function schema(): array + public static function command(): array { return [ - // schema + // default [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), + Schema::TYPE_BIGAUTO, 'bigint(20) AUTO_INCREMENT', true, 'bigint', 2, ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, 1), + Schema::TYPE_BIGAUTO . '(1)', 'bigint(1) AUTO_INCREMENT', true, 'bigint', 2, ], - // schema with unsigned - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO)->unsigned(), - 'bigint(20) UNSIGNED AUTO_INCREMENT', - true, - 'bigint', - 2, - ], [ - 'id' => static fn (Schema $schema) => $schema - ->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, 1) - ->unsigned(), - 'bigint(1) UNSIGNED AUTO_INCREMENT', - true, - 'bigint', - 2, - ], - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO), + \yii\db\mysql\Schema::TYPE_UBIGAUTO, 'bigint(20) UNSIGNED AUTO_INCREMENT', true, 'bigint', 2, ], [ - 'id' => static fn (Schema $schema) => $schema - ->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO, 1), + \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', 'bigint(1) UNSIGNED AUTO_INCREMENT', true, 'bigint', 2, ], - // builder generator + // builder [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO), 'bigint(20) AUTO_INCREMENT', true, 'bigint', 2, ], [ - 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(1), + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGAUTO, 1), 'bigint(1) AUTO_INCREMENT', true, 'bigint', 2, ], - // builder generator with unsigned [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement()->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO), 'bigint(20) UNSIGNED AUTO_INCREMENT', true, 'bigint', @@ -123,39 +66,42 @@ public static function schema(): array ], [ 'id' => static fn (Schema $schema) => $schema - ->createColumnSchemaBuilder() - ->bigAutoIncrement(1) - ->unsigned(), + ->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGAUTO, 1), 'bigint(1) UNSIGNED AUTO_INCREMENT', true, 'bigint', 2, ], - // raw sql + // builder shortcut [ - 'bigint(20) AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(), 'bigint(20) AUTO_INCREMENT', true, 'bigint', 2, ], [ - 'bigint(1) AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigAutoIncrement(1), 'bigint(1) AUTO_INCREMENT', true, 'bigint', 2, ], - // raw sql with unsigned [ - 'bigint(20) UNSIGNED AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder() + ->bigAutoIncrement() + ->unsigned(), 'bigint(20) UNSIGNED AUTO_INCREMENT', true, 'bigint', 2, ], [ - 'bigint(1) UNSIGNED AUTO_INCREMENT', + 'id' => static fn (Schema $schema) => $schema + ->createColumnSchemaBuilder() + ->bigAutoIncrement(1) + ->unsigned(), 'bigint(1) UNSIGNED AUTO_INCREMENT', true, 'bigint', @@ -164,37 +110,35 @@ public static function schema(): array ]; } - public function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'bigint(20) AUTO_INCREMENT', - 'bigauto', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + $expected = [ + 'bigauto' => [ + 1 => 'bigauto', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(), + 3 => 'bigint(20) AUTO_INCREMENT', ], - [ - 'bigint(1) AUTO_INCREMENT', - 'bigauto(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), - 'bigint(1) AUTO_INCREMENT', + 'bigauto(1)' => [ + 1 => 'bigauto(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1), + 3 => 'bigint(1) AUTO_INCREMENT', ], - ]; - } - - public function rawWithUnsigned(): array - { - return [ [ - 'bigint(20) UNSIGNED AUTO_INCREMENT', + \yii\db\mysql\Schema::TYPE_UBIGAUTO, 'ubigauto', static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement()->unsigned(), + 'bigint(20) UNSIGNED AUTO_INCREMENT', ], [ - 'bigint(1) UNSIGNED AUTO_INCREMENT', + \yii\db\mysql\Schema::TYPE_UBIGAUTO . '(1)', 'ubigauto(1)', static fn (ColumnSchemaBuilder $builder) => $builder->bigAutoIncrement(1)->unsigned(), 'bigint(1) UNSIGNED AUTO_INCREMENT', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php index 1125b1a3..07196343 100644 --- a/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/BigPrimaryKeyProvider.php @@ -10,185 +10,128 @@ final class BigPrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractBigPrimaryKeyProvider { - public static function builder(): array - { - $expected = [ - 'bigpk' => [ - 1 => 'bigpk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), - 3 => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - 'bigpk(1)' => [ - 1 => 'bigpk(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 3 => 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - - $types = parent::bigPrimaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public function builderWithUnsigned(): array + public static function command(): array { return [ + // default [ - \yii\db\mysql\Schema::TYPE_UBIGPK, - 'ubigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - [ - \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', - 'ubigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public static function schema(): array - { - return [ - // schema - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), + Schema::TYPE_BIGPK, 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, 1), + Schema::TYPE_BIGPK . '(1)', 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], - // schema with unsigned - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK)->unsigned(), - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - true, - 'bigint', - 2, - ], - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, 1)->unsigned(), - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - true, - 'bigint', - 2, - ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK), + \yii\db\mysql\Schema::TYPE_UBIGPK, 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK, 1), + \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], - // builder generator + // builder [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK), 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_BIGPK, 1), 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], - // builder generator with unsigned [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey()->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK), 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1)->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UBIGPK, 1), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], - // raw sql + // builder shortcut [ - 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(), 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1), 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], - // raw sql with unsigned [ - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey()->unsigned(), 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, ], [ - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->bigPrimaryKey(1)->unsigned(), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'bigint', 2, - ] + ], ]; } - public function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'bigpk', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + $expected = [ + 'bigpk' => [ + 1 => 'bigpk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(), + 3 => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', ], - [ - 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'bigpk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), - 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'bigpk(1)' => [ + 1 => 'bigpk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1), + 3 => 'bigint(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', ], - ]; - } - - public function rawWithUnsigned(): array - { - return [ [ - 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + \yii\db\mysql\Schema::TYPE_UBIGPK, 'ubigpk', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey()->unsigned(), + 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ - 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + \yii\db\mysql\Schema::TYPE_UBIGPK . '(1)', 'ubigpk(1)', static fn (ColumnSchemaBuilder $builder) => $builder->bigPrimaryKey(1)->unsigned(), 'bigint(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php index 9b15e51f..aa0c2ede 100644 --- a/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php +++ b/tests/framework/db/mysql/provider/types/PrimaryKeyProvider.php @@ -10,146 +10,91 @@ final class PrimaryKeyProvider extends \yiiunit\framework\db\provider\types\AbstractPrimaryKeyProvider { - public static function builder(): array - { - $expected = [ - 'pk' => [ - 1 => 'pk', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), - 3 => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - 'pk(1)' => [ - 1 => 'pk(1)', - 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 3 => 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - - $types = parent::primaryKey(); - - return TestHelper::addExpected($expected, $types); - } - - public function builderWithUnsigned(): array + public static function command(): array { return [ + // default [ - \yii\db\mysql\Schema::TYPE_UPK, - 'upk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - [ - \yii\db\mysql\Schema::TYPE_UPK . '(1)', - 'upk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - ], - ]; - } - - public static function schema(): array - { - return [ - // schema - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), + Schema::TYPE_PK, 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, 1), + Schema::TYPE_PK . '(1)', 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], - // schema with unsigned - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK)->unsigned(), - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - true, - 'integer', - 2, - ], - [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, 1)->unsigned(), - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', - true, - 'integer', - 2, - ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK), + \yii\db\mysql\Schema::TYPE_UPK, 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK, 1), + \yii\db\mysql\Schema::TYPE_UPK . '(1)', 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], - // builder generator + // builder [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK), 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(Schema::TYPE_PK, 1), 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], - // builder generator with unsigned [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey()->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK), 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1)->unsigned(), + static fn (Schema $schema) => $schema->createColumnSchemaBuilder(\yii\db\mysql\Schema::TYPE_UPK, 1), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], - // raw sql + // builder shorcut [ - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(), 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1), 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], - // raw sql with usigned [ - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey()->unsigned(), 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', 2, ], [ - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + static fn (Schema $schema) => $schema->createColumnSchemaBuilder()->primaryKey(1)->unsigned(), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', true, 'integer', @@ -158,37 +103,35 @@ public static function schema(): array ]; } - public function raw(): array + public static function queryBuilder(): array { - return [ - [ - 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pk', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + $expected = [ + 'pk' => [ + 1 => 'pk', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(), + 3 => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', ], - [ - 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', - 'pk(1)', - static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), - 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', + 'pk(1)' => [ + 1 => 'pk(1)', + 2 => static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1), + 3 => 'int(1) NOT NULL AUTO_INCREMENT PRIMARY KEY', ], - ]; - } - - public function rawWithUnsigned(): array - { - return [ [ - 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + \yii\db\mysql\Schema::TYPE_UPK, 'upk', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey()->unsigned(), + 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], [ - 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', + \yii\db\mysql\Schema::TYPE_UPK . '(1)', 'upk(1)', static fn (ColumnSchemaBuilder $builder) => $builder->primaryKey(1)->unsigned(), 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', ], ]; + + $types = parent::queryBuilder(); + + return TestHelper::addExpected($expected, $types); } } diff --git a/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php b/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php index 92a6964c..3680005c 100644 --- a/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php +++ b/tests/framework/db/mysql/querybuilder/types/AutoIncrementTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::builder + * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,40 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::builderWithUnsigned - */ - public function testBuilderWithUnsigned( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\AutoIncrementProvider::rawWithUnsigned - */ - public function testRawWithUnsigned( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php b/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php index 6627f28a..d9503f06 100644 --- a/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php +++ b/tests/framework/db/mysql/querybuilder/types/BigAutoIncrementTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::builder + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,40 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::builderWithUnsigned - */ - public function testBuilderWithUnsigned( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigAutoIncrementProvider::rawWithUnsigned - */ - public function testRawWithUnsigned( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php b/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php index 37524e0c..3e465038 100644 --- a/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php +++ b/tests/framework/db/mysql/querybuilder/types/BigPrimaryKeyTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::builder + * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,40 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::builderWithUnsigned - */ - public function testBuilderWithUnsigned( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\BigPrimaryKeyProvider::rawWithUnsigned - */ - public function testRawWithUnsigned( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php b/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php index dd1bc3ec..9537b648 100644 --- a/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php +++ b/tests/framework/db/mysql/querybuilder/types/PrimaryKeyTest.php @@ -24,9 +24,9 @@ protected function setUp(): void } /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::builder + * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::queryBuilder */ - public function testBuilder( + public function testGenerateColumnType( string $column, string $expectedColumn, Closure $builder, @@ -34,40 +34,4 @@ public function testBuilder( ): void { $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::builderWithUnsigned - */ - public function testBuilderWithUnsigned( - string $column, - string $expectedColumn, - Closure $builder, - string $expectedBuilder, - ): void { - $this->getColumnType($column, $expectedColumn, $builder, $expectedBuilder); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::raw - */ - public function testRaw( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } - - /** - * @dataProvider \yiiunit\framework\db\mysql\provider\types\PrimaryKeyProvider::rawWithUnsigned - */ - public function testRawWithUnsigned( - string $columnRaw, - string $column, - Closure $builder, - string $expectColumn = '', - ): void { - $this->getColumnTypeRaw($columnRaw, $column, $builder, $expectColumn); - } } diff --git a/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php b/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php index 829c45d7..d2c355dd 100644 --- a/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php +++ b/tests/framework/db/provider/types/AbstractAutoIncrementProvider.php @@ -8,7 +8,7 @@ abstract class AbstractAutoIncrementProvider { - public static function autoIncrement(): array + public static function queryBuilder(): array { return [ 'auto' => [ diff --git a/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php b/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php index 62708772..c85eb222 100644 --- a/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php +++ b/tests/framework/db/provider/types/AbstractBigAutoIncrementProvider.php @@ -8,7 +8,7 @@ abstract class AbstractBigAutoIncrementProvider { - public static function bigAutoIncrement(): array + public static function queryBuilder(): array { return [ 'bigauto' => [ diff --git a/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php b/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php index 9a108732..001f887e 100644 --- a/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php +++ b/tests/framework/db/provider/types/AbstractBigPrimaryKeyProvider.php @@ -8,7 +8,7 @@ abstract class AbstractBigPrimaryKeyProvider { - public static function bigPrimaryKey(): array + public static function queryBuilder(): array { return [ 'bigpk' => [ diff --git a/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php b/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php index 783c3aa7..f97e35ef 100644 --- a/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php +++ b/tests/framework/db/provider/types/AbstractPrimaryKeyProvider.php @@ -8,7 +8,7 @@ abstract class AbstractPrimaryKeyProvider { - public static function primaryKey(): array + public static function queryBuilder(): array { return [ 'pk' => [