Skip to content

Commit

Permalink
Added configuration options for the migration bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
kreemer committed Aug 11, 2023
1 parent c0837bd commit 72b4680
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,24 @@ sym_sensor_actuator_doctrine:
default:
service: doctrine.dbal.default_connection
check_sql: SELECT 1

migrations:
enabled: true
check_unavailable: true
report_unavailable_as_down: false
```
Following table outlines the configuration:
| key | default | description |
| --------------------------------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| sym_sensor_actuator_doctrine.connections | Array | Contains a list of names, where each represents an connection to e database. The name itself can be chosen at will |
| sym_sensor_actuator_doctrine.connections.`name`.enabled | true | If the connection associated with this name should monitored |
| sym_sensor_actuator_doctrine.connections.`name`.service | 'Doctrine\DBAL\Connection' | The service name inside the dependency injection container. You can lookup your connection name with `bin/console debug:container` |
| sym_sensor_actuator_doctrine.connections.`name`.check_sql | 'Select 1' | The SQL which will be executed to determine if the database is up. The response will be ignored, it only matters if the sql can be executed without error. If you set this to `~` it will only check if a connection to the database can be established |
| key | default | description |
| ------------------------------------------------------------------ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| sym_sensor_actuator_doctrine.connections | Array | Contains a list of names, where each represents an connection to e database. The name itself can be chosen at will |
| sym_sensor_actuator_doctrine.connections.`name`.enabled | true | If the connection associated with this name should monitored |
| sym_sensor_actuator_doctrine.connections.`name`.service | 'Doctrine\DBAL\Connection' | The service name inside the dependency injection container. You can lookup your connection name with `bin/console debug:container` |
| sym_sensor_actuator_doctrine.connections.`name`.check_sql | 'Select 1' | The SQL which will be executed to determine if the database is up. The response will be ignored, it only matters if the sql can be executed without error. If you set this to `~` it will only check if a connection to the database can be established |
| sym_sensor_actuator_doctrine.migrations.enabled | true | If the migration check should be enabled in health / info endpoint |
| sym_sensor_actuator_doctrine.migrations.check_unavailable | true | If the health endpoint should check the unavailable migrations |
| sym_sensor_actuator_doctrine.migrations.report_unavailable_as_down | false | If unavailable migrations should count as unknown or down |


## License
Expand Down
7 changes: 7 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('migrations')
->canBeDisabled()
->children()
->scalarNode('check_unavailable')->defaultTrue()->end()
->scalarNode('report_unavailable_as_down')->defaultFalse()->end()
->end()
->end()
->end();

return $treeBuilder;
Expand Down
25 changes: 17 additions & 8 deletions src/DependencyInjection/SymSensorActuatorDoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,26 @@ public function load(array $configs, ContainerBuilder $container): void
$infoDefinition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Info\Collector\Doctrine::class);
$infoDefinition->replaceArgument(0, $infoArgument);
}
}

$migrationsConfig = $config['migrations'];
\assert(\is_array($migrationsConfig));
if (
$container->willBeAvailable('doctrine/doctrine-migrations-bundle', DependencyFactory::class, [])
&& $this->isConfigEnabled($container, $migrationsConfig)
) {
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services_migrations.yaml');

if ($container->willBeAvailable('doctrine/doctrine-migrations-bundle', DependencyFactory::class, [])) {
$reference = new Reference('doctrine.migrations.dependency_factory');
$reference = new Reference('doctrine.migrations.dependency_factory');

$loader->load('services_migrations.yaml');
$definition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Health\Indicator\DoctrineMigrations::class);
$definition->replaceArgument(0, $reference);
$definition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Health\Indicator\DoctrineMigrations::class);
$definition->setArgument('$dependencyFactory', $reference);
$definition->setArgument('$checkUnavailable', $migrationsConfig['check_unavailable'] ?? true);
$definition->setArgument('$reportUnavailableAsDown', $migrationsConfig['report_unavailable_as_down'] ?? false);

$infoDefinition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Info\Collector\DoctrineMigrations::class);
$infoDefinition->replaceArgument(0, $reference);
}
$infoDefinition = $container->getDefinition(\SymSensor\ActuatorDoctrineBundle\Service\Info\Collector\DoctrineMigrations::class);
$infoDefinition->replaceArgument(0, $reference);
}
}
}
13 changes: 10 additions & 3 deletions src/Service/Health/Indicator/DoctrineMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ class DoctrineMigrations implements HealthIndicator

private MigrationStatusCalculator $statusCalculator;

public function __construct(private readonly DependencyFactory $dependencyFactory)
{
public function __construct(
private readonly DependencyFactory $dependencyFactory,
private readonly bool $checkUnavailable = true,
private readonly bool $reportUnavailableAsDown = false
) {
$this->metadataStorage = $this->dependencyFactory->getMetadataStorage();
$this->migrationPlanCalculator = $this->dependencyFactory->getMigrationPlanCalculator();
$this->statusCalculator = $this->dependencyFactory->getMigrationStatusCalculator();
Expand Down Expand Up @@ -60,7 +63,11 @@ public function health(): HealthInterface
return Health::down('Not all migrations were executed', $dataGroup);
}

if (0 !== \count($executedUnavailableMigrations)) {
if ($this->checkUnavailable && 0 !== \count($executedUnavailableMigrations)) {
if ($this->reportUnavailableAsDown) {
return Health::down('Not all migrations, that were executed, are available anymore', $dataGroup);
}

return Health::unknown('Not all migrations, that were executed, are available anymore', $dataGroup);
}

Expand Down

0 comments on commit 72b4680

Please sign in to comment.