-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathUpgradeCommandTest.php
More file actions
230 lines (194 loc) · 7.78 KB
/
UpgradeCommandTest.php
File metadata and controls
230 lines (194 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
declare(strict_types=1);
namespace Migrations\Test\TestCase\Command;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Exception;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Migration\Environment;
use Migrations\Test\TestCase\TestCase;
class UpgradeCommandTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
$this->clearMigrationRecords('test');
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('test');
$connection->execute('DROP TABLE IF EXISTS cake_migrations');
}
public function tearDown(): void
{
$this->clearMigrationRecords('test');
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('test');
$connection->execute('DROP TABLE IF EXISTS cake_migrations');
parent::tearDown();
}
protected function getAdapter(): AdapterInterface
{
$config = ConnectionManager::getConfig('test');
$environment = new Environment('default', [
'connection' => 'test',
'database' => $config['database'],
'migration_table' => 'phinxlog',
]);
return $environment->getAdapter();
}
public function testHelp(): void
{
Configure::write('Migrations.legacyTables', null);
$this->exec('migrations upgrade --help');
$this->assertExitSuccess();
$this->assertOutputContains('Upgrades migration tracking');
$this->assertOutputContains('migrations upgrade --dry-run');
}
public function testExecuteSimpleDryRun(): void
{
Configure::write('Migrations.legacyTables', true);
try {
$this->getAdapter()->createSchemaTable();
} catch (Exception $e) {
// Table probably exists
}
$this->exec('migrations upgrade -c test --dry-run');
$this->assertExitSuccess();
// Check for status output
$this->assertOutputContains('DRY RUN');
$this->assertOutputContains('Creating unified table');
$this->assertOutputContains('Total records migrated');
}
public function testExecuteSimpleExecute(): void
{
Configure::write('Migrations.legacyTables', true);
$config = ConnectionManager::getConfig('test');
$environment = new Environment('default', [
'connection' => 'test',
'database' => $config['database'],
'migration_table' => 'phinxlog',
]);
$adapter = $environment->getAdapter();
try {
$adapter->createSchemaTable();
} catch (Exception $e) {
// Table probably exists
}
$this->exec('migrations upgrade -c test');
$this->assertExitSuccess();
// No dry run and drop table output is present.
$this->assertOutputNotContains('DRY RUN');
$this->assertOutputContains('Creating unified table');
$this->assertOutputContains('Total records migrated');
$this->assertTrue($adapter->hasTable('cake_migrations'));
$this->assertTrue($adapter->hasTable('phinxlog'));
}
public function testExecuteSimpleExecuteDropTables(): void
{
Configure::write('Migrations.legacyTables', true);
$config = ConnectionManager::getConfig('test');
$environment = new Environment('default', [
'connection' => 'test',
'database' => $config['database'],
'migration_table' => 'phinxlog',
]);
$adapter = $environment->getAdapter();
try {
$adapter->createSchemaTable();
} catch (Exception $e) {
// Table probably exists
}
$this->exec('migrations upgrade -c test --drop-tables');
$this->assertExitSuccess();
// Check for status output
$this->assertOutputNotContains('DRY RUN');
$this->assertOutputContains('Creating unified table');
$this->assertOutputContains('Dropping legacy table');
$this->assertOutputContains('Total records migrated');
$this->assertTrue($adapter->hasTable('cake_migrations'));
$this->assertFalse($adapter->hasTable('phinxlog'));
}
public function testExecuteWithMigrations(): void
{
Configure::write('Migrations.legacyTables', true);
try {
$this->getAdapter()->createSchemaTable();
} catch (Exception $e) {
// Table probably exists
}
$this->getAdapter()->getInsertBuilder()
->insert(['version', 'migration_name', 'breakpoint'])
->into('phinxlog')
->values([
'version' => '20250118143003',
'migration_name' => 'TestMigration',
'breakpoint' => 0,
])
->execute();
$this->exec('migrations upgrade -c test');
$this->assertExitSuccess();
// Check for status output
$this->assertOutputContains('Creating unified table');
$this->assertOutputContains('Total records migrated');
// Validate record in the unified table
$this->assertTrue($this->getAdapter()->hasTable('cake_migrations'));
$rows = $this->getAdapter()->getSelectBuilder()
->select(['version', 'migration_name', 'breakpoint'])
->from('cake_migrations')
->where(['migration_name' => 'TestMigration'])
->all();
$this->assertCount(1, $rows);
}
/**
* Test that plugins with slashes (like CakeDC/Users) are correctly identified
* during upgrade from legacy phinxlog tables.
*/
public function testExecuteWithSlashInPluginName(): void
{
Configure::write('Migrations.legacyTables', true);
// Create the plugin's phinxlog table using the adapter for cross-database compatibility
$config = ConnectionManager::getConfig('test');
$environment = new Environment('default', [
'connection' => 'test',
'database' => $config['database'],
'migration_table' => 'cake_d_c_users_phinxlog',
]);
$adapter = $environment->getAdapter();
try {
$adapter->createSchemaTable();
} catch (Exception $e) {
// Table probably exists
}
// Insert a migration record
$adapter->getInsertBuilder()
->insert(['version', 'migration_name', 'breakpoint'])
->into('cake_d_c_users_phinxlog')
->values([
'version' => '20250118143003',
'migration_name' => 'SlashPluginMigration',
'breakpoint' => 0,
])
->execute();
// Load a fake plugin with a slash in the name using loadPlugins
// which properly integrates with the console application
$this->loadPlugins(['CakeDC/Users' => ['path' => TMP]]);
try {
$this->exec('migrations upgrade -c test');
$this->assertExitSuccess();
$this->assertOutputContains('cake_d_c_users_phinxlog (CakeDC/Users)');
// Verify the plugin column has the correct value with slash
$rows = $this->getAdapter()->getSelectBuilder()
->select(['version', 'migration_name', 'plugin'])
->from('cake_migrations')
->where(['migration_name' => 'SlashPluginMigration'])
->all();
$this->assertCount(1, $rows);
$this->assertSame('CakeDC/Users', $rows[0]['plugin']);
} finally {
// Cleanup
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('test');
$connection->execute('DROP TABLE ' . $connection->getDriver()->quoteIdentifier('cake_d_c_users_phinxlog'));
$this->removePlugins(['CakeDC/Users']);
}
}
}