From 866199675bc9d8368aa430a4c7561c0dfbb6f92d Mon Sep 17 00:00:00 2001 From: Remo Liebi Date: Thu, 15 Feb 2024 16:19:51 +0100 Subject: [PATCH] Refactor: Remove 'o_' prefix from EcommerceFramework table columns 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. --- src/Migrations/Version20240215093348.php | 79 ++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/Migrations/Version20240215093348.php diff --git a/src/Migrations/Version20240215093348.php b/src/Migrations/Version20240215093348.php new file mode 100644 index 000000000..436749b52 --- /dev/null +++ b/src/Migrations/Version20240215093348.php @@ -0,0 +1,79 @@ +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', + ) + ); + } +}