diff --git a/src/Db/Adapter/AbstractAdapter.php b/src/Db/Adapter/AbstractAdapter.php index 92a1e022a..119c28a92 100644 --- a/src/Db/Adapter/AbstractAdapter.php +++ b/src/Db/Adapter/AbstractAdapter.php @@ -789,7 +789,11 @@ public function getVersions(): array protected function migrationsTable(): MigrationsTableStorage { // TODO Use configure/auto-detect which implmentation to use. - return new MigrationsTableStorage($this, $this->getSchemaTableName()); + return new MigrationsTableStorage( + $this, + $this->getSchemaTableName(), + $this->getOption('plugin'), + ); } /** diff --git a/src/Db/Adapter/MigrationsTableStorage.php b/src/Db/Adapter/MigrationsTableStorage.php index 9c3a1b20c..88950a978 100644 --- a/src/Db/Adapter/MigrationsTableStorage.php +++ b/src/Db/Adapter/MigrationsTableStorage.php @@ -34,23 +34,15 @@ class MigrationsTableStorage * * @param \Migrations\Db\Adapter\AbstractAdapter $adapter The database adapter. * @param string $schemaTableName The schema table name. + * @param string|null $plugin The plugin name. */ public function __construct( protected AbstractAdapter $adapter, protected string $schemaTableName = 'phinxlog', + protected ?string $plugin = null, ) { } - /** - * Gets the schema table name. - * - * @return string - */ - public function getSchemaTableName(): string - { - return $this->schemaTableName; - } - /** * Gets all the migration versions. * @@ -61,7 +53,7 @@ public function getVersions(array $orderBy): SelectQuery { $query = $this->adapter->getSelectBuilder(); $query->select('*') - ->from($this->getSchemaTableName()) + ->from($this->schemaTableName) ->orderBy($orderBy); return $query; @@ -79,7 +71,7 @@ public function recordUp(MigrationInterface $migration, string $startTime, strin { $query = $this->adapter->getInsertBuilder(); $query->insert(['version', 'migration_name', 'start_time', 'end_time', 'breakpoint']) - ->into($this->getSchemaTableName()) + ->into($this->schemaTableName) ->values([ 'version' => (string)$migration->getVersion(), 'migration_name' => substr($migration->getName(), 0, 100), @@ -100,7 +92,7 @@ public function recordDown(MigrationInterface $migration): void { $query = $this->adapter->getDeleteBuilder(); $query->delete() - ->from($this->getSchemaTableName()) + ->from($this->schemaTableName) ->where(['version' => (string)$migration->getVersion()]); $this->adapter->executeQuery($query); } @@ -119,7 +111,7 @@ public function toggleBreakpoint(MigrationInterface $migration): void $this->adapter->query( sprintf( 'UPDATE %1$s SET %2$s = CASE %2$s WHEN true THEN false ELSE true END, %4$s = %4$s WHERE %3$s = ?;', - $this->adapter->quoteTableName($this->getSchemaTableName()), + $this->adapter->quoteTableName($this->schemaTableName), $this->adapter->quoteColumnName('breakpoint'), $this->adapter->quoteColumnName('version'), $this->adapter->quoteColumnName('start_time'), @@ -136,7 +128,7 @@ public function toggleBreakpoint(MigrationInterface $migration): void public function resetAllBreakpoints(): int { $query = $this->adapter->getUpdateBuilder(); - $query->update($this->getSchemaTableName()) + $query->update($this->schemaTableName) ->set([ 'breakpoint' => 0, 'start_time' => $query->identifier('start_time'), @@ -158,7 +150,7 @@ public function resetAllBreakpoints(): int public function markBreakpoint(MigrationInterface $migration, bool $state): void { $query = $this->adapter->getUpdateBuilder(); - $query->update($this->getSchemaTableName()) + $query->update($this->schemaTableName) ->set([ 'breakpoint' => (int)$state, 'start_time' => $query->identifier('start_time'), @@ -183,7 +175,7 @@ public function createTable(): void 'primary_key' => 'version', ]; - $table = new Table($this->getSchemaTableName(), $options, $this->adapter); + $table = new Table($this->schemaTableName, $options, $this->adapter); $table->addColumn('version', 'biginteger', ['null' => false]) ->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true]) ->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true]) @@ -206,7 +198,7 @@ public function createTable(): void */ public function upgradeTable(): void { - $table = new Table($this->getSchemaTableName(), [], $this->adapter); + $table = new Table($this->schemaTableName, [], $this->adapter); if (!$table->hasColumn('migration_name')) { $table ->addColumn( diff --git a/src/Migration/ManagerFactory.php b/src/Migration/ManagerFactory.php index 4a18f9dd1..321c99c00 100644 --- a/src/Migration/ManagerFactory.php +++ b/src/Migration/ManagerFactory.php @@ -112,6 +112,7 @@ public function createConfig(): ConfigInterface 'database' => $connectionConfig['database'], 'migration_table' => $table, 'dryrun' => $this->getOption('dry-run'), + 'plugin' => $plugin, ]; $configData = [ diff --git a/tests/TestCase/Migration/ManagerFactoryTest.php b/tests/TestCase/Migration/ManagerFactoryTest.php index f7fe3a0a0..0352e887f 100644 --- a/tests/TestCase/Migration/ManagerFactoryTest.php +++ b/tests/TestCase/Migration/ManagerFactoryTest.php @@ -26,6 +26,17 @@ public function testConnection(): void $this->assertSame('test', $result->getConfig()->getConnection()); } + public function testCreateConfigPluginAdapter(): void + { + $factory = new ManagerFactory([ + 'connection' => 'test', + 'plugin' => 'Migrator', + ]); + + $config = $factory->createConfig(); + $this->assertSame('Migrator', $config['environment']['plugin']); + } + public function testDsnConnection(): void { $out = new StubConsoleOutput();