Stop using ContainerAwareInterface in migrations

This commit is contained in:
Yassine Guedidi 2025-01-18 14:44:31 +01:00
parent bcf0f2f52c
commit e715da7e91
19 changed files with 83 additions and 66 deletions

View File

@ -95,6 +95,8 @@ doctrine_migrations:
version_column_name: 'version'
version_column_length: 192
executed_at_column_name: 'executed_at'
services:
Doctrine\Migrations\Version\MigrationFactory: Wallabag\Doctrine\MigrationFactoryDecorator
fos_rest:
param_fetcher_listener: true

View File

@ -30,6 +30,7 @@ services:
$storeArticleHeaders: '@=service(''craue_config'').get(''store_article_headers'')'
$supportUrl: '@=service(''craue_config'').get(''wallabag_support_url'')'
$fonts: '%wallabag.fonts%'
$defaultIgnoreOriginInstanceRules: '%wallabag.default_ignore_origin_instance_rules%'
Wallabag\:
resource: '../../src/*'
@ -107,6 +108,9 @@ services:
$rabbitMqProducer: '@old_sound_rabbit_mq.import_pocket_html_producer'
$redisProducer: '@wallabag.producer.redis.pocket_html'
Wallabag\Doctrine\MigrationFactoryDecorator:
decorates: doctrine.migrations.migrations_factory
Doctrine\DBAL\Connection:
alias: doctrine.dbal.default_connection

View File

@ -21,9 +21,7 @@ class Version20160410190541 extends WallabagMigration
'length' => 23,
]);
$sharePublic = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$sharePublic = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'share_public'");
if (false === $sharePublic) {

View File

@ -12,18 +12,14 @@ class Version20160911214952 extends WallabagMigration
{
public function up(Schema $schema): void
{
$redis = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$redis = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'import_with_redis'");
if (false === $redis) {
$this->addSql('INSERT INTO ' . $this->getTable('craue_config_setting') . " (name, value, section) VALUES ('import_with_redis', 0, 'import')");
}
$rabbitmq = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$rabbitmq = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'import_with_rabbitmq'");
if (false === $rabbitmq) {

View File

@ -12,9 +12,7 @@ class Version20161031132655 extends WallabagMigration
{
public function up(Schema $schema): void
{
$images = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$images = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'download_images_enabled'");
$this->skipIf(false !== $images, 'It seems that you already played this migration.');

View File

@ -12,18 +12,14 @@ class Version20161117071626 extends WallabagMigration
{
public function up(Schema $schema): void
{
$share = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$share = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'share_unmark'");
if (false === $share) {
$this->addSql('INSERT INTO ' . $this->getTable('craue_config_setting') . " (name, value, section) VALUES ('share_unmark', 0, 'entry')");
}
$unmark = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$unmark = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'unmark_url'");
if (false === $unmark) {

View File

@ -12,9 +12,7 @@ class Version20161122144743 extends WallabagMigration
{
public function up(Schema $schema): void
{
$access = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$access = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'restricted_access'");
$this->skipIf(false !== $access, 'It seems that you already played this migration.');

View File

@ -12,9 +12,7 @@ class Version20170327194233 extends WallabagMigration
{
public function up(Schema $schema): void
{
$scuttle = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$scuttle = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'share_scuttle'");
$this->skipIf(false !== $scuttle, 'It seems that you already played this migration.');

View File

@ -17,9 +17,7 @@ class Version20170420134133 extends WallabagMigration
public function down(Schema $schema): void
{
$downloadPictures = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$downloadPictures = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'download_pictures'");
$this->skipIf(false !== $downloadPictures, 'It seems that you already played this migration.');

View File

@ -12,9 +12,7 @@ class Version20170602075214 extends WallabagMigration
{
public function up(Schema $schema): void
{
$apiUserRegistration = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$apiUserRegistration = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'api_user_registration'");
$this->skipIf(false !== $apiUserRegistration, 'It seems that you already played this migration.');

View File

@ -19,9 +19,7 @@ class Version20170606155640 extends WallabagMigration
return;
}
$apiUserRegistration = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$apiUserRegistration = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'wallabag_url'");
if (false === $apiUserRegistration) {

View File

@ -12,9 +12,7 @@ class Version20171120163128 extends WallabagMigration
{
public function up(Schema $schema): void
{
$storeArticleHeaders = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$storeArticleHeaders = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'store_article_headers'");
$this->skipIf(false !== $storeArticleHeaders, 'It seems that you already played this migration.');

View File

@ -12,9 +12,7 @@ class Version20171125164500 extends WallabagMigration
{
public function up(Schema $schema): void
{
$shaarliShareOriginUrl = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$shaarliShareOriginUrl = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = 'shaarli_share_origin_url'");
$this->skipIf(false !== $shaarliShareOriginUrl, 'It seems that you already played this migration.');

View File

@ -121,9 +121,7 @@ final class Version20190129120000 extends WallabagMigration
public function up(Schema $schema): void
{
foreach ($this->settings as $setting) {
$settingEnabled = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$settingEnabled = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('craue_config_setting') . " WHERE name = '" . $setting['name'] . "'");
if (false !== $settingEnabled) {

View File

@ -45,10 +45,8 @@ final class Version20190826204730 extends WallabagMigration
public function postUp(Schema $schema): void
{
foreach ($this->container->getParameter('wallabag.default_ignore_origin_instance_rules') as $entity) {
$previous_rule = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
foreach ($this->defaultIgnoreOriginInstanceRules as $entity) {
$previous_rule = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('ignore_origin_instance_rule') . " WHERE rule = '" . $entity['rule'] . "'");
if (false === $previous_rule) {

View File

@ -37,7 +37,7 @@ final class Version20230728093912 extends WallabagMigration
'UPDATE ' . $this->getTable('entry') . ' SET is_not_parsed = :isNotParsed WHERE content LIKE :content',
[
'isNotParsed' => true,
'content' => str_replace("\n", '', addslashes($this->container->getParameter('wallabag.fetching_error_message'))) . '%',
'content' => str_replace("\n", '', addslashes($this->fetchingErrorMessage)) . '%',
]
);
}

View File

@ -12,18 +12,14 @@ final class Version20240521152037 extends WallabagMigration
{
public function up(Schema $schema): void
{
$share = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$share = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('internal_setting') . " WHERE name = 'share_linkding'");
if (false === $share) {
$this->addSql('INSERT INTO ' . $this->getTable('internal_setting') . " (name, value, section) VALUES ('share_linkding', 0, 'entry')");
}
$linkding = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
$linkding = $this->connection
->fetchOne('SELECT * FROM ' . $this->getTable('internal_setting') . " WHERE name = 'linkding_url'");
if (false === $linkding) {

View File

@ -0,0 +1,38 @@
<?php
namespace Wallabag\Doctrine;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\MigrationFactory;
/**
* Decorates the migration factory to pass some additional information to the migration instances.
*/
class MigrationFactoryDecorator implements MigrationFactory
{
private MigrationFactory $migrationFactory;
private string $tablePrefix;
private array $defaultIgnoreOriginInstanceRules;
private string $fetchingErrorMessage;
public function __construct(MigrationFactory $migrationFactory, string $tablePrefix, array $defaultIgnoreOriginInstanceRules, string $fetchingErrorMessage)
{
$this->migrationFactory = $migrationFactory;
$this->tablePrefix = $tablePrefix;
$this->defaultIgnoreOriginInstanceRules = $defaultIgnoreOriginInstanceRules;
$this->fetchingErrorMessage = $fetchingErrorMessage;
}
public function createVersion(string $migrationClassName): AbstractMigration
{
$instance = $this->migrationFactory->createVersion($migrationClassName);
if ($instance instanceof WallabagMigration) {
$instance->setTablePrefix($this->tablePrefix);
$instance->setDefaultIgnoreOriginInstanceRules($this->defaultIgnoreOriginInstanceRules);
$instance->setFetchingErrorMessage($this->fetchingErrorMessage);
}
return $instance;
}
}

View File

@ -5,17 +5,14 @@ namespace Wallabag\Doctrine;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WallabagMigration extends AbstractMigration implements ContainerAwareInterface
abstract class WallabagMigration extends AbstractMigration
{
public const UN_ESCAPED_TABLE = true;
/**
* @var ContainerInterface
*/
protected $container;
protected string $tablePrefix;
protected array $defaultIgnoreOriginInstanceRules;
protected string $fetchingErrorMessage;
// because there are declared as abstract in `AbstractMigration` we need to delarer here too
public function up(Schema $schema): void
@ -26,11 +23,6 @@ abstract class WallabagMigration extends AbstractMigration implements ContainerA
{
}
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* @todo remove when upgrading DoctrineMigration (only needed for PHP 8)
*
@ -41,9 +33,24 @@ abstract class WallabagMigration extends AbstractMigration implements ContainerA
return false;
}
public function setTablePrefix(string $tablePrefix): void
{
$this->tablePrefix = $tablePrefix;
}
public function setDefaultIgnoreOriginInstanceRules(array $defaultIgnoreOriginInstanceRules): void
{
$this->defaultIgnoreOriginInstanceRules = $defaultIgnoreOriginInstanceRules;
}
public function setFetchingErrorMessage(string $fetchingErrorMessage): void
{
$this->fetchingErrorMessage = $fetchingErrorMessage;
}
protected function getTable($tableName, $unEscaped = false)
{
$table = $this->container->getParameter('database_table_prefix') . $tableName;
$table = $this->tablePrefix . $tableName;
if (self::UN_ESCAPED_TABLE === $unEscaped) {
return $table;