Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions proofs/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Path,
};
use Innmind\Immutable\Sequence;
use Innmind\BlackBox\Set;

return static function() {
yield test(
Expand Down Expand Up @@ -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")),
);
},
);
};
13 changes: 11 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down