diff --git a/CHANGELOG.md b/CHANGELOG.md index 2723536..c9ea90e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Support for `formal/orm` `4` +- The versions table can be modified in `Factory::storeVersionsInDatabase()` via its second parameter ## 1.0.0 - 2024-10-04 diff --git a/proofs/factory.php b/proofs/factory.php index 2bdb8c3..f925fbe 100644 --- a/proofs/factory.php +++ b/proofs/factory.php @@ -14,6 +14,7 @@ Path, }; use Innmind\Immutable\Sequence; +use Innmind\BlackBox\Set; return static function() { yield test( @@ -97,4 +98,59 @@ static function($assert) { $assert->count(1, $versions); }, ); + + yield proof( + 'Store migrations versions in a specified table name', + given( + Set\Strings::madeOf( + Set\Chars::uppercaseLetter(), + Set\Chars::lowercaseLetter(), + )->between(1, 64), + ), + static function($assert, $table) { + $os = OS::build(); + + $port = \getenv('DB_PORT') ?: '3306'; + $dsn = Url::of("mysql://root:root@127.0.0.1:$port/example"); + $sql = $os->remote()->sql($dsn); + + $sql(Query\SQL::of("drop table if exists $table")); + + $migrations = Factory::of($os) + ->storeVersionsInDatabase($dsn, $table) + ->sql() + ->of(Sequence::of( + SQL\Migration::of( + 'a', + Query\SQL::of('create table if not exists `test` (`value` int not null)'), + Query\SQL::of('drop table `test`'), + ), + )); + + [$successfully, $versions] = $migrations + ->migrate($dsn) + ->match( + static fn($versions) => [true, $versions], + static fn($_, $versions) => [false, $versions], + ); + + $assert->true($successfully); + $assert->count(1, $versions); + + [$successfully, $versions] = $migrations + ->migrate($dsn) + ->match( + static fn($versions) => [true, $versions], + static fn($_, $versions) => [false, $versions], + ); + + $assert->true($successfully); + $assert->count(0, $versions); + + $assert->count( + 1, + $sql(Query\SQL::of("select * from $table")), + ); + }, + ); }; diff --git a/src/Factory.php b/src/Factory.php index a6caadd..af3210c 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -30,8 +30,13 @@ public static function of(OperatingSystem $os): self return new self($os); } - public function storeVersionsInDatabase(Url $dsn): Factory\Configured - { + /** + * @param ?non-empty-string $table + */ + public function storeVersionsInDatabase( + Url $dsn, + ?string $table = null, + ): Factory\Configured { $connection = $this->os->remote()->sql($dsn); $aggregates = Aggregates::of( Types::of( @@ -42,6 +47,10 @@ public function storeVersionsInDatabase(Url $dsn): Factory\Configured ), ); + if (\is_string($table)) { + $aggregates = $aggregates->mapName(static fn() => $table); + } + return Factory\Configured::of( $this->os, Manager::sql($connection, $aggregates),