mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-07-06 09:21:39 +00:00
Compare commits
4 commits
00b35e3306
...
ddbfc87ce1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddbfc87ce1 | ||
|
|
3454fa51de | ||
|
|
343ad6beff | ||
|
|
d385303a52 |
3 changed files with 133 additions and 69 deletions
|
|
@ -1,36 +1,45 @@
|
||||||
doctrine:
|
doctrine:
|
||||||
dbal:
|
dbal:
|
||||||
# 1. GLOBAL SETTINGS (Apply to all connections)
|
|
||||||
types:
|
|
||||||
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
|
|
||||||
|
|
||||||
connections:
|
|
||||||
default:
|
|
||||||
use_savepoints: true
|
|
||||||
schema_filter: ~^(?!internal)~
|
|
||||||
url: '%env(resolve:DATABASE_URL)%'
|
url: '%env(resolve:DATABASE_URL)%'
|
||||||
|
|
||||||
migration_source:
|
# Required for DAMA doctrine test bundle
|
||||||
use_savepoints: true
|
use_savepoints: true
|
||||||
|
|
||||||
|
# IMPORTANT: You MUST configure your server version,
|
||||||
|
# either here or in the DATABASE_URL env var (see .env file)
|
||||||
|
|
||||||
|
types:
|
||||||
|
# UTC datetimes
|
||||||
|
datetime:
|
||||||
|
class: App\Doctrine\Types\UTCDateTimeType
|
||||||
|
date:
|
||||||
|
class: App\Doctrine\Types\UTCDateTimeType
|
||||||
|
|
||||||
|
datetime_immutable:
|
||||||
|
class: App\Doctrine\Types\UTCDateTimeImmutableType
|
||||||
|
date_immutable:
|
||||||
|
class: App\Doctrine\Types\UTCDateTimeImmutableType
|
||||||
|
|
||||||
|
big_decimal:
|
||||||
|
class: App\Doctrine\Types\BigDecimalType
|
||||||
|
tinyint:
|
||||||
|
class: App\Doctrine\Types\TinyIntType
|
||||||
|
|
||||||
schema_filter: ~^(?!internal)~
|
schema_filter: ~^(?!internal)~
|
||||||
url: '%env(resolve:DB_MIGRATION_SOURCE)%'
|
# Only enable this when needed
|
||||||
|
profiling_collect_backtrace: false
|
||||||
|
|
||||||
orm:
|
orm:
|
||||||
auto_generate_proxy_classes: true
|
auto_generate_proxy_classes: true
|
||||||
enable_lazy_ghost_objects: true
|
enable_lazy_ghost_objects: true
|
||||||
|
|
||||||
entity_managers:
|
|
||||||
default: &common_orm_settings
|
|
||||||
report_fields_where_declared: true
|
report_fields_where_declared: true
|
||||||
validate_xml_mapping: true
|
validate_xml_mapping: true
|
||||||
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
|
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
|
||||||
identity_generation_preferences:
|
identity_generation_preferences:
|
||||||
Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
|
Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
|
||||||
|
auto_mapping: true
|
||||||
|
controller_resolver:
|
||||||
|
auto_mapping: true
|
||||||
mappings:
|
mappings:
|
||||||
App:
|
App:
|
||||||
type: attribute
|
type: attribute
|
||||||
|
|
@ -38,6 +47,7 @@ doctrine:
|
||||||
dir: '%kernel.project_dir%/src/Entity'
|
dir: '%kernel.project_dir%/src/Entity'
|
||||||
prefix: 'App\Entity'
|
prefix: 'App\Entity'
|
||||||
alias: App
|
alias: App
|
||||||
|
|
||||||
dql:
|
dql:
|
||||||
string_functions:
|
string_functions:
|
||||||
regexp: App\Doctrine\Functions\Regexp
|
regexp: App\Doctrine\Functions\Regexp
|
||||||
|
|
@ -46,11 +56,6 @@ doctrine:
|
||||||
natsort: App\Doctrine\Functions\Natsort
|
natsort: App\Doctrine\Functions\Natsort
|
||||||
array_position: App\Doctrine\Functions\ArrayPosition
|
array_position: App\Doctrine\Functions\ArrayPosition
|
||||||
ilike: App\Doctrine\Functions\ILike
|
ilike: App\Doctrine\Functions\ILike
|
||||||
connection: default
|
|
||||||
|
|
||||||
migration_source:
|
|
||||||
<<: *common_orm_settings
|
|
||||||
connection: migration_source
|
|
||||||
|
|
||||||
when@test:
|
when@test:
|
||||||
doctrine:
|
doctrine:
|
||||||
|
|
|
||||||
|
|
@ -1622,6 +1622,7 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||||
* flysystem?: array{
|
* flysystem?: array{
|
||||||
* filesystem_service: scalar|null|Param,
|
* filesystem_service: scalar|null|Param,
|
||||||
* },
|
* },
|
||||||
|
* asset_mapper?: array<mixed>,
|
||||||
* chain?: array{
|
* chain?: array{
|
||||||
* loaders: list<scalar|null|Param>,
|
* loaders: list<scalar|null|Param>,
|
||||||
* },
|
* },
|
||||||
|
|
@ -2301,11 +2302,13 @@ use Symfony\Component\Config\Loader\ParamConfigurator as Param;
|
||||||
* controllers_json?: scalar|null|Param, // Default: "%kernel.project_dir%/assets/controllers.json"
|
* controllers_json?: scalar|null|Param, // Default: "%kernel.project_dir%/assets/controllers.json"
|
||||||
* }
|
* }
|
||||||
* @psalm-type UxTranslatorConfig = array{
|
* @psalm-type UxTranslatorConfig = array{
|
||||||
* dump_directory?: scalar|null|Param, // Default: "%kernel.project_dir%/var/translations"
|
* 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
|
||||||
* domains?: string|array{ // List of domains to include/exclude from the generated translations. Prefix with a `!` to exclude a domain.
|
* domains?: string|array{ // List of domains to include/exclude from the generated translations. Prefix with a `!` to exclude a domain.
|
||||||
* type?: scalar|null|Param,
|
* type?: scalar|null|Param,
|
||||||
* elements?: list<scalar|null|Param>,
|
* elements?: list<scalar|null|Param>,
|
||||||
* },
|
* },
|
||||||
|
* keys_patterns?: list<scalar|null|Param>,
|
||||||
* }
|
* }
|
||||||
* @psalm-type DompdfFontLoaderConfig = array{
|
* @psalm-type DompdfFontLoaderConfig = array{
|
||||||
* autodiscovery?: bool|array{
|
* autodiscovery?: bool|array{
|
||||||
|
|
|
||||||
|
|
@ -23,43 +23,75 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Command\Migrations;
|
namespace App\Command\Migrations;
|
||||||
|
|
||||||
use App\DataTables\Helpers\ColumnSortHelper;
|
|
||||||
use App\Entity\Parts\Manufacturer;
|
|
||||||
use App\Services\ImportExportSystem\PartKeeprImporter\PKImportHelper;
|
use App\Services\ImportExportSystem\PartKeeprImporter\PKImportHelper;
|
||||||
|
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
|
||||||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
|
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
|
||||||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
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\ORM\EntityManagerInterface;
|
||||||
|
use Doctrine\Migrations\DependencyFactory;
|
||||||
use Doctrine\ORM\Id\AssignedGenerator;
|
use Doctrine\ORM\Id\AssignedGenerator;
|
||||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
use Doctrine\Persistence\ManagerRegistry;
|
|
||||||
use Doctrine\Persistence\ObjectManager;
|
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
|
||||||
#[AsCommand('partdb:migrate-db', 'Migrate the database to a different platform')]
|
#[AsCommand('partdb:migrations:convert-db-platform', 'Convert the database to a different platform')]
|
||||||
class DBMigrationCommand extends Command
|
class DBPlatformConvertCommand extends Command
|
||||||
{
|
{
|
||||||
private readonly EntityManagerInterface $sourceEM;
|
|
||||||
private readonly EntityManagerInterface $targetEM;
|
|
||||||
|
|
||||||
public function __construct(private readonly ManagerRegistry $managerRegistry,
|
public function __construct(
|
||||||
|
private readonly EntityManagerInterface $targetEM,
|
||||||
private readonly PKImportHelper $importHelper,
|
private readonly PKImportHelper $importHelper,
|
||||||
|
private readonly DependencyFactory $dependencyFactory,
|
||||||
|
#[Autowire('%kernel.project_dir%')]
|
||||||
|
private readonly string $kernelProjectDir,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->sourceEM = $this->managerRegistry->getManager('migration_source');
|
|
||||||
$this->targetEM = $this->managerRegistry->getManager('default');
|
|
||||||
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
public function execute(InputInterface $input, OutputInterface $output): int
|
public function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$io = new SymfonyStyle($input, $output);
|
$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)
|
// Example migration logic (to be replaced with actual migration code)
|
||||||
$io->info('Starting database migration...');
|
$io->info('Starting database migration...');
|
||||||
|
|
||||||
|
|
@ -94,7 +126,7 @@ class DBMigrationCommand extends Command
|
||||||
|
|
||||||
$io->progressStart(count($metadata));
|
$io->progressStart(count($metadata));
|
||||||
|
|
||||||
//Afterwards we migrate all entities
|
//Afterward we migrate all entities
|
||||||
foreach ($metadata as $metadatum) {
|
foreach ($metadata as $metadatum) {
|
||||||
//skip all superclasses
|
//skip all superclasses
|
||||||
if ($metadatum->isMappedSuperclass) {
|
if ($metadatum->isMappedSuperclass) {
|
||||||
|
|
@ -105,7 +137,7 @@ class DBMigrationCommand extends Command
|
||||||
|
|
||||||
$io->note('Migrating entity: ' . $entityClass);
|
$io->note('Migrating entity: ' . $entityClass);
|
||||||
|
|
||||||
$repo = $this->sourceEM->getRepository($entityClass);
|
$repo = $sourceEM->getRepository($entityClass);
|
||||||
$items = $repo->findAll();
|
$items = $repo->findAll();
|
||||||
foreach ($items as $index => $item) {
|
foreach ($items as $index => $item) {
|
||||||
$this->targetEM->persist($item);
|
$this->targetEM->persist($item);
|
||||||
|
|
@ -115,20 +147,12 @@ class DBMigrationCommand extends Command
|
||||||
|
|
||||||
$io->progressFinish();
|
$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
|
//Fix sequences / auto increment values on target database
|
||||||
$io->info('Fixing sequences / auto increment values on target database...');
|
$io->info('Fixing sequences / auto increment values on target database...');
|
||||||
$this->fixAutoIncrements($this->targetEM);
|
$this->fixAutoIncrements($this->targetEM);
|
||||||
|
|
||||||
$output->writeln('Database migration completed successfully.');
|
$io->success('Database migration completed successfully.');
|
||||||
|
|
||||||
if ($io->isVerbose()) {
|
if ($io->isVerbose()) {
|
||||||
$io->info('Process took peak memory: ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB');
|
$io->info('Process took peak memory: ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . ' MB');
|
||||||
|
|
@ -137,6 +161,38 @@ class DBMigrationCommand extends Command
|
||||||
return Command::SUCCESS;
|
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
|
private function fixAutoIncrements(EntityManagerInterface $em): void
|
||||||
{
|
{
|
||||||
$connection = $em->getConnection();
|
$connection = $em->getConnection();
|
||||||
Loading…
Add table
Add a link
Reference in a new issue