Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 7754878

Browse files
committed
Fix-Enum on MariaDb
1 parent a934859 commit 7754878

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ $this->alterColumn('{{%company}}', 'name', $this->string(128)->notNull());
250250

251251
### Handling of `NOT NULL` constraints (#required, #nullable)
252252

253-
e.g. attribute = 'alpha'.
253+
e.g. attribute = 'my_property'.
254254

255255
1) If you define attribute neither "required" nor via "nullable", then it is by default:
256256
ALLOW 'NOT NULL'
257257
```yaml
258258
test_table:
259259
required:
260260
properties:
261-
alpha:
261+
my_property:
262262
type: string
263263
```
264264
@@ -267,9 +267,9 @@ e.g. attribute = 'alpha'.
267267
```yaml
268268
test_table:
269269
required:
270-
- alpha
270+
- my_property
271271
properties:
272-
alpha:
272+
my_property:
273273
type: string
274274
```
275275
@@ -278,9 +278,9 @@ e.g. attribute = 'alpha'.
278278
```yaml
279279
test_table:
280280
required:
281-
- alpha
281+
- my_property
282282
properties:
283-
alpha:
283+
my_property:
284284
type: string
285285
nullable: true
286286
```
@@ -290,11 +290,24 @@ FORBIDDEN 'NOT NULL'
290290
test_table:
291291
required:
292292
properties:
293-
alpha:
293+
my_property:
294294
type: string
295295
nullable: false
296296
```
297297
298+
### Handling of `enum` (#enum, #MariaDb)
299+
It work on MariaDb.
300+
301+
```yaml
302+
test_table:
303+
properties:
304+
my_property:
305+
enum:
306+
- one
307+
- two
308+
- three
309+
```
310+
298311
## Screenshots
299312

300313
Gii Generator Form:

src/lib/ColumnToCode.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function isJson():bool
147147

148148
public function isEnum():bool
149149
{
150-
return StringHelper::startsWith($this->column->dbType, 'enum');
150+
return !empty($this->column->enumValues);
151151
}
152152

153153
public static function escapeQuotes(string $str):string
@@ -208,11 +208,7 @@ private function resolve():void
208208
if ($dbType === 'varchar') {
209209
$type = $dbType = 'string';
210210
}
211-
if ($this->fromDb === true) {
212-
$this->isBuiltinType = isset((new ColumnSchemaBuilder(''))->categoryMap[$type]);
213-
} else {
214-
$this->isBuiltinType = isset((new ColumnSchemaBuilder(''))->categoryMap[$dbType]);
215-
}
211+
$this->isBuiltinType = $this->getIsBuiltinType($type, $dbType);
216212
$fluentSize = $this->column->size ? '(' . $this->column->size . ')' : '()';
217213
$rawSize = $this->column->size ? '(' . $this->column->size . ')' : '';
218214
$this->rawParts['nullable'] = $this->column->allowNull ? 'NULL' : 'NOT NULL';
@@ -235,6 +231,23 @@ private function resolve():void
235231
$this->resolveDefaultValue();
236232
}
237233

234+
/**
235+
* @param $type
236+
* @param $dbType
237+
* @return bool
238+
*/
239+
private function getIsBuiltinType($type, $dbType)
240+
{
241+
if ($this->isEnum() && $this->isMariaDb()){
242+
return false;
243+
}
244+
if ($this->fromDb === true){
245+
return isset((new ColumnSchemaBuilder(''))->categoryMap[$type]);
246+
} else {
247+
return isset((new ColumnSchemaBuilder(''))->categoryMap[$dbType]);
248+
}
249+
}
250+
238251
private function resolveEnumType():void
239252
{
240253
if ($this->isPostgres()) {
@@ -314,10 +327,18 @@ private function resolveDefaultValue():void
314327
private function isDefaultAllowed():bool
315328
{
316329
$type = strtolower($this->column->dbType);
317-
if ($type === 'tsvector') {
318-
return false;
330+
switch ($type){
331+
case 'tsvector':
332+
return false;
333+
case 'blob':
334+
case 'geometry':
335+
case 'text':
336+
case 'json':
337+
return ($this->isMysql() && !$this->isMariaDb()) === false;
338+
case 'enum':
339+
default:
340+
return true;
319341
}
320-
return !($this->isMysql() && !$this->isMariaDb() && in_array($type, ['blob', 'geometry', 'text', 'json']));
321342
}
322343

323344
private function typeWithoutSize(string $type):string

src/lib/items/DbModel.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ public function getEnumAttributes():array
139139
return array_filter(
140140
$this->attributes,
141141
static function (Attribute $attribute) {
142-
return !$attribute->isVirtual && StringHelper::startsWith($attribute->dbType, 'enum')
143-
&& !empty($attribute->enumValues);
142+
return !$attribute->isVirtual && !empty($attribute->enumValues);
144143
}
145144
);
146145
}

src/lib/migrations/BaseMigrationBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ function (string $unknownColumn) {
205205
$current->type = 'enum';
206206
$current->dbType = 'enum';
207207
}
208+
if (!empty($desired->enumValues)) {
209+
$desired->type = 'enum';
210+
$desired->dbType = 'enum';
211+
}
208212
$changedAttributes = $this->compareColumns($current, $desired);
209213
if (empty($changedAttributes)) {
210214
continue;

src/lib/migrations/MysqlMigrationBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function compareColumns(ColumnSchema $current, ColumnSchema $desired):
6666

6767
protected function createEnumMigrations():void
6868
{
69-
// Postgres only case
69+
// execute via default
7070
}
7171

7272
protected function isDbDefaultSize(ColumnSchema $current):bool

0 commit comments

Comments
 (0)