Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve: Setting dbType to enum is not totally correct #45 #46

Merged
merged 9 commits into from
Dec 11, 2024
Merged
14 changes: 4 additions & 10 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,16 @@ public function getCode(bool $quoted = false):string
array_unshift($parts, '$this');
return implode('->', array_filter(array_map('trim', $parts)));
}
if ($this->rawParts['default'] === null) {
$default = '';
} elseif (ApiGenerator::isPostgres() && $this->isEnum()) {
$default =
$this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
} else {
$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';

if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
}

$default = $this->rawParts['default'] !== null ? ' DEFAULT ' . trim($this->rawParts['default']) : '';
$code = $this->rawParts['type'] . ' ' . $this->rawParts['nullable'] . $default;
if ((ApiGenerator::isMysql() || ApiGenerator::isMariaDb()) && $this->rawParts['position']) {
$code .= ' ' . $this->rawParts['position'];
}
if (ApiGenerator::isPostgres() && $this->alterByXDbType) {
return $quoted ? VarDumper::export($this->rawParts['type']) : $this->rawParts['type'];
}
return $quoted ? VarDumper::export($code) : $code;
}

Expand Down
8 changes: 2 additions & 6 deletions src/lib/migrations/MysqlMigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ protected function buildColumnChanges(ColumnSchema $current, ColumnSchema $desir
foreach ($changed as $attr) {
$newColumn->$attr = $desired->$attr;
}
if (static::isEnum($newColumn)) {
$newColumn->dbType = 'enum'; // TODO this is concretely not correct
}
$this->migration->addUpCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $newColumn, $positionDesired))
->addDownCode($this->recordBuilder->alterColumn($this->model->getTableAlias(), $current, $positionCurrent));
}
Expand Down Expand Up @@ -127,13 +124,12 @@ protected function findTableIndexes():array

public static function getColumnSchemaBuilderClass(): string
{
if (ApiGenerator::isMysql()) {
return \yii\db\mysql\ColumnSchemaBuilder::class;
} elseif (ApiGenerator::isMariaDb()) {
if (ApiGenerator::isMariaDb()) {
return \SamIT\Yii2\MariaDb\ColumnSchemaBuilder::class;
} else {
throw new \Exception('Unknown database');
}
return \yii\db\mysql\ColumnSchemaBuilder::class;
}

public function modifyCurrent(ColumnSchema $current): void
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa
$this->deleteTables();
}

// TODO ENH enum change is more work than just changing the eunm values. And for PgSQL it is even more
// public function testEnumValuesChange()
// {
// $this->deleteTables();
Expand Down Expand Up @@ -201,7 +200,6 @@ public function testChangeToAndFromEnum() // edit enum to string and vice versa

// public function testChangeEnumValues()
// {
// // TODO
// // add a value to list
// // fix a typo in a enum value present in existing list
// // remove a value from list
Expand Down
Loading