Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Doctrine Migrations STABLE #2528

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"imsglobal/lti": "^3.0",
"paragonie/random_compat": "^2.0",
"symfony/dotenv": "~4.3",
"doctrine/migrations" : "3.0.0-alpha1"
"doctrine/migrations" : "3.0.0-beta1"
},
"autoload": {
"psr-4": {
Expand Down
19 changes: 10 additions & 9 deletions scripts/tools/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Generator\ClassNameGenerator;
use Doctrine\Migrations\MigrationRepository;
use Doctrine\Migrations\Exception\MigrationException;
use Doctrine\Migrations\Exception\NoMigrationsToExecute;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
Expand All @@ -37,6 +36,7 @@
use Doctrine\Migrations\Tools\Console\Command\VersionCommand;
use Doctrine\Migrations\Tools\Console\Command\SyncMetadataCommand;
use Doctrine\Migrations\Version\Comparator;
use oat\oatbox\service\exception\InvalidServiceManagerException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -48,7 +48,6 @@
use oat\tao\scripts\tools\migrations\TaoClassNameGenerator;
use oat\tao\scripts\tools\migrations\commands\GenerateCommand;
use oat\tao\scripts\tools\migrations\TaoComparator;
use oat\tao\scripts\tools\migrations\TaoMigrationRepository;
use common_ext_Extension;
use common_report_Report as Report;
use common_ext_ExtensionsManager as ExtensionsManager;
Expand Down Expand Up @@ -288,25 +287,27 @@ private function executeMigration(DependencyFactory $dependencyFactory, InputInt
}
}

/**
* @param Configuration $configuration
* @return DependencyFactory
* @throws ScriptException
* @throws InvalidServiceManagerException
*/
private function getDependencyFactory($configuration)
{
$connection = $this->getConnection();
$dependencyFactory = DependencyFactory::fromConnection(
new ExistingConfiguration($configuration),
new ExistingConnection($connection)
);

/** @var ExtensionsManager $extManager */
$extManager = $this->getServiceManager()->get(ExtensionsManager::SERVICE_ID);
if ($this->hasOption('extension')) {
$dependencyFactory->setService(ClassNameGenerator::class, new TaoClassNameGenerator($this->getExtension()));
}
$dependencyFactory->setService(Comparator::class, new TaoComparator($extManager, new \helpers_ExtensionHelper()));
$dependencyFactory->setService(MigrationRepository::class, new TaoMigrationRepository(
$configuration->getMigrationClasses(),
$configuration->getMigrationDirectories(),
$dependencyFactory->getMigrationsFinder(),
$dependencyFactory->getMigrationFactory(),
$dependencyFactory->getVersionComparator()
));

return $dependencyFactory;
}

Expand Down
34 changes: 33 additions & 1 deletion scripts/tools/migrations/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,47 @@
namespace oat\tao\scripts\tools\migrations;

use Doctrine\Migrations\AbstractMigration as DoctrineAbstractMigration;
use oat\oatbox\service\ServiceManager;
use oat\oatbox\service\ServiceManagerAwareInterface;
use oat\oatbox\service\ServiceManagerAwareTrait;
use oat\oatbox\log\LoggerAwareTrait;
use oat\oatbox\log\TaoLoggerAwareInterface;
use \common_report_Report as Report;
use \helpers_Report as ReportHelper;

/**
* Class AbstractMigration
*
* The base class for all TAO Extension Migrations.
*
* @package oat\tao\scripts\tools\migrations
*/
abstract class AbstractMigration
extends DoctrineAbstractMigration
implements ServiceManagerAwareInterface, TaoLoggerAwareInterface
{
use ServiceManagerAwareTrait;
use LoggerAwareTrait;
}

/**
* @return ServiceManager
*/
public function getServiceLocator(): ServiceManager
{
return ServiceManager::getServiceManager();
}

/**
* Add a Report
*
* Will write to output the given $report Report object. This method
* enables developers to communicate important information about the execution
* of their migrations e.g. to Operations.
*
* @param Report $report
*/
protected function addReport(Report $report): void
{
$this->write(ReportHelper::renderToCommandLine($report, false));
bugalot marked this conversation as resolved.
Show resolved Hide resolved
}
}
44 changes: 0 additions & 44 deletions scripts/tools/migrations/TaoMigrationRepository.php

This file was deleted.

66 changes: 66 additions & 0 deletions test/unit/scripts/tools/migrations/AbstractMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2020 (original work) Open Assessment Technologies SA
*
*/

declare(strict_types=1);

namespace unit\scripts\tools\migrations;

use oat\generis\test\TestCase;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Connection;
use oat\tao\scripts\tools\migrations\AbstractMigration;
use Psr\Log\LoggerInterface;
use \common_report_Report as Report;

class AbstractMigrationTest extends TestCase
{
public function testAddReport()
{
$connectionMock = $this->createMock(Connection::class);
$loggerMock = $this->createMock(LoggerInterface::class);
$schemaMock = $this->createMock(Schema::class);

$migration = new class($connectionMock, $loggerMock) extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addReport(Report::createInfo('Migration Up!'));
bugalot marked this conversation as resolved.
Show resolved Hide resolved
}

public function down(Schema $schema): void
{
$this->addReport(Report::createFailure('Migration Down!'));
bugalot marked this conversation as resolved.
Show resolved Hide resolved
}
};

$expectedUpReportMessage = 'Migration Up!' . PHP_EOL;
$expectedDownReportMessage = 'Migration Down!' . PHP_EOL;

$loggerMock->expects($this->exactly(2))
->method('notice')
bugalot marked this conversation as resolved.
Show resolved Hide resolved
->withConsecutive(
[$this->equalTo($expectedUpReportMessage)],
[$this->equalTo($expectedDownReportMessage)]
);

$migration->up($schemaMock);
$migration->down($schemaMock);
}
}