Skip to content

Commit a517980

Browse files
Merge pull request #7 from vierge-noire/cake3_next
Alias connections before running migrations
2 parents 504eb5b + 92d9ec8 commit a517980

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

src/Migrator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static function migrate(array $config = [])
4646
{
4747
$migrator = new static();
4848

49+
// Make sure that the connections are aliased, in case
50+
// the migrations invoke the table registry.
51+
$migrator->getFixtureManager()->aliasConnections();
52+
4953
$migrator
5054
->prepareConfig($config)
5155
->dropTablesForMissingMigrations()
@@ -129,4 +133,4 @@ protected function getFixtureManager(): FixtureManager
129133
{
130134
return $this->fixtureManager;
131135
}
132-
}
136+
}

tests/TestApp/config/Migrations/20200208100000_app_migration.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717
class AppMigration extends AbstractMigration
1818
{
1919
public function up()
20-
{}
20+
{
21+
$this->table('articles')
22+
->addPrimaryKey(['id'])
23+
->addColumn('title', 'string', [
24+
'limit' => 128,
25+
'null' => false,
26+
])
27+
->addTimestamps('created', 'modified')
28+
->create();
29+
}
2130

2231
public function down()
2332
{}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright (c) 2020 Juan Pablo Ramirez and Nicolas Masson
10+
* @link https://webrider.de/
11+
* @since 1.0.0
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
namespace TestApp\Model\Table;
15+
16+
use Cake\ORM\Query;
17+
use Cake\ORM\Table;
18+
19+
class ArticlesTable extends Table
20+
{}

tests/TestCase/MigratorTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616

1717
use Cake\Datasource\ConnectionManager;
18+
use Cake\ORM\TableRegistry;
1819
use Cake\TestSuite\TestCase;
1920
use CakephpTestMigrator\Migrator;
2021

@@ -64,4 +65,11 @@ public function testMigrate()
6465
$this->assertSame(['FooMigration'], $fooPluginMigrations);
6566
$this->assertSame(['BarMigration'], $barPluginMigrations);
6667
}
67-
}
68+
69+
public function testTableRegistryConnectionName()
70+
{
71+
$Articles = TableRegistry::getTableLocator()->get('Articles');
72+
ConnectionManager::getConfigOrFail('default');
73+
$this->assertSame('test', $Articles->getConnection()->configName());
74+
}
75+
}

tests/bootstrap.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Cake\Core\Configure;
4+
use Cake\Datasource\ConnectionManager;
45

56
if (!defined('DS')) {
67
define('DS', DIRECTORY_SEPARATOR);
@@ -84,13 +85,17 @@ function loadPHPUnitAliases()
8485
],
8586
];
8687

87-
\Cake\Datasource\ConnectionManager::setConfig('test', $dbConnection);
88+
ConnectionManager::setConfig('test', $dbConnection);
89+
90+
$dbDefaultConnection = $dbConnection;
91+
$dbDefaultConnection['database'] = 'migrator';
92+
ConnectionManager::setConfig('default', $dbConnection);
8893

8994
$dbConnection['migrations'] = ['plugin' => 'BarPlugin'];
90-
\Cake\Datasource\ConnectionManager::setConfig('test_2', $dbConnection);
95+
ConnectionManager::setConfig('test_2', $dbConnection);
9196

9297
$dbConnection['migrations'] = true;
93-
\Cake\Datasource\ConnectionManager::setConfig('test_3', $dbConnection);
98+
ConnectionManager::setConfig('test_3', $dbConnection);
9499

95100
\Cake\Core\Plugin::load('FooPlugin');
96-
\Cake\Core\Plugin::load('BarPlugin');
101+
\Cake\Core\Plugin::load('BarPlugin');

0 commit comments

Comments
 (0)