From 295fa65f15992399cc8f230b914ce373e6635088 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Fri, 20 Sep 2024 17:30:03 +0700 Subject: [PATCH] Update according changes in `ColumnSchemaInterface` --- src/Schema.php | 7 +- tests/Provider/SchemaProvider.php | 151 +++++++++++++++++++----------- tests/SchemaTest.php | 6 -- 3 files changed, 101 insertions(+), 63 deletions(-) diff --git a/src/Schema.php b/src/Schema.php index a0e94ecd..2a7c1a28 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -50,7 +50,6 @@ * comment: string, * enum_values?: string[], * size?: int, - * precision?: int, * scale?: int, * } * @psalm-type RowConstraint = array{ @@ -416,10 +415,12 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface $dbType = $info['type']; /** @psalm-var ColumnInfoArray $info */ $column = $columnFactory->fromDefinition($dbType); + /** @psalm-suppress DeprecatedMethod */ $column->name($info['field']); - $column->allowNull($info['null'] === 'YES'); - $column->primaryKey(str_contains($info['key'], 'PRI')); + $column->notNull($info['null'] !== 'YES'); + $column->primaryKey($info['key'] === 'PRI'); $column->autoIncrement(stripos($info['extra'], 'auto_increment') !== false); + $column->unique($info['key'] === 'UNI'); $column->comment($info['comment']); $column->dbType($dbType); diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index ef5aedc8..218df99f 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -21,11 +21,10 @@ public static function columns(): array 'dbType' => 'int(11)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 11, - 'precision' => 11, 'scale' => null, 'defaultValue' => null, ], @@ -34,11 +33,10 @@ public static function columns(): array 'dbType' => 'int(11)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 11, - 'precision' => 11, 'scale' => null, 'defaultValue' => 1, ], @@ -47,11 +45,10 @@ public static function columns(): array 'dbType' => 'bigint(20) unsigned', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 20, - 'precision' => 20, 'scale' => null, 'unsigned' => true, 'defaultValue' => '12345678901234567890', @@ -61,11 +58,10 @@ public static function columns(): array 'dbType' => 'tinyint(3)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 3, - 'precision' => 3, 'scale' => null, 'defaultValue' => 1, ], @@ -74,11 +70,10 @@ public static function columns(): array 'dbType' => 'smallint(1)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => 1, 'scale' => null, 'defaultValue' => 1, ], @@ -87,11 +82,10 @@ public static function columns(): array 'dbType' => 'char(100)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 100, - 'precision' => 100, 'scale' => null, 'defaultValue' => null, ], @@ -100,11 +94,10 @@ public static function columns(): array 'dbType' => 'varchar(100)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 100, - 'precision' => 100, 'scale' => null, 'defaultValue' => 'something', ], @@ -113,11 +106,10 @@ public static function columns(): array 'dbType' => 'text', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -126,11 +118,10 @@ public static function columns(): array 'dbType' => "enum('a','B','c,D')", 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => ['a', 'B', 'c,D'], 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -139,11 +130,10 @@ public static function columns(): array 'dbType' => 'double(4,3)', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 4, - 'precision' => 4, 'scale' => 3, 'defaultValue' => null, ], @@ -152,11 +142,10 @@ public static function columns(): array 'dbType' => 'double', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => 1.23, ], @@ -165,11 +154,10 @@ public static function columns(): array 'dbType' => 'blob', 'phpType' => 'mixed', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -178,11 +166,10 @@ public static function columns(): array 'dbType' => 'decimal(5,2)', 'phpType' => 'float', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 5, - 'precision' => 5, 'scale' => 2, 'defaultValue' => 33.22, ], @@ -191,11 +178,10 @@ public static function columns(): array 'dbType' => 'timestamp', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => '2002-01-01 00:00:00', ], @@ -204,11 +190,10 @@ public static function columns(): array 'dbType' => 'bit(1)', 'phpType' => 'bool', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => 1, 'scale' => null, 'defaultValue' => null, ], @@ -217,11 +202,10 @@ public static function columns(): array 'dbType' => 'tinyint(1)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => 1, 'scale' => null, 'defaultValue' => 2, ], @@ -230,11 +214,10 @@ public static function columns(): array 'dbType' => 'timestamp', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => new Expression('CURRENT_TIMESTAMP'), ], @@ -243,11 +226,10 @@ public static function columns(): array 'dbType' => 'bit(8)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 8, - 'precision' => 8, 'scale' => null, 'defaultValue' => 130, // b'10000010' ], @@ -256,11 +238,10 @@ public static function columns(): array 'dbType' => 'json', 'phpType' => 'mixed', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => null, - 'precision' => null, 'scale' => null, 'defaultValue' => null, ], @@ -274,11 +255,10 @@ public static function columns(): array 'dbType' => 'int(11)', 'phpType' => 'int', 'primaryKey' => true, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => true, 'enumValues' => null, 'size' => 11, - 'precision' => 11, 'scale' => null, 'defaultValue' => null, ], @@ -287,17 +267,86 @@ public static function columns(): array 'dbType' => 'varchar(255)', 'phpType' => 'string', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 255, - 'precision' => 255, 'scale' => null, 'defaultValue' => null, ], ], 'animal', ], + [ + [ + 'C_id' => [ + 'type' => 'integer', + 'dbType' => 'int(11)', + 'phpType' => 'int', + 'primaryKey' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'enumValues' => null, + 'size' => 11, + 'scale' => null, + 'defaultValue' => null, + 'unique' => false, + ], + 'C_not_null' => [ + 'type' => 'integer', + 'dbType' => 'int(11)', + 'phpType' => 'int', + 'primaryKey' => false, + 'notNull' => true, + 'autoIncrement' => false, + 'enumValues' => null, + 'size' => 11, + 'scale' => null, + 'defaultValue' => null, + 'unique' => false, + ], + 'C_check' => [ + 'type' => 'string', + 'dbType' => 'varchar(255)', + 'phpType' => 'string', + 'primaryKey' => false, + 'notNull' => false, + 'autoIncrement' => false, + 'enumValues' => null, + 'size' => 255, + 'scale' => null, + 'defaultValue' => null, + 'unique' => false, + ], + 'C_unique' => [ + 'type' => 'integer', + 'dbType' => 'int(11)', + 'phpType' => 'int', + 'primaryKey' => false, + 'notNull' => true, + 'autoIncrement' => false, + 'enumValues' => null, + 'size' => 11, + 'scale' => null, + 'defaultValue' => null, + 'unique' => true, + ], + 'C_default' => [ + 'type' => 'integer', + 'dbType' => 'int(11)', + 'phpType' => 'int', + 'primaryKey' => false, + 'notNull' => true, + 'autoIncrement' => false, + 'enumValues' => null, + 'size' => 11, + 'scale' => null, + 'defaultValue' => 0, + 'unique' => false, + ], + ], + 'T_constraints_1', + ], ]; } @@ -311,11 +360,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(1)', 'phpType' => 'bool', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => 1, 'scale' => null, 'defaultValue' => false, ], @@ -324,11 +372,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(1)', 'phpType' => 'bool', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 1, - 'precision' => 1, 'scale' => null, 'defaultValue' => true, ], @@ -337,11 +384,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(32)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 32, - 'precision' => 32, 'scale' => null, 'defaultValue' => null, ], @@ -350,11 +396,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(32)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 32, - 'precision' => 32, 'scale' => null, 'defaultValue' => 130, ], @@ -363,11 +408,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(64)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => false, + 'notNull' => true, 'autoIncrement' => false, 'enumValues' => null, 'size' => 64, - 'precision' => 64, 'scale' => null, 'defaultValue' => null, ], @@ -376,11 +420,10 @@ public static function columnsTypeBit(): array 'dbType' => 'bit(64)', 'phpType' => 'int', 'primaryKey' => false, - 'allowNull' => true, + 'notNull' => false, 'autoIncrement' => false, 'enumValues' => null, 'size' => 64, - 'precision' => 64, 'scale' => null, 'defaultValue' => 130, ], diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 7757d109..6704fb56 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -119,33 +119,27 @@ public function testColumnSchema(array $columns, string $tableName): void // int_col Mysql 8.0.17+. $columns['int_col']['dbType'] = 'int'; $columns['int_col']['size'] = null; - $columns['int_col']['precision'] = null; // int_col2 Mysql 8.0.17+. $columns['int_col2']['dbType'] = 'int'; $columns['int_col2']['size'] = null; - $columns['int_col2']['precision'] = null; // bigunsigned_col Mysql 8.0.17+. $columns['bigunsigned_col']['dbType'] = 'bigint unsigned'; $columns['bigunsigned_col']['size'] = null; - $columns['bigunsigned_col']['precision'] = null; // tinyint_col Mysql 8.0.17+. $columns['tinyint_col']['dbType'] = 'tinyint'; $columns['tinyint_col']['size'] = null; - $columns['tinyint_col']['precision'] = null; // smallint_col Mysql 8.0.17+. $columns['smallint_col']['dbType'] = 'smallint'; $columns['smallint_col']['size'] = null; - $columns['smallint_col']['precision'] = null; } if ($tableName === 'animal') { $columns['id']['dbType'] = 'int'; $columns['id']['size'] = null; - $columns['id']['precision'] = null; } }