Skip to content

Commit

Permalink
Merge pull request #6 from vierge-noire/next
Browse files Browse the repository at this point in the history
Alias connections before running migrations
  • Loading branch information
pabloelcolombiano authored Mar 12, 2021
2 parents 9a87e5a + 025aa1b commit c373fc8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static function migrate(array $config = []): void
{
$migrator = new static();

// Make sure that the connections are aliased, in case
// the migrations invoke the table registry.
$migrator->getFixtureManager()->aliasConnections();

$migrator
->prepareConfig($config)
->dropTablesForMissingMigrations()
Expand Down Expand Up @@ -129,4 +133,4 @@ protected function getFixtureManager(): FixtureManager
{
return $this->fixtureManager;
}
}
}
11 changes: 10 additions & 1 deletion tests/TestApp/config/Migrations/20200208100000_app_migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
class AppMigration extends AbstractMigration
{
public function up()
{}
{
$this->table('articles')
->addPrimaryKey(['id'])
->addColumn('title', 'string', [
'limit' => 128,
'null' => false,
])
->addTimestamps('created', 'modified')
->create();
}

public function down()
{}
Expand Down
20 changes: 20 additions & 0 deletions tests/TestApp/src/Model/Table/ArticlesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

/**
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson
* @link https://webrider.de/
* @since 1.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\Table;

class ArticlesTable extends Table
{}
10 changes: 9 additions & 1 deletion tests/TestCase/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use CakephpTestMigrator\Migrator;

Expand Down Expand Up @@ -59,4 +60,11 @@ public function testMigrate()
$this->assertSame(['FooMigration'], $fooPluginMigrations);
$this->assertSame(['BarMigration'], $barPluginMigrations);
}
}

public function testTableRegistryConnectionName()
{
$Articles = TableRegistry::getTableLocator()->get('Articles');
ConnectionManager::getConfigOrFail('default');
$this->assertSame('test', $Articles->getConnection()->configName());
}
}
11 changes: 8 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;

if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
Expand Down Expand Up @@ -67,10 +68,14 @@
],
];

\Cake\Datasource\ConnectionManager::setConfig('test', $dbConnection);
ConnectionManager::setConfig('test', $dbConnection);

$dbDefaultConnection = $dbConnection;
$dbDefaultConnection['database'] = 'migrator';
ConnectionManager::setConfig('default', $dbConnection);

$dbConnection['migrations'] = ['plugin' => 'BarPlugin'];
\Cake\Datasource\ConnectionManager::setConfig('test_2', $dbConnection);
ConnectionManager::setConfig('test_2', $dbConnection);

$dbConnection['migrations'] = true;
\Cake\Datasource\ConnectionManager::setConfig('test_3', $dbConnection);
ConnectionManager::setConfig('test_3', $dbConnection);

0 comments on commit c373fc8

Please sign in to comment.