Compare commits

..

No commits in common. "ddbfc87ce16c86977c0355d99cf5f7aca1726baa" and "00b35e3306579ef42a45912aedc1507fe6678a2d" have entirely different histories.

3 changed files with 69 additions and 133 deletions

View file

@ -1,61 +1,56 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
# Required for DAMA doctrine test bundle
use_savepoints: true
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
# 1. GLOBAL SETTINGS (Apply to all connections)
types:
# UTC datetimes
datetime:
class: App\Doctrine\Types\UTCDateTimeType
date:
class: App\Doctrine\Types\UTCDateTimeType
datetime: App\Doctrine\Types\UTCDateTimeType
date: App\Doctrine\Types\UTCDateTimeType
datetime_immutable: App\Doctrine\Types\UTCDateTimeImmutableType
date_immutable: App\Doctrine\Types\UTCDateTimeImmutableType
big_decimal: App\Doctrine\Types\BigDecimalType
tinyint: App\Doctrine\Types\TinyIntType
datetime_immutable:
class: App\Doctrine\Types\UTCDateTimeImmutableType
date_immutable:
class: App\Doctrine\Types\UTCDateTimeImmutableType
connections:
default:
use_savepoints: true
schema_filter: ~^(?!internal)~
url: '%env(resolve:DATABASE_URL)%'
big_decimal:
class: App\Doctrine\Types\BigDecimalType
tinyint:
class: App\Doctrine\Types\TinyIntType
schema_filter: ~^(?!internal)~
# Only enable this when needed
profiling_collect_backtrace: false
migration_source:
use_savepoints: true
schema_filter: ~^(?!internal)~
url: '%env(resolve:DB_MIGRATION_SOURCE)%'
orm:
auto_generate_proxy_classes: true
enable_lazy_ghost_objects: true
report_fields_where_declared: true
validate_xml_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
identity_generation_preferences:
Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
auto_mapping: true
controller_resolver:
auto_mapping: true
mappings:
App:
type: attribute
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
dql:
string_functions:
regexp: App\Doctrine\Functions\Regexp
field: DoctrineExtensions\Query\Mysql\Field
field2: App\Doctrine\Functions\Field2
natsort: App\Doctrine\Functions\Natsort
array_position: App\Doctrine\Functions\ArrayPosition
ilike: App\Doctrine\Functions\ILike
entity_managers:
default: &common_orm_settings
report_fields_where_declared: true
validate_xml_mapping: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
identity_generation_preferences:
Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
mappings:
App:
type: attribute
is_bundle: false
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
dql:
string_functions:
regexp: App\Doctrine\Functions\Regexp
field: DoctrineExtensions\Query\Mysql\Field
field2: App\Doctrine\Functions\Field2
natsort: App\Doctrine\Functions\Natsort
array_position: App\Doctrine\Functions\ArrayPosition
ilike: App\Doctrine\Functions\ILike
connection: default
migration_source:
<<: *common_orm_settings
connection: migration_source
when@test:
doctrine:

View file

@ -1622,7 +1622,6 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
* flysystem?: array{
* filesystem_service: scalar|null|Param,
* },
* asset_mapper?: array<mixed>,
* chain?: array{
* loaders: list<scalar|null|Param>,
* },
@ -2302,13 +2301,11 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
* controllers_json?: scalar|null|Param, // Default: "%kernel.project_dir%/assets/controllers.json"
* }
* @psalm-type UxTranslatorConfig = array{
* dump_directory?: scalar|null|Param, // The directory where translations and TypeScript types are dumped. // Default: "%kernel.project_dir%/var/translations"
* dump_typescript?: bool|Param, // Control whether TypeScript types are dumped alongside translations. Disable this if you do not use TypeScript (e.g. in production when using AssetMapper). // Default: true
* dump_directory?: scalar|null|Param, // Default: "%kernel.project_dir%/var/translations"
* domains?: string|array{ // List of domains to include/exclude from the generated translations. Prefix with a `!` to exclude a domain.
* type?: scalar|null|Param,
* elements?: list<scalar|null|Param>,
* },
* keys_patterns?: list<scalar|null|Param>,
* }
* @psalm-type DompdfFontLoaderConfig = array{
* autodiscovery?: bool|array{

View file

@ -23,75 +23,43 @@ declare(strict_types=1);
namespace App\Command\Migrations;
use App\DataTables\Helpers\ColumnSortHelper;
use App\Entity\Parts\Manufacturer;
use App\Services\ImportExportSystem\PartKeeprImporter\PKImportHelper;
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\ORM\Id\AssignedGenerator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
#[AsCommand('partdb:migrations:convert-db-platform', 'Convert the database to a different platform')]
class DBPlatformConvertCommand extends Command
#[AsCommand('partdb:migrate-db', 'Migrate the database to a different platform')]
class DBMigrationCommand extends Command
{
private readonly EntityManagerInterface $sourceEM;
private readonly EntityManagerInterface $targetEM;
public function __construct(
private readonly EntityManagerInterface $targetEM,
public function __construct(private readonly ManagerRegistry $managerRegistry,
private readonly PKImportHelper $importHelper,
private readonly DependencyFactory $dependencyFactory,
#[Autowire('%kernel.project_dir%')]
private readonly string $kernelProjectDir,
)
{
parent::__construct();
}
$this->sourceEM = $this->managerRegistry->getManager('migration_source');
$this->targetEM = $this->managerRegistry->getManager('default');
public function configure(): void
{
$this
->setHelp('This command allows you to migrate the database from one database platform to another (e.g. from MySQL to PostgreSQL).')
->addArgument('url', InputArgument::REQUIRED, 'The database connection URL of the source database to migrate from');
parent::__construct();
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$sourceEM = $this->getSourceEm($input->getArgument('url'));
//Check that both databases are not using the same driver
if ($sourceEM->getConnection()->getDatabasePlatform()::class === $this->targetEM->getConnection()->getDatabasePlatform()::class) {
$io->error('Source and target database are using the same database platform / driver. This command is only intended to migrate between different database platforms (e.g. from MySQL to PostgreSQL).');
return 1;
}
$this->ensureVersionUpToDate($sourceEM);
$io->note('This command is still in development. If you encounter any problems, please report them to the issue tracker on GitHub.');
$io->warning(sprintf('This command will delete all existing data in the target database "%s". Make sure that you have no important data in the database before you continue!',
$this->targetEM->getConnection()->getDatabase() ?? 'unknown'
));
$io->ask('Please type "DELETE ALL DATA" to continue.', '', function ($answer) {
if (strtoupper($answer) !== 'DELETE ALL DATA') {
throw new \RuntimeException('You did not type "DELETE ALL DATA"!');
}
return $answer;
});
// Example migration logic (to be replaced with actual migration code)
$io->info('Starting database migration...');
@ -126,7 +94,7 @@ class DBPlatformConvertCommand extends Command
$io->progressStart(count($metadata));
//Afterward we migrate all entities
//Afterwards we migrate all entities
foreach ($metadata as $metadatum) {
//skip all superclasses
if ($metadatum->isMappedSuperclass) {
@ -137,7 +105,7 @@ class DBPlatformConvertCommand extends Command
$io->note('Migrating entity: ' . $entityClass);
$repo = $sourceEM->getRepository($entityClass);
$repo = $this->sourceEM->getRepository($entityClass);
$items = $repo->findAll();
foreach ($items as $index => $item) {
$this->targetEM->persist($item);
@ -147,12 +115,20 @@ class DBPlatformConvertCommand extends Command
$io->progressFinish();
//Migrate all manufacturers from source to target
/*$manufacturerRepo = $this->sourceEM->getRepository(Manufacturer::class);
$manufacturers = $manufacturerRepo->findAll();
foreach ($manufacturers as $manufacturer) {
$this->targetEM->persist($manufacturer);
}
$this->targetEM->flush();
*/
//Fix sequences / auto increment values on target database
$io->info('Fixing sequences / auto increment values on target database...');
$this->fixAutoIncrements($this->targetEM);
$io->success('Database migration completed successfully.');
$output->writeln('Database migration completed successfully.');
if ($io->isVerbose()) {
$io->info('Process took peak memory: ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB');
@ -161,38 +137,6 @@ class DBPlatformConvertCommand extends Command
return Command::SUCCESS;
}
/**
* Construct a source EntityManager based on the given connection URL
* @param string $url
* @return EntityManagerInterface
*/
private function getSourceEm(string $url): EntityManagerInterface
{
//Replace any %kernel.project_dir% placeholders
$url = str_replace('%kernel.project_dir%', $this->kernelProjectDir, $url);
$connectionFactory = new ConnectionFactory();
$connection = $connectionFactory->createConnection(['url' => $url]);
return new EntityManager($connection, $this->targetEM->getConfiguration());
}
private function ensureVersionUpToDate(EntityManagerInterface $sourceEM): void
{
//Ensure that target database is up to date
$migrationStatusCalculator = $this->dependencyFactory->getMigrationStatusCalculator();
$newMigrations = $migrationStatusCalculator->getNewMigrations();
if (count($newMigrations->getItems()) > 0) {
throw new \RuntimeException("Target database is not up to date. Please run all migrations (with doctrine:migrations:migrate) before starting the migration process.");
}
$sourceDependencyLoader = DependencyFactory::fromEntityManager(new ExistingConfiguration($this->dependencyFactory->getConfiguration()), new ExistingEntityManager($sourceEM));
$sourceMigrationStatusCalculator = $sourceDependencyLoader->getMigrationStatusCalculator();
$sourceNewMigrations = $sourceMigrationStatusCalculator->getNewMigrations();
if (count($sourceNewMigrations->getItems()) > 0) {
throw new \RuntimeException("Source database is not up to date. Please run all migrations (with doctrine:migrations:migrate) on the source database before starting the migration process.");
}
}
private function fixAutoIncrements(EntityManagerInterface $em): void
{
$connection = $em->getConnection();
@ -200,7 +144,7 @@ class DBPlatformConvertCommand extends Command
if ($platform instanceof PostgreSQLPlatform) {
$connection->executeStatement(
//From: https://wiki.postgresql.org/wiki/Fixing_Sequences
//From: https://wiki.postgresql.org/wiki/Fixing_Sequences
<<<SQL
SELECT 'SELECT SETVAL(' ||
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||