|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | + |
| 24 | +namespace App\Command\Migrations; |
| 25 | + |
| 26 | +use App\DataTables\Helpers\ColumnSortHelper; |
| 27 | +use App\Entity\Parts\Manufacturer; |
| 28 | +use App\Services\ImportExportSystem\PartKeeprImporter\PKImportHelper; |
| 29 | +use Doctrine\ORM\EntityManagerInterface; |
| 30 | + |
| 31 | +use Doctrine\ORM\Id\AssignedGenerator; |
| 32 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 33 | +use Doctrine\Persistence\ManagerRegistry; |
| 34 | +use Doctrine\Persistence\ObjectManager; |
| 35 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 36 | +use Symfony\Component\Console\Command\Command; |
| 37 | +use Symfony\Component\Console\Input\InputInterface; |
| 38 | +use Symfony\Component\Console\Output\OutputInterface; |
| 39 | + |
| 40 | +#[AsCommand('partdb:migrate-db', 'Migrate the database to a different platform')] |
| 41 | +class DBMigrationCommand extends Command |
| 42 | +{ |
| 43 | + private readonly EntityManagerInterface $sourceEM; |
| 44 | + private readonly EntityManagerInterface $targetEM; |
| 45 | + |
| 46 | + public function __construct(private readonly ManagerRegistry $managerRegistry, |
| 47 | + private readonly PKImportHelper $importHelper, |
| 48 | + ) |
| 49 | + { |
| 50 | + $this->sourceEM = $this->managerRegistry->getManager('migration_source'); |
| 51 | + $this->targetEM = $this->managerRegistry->getManager('default'); |
| 52 | + |
| 53 | + parent::__construct(); |
| 54 | + } |
| 55 | + |
| 56 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 57 | + { |
| 58 | + // Example migration logic (to be replaced with actual migration code) |
| 59 | + $output->writeln('Starting database migration...'); |
| 60 | + |
| 61 | + //Disable all event listeners on target EM to avoid unwanted side effects |
| 62 | + $eventManager = $this->targetEM->getEventManager(); |
| 63 | + foreach ($eventManager->getAllListeners() as $event => $listeners) { |
| 64 | + foreach ($listeners as $listener) { |
| 65 | + $eventManager->removeEventListener($event, $listener); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $output->writeln('Clear target database...'); |
| 70 | + $this->importHelper->purgeDatabaseForImport($this->targetEM, ['internal', 'migration_versions']); |
| 71 | + |
| 72 | + $metadata = $this->targetEM->getMetadataFactory()->getAllMetadata(); |
| 73 | + |
| 74 | + //First we modify each entity metadata to have an persist cascade on all relations |
| 75 | + foreach ($metadata as $metadatum) { |
| 76 | + $entityClass = $metadatum->getName(); |
| 77 | + $output->writeln('Modifying cascade and ID settings for entity: ' . $entityClass); |
| 78 | + |
| 79 | + foreach ($metadatum->getAssociationNames() as $fieldName) { |
| 80 | + $mapping = $metadatum->getAssociationMapping($fieldName); |
| 81 | + $mapping->cascade = array_unique(array_merge($mapping->cascade, ['persist'])); |
| 82 | + |
| 83 | + $metadatum->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); |
| 84 | + $metadatum->setIdGenerator(new AssignedGenerator()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + //Afterwards we migrate all entities |
| 90 | + foreach ($metadata as $metadatum) { |
| 91 | + //skip all superclasses |
| 92 | + if ($metadatum->isMappedSuperclass) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + $entityClass = $metadatum->getName(); |
| 97 | + |
| 98 | + $output->writeln('Migrating entity: ' . $entityClass); |
| 99 | + |
| 100 | + $repo = $this->sourceEM->getRepository($entityClass); |
| 101 | + $items = $repo->findAll(); |
| 102 | + foreach ($items as $item) { |
| 103 | + $this->targetEM->persist($item); |
| 104 | + } |
| 105 | + $this->targetEM->flush(); |
| 106 | + } |
| 107 | + |
| 108 | + //Migrate all manufacturers from source to target |
| 109 | + /*$manufacturerRepo = $this->sourceEM->getRepository(Manufacturer::class); |
| 110 | + $manufacturers = $manufacturerRepo->findAll(); |
| 111 | + foreach ($manufacturers as $manufacturer) { |
| 112 | + $this->targetEM->persist($manufacturer); |
| 113 | + } |
| 114 | + $this->targetEM->flush(); |
| 115 | + */ |
| 116 | + |
| 117 | + $output->writeln('Database migration completed successfully.'); |
| 118 | + |
| 119 | + return Command::SUCCESS; |
| 120 | + } |
| 121 | +} |
0 commit comments