|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cycle\Migrations\Tests; |
| 6 | + |
| 7 | +use Cycle\Database\Driver\DriverInterface; |
| 8 | +use Cycle\Migrations\Migration; |
| 9 | +use Cycle\Migrations\State; |
| 10 | +use Psr\Log\LoggerAwareInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * The Migrator must correctly resolve the state of executed migrations |
| 14 | + * when the driver is configured with the `withDatetimeMicroseconds` option. |
| 15 | + * |
| 16 | + * @see https://github.com/cycle/migrations/issues/66 |
| 17 | + */ |
| 18 | +abstract class DatetimeMicrosecondsTest extends BaseTest |
| 19 | +{ |
| 20 | + public function getDriver(): DriverInterface |
| 21 | + { |
| 22 | + if (!isset($this->driver)) { |
| 23 | + $config = clone self::$config[static::DRIVER]; |
| 24 | + $config->options['withDatetimeMicroseconds'] = true; |
| 25 | + |
| 26 | + $this->driver = $config->driver::create($config); |
| 27 | + } |
| 28 | + |
| 29 | + if (self::$config['debug'] && $this->driver instanceof LoggerAwareInterface) { |
| 30 | + $this->driver->setLogger(new TestLogger()); |
| 31 | + } |
| 32 | + |
| 33 | + return $this->driver; |
| 34 | + } |
| 35 | + |
| 36 | + public function testMigrationResolvedAsExecutedAfterRun(): void |
| 37 | + { |
| 38 | + $this->migrator->configure(); |
| 39 | + |
| 40 | + $schema = $this->schema('sample'); |
| 41 | + $schema->primary('id'); |
| 42 | + $schema->integer('value'); |
| 43 | + $this->atomize('migration1', [$schema]); |
| 44 | + |
| 45 | + $migration = $this->migrator->run(); |
| 46 | + |
| 47 | + $this->assertInstanceOf(Migration::class, $migration); |
| 48 | + $this->assertSame(State::STATUS_EXECUTED, $migration->getState()->getStatus()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testSecondRunHasNothingToExecute(): void |
| 52 | + { |
| 53 | + $this->migrator->configure(); |
| 54 | + |
| 55 | + $schema = $this->schema('sample'); |
| 56 | + $schema->primary('id'); |
| 57 | + $schema->integer('value'); |
| 58 | + $this->atomize('migration1', [$schema]); |
| 59 | + |
| 60 | + $this->migrator->run(); |
| 61 | + |
| 62 | + // The only migration has been executed, nothing is pending |
| 63 | + $this->assertNull($this->migrator->run()); |
| 64 | + } |
| 65 | + |
| 66 | + public function testUpgradeFromLegacyTableStructure(): void |
| 67 | + { |
| 68 | + // The migration table as it was created by previous versions of the package: |
| 69 | + // datetime columns without precision |
| 70 | + $schema = $this->db->table('migrations')->getSchema(); |
| 71 | + $schema->primary('id'); |
| 72 | + $schema->string('migration', 191)->nullable(false); |
| 73 | + $schema->datetime('time_executed')->datetime(); |
| 74 | + $schema->datetime('created_at')->datetime(); |
| 75 | + $schema->index(['migration', 'created_at'])->unique(true); |
| 76 | + $schema->save(); |
| 77 | + |
| 78 | + $this->migrator->configure(); |
| 79 | + $this->assertTrue($this->migrator->isConfigured()); |
| 80 | + |
| 81 | + $schema = $this->schema('sample'); |
| 82 | + $schema->primary('id'); |
| 83 | + $schema->integer('value'); |
| 84 | + $this->atomize('migration1', [$schema]); |
| 85 | + |
| 86 | + $migration = $this->migrator->run(); |
| 87 | + |
| 88 | + $this->assertInstanceOf(Migration::class, $migration); |
| 89 | + $this->assertSame(State::STATUS_EXECUTED, $migration->getState()->getStatus()); |
| 90 | + $this->assertNull($this->migrator->run()); |
| 91 | + } |
| 92 | + |
| 93 | + public function testRollbackAfterRun(): void |
| 94 | + { |
| 95 | + $this->migrator->configure(); |
| 96 | + |
| 97 | + $schema = $this->schema('sample'); |
| 98 | + $schema->primary('id'); |
| 99 | + $schema->integer('value'); |
| 100 | + $this->atomize('migration1', [$schema]); |
| 101 | + |
| 102 | + $this->migrator->run(); |
| 103 | + $this->assertTrue($this->db->hasTable('sample')); |
| 104 | + |
| 105 | + $migration = $this->migrator->rollback(); |
| 106 | + |
| 107 | + $this->assertInstanceOf(Migration::class, $migration); |
| 108 | + $this->assertFalse($this->db->hasTable('sample')); |
| 109 | + } |
| 110 | +} |
0 commit comments