mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-07-06 09:21:39 +00:00
Compare commits
No commits in common. "ddbfc87ce16c86977c0355d99cf5f7aca1726baa" and "00b35e3306579ef42a45912aedc1507fe6678a2d" have entirely different histories.
ddbfc87ce1
...
00b35e3306
3 changed files with 69 additions and 133 deletions
|
|
@ -1,61 +1,56 @@
|
||||||
doctrine:
|
doctrine:
|
||||||
dbal:
|
dbal:
|
||||||
url: '%env(resolve:DATABASE_URL)%'
|
# 1. GLOBAL SETTINGS (Apply to all connections)
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
types:
|
types:
|
||||||
# UTC datetimes
|
datetime: App\Doctrine\Types\UTCDateTimeType
|
||||||
datetime:
|
date: App\Doctrine\Types\UTCDateTimeType
|
||||||
class: App\Doctrine\Types\UTCDateTimeType
|
datetime_immutable: App\Doctrine\Types\UTCDateTimeImmutableType
|
||||||
date:
|
date_immutable: App\Doctrine\Types\UTCDateTimeImmutableType
|
||||||
class: App\Doctrine\Types\UTCDateTimeType
|
big_decimal: App\Doctrine\Types\BigDecimalType
|
||||||
|
tinyint: App\Doctrine\Types\TinyIntType
|
||||||
|
|
||||||
datetime_immutable:
|
connections:
|
||||||
class: App\Doctrine\Types\UTCDateTimeImmutableType
|
default:
|
||||||
date_immutable:
|
use_savepoints: true
|
||||||
class: App\Doctrine\Types\UTCDateTimeImmutableType
|
schema_filter: ~^(?!internal)~
|
||||||
|
url: '%env(resolve:DATABASE_URL)%'
|
||||||
|
|
||||||
big_decimal:
|
migration_source:
|
||||||
class: App\Doctrine\Types\BigDecimalType
|
use_savepoints: true
|
||||||
tinyint:
|
schema_filter: ~^(?!internal)~
|
||||||
class: App\Doctrine\Types\TinyIntType
|
url: '%env(resolve:DB_MIGRATION_SOURCE)%'
|
||||||
|
|
||||||
schema_filter: ~^(?!internal)~
|
|
||||||
# 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
|
||||||
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:
|
entity_managers:
|
||||||
string_functions:
|
default: &common_orm_settings
|
||||||
regexp: App\Doctrine\Functions\Regexp
|
report_fields_where_declared: true
|
||||||
field: DoctrineExtensions\Query\Mysql\Field
|
validate_xml_mapping: true
|
||||||
field2: App\Doctrine\Functions\Field2
|
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
|
||||||
natsort: App\Doctrine\Functions\Natsort
|
identity_generation_preferences:
|
||||||
array_position: App\Doctrine\Functions\ArrayPosition
|
Doctrine\DBAL\Platforms\PostgreSQLPlatform: identity
|
||||||
ilike: App\Doctrine\Functions\ILike
|
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:
|
when@test:
|
||||||
doctrine:
|
doctrine:
|
||||||
|
|
|
||||||
|
|
@ -1622,7 +1622,6 @@ 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>,
|
||||||
* },
|
* },
|
||||||
|
|
@ -2302,13 +2301,11 @@ 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, // The directory where translations and TypeScript types are dumped. // Default: "%kernel.project_dir%/var/translations"
|
* dump_directory?: scalar|null|Param, // 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,75 +23,43 @@ 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:migrations:convert-db-platform', 'Convert the database to a different platform')]
|
#[AsCommand('partdb:migrate-db', 'Migrate the database to a different platform')]
|
||||||
class DBPlatformConvertCommand extends Command
|
class DBMigrationCommand extends Command
|
||||||
{
|
{
|
||||||
|
private readonly EntityManagerInterface $sourceEM;
|
||||||
|
private readonly EntityManagerInterface $targetEM;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(private readonly ManagerRegistry $managerRegistry,
|
||||||
private readonly EntityManagerInterface $targetEM,
|
|
||||||
private readonly PKImportHelper $importHelper,
|
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
|
parent::__construct();
|
||||||
{
|
|
||||||
$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...');
|
||||||
|
|
||||||
|
|
@ -126,7 +94,7 @@ class DBPlatformConvertCommand extends Command
|
||||||
|
|
||||||
$io->progressStart(count($metadata));
|
$io->progressStart(count($metadata));
|
||||||
|
|
||||||
//Afterward we migrate all entities
|
//Afterwards we migrate all entities
|
||||||
foreach ($metadata as $metadatum) {
|
foreach ($metadata as $metadatum) {
|
||||||
//skip all superclasses
|
//skip all superclasses
|
||||||
if ($metadatum->isMappedSuperclass) {
|
if ($metadatum->isMappedSuperclass) {
|
||||||
|
|
@ -137,7 +105,7 @@ class DBPlatformConvertCommand extends Command
|
||||||
|
|
||||||
$io->note('Migrating entity: ' . $entityClass);
|
$io->note('Migrating entity: ' . $entityClass);
|
||||||
|
|
||||||
$repo = $sourceEM->getRepository($entityClass);
|
$repo = $this->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);
|
||||||
|
|
@ -147,12 +115,20 @@ class DBPlatformConvertCommand 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);
|
||||||
|
|
||||||
$io->success('Database migration completed successfully.');
|
$output->writeln('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');
|
||||||
|
|
@ -161,38 +137,6 @@ class DBPlatformConvertCommand 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();
|
||||||
|
|
@ -200,7 +144,7 @@ class DBPlatformConvertCommand extends Command
|
||||||
|
|
||||||
if ($platform instanceof PostgreSQLPlatform) {
|
if ($platform instanceof PostgreSQLPlatform) {
|
||||||
$connection->executeStatement(
|
$connection->executeStatement(
|
||||||
//From: https://wiki.postgresql.org/wiki/Fixing_Sequences
|
//From: https://wiki.postgresql.org/wiki/Fixing_Sequences
|
||||||
<<<SQL
|
<<<SQL
|
||||||
SELECT 'SELECT SETVAL(' ||
|
SELECT 'SELECT SETVAL(' ||
|
||||||
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
|
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
|
||||||
Loading…
Add table
Add a link
Reference in a new issue