Skip to content

Commit a5b8737

Browse files
committed
wip
1 parent 0c56e2e commit a5b8737

File tree

7 files changed

+122
-10
lines changed

7 files changed

+122
-10
lines changed

packages/database/src/functions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
namespace Tempest\Database {
4-
use ReflectionException;
54
use Tempest\Database\Builder\ModelInspector;
65
use Tempest\Database\Builder\QueryBuilders\QueryBuilder;
76

src/Tempest/Framework/Commands/MigrateDownCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\Framework\Commands;
66

77
use Tempest\Console\Console;
8+
use Tempest\Console\ConsoleArgument;
89
use Tempest\Console\ConsoleCommand;
910
use Tempest\Console\Middleware\CautionMiddleware;
1011
use Tempest\Console\Middleware\ForceMiddleware;
@@ -28,9 +29,11 @@ public function __construct(
2829
description: 'Rollbacks all executed migrations',
2930
middleware: [ForceMiddleware::class, CautionMiddleware::class],
3031
)]
31-
public function __invoke(): void
32-
{
33-
$this->migrationManager->down();
32+
public function __invoke(
33+
#[ConsoleArgument(description: 'Use a specific database.')]
34+
?string $database = null,
35+
): void {
36+
$this->migrationManager->useDatabase($database)->down();
3437

3538
$this->console->success(sprintf('Rolled back %s migrations', $this->count));
3639
}

src/Tempest/Framework/Commands/MigrateFreshCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function __construct(
3434
public function __invoke(
3535
#[ConsoleArgument(description: 'Validates the integrity of existing migration files by checking if they have been tampered with.')]
3636
bool $validate = true,
37+
#[ConsoleArgument(description: 'Use a specific database.')]
38+
?string $database = null,
3739
): ExitCode {
3840
if ($validate) {
3941
$validationSuccess = $this->console->call(MigrateValidateCommand::class);
@@ -44,13 +46,13 @@ public function __invoke(
4446
}
4547

4648
$this->console->header('Dropping tables');
47-
$this->migrationManager->dropAll();
49+
$this->migrationManager->useDatabase($database)->dropAll();
4850

4951
if ($this->count === 0) {
5052
$this->console->info('There is no migration to drop.');
5153
}
5254

53-
return $this->console->call(MigrateUpCommand::class, ['fresh' => false, 'validate' => false]);
55+
return $this->console->call(MigrateUpCommand::class, ['fresh' => false, 'validate' => false, 'database' => $database]);
5456
}
5557

5658
#[EventHandler]

src/Tempest/Framework/Commands/MigrateUpCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function __invoke(
3939
bool $validate = true,
4040
#[ConsoleArgument(description: 'Drops all tables and rerun migrations from scratch.')]
4141
bool $fresh = false,
42+
#[ConsoleArgument(description: 'Use a specific database.')]
43+
?string $database = null,
4244
): ExitCode {
4345
if ($validate) {
4446
$validationSuccess = $this->console->call(MigrateValidateCommand::class);
@@ -49,11 +51,11 @@ public function __invoke(
4951
}
5052

5153
if ($fresh) {
52-
return $this->console->call(MigrateFreshCommand::class, ['validate' => false, 'fresh' => false]);
54+
return $this->console->call(MigrateFreshCommand::class, ['validate' => false, 'database' => $database]);
5355
}
5456

5557
$this->console->header('Migrating');
56-
$this->migrationManager->up();
58+
$this->migrationManager->useDatabase($database)->up();
5759

5860
if ($this->count === 0) {
5961
$this->console->info('There is no new migration to run.');

tests/Integration/Database/MultiDatabaseTest.php

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Integration\Database;
44

5+
use PDOException;
56
use Tempest\Container\Exceptions\CannotResolveTaggedDependency;
67
use Tempest\Database\Config\DatabaseDialect;
78
use Tempest\Database\Config\MysqlConfig;
89
use Tempest\Database\Config\SQLiteConfig;
910
use Tempest\Database\DatabaseInitializer;
1011
use Tempest\Database\Id;
1112
use Tempest\Database\Migrations\CreateMigrationsTable;
13+
use Tempest\Database\Migrations\Migration;
1214
use Tempest\Database\Migrations\MigrationManager;
1315
use Tests\Tempest\Fixtures\Migrations\CreatePublishersTable;
1416
use Tests\Tempest\Fixtures\Modules\Books\Models\Publisher;
@@ -17,6 +19,9 @@
1719

1820
use function Tempest\Database\query;
1921

22+
/**
23+
* @property \Tempest\Console\Testing\ConsoleTester $console
24+
*/
2025
final class MultiDatabaseTest extends FrameworkIntegrationTestCase
2126
{
2227
protected function setUp(): void
@@ -151,7 +156,85 @@ public function test_fails_with_unknown_connection(): void
151156
}
152157
}
153158

154-
public function test_with_migration_command(): void
159+
public function test_migrate_up_command(): void
155160
{
161+
$this->console
162+
->call('migrate:up --database=main')
163+
->assertSuccess();
164+
165+
$this->assertTrue(query(Migration::class)->count()->useDatabase('main')->execute() > 0);
166+
167+
$this->assertException(
168+
PDOException::class,
169+
fn () => $this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0),
170+
);
171+
172+
$this->console
173+
->call('migrate:up --database=backup')
174+
->assertSuccess();
175+
176+
$this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0);
177+
}
178+
179+
public function test_migrate_fresh_command(): void
180+
{
181+
$this->console
182+
->call('migrate:fresh --database=main')
183+
->assertSuccess();
184+
185+
$this->assertTrue(query(Migration::class)->count()->useDatabase('main')->execute() > 0);
186+
187+
$this->assertException(
188+
PDOException::class,
189+
fn () => $this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0),
190+
);
191+
192+
$this->console
193+
->call('migrate:fresh --database=backup')
194+
->assertSuccess();
195+
196+
$this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0);
197+
}
198+
199+
public function test_migrate_up_fresh_command(): void
200+
{
201+
$this->console
202+
->call('migrate:up --fresh --database=main')
203+
->assertSuccess();
204+
205+
$this->assertTrue(query(Migration::class)->count()->useDatabase('main')->execute() > 0);
206+
207+
$this->assertException(
208+
PDOException::class,
209+
fn () => $this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0),
210+
);
211+
212+
$this->console
213+
->call('migrate:up --fresh --database=backup')
214+
->assertSuccess();
215+
216+
$this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0);
217+
}
218+
219+
public function test_migrate_down_command(): void
220+
{
221+
$this->console
222+
->call('migrate:up --database=main')
223+
->assertSuccess();
224+
225+
$this->console
226+
->call('migrate:up --database=backup')
227+
->assertSuccess();
228+
229+
$this->console
230+
->call('migrate:down --database=backup')
231+
->assertSuccess();
232+
233+
$this->assertTrue(query(Migration::class)->count()->useDatabase('main')->execute() > 0);
234+
235+
$this->assertException(
236+
PDOException::class,
237+
fn () => $this->assertTrue(query(Migration::class)->count()->useDatabase('backup')->execute() > 0),
238+
);
156239
}
157240
}

tests/Integration/FrameworkIntegrationTestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Tempest\Integration;
66

7+
use Closure;
78
use InvalidArgumentException;
89
use Tempest\Console\ConsoleApplication;
910
use Tempest\Console\Input\ConsoleArgumentBag;
@@ -32,6 +33,7 @@
3233
use Tempest\View\View;
3334
use Tempest\View\ViewConfig;
3435
use Tempest\View\ViewRenderer;
36+
use Throwable;
3537

3638
use function Tempest\Support\Path\normalize;
3739

@@ -188,4 +190,25 @@ protected function assertSameWithoutBackticks(string $expected, string $actual):
188190
$clean($actual),
189191
);
190192
}
193+
194+
protected function assertException(
195+
string $expectedExceptionClass,
196+
Closure $handler,
197+
?Closure $assertException = null,
198+
): void {
199+
try {
200+
$handler();
201+
} catch (Throwable $throwable) {
202+
$this->assertInstanceOf($expectedExceptionClass, $throwable);
203+
204+
if ($assertException !== null) {
205+
$assertException($throwable);
206+
}
207+
208+
return;
209+
}
210+
211+
/* @phpstan-ignore-next-line */
212+
$this->assertTrue(false, "Expected exception {$expectedExceptionClass} was not thrown");
213+
}
191214
}

tests/Integration/TestingDatabaseInitializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function initialize(ClassReflector $class, null|string|UnitEnum $tag, Con
3636
default => '',
3737
};
3838

39+
/** @var PDOConnection|null $connection */
3940
$connection = self::$connections[$tag] ?? null;
4041

4142
if ($connection === null) {
@@ -55,7 +56,6 @@ public function initialize(ClassReflector $class, null|string|UnitEnum $tag, Con
5556
return new GenericDatabase(
5657
$connection,
5758
new GenericTransactionManager($connection),
58-
$container->get(DatabaseDialect::class),
5959
);
6060
}
6161
}

0 commit comments

Comments
 (0)