|
3 | 3 | namespace Blueprint\Commands;
|
4 | 4 |
|
5 | 5 | use Blueprint\Blueprint;
|
| 6 | +use Blueprint\EnumType; |
| 7 | +use Doctrine\DBAL\Types\Type; |
6 | 8 | use Illuminate\Console\Command;
|
7 | 9 | use Illuminate\Database\Eloquent\Model;
|
8 | 10 | use Illuminate\Filesystem\Filesystem;
|
@@ -119,13 +121,35 @@ private function extractColumns(Model $model)
|
119 | 121 | $table = $model->getConnection()->getTablePrefix() . $model->getTable();
|
120 | 122 | $schema = $model->getConnection()->getDoctrineSchemaManager();
|
121 | 123 |
|
| 124 | + if (!Type::hasType('enum')) { |
| 125 | + Type::addType('enum', EnumType::class); |
| 126 | + $databasePlatform = $schema->getDatabasePlatform(); |
| 127 | + $databasePlatform->registerDoctrineTypeMapping('enum', 'enum'); |
| 128 | + } |
| 129 | + |
122 | 130 | $database = null;
|
123 | 131 | if (strpos($table, '.')) {
|
124 |
| - list($database, $table) = explode('.', $table); |
| 132 | + [$database, $table] = explode('.', $table); |
125 | 133 | }
|
126 | 134 |
|
127 | 135 | $columns = $schema->listTableColumns($table, $database);
|
128 | 136 |
|
| 137 | + $uses_enums = collect($columns)->contains(function ($column) { |
| 138 | + return $column->getType() instanceof \Blueprint\EnumType; |
| 139 | + }); |
| 140 | + |
| 141 | + if ($uses_enums) { |
| 142 | + $definitions = $model->getConnection()->getDoctrineConnection()->fetchAll($schema->getDatabasePlatform()->getListTableColumnsSQL($table, $database)); |
| 143 | + |
| 144 | + collect($columns)->filter(function ($column) { |
| 145 | + return $column->getType() instanceof \Blueprint\EnumType; |
| 146 | + })->each(function (&$column, $key) use ($definitions) { |
| 147 | + $definition = collect($definitions)->where('Field', $key)->first(); |
| 148 | + |
| 149 | + $column->options = \Blueprint\EnumType::extractOptions($definition['Type']); |
| 150 | + }); |
| 151 | + } |
| 152 | + |
129 | 153 | return $columns;
|
130 | 154 | }
|
131 | 155 |
|
@@ -160,9 +184,11 @@ public static function columns(\Doctrine\DBAL\Schema\Column $column, string $key
|
160 | 184 | if ($column->getLength() > 65535) {
|
161 | 185 | $type = 'longtext';
|
162 | 186 | }
|
| 187 | + } elseif ($type === 'enum' && !empty($column->options)) { |
| 188 | + $type .= ':' . implode(',', $column->options); |
163 | 189 | }
|
164 | 190 |
|
165 |
| - // TODO: enums, guid/uuid |
| 191 | + // TODO: guid/uuid |
166 | 192 |
|
167 | 193 | $attributes[] = $type;
|
168 | 194 |
|
@@ -201,6 +227,7 @@ private static function translations(string $type)
|
201 | 227 | 'datetimetz' => 'datetimetz',
|
202 | 228 | 'datetimetz_immutable' => 'datetimetz',
|
203 | 229 | 'decimal' => 'decimal',
|
| 230 | + 'enum' => 'enum', |
204 | 231 | 'float' => 'float',
|
205 | 232 | 'guid' => 'string',
|
206 | 233 | 'integer' => 'integer',
|
|
0 commit comments