cc9591a57ccbb0587e0e6f195afdc17a102d1c3c
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / Database / Schema / SchemaMigratorTest.php
1 <?php
2 declare(strict_types=1);
3
4 namespace TYPO3\CMS\Core\Tests\Functional\Database\Schema;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Doctrine\DBAL\Schema\AbstractSchemaManager;
20 use Doctrine\DBAL\Schema\Table;
21 use Doctrine\DBAL\Types\BigIntType;
22 use Doctrine\DBAL\Types\TextType;
23 use TYPO3\CMS\Core\Database\ConnectionPool;
24 use TYPO3\CMS\Core\Database\Schema\SchemaMigrator;
25 use TYPO3\CMS\Core\Database\Schema\SqlReader;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27
28 /**
29 * Test case for \TYPO3\CMS\Core\Database\Schema\SchemaMigratorTest
30 */
31 class SchemaMigratorTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
32 {
33 /**
34 * @var SqlReader
35 */
36 protected $sqlReader;
37
38 /**
39 * @var ConnectionPool
40 */
41 protected $connectionPool;
42
43 /**
44 * @var AbstractSchemaManager
45 */
46 protected $schemaManager;
47
48 /**
49 * @var \TYPO3\CMS\Core\Database\Schema\SchemaMigrator
50 */
51 protected $subject;
52
53 /**
54 * @var string
55 */
56 protected $tableName = 'a_test_table';
57
58 /**
59 * Sets up this test suite.
60 */
61 protected function setUp()
62 {
63 parent::setUp();
64 $this->subject = GeneralUtility::makeInstance(SchemaMigrator::class);
65 $this->sqlReader = GeneralUtility::makeInstance(SqlReader::class);
66 $this->connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
67 $this->schemaManager = $this->connectionPool->getConnectionForTable($this->tableName)->getSchemaManager();
68 $this->prepareTestTable();
69 }
70
71 /**
72 * Tears down this test suite.
73 */
74 protected function tearDown()
75 {
76 parent::tearDown();
77
78 if ($this->schemaManager->tablesExist([$this->tableName])) {
79 $this->schemaManager->dropTable($this->tableName);
80 }
81 if ($this->schemaManager->tablesExist(['zzz_deleted_' . $this->tableName])) {
82 $this->schemaManager->dropTable('zzz_deleted_' . $this->tableName);
83 }
84 if ($this->schemaManager->tablesExist(['another_test_table'])) {
85 $this->schemaManager->dropTable('another_test_table');
86 }
87 }
88
89 /**
90 * @test
91 */
92 public function createNewTable()
93 {
94 if ($this->schemaManager->tablesExist([$this->tableName])) {
95 $this->schemaManager->dropTable($this->tableName);
96 }
97
98 $statements = $this->readFixtureFile('newTable');
99 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
100
101 $this->subject->migrate(
102 $statements,
103 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['create_table']
104 );
105
106 $this->assertCount(6, $this->getTableDetails()->getColumns());
107 }
108
109 /**
110 * @test
111 */
112 public function createNewTableIfNotExists()
113 {
114 $statements = $this->readFixtureFile('ifNotExists');
115 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
116
117 $this->subject->migrate(
118 $statements,
119 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['create_table']
120 );
121
122 $this->assertTrue($this->schemaManager->tablesExist(['another_test_table']));
123 }
124
125 /**
126 * @test
127 */
128 public function addNewColumns()
129 {
130 $statements = $this->readFixtureFile('addColumnsToTable');
131 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
132
133 $this->subject->migrate(
134 $statements,
135 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
136 );
137
138 $this->assertCount(7, $this->getTableDetails()->getColumns());
139 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
140 $this->assertTrue($this->getTableDetails()->hasColumn('description'));
141 }
142
143 /**
144 * @test
145 */
146 public function changeExistingColumn()
147 {
148 $statements = $this->readFixtureFile('changeExistingColumn');
149 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
150
151 $this->assertEquals(50, $this->getTableDetails()->getColumn('title')->getLength());
152 $this->assertEmpty($this->getTableDetails()->getColumn('title')->getDefault());
153
154 $this->subject->migrate(
155 $statements,
156 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
157 );
158
159 $this->assertEquals(100, $this->getTableDetails()->getColumn('title')->getLength());
160 $this->assertEquals('Title', $this->getTableDetails()->getColumn('title')->getDefault());
161 }
162
163 /**
164 * @test
165 */
166 public function notNullWithoutDefaultValue()
167 {
168 $statements = $this->readFixtureFile('notNullWithoutDefaultValue');
169 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
170
171 $this->subject->migrate(
172 $statements,
173 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
174 );
175
176 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
177 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
178 $this->assertTrue($this->getTableDetails()->getColumn('aTestField')->getNotnull());
179 }
180
181 /**
182 * @test
183 */
184 public function defaultNullWithoutNotNull()
185 {
186 $statements = $this->readFixtureFile('defaultNullWithoutNotNull');
187 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
188
189 $this->subject->migrate(
190 $statements,
191 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['add']
192 );
193
194 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
195 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
196 $this->assertFalse($this->getTableDetails()->getColumn('aTestField')->getNotnull());
197 $this->assertNull($this->getTableDetails()->getColumn('aTestField')->getDefault());
198 }
199
200 /**
201 * @test
202 * @group mysql
203 */
204 public function renameUnusedField()
205 {
206 $statements = $this->readFixtureFile('unusedColumn');
207 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
208
209 $this->subject->migrate(
210 $statements,
211 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
212 );
213
214 $this->assertFalse($this->getTableDetails()->hasColumn('hidden'));
215 $this->assertTrue($this->getTableDetails()->hasColumn('zzz_deleted_hidden'));
216 }
217
218 /**
219 * @test
220 */
221 public function renameUnusedTable()
222 {
223 $statements = $this->readFixtureFile('unusedTable');
224 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
225
226 $this->subject->migrate(
227 $statements,
228 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change_table']
229 );
230
231 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
232 $this->assertContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
233 }
234
235 /**
236 * @test
237 */
238 public function dropUnusedField()
239 {
240 $connection = $this->connectionPool->getConnectionForTable($this->tableName);
241 $fromSchema = $this->schemaManager->createSchema();
242 $toSchema = clone $fromSchema;
243 $toSchema->getTable($this->tableName)->addColumn('zzz_deleted_testfield', 'integer');
244 $statements = $fromSchema->getMigrateToSql(
245 $toSchema,
246 $connection->getDatabasePlatform()
247 );
248 $connection->executeUpdate($statements[0]);
249 $this->assertTrue($this->getTableDetails()->hasColumn('zzz_deleted_testfield'));
250
251 $statements = $this->readFixtureFile('newTable');
252 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
253 $this->subject->migrate(
254 $statements,
255 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['drop']
256 );
257
258 $this->assertFalse($this->getTableDetails()->hasColumn('zzz_deleted_testfield'));
259 }
260
261 /**
262 * @test
263 */
264 public function dropUnusedTable()
265 {
266 $this->schemaManager->renameTable($this->tableName, 'zzz_deleted_' . $this->tableName);
267 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
268 $this->assertContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
269
270 $statements = $this->readFixtureFile('newTable');
271 $updateSuggestions = $this->subject->getUpdateSuggestions($statements, true);
272 $this->subject->migrate(
273 $statements,
274 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['drop_table']
275 );
276
277 $this->assertNotContains($this->tableName, $this->schemaManager->listTableNames());
278 $this->assertNotContains('zzz_deleted_' . $this->tableName, $this->schemaManager->listTableNames());
279 }
280
281 /**
282 * @test
283 * @group mysql
284 */
285 public function installPerformsOnlyAddAndCreateOperations()
286 {
287 $statements = $this->readFixtureFile('addCreateChange');
288 $this->subject->install($statements, true);
289
290 $this->assertContains('another_test_table', $this->schemaManager->listTableNames());
291 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
292 $this->assertTrue($this->getTableDetails()->hasIndex('title'));
293 $this->assertTrue($this->getTableDetails()->getIndex('title')->isUnique());
294 $this->assertNotInstanceOf(BigIntType::class, $this->getTableDetails()->getColumn('pid')->getType());
295 }
296
297 /**
298 * @test
299 */
300 public function installDoesNotAddIndexOnChangedColumn()
301 {
302 $statements = $this->readFixtureFile('addIndexOnChangedColumn');
303 $this->subject->install($statements, true);
304
305 $this->assertNotInstanceOf(TextType::class, $this->getTableDetails()->getColumn('title')->getType());
306 $this->assertFalse($this->getTableDetails()->hasIndex('title'));
307 }
308
309 /**
310 * @test
311 * @group mysql
312 */
313 public function installCanPerformChangeOperations()
314 {
315 $statements = $this->readFixtureFile('addCreateChange');
316 $this->subject->install($statements);
317
318 $this->assertContains('another_test_table', $this->schemaManager->listTableNames());
319 $this->assertTrue($this->getTableDetails()->hasColumn('title'));
320 $this->assertTrue($this->getTableDetails()->hasIndex('title'));
321 $this->assertTrue($this->getTableDetails()->getIndex('title')->isUnique());
322 $this->assertInstanceOf(BigIntType::class, $this->getTableDetails()->getColumn('pid')->getType());
323 }
324
325 /**
326 * @test
327 * @group mysql
328 */
329 public function importStaticDataInsertsRecords()
330 {
331 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'importStaticData.sql']));
332 $connection = $this->connectionPool->getConnectionForTable($this->tableName);
333 $statements = $this->sqlReader->getInsertStatementArray($sqlCode);
334 $this->subject->importStaticData($statements);
335
336 $this->assertEquals(2, $connection->count('*', $this->tableName, []));
337 }
338
339 /**
340 * @test
341 */
342 public function importStaticDataIgnoresTableDefinitions()
343 {
344 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', 'importStaticData.sql']));
345 $statements = $this->sqlReader->getStatementArray($sqlCode);
346 $this->subject->importStaticData($statements);
347
348 $this->assertNotContains('another_test_table', $this->schemaManager->listTableNames());
349 }
350
351 /**
352 * @test
353 * @group mysql
354 */
355 public function changeTableEngine()
356 {
357 $statements = $this->readFixtureFile('alterTableEngine');
358 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
359
360 $index = array_keys($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change'])[0];
361 $this->assertStringEndsWith(
362 'ENGINE = MyISAM',
363 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change'][$index]
364 );
365
366 $this->subject->migrate(
367 $statements,
368 $updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']
369 );
370
371 $updateSuggestions = $this->subject->getUpdateSuggestions($statements);
372 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
373 $this->assertEmpty($updateSuggestions[ConnectionPool::DEFAULT_CONNECTION_NAME]['change']);
374 }
375
376 /**
377 * Create the base table for all migration tests
378 */
379 protected function prepareTestTable()
380 {
381 $statements = $this->readFixtureFile('newTable');
382 $this->subject->install($statements, true);
383 }
384
385 /**
386 * Helper to return the Doctrine Table object for the test table
387 *
388 * @return \Doctrine\DBAL\Schema\Table
389 */
390 protected function getTableDetails(): Table
391 {
392 return $this->schemaManager->listTableDetails($this->tableName);
393 }
394
395 /**
396 * Helper to read a fixture SQL file and convert it into a statement array.
397 *
398 * @param string $fixtureName
399 * @return array
400 */
401 protected function readFixtureFile(string $fixtureName): array
402 {
403 $sqlCode = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'Fixtures', $fixtureName]) . '.sql');
404
405 return $this->sqlReader->getCreateTableStatementArray($sqlCode);
406 }
407 }