Skip to content

Commit 73e974d

Browse files
committed
stancl/tenancy support
1 parent ad1cb95 commit 73e974d

4 files changed

+68
-12
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Laravel Database State
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/pxlrbt/laravel-db-state.svg?style=flat-square)](https://packagist.org/packages/pxlrbt/laravel-db-state)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/pxlrbt/laravel-db-state/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/pxlrbt/laravel-db-state/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/pxlrbt/laravel-db-state/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/pxlrbt/laravel-db-state/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/pxlrbt/laravel-db-state.svg?style=flat-square)](https://packagist.org/packages/pxlrbt/laravel-db-state)
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/pxlrbt/laravel-database-state.svg?style=flat-square)](https://packagist.org/packages/pxlrbt/laravel-database-state)
4+
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/pxlrbt/laravel-database-state/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/pxlrbt/laravel-database-state/actions?query=workflow%3Arun-tests+branch%3Amain)
5+
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/pxlrbt/laravel-database-state/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/pxlrbt/laravel-database-state/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/pxlrbt/laravel-database-state.svg?style=flat-square)](https://packagist.org/packages/pxlrbt/laravel-database-state)
77

88
Seed critical state your databases with production data.
99

@@ -12,12 +12,12 @@ Seed critical state your databases with production data.
1212
You can install the package via composer:
1313

1414
```bash
15-
composer require pxlrbt/laravel-db-state
15+
composer require pxlrbt/laravel-database-state
1616
```
1717

1818
### Add autoloader
1919

20-
Add the namespace to the `composer.json`
20+
Add the `Database\States` namespace to the `composer.json`
2121

2222
```json
2323
{

src/Commands/SeedDatabaseStateCommand.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public function handle(): int
1818
{
1919
$this->components->info('Seeding database state.');
2020

21+
$this->seedStates();
22+
23+
return self::SUCCESS;
24+
}
25+
26+
protected function seedStates(): void
27+
{
2128
$this->getStateClasses()->each(function ($class) {
2229
$this->components->twoColumnDetail(
2330
$class,
@@ -35,13 +42,16 @@ public function handle(): int
3542
"<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>"
3643
);
3744
});
45+
}
3846

39-
return self::SUCCESS;
47+
protected function getFilePath(): string
48+
{
49+
return database_path('States');
4050
}
4151

4252
protected function getStateClasses(): Collection
4353
{
44-
return collect(app(Filesystem::class)->allFiles(database_path('States')))
54+
return collect(app(Filesystem::class)->files($this->getFilePath()))
4555
->map(fn ($file) => str($file->getPathname())
4656
->replace([base_path(), '.php', '/'], ['', '', '\\'])
4757
->ltrim('\\')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace pxlrbt\LaravelDatabaseState\Commands;
4+
5+
use Illuminate\Database\QueryException;
6+
use Stancl\Tenancy\Concerns\HasTenantOptions;
7+
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
8+
9+
class SeedTenantsDatabaseStateCommand extends SeedDatabaseStateCommand
10+
{
11+
use HasTenantOptions;
12+
13+
protected $signature = 'tenants:db:seed-state';
14+
15+
protected $description = 'Seed tenants database state';
16+
17+
public function handle(): int
18+
{
19+
$this->components->info('Seeding tenants database state.');
20+
21+
foreach ($this->getTenants() as $tenant) {
22+
try {
23+
$tenant->run(function ($tenant) {
24+
$this->components->info("Tenant {$tenant->getTenantKey()}");
25+
26+
// Seed
27+
parent::seedStates();
28+
});
29+
} catch (TenantDatabaseDoesNotExistException|QueryException $th) {
30+
if (! $this->option('skip-failing')) {
31+
throw $th;
32+
}
33+
}
34+
}
35+
36+
return self::SUCCESS;
37+
}
38+
39+
protected function getFilePath(): string
40+
{
41+
return database_path('States/Tenant');
42+
}
43+
}

src/DatabaseStateServiceProvider.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function configurePackage(Package $package): void
2323
->hasConfigFile()
2424
->hasCommands([
2525
Commands\MakeCommand::class,
26-
Commands\SeedDatabaseStateCommand::class
26+
Commands\SeedDatabaseStateCommand::class,
27+
Commands\SeedTenantsDatabaseStateCommand::class,
2728
]);
2829
}
2930

@@ -46,10 +47,12 @@ public function runDatabaseStateSeeder(CommandFinished $event): void
4647
return;
4748
}
4849

49-
if (! str($event->command)->startsWith('migrate')) {
50-
return;
50+
if (str($event->command)->startsWith('migrate')) {
51+
Artisan::call(Commands\SeedDatabaseStateCommand::class, [], $event->output);
5152
}
5253

53-
Artisan::call(Commands\SeedDatabaseStateCommand::class, [], $event->output);
54+
if (str($event->command)->startsWith('tenants:migrate')) {
55+
Artisan::call(Commands\SeedTenantsDatabaseStateCommand::class, [], $event->output);
56+
}
5457
}
5558
}

0 commit comments

Comments
 (0)