Skip to content

Commit

Permalink
Merge PR #46 branch '45-setting-dbtype-to-enum-is-not-totally-correct'
Browse files Browse the repository at this point in the history
* 45-setting-dbtype-to-enum-is-not-totally-correct:
  Already have a task in issue
  Present in cebe#122 and #10
  Remove redundant code
  Refactor
  Refactor
  Refactor
  Fix issue of inconsistent return value
  Create PR
  • Loading branch information
cebe committed Dec 11, 2024
2 parents 5b785c4 + a0b3156 commit ccd2f28
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
15 changes: 4 additions & 11 deletions src/lib/ColumnToCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,18 @@ 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())) {
$code .= $this->rawParts['position'] ? ' ' . $this->rawParts['position'] : '';
$code .= $this->rawParts['comment'] ? ' '.$this->rawParts['comment'] : '';
}

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 @@ -131,13 +128,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

0 comments on commit ccd2f28

Please sign in to comment.