77use bizley \migration \Schema ;
88
99use function in_array ;
10+ use function version_compare ;
1011
1112final class TinyIntegerColumn extends Column implements ColumnInterface
1213{
1314 /** @var array<string> Schemas using length for this column */
14- private $ lengthSchemas = [
15- Schema::MYSQL ,
16- Schema::OCI ,
17- ];
15+ private $ lengthSchemas = [Schema::OCI ];
16+
17+ /**
18+ * Checks if schema supports length for this column.
19+ * In case of MySQL the engine version must be lower than 8.0.17.
20+ * @param string|null $schema
21+ * @param string|null $engineVersion
22+ * @return bool
23+ */
24+ private function isSchemaLengthSupporting (?string $ schema , ?string $ engineVersion ): bool
25+ {
26+ if ($ engineVersion && $ schema === Schema::MYSQL && version_compare ($ engineVersion , '8.0.17 ' , '< ' )) {
27+ return true ;
28+ }
29+
30+ return in_array ($ schema , $ this ->lengthSchemas , true );
31+ }
1832
1933 /**
2034 * Returns length of the column.
@@ -24,7 +38,16 @@ final class TinyIntegerColumn extends Column implements ColumnInterface
2438 */
2539 public function getLength (string $ schema = null , string $ engineVersion = null )
2640 {
27- return in_array ($ schema , $ this ->lengthSchemas , true ) ? $ this ->getSize () : null ;
41+ $ size = $ this ->getSize ();
42+ if ($ this ->isSchemaLengthSupporting ($ schema , $ engineVersion )) {
43+ return $ size ;
44+ }
45+ if ($ schema === Schema::MYSQL && (string )$ size === '1 ' ) {
46+ // MySQL 8.0.17+ allows tiny integer to be set with size 1 for boolean columns
47+ return $ size ;
48+ }
49+
50+ return null ;
2851 }
2952
3053 /**
@@ -35,7 +58,10 @@ public function getLength(string $schema = null, string $engineVersion = null)
3558 */
3659 public function setLength ($ value , string $ schema = null , string $ engineVersion = null ): void
3760 {
38- if (in_array ($ schema , $ this ->lengthSchemas , true )) {
61+ if (
62+ $ this ->isSchemaLengthSupporting ($ schema , $ engineVersion )
63+ || ($ schema === Schema::MYSQL && (string )$ value === '1 ' )
64+ ) {
3965 $ this ->setSize ($ value );
4066 $ this ->setPrecision ($ value );
4167 }
0 commit comments