Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloOntivero committed Oct 6, 2020
0 parents commit bebd547
Show file tree
Hide file tree
Showing 16 changed files with 1,671 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[*.php]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 4

[composer.json]
indent_style = tab
indent_size = 4
trim_trailing_whitespace = true
insert_final_newline = true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Initial support for configuration files
- Initial override of [DoctrineMigrationsBundle](https://github.com/doctrine/DoctrineMigrationsBundle) commands
- Ability to execute migrations commands to all entity managers, or filtered by the option `--em=default`
- Supported command `doctrine:migrations:diff`
- Supported command `doctrine:migrations:migrate`
- Supported command `doctrine:migrations:version`
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# DoctrineMigrationsMultipleDatabaseBundle

This bundle extends the [DoctrineMigrationsBundle](https://github.com/doctrine/DoctrineMigrationsBundle) functionality
in a hacky and dirty way to provide a easy way to configure migrations paths for multiple entity managers.

## Configuration

- Install the package by running `composer require avaibooksports/DoctrineMigrationsMultipleDatabaseBundle`
- Go to your `config/bundles.php` file and **before** the `DoctrineMigrationsBundle` line, add:
```php
AvaiBookSports\Bundle\MigrationsMutlipleDatabase\DoctrineMigrationsMultipleDatabaseBundle::class => ['all' => true],
```
- Finally, create a file in `config/packages/` called `doctrine_migrations_multiple_database.yaml` and follow the next example:
```yaml
doctrine_migrations_multiple_database:
entity_managers:
default:
migrations_paths:
DoctrineMigrations\Main: '%kernel.project_dir%/migrations/Main'
geonames:
migrations_paths:
DoctrineMigrations\Geonames: '%kernel.project_dir%/migrations/Geonames'
```
- You can leave your `doctirne_migrations.yaml` file untouched. Unmapped commands will fallback to that configuration, and if you need to disable this bundle everything should work as always.

## Usage

Just call the same commands as always, with the same parameters. Right now only the following commands are mapped:

- `doctrine:migrations:diff`
- `doctrine:migrations:migrate`
- `doctrine:migrations:version`

You can run a command for a specific entity manager adding the option `--em=example`

If you call any of the supported commands, they will work as always iterating over all the defined configurations.

### Supported configuration

For now, all configuration parameters should work except `connection` and `em`, because we are already specifying which entity manager we want to connect.

This would be the [example configuration of DoctrineMigrationsBundle](https://symfony.com/doc/master/bundles/DoctrineMigrationsBundle/index.html#configuration) translated to this bundle:

```yaml
# config/packages/doctrine_migrations_multiple_database.yaml
doctrine_migrations_multiple_database:
entity_managers:
default:
# List of namespace/path pairs to search for migrations, at least one required
migrations_paths:
'App\Migrations': 'src/App'
'AnotherApp\Migrations': '/path/to/other/migrations'
'SomeBundle\Migrations': '@SomeBundle/Migrations'
# List of additional migration classes to be loaded, optional
migrations:
- 'App\Migrations\Version123'
- 'App\Migrations\Version123'
storage:
# Default (SQL table) metadata storage configuration
table_storage:
table_name: 'doctrine_migration_versions'
version_column_name: 'version'
version_column_length: 1024
executed_at_column_name: 'executed_at'
execution_time_column_name: 'execution_time'
# Possible values: "BY_YEAR", "BY_YEAR_AND_MONTH", false
organize_migrations: false
# Path to your custom migrations template
custom_template: ~
# Run all migrations in a transaction.
all_or_nothing: false
# Adds an extra check in the generated migrations to ensure that is executed on the same database type.
check_database_platform: true
services:
# Custom migration sorting service id
'Doctrine\Migrations\Version\Comparator': ~
# Custom migration classes factory
'Doctrine\Migrations\Version\MigrationFactory': ~
factories:
# Custom migration sorting service id via callables (MyCallableFactory must be a callable)
'Doctrine\Migrations\Version\Comparator': 'MyCallableFactory'
```

### Supported commands

- `doctrine:migrations:diff`
- `doctrine:migrations:migrate`
- `doctrine:migrations:version`

## Pitfalls

This package is being actively developed to satisfy a very specific scenario in our workflow, but we wanted to share
this solution with more people struggling with this particular need.

As we are basing our configuration in YAML files, XML and PHP formats are not tested right now. We would love to have
[feedback](../../issues) from you if you have any problems configuring the bundle. Unit tests should come sooner or later.

Also, we are supporting partially the configuration parameters, and not all commands are mapped.

All releases tagged like `0.x` will be affected by this pitfalls, and release `1.0` will cover a full configuration file
and all commands.
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "avaibooksports/doctrine-migrations-multiple-database-bundle",
"type": "symfony-bundle",
"description": "Symfony DoctrineMigrationsMultipleDatabaseBundle",
"keywords": ["DBAL", "Migrations", "Schema", "Multiple databases"],
"license": "GPL-3.0-only",
"authors": [
{
"name": "Pablo Largo",
"email": "[email protected]"

},
{
"name": "AvaiBook Sports",
"email": "[email protected]",
"homepage": "https://www.avaibooksports.com"
}
],
"require": {
"php": "^7.1",
"doctrine/doctrine-migrations-bundle": "^3"
},
"autoload": {
"psr-4": {
"AvaiBookSports\\Bundle\\MigrationsMutlipleDatabase\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "main"
},
"phpstan": {
"includes": [
"extension.neon"
]
}
},
"require-dev": {
"phpstan/phpstan": "^0.12.48",
"phpstan/phpstan-symfony": "^0.12.8",
"phpstan/phpstan-strict-rules": "^0.12.5"
}
}
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: max
paths:
- src
ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#'
- '#should be contravariant with parameter \$configs \(array\) of method Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface::load\(\)#'

includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
100 changes: 100 additions & 0 deletions src/Command/Doctrine/MigrationsDiffCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace AvaiBookSports\Bundle\MigrationsMutlipleDatabase\Command\Doctrine;

use AvaiBookSports\Bundle\MigrationsMutlipleDatabase\MultipleEntityManagerLoader;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Doctrine migrations' commands are final classes. That's why we cannot extend and override them.
*/
class MigrationsDiffCommand extends Command
{
/**
* @var MultipleEntityManagerLoader
*/
private $multipleEntityManagerLoader;

public function __construct(MultipleEntityManagerLoader $multipleEntityManagerLoader)
{
parent::__construct();
$this->multipleEntityManagerLoader = $multipleEntityManagerLoader;
}

protected static $defaultName = 'doctrine:migrations:diff';

protected function configure(): void
{
$this
->setDescription('Proxy to launch doctrine:migrations:diff command as it would require a "configuration" option, and we can\'t define em/connection in config.')
->setHelp(
<<<EOT
The <info>%command.name%</info> command generates a migration by comparing your current database to your mapping information:
<info>%command.full_name%</info>
EOT
)
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the Entity Manager to handle.', '')
->addOption(
'formatted',
null,
InputOption::VALUE_NONE,
'Format the generated SQL.'
)
->addOption(
'line-length',
null,
InputOption::VALUE_REQUIRED,
'Max line length of unformatted lines.',
120
)
->addOption(
'check-database-platform',
null,
InputOption::VALUE_OPTIONAL,
'Check Database Platform to the generated code.',
false
)
->addOption(
'allow-empty-diff',
null,
InputOption::VALUE_NONE,
'Do not throw an exception when no changes are detected.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (strlen($input->getOption('em')) === 0) {
$dependencyFactories = $this->multipleEntityManagerLoader->getAllDependencyFactories();
} elseif (is_string($input->getOption('em'))) {
$dependencyFactories = [$this->multipleEntityManagerLoader->getDependencyFactory($input->getOption('em'))];
} else {
throw new RuntimeException('Invalid value for "em" option');
}

$newInput = new ArrayInput([
// '--namespace' => $input->getOption('namespace'),
// '--filter-expression' => $input->getOption('filter-expression'),
'--formatted' => $input->getOption('formatted'),
'--line-length' => $input->getOption('line-length'),
'--check-database-platform' => $input->getOption('check-database-platform'),
'--allow-empty-diff' => $input->getOption('allow-empty-diff'),
]);

$newInput->setInteractive($input->isInteractive());

foreach ($dependencyFactories as $dependencyFactory) {
$otherCommand = new \Doctrine\Migrations\Tools\Console\Command\DiffCommand($dependencyFactory);
$otherCommand->run($newInput, $output);
}

return Command::SUCCESS;
}
}
Loading

0 comments on commit bebd547

Please sign in to comment.