Skip to content

Commit

Permalink
Added migration for tl_page fields
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Apr 8, 2020
1 parent 62f3656 commit c1f2f93
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
80 changes: 80 additions & 0 deletions core-bundle/src/Migration/Version410/RoutingMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Migration\Version410;

use Contao\CoreBundle\Migration\AbstractMigration;
use Contao\CoreBundle\Migration\MigrationResult;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\StringType;

/**
* @internal
*/
class RoutingMigration extends AbstractMigration
{
/**
* @var Connection
*/
private $connection;

/**
* @var string
*/
private $urlSuffix;

/**
* @var bool
*/
private $prependLocale;

public function __construct(Connection $connection, string $urlSuffix = '.html', bool $prependLocale = false)
{
$this->connection = $connection;
$this->urlSuffix = $urlSuffix;
$this->prependLocale = $prependLocale;
}

public function shouldRun(): bool
{
$schemaManager = $this->connection->getSchemaManager();

if (!$schemaManager->tablesExist('tl_page')) {
return false;
}

$columns = $schemaManager->listTableColumns('tl_page');

return !isset($columns['languageprefix']) && !isset($columns['urlsuffix']);
}

public function run(): MigrationResult
{
$languagePrefix = new Column('languagePrefix', new StringType());
$languagePrefix->setColumnDefinition("varchar(128) BINARY NOT NULL default ''");

$urlSuffix = new Column('urlSuffix', new StringType());
$urlSuffix->setColumnDefinition("varchar(16) NOT NULL default '.html'");

$diff = new TableDiff('tl_page', [$languagePrefix, $urlSuffix]);

$sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff);

foreach ($sql as $statement) {
$this->connection->exec($statement);
}

return new MigrationResult(true, '');
}
}
87 changes: 87 additions & 0 deletions core-bundle/tests/Functional/Migration/RoutingMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Tests\Functional;

use Contao\CoreBundle\Migration\MigrationResult;
use Contao\CoreBundle\Migration\Version410\RoutingMigration;
use Contao\TestCase\FunctionalTestCase;
use Doctrine\DBAL\Connection;

class RoutingMigrationTest extends FunctionalTestCase
{
/**
* @dataProvider shouldRunProvider
*/
public function testShouldRun(array $dropFields, bool $expected)
{
static::resetDatabaseSchema();

/** @var Connection $connection */
$connection = static::$container->get('database_connection');

foreach ($dropFields as $field) {
$connection->exec('ALTER TABLE tl_page DROP '.$field);
}

$migration = new RoutingMigration($connection);

$this->assertSame($expected, $migration->shouldRun());
}

public function shouldRunProvider()
{
yield 'should not run if both fields exist' => [
[],
false,
];

yield 'should not run if urlSuffix exist' => [
['languagePrefix'],
false,
];

yield 'should not run if languagePrefix exist' => [
['urlSuffix'],
false,
];

yield 'should run if both fields do not exist' => [
['languagePrefix', 'urlSuffix'],
true,
];
}

public function testRun()
{
static::resetDatabaseSchema();

/** @var Connection $connection */
$connection = static::$container->get('database_connection');

$connection->exec('ALTER TABLE tl_page DROP languagePrefix, DROP urlSuffix');
$columns = $connection->getSchemaManager()->listTableColumns('tl_page');
$this->assertFalse(isset($columns['languageprefix']));
$this->assertFalse(isset($columns['urlsuffix']));

$migration = new RoutingMigration($connection);

$result = $migration->run();

$this->assertInstanceOf(MigrationResult::class, $result);
$this->assertTrue($result->isSuccessful());

$columns = $connection->getSchemaManager()->listTableColumns('tl_page');
$this->assertTrue(isset($columns['languageprefix']));
$this->assertTrue(isset($columns['urlsuffix']));
}
}

0 comments on commit c1f2f93

Please sign in to comment.