Skip to content

Commit

Permalink
Refactor: Remove 'o_' prefix from EcommerceFramework table columns
Browse files Browse the repository at this point in the history
This commit removes the 'o_' prefix from columns in the 'ecommerceframework_productindex' table to standardize column naming conventions across the EcommerceFrameworkBundle. The update includes both the up and down migration methods to ensure seamless transition and rollback capability.
  • Loading branch information
rliebi committed Mar 16, 2024
1 parent 95e2f7e commit 8661996
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/Migrations/Version20240215093348.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Pimcore\Bundle\EcommerceFrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\AbstractMigration;

final class Version20240215093348 extends AbstractMigration
{
public function getDescription(): string
{
return 'remove o_ prefix from table names';
}

public function up(Schema $schema): void
{
$table = $schema->getTable('ecommerceframework_productindex');

foreach ($this->getPrefixedColumnNames() as $columnNamePrefixed) {
if ($table->hasColumn($columnNamePrefixed)) {
$columnNamePlain = ltrim($columnNamePrefixed, 'o_');
$this->write(sprintf('Changing column [%s] to [%s] in table %s', $columnNamePrefixed, $columnNamePlain, $table->getName()));
$this->renameColumn($columnNamePrefixed, $columnNamePlain, $table);
} else {
$this->write(sprintf('Column [%s] does not exist in table %s', $columnNamePrefixed, $table->getName()));
}
}
}

public function down(Schema $schema): void
{
$table = $schema->getTable('ecommerceframework_productindex');

foreach ($this->getPrefixedColumnNames() as $columnNamePrefixed) {
$columnNamePlain = ltrim($columnNamePrefixed, 'o_');

if ($table->hasColumn($columnNamePlain)) {
$this->renameColumn($columnNamePlain, $columnNamePrefixed, $table);
}
}
}

/**
* @return string[]
*/
private function getPrefixedColumnNames(): array
{
return [
'o_id',
'o_virtualProductId',
'o_virtualProductActive',
'o_classId',
'o_parentId',
'o_type',
];
}

/**
* @throws \Doctrine\DBAL\Schema\SchemaException
*/
private function renameColumn(string $from, string $to, Table $table): void
{
$this->addSql(
sprintf(
'ALTER TABLE ecommerceframework_productindex CHANGE %s %s %s %s;',
$from,
$to,
$table->getColumn($from)->getType()->getSQLDeclaration(
$table->getColumn($from)->toArray(),
$this->platform
),
$table->getColumn($from)->getNotnull() ? 'NOT NULL' : 'NULL',
)
);
}
}

0 comments on commit 8661996

Please sign in to comment.