2020use yii \db \Connection ;
2121use yii \db \Constraint ;
2222use yii \db \cubrid \Schema as CubridSchema ;
23- use yii \db \ForeignKeyConstraint ;
24- use yii \db \IndexConstraint ;
2523use yii \db \mssql \Schema as MssqlSchema ;
2624use yii \db \mysql \Schema as MysqlSchema ;
2725use yii \db \oci \Schema as OciSchema ;
2826use yii \db \pgsql \Schema as PgsqlSchema ;
2927use yii \db \sqlite \Schema as SqliteSchema ;
3028use yii \db \TableSchema ;
3129
30+ use function count ;
31+ use function in_array ;
32+
3233final class TableMapper implements TableMapperInterface
3334{
3435 /** @var Connection */
@@ -86,7 +87,6 @@ private function getForeignKeys(string $table): array
8687 $ schema = $ this ->db ->getSchema ();
8788 $ tableForeignKeys = $ schema ->getTableForeignKeys ($ table , true );
8889
89- /** @var ForeignKeyConstraint $foreignKey */
9090 foreach ($ tableForeignKeys as $ foreignKey ) {
9191 $ mappedForeignKey = new ForeignKey ();
9292 $ mappedForeignKey ->setTableName ($ table );
@@ -116,7 +116,6 @@ private function getIndexes(string $table): array
116116 $ schema = $ this ->db ->getSchema ();
117117 $ tableIndexes = $ schema ->getTableIndexes ($ table , true );
118118
119- /** @var IndexConstraint $index */
120119 foreach ($ tableIndexes as $ index ) {
121120 if ($ index ->isPrimary === false ) {
122121 $ mappedIndex = new Index ();
@@ -159,6 +158,7 @@ private function getPrimaryKey(string $table): ?PrimaryKeyInterface
159158 * @param string $table
160159 * @param array<IndexInterface> $indexes
161160 * @return array<string, ColumnInterface>
161+ * @throws NotSupportedException
162162 */
163163 private function getColumns (string $ table , array $ indexes = []): array
164164 {
@@ -167,6 +167,8 @@ private function getColumns(string $table, array $indexes = []): array
167167 if ($ tableSchema === null ) {
168168 return [];
169169 }
170+ $ schema = $ this ->getSchemaType ();
171+ $ engineVersion = $ this ->getEngineVersion ();
170172
171173 foreach ($ tableSchema ->columns as $ column ) {
172174 $ isUnique = false ;
@@ -182,9 +184,17 @@ private function getColumns(string $table, array $indexes = []): array
182184
183185 $ mappedColumn = ColumnFactory::build ($ column ->type );
184186 $ mappedColumn ->setName ($ column ->name );
185- $ mappedColumn ->setSize ($ column ->size );
186- $ mappedColumn ->setPrecision ($ column ->precision );
187- $ mappedColumn ->setScale ($ column ->scale );
187+ if ($ column ->size === null && $ column ->precision === null && $ column ->scale === null ) {
188+ $ mappedColumn ->setLength (
189+ Schema::getDefaultLength ($ schema , $ mappedColumn ->getType (), $ engineVersion ),
190+ $ schema ,
191+ $ engineVersion
192+ );
193+ } else {
194+ $ mappedColumn ->setSize ($ column ->size );
195+ $ mappedColumn ->setPrecision ($ column ->precision );
196+ $ mappedColumn ->setScale ($ column ->scale );
197+ }
188198 $ mappedColumn ->setNotNull ($ column ->allowNull ? null : true );
189199 $ mappedColumn ->setUnique ($ isUnique );
190200 $ mappedColumn ->setDefault ($ column ->defaultValue );
0 commit comments