feat : 테넌트 부트스트랩 오케스트레이션

Notion : https://www.notion.so/hamss/2579c8d34ba080d586b6faaae249f476?source=copy_link
This commit is contained in:
2025-08-22 15:57:44 +09:00
parent 189bdbfd80
commit cd81285731
12 changed files with 338 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Services\TenantBootstrap\Contracts;
interface TenantBootstrapStep
{
/**
* 스텝 식별자(고유)
*/
public function key(): string;
/**
* 여러 번 실행해도 안전하게 동작해야 함 (idempotent).
*/
public function run(int $tenantId): void;
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Services\TenantBootstrap;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use App\Services\TenantBootstrap\Steps\CapabilityProfilesStep;
use App\Services\TenantBootstrap\Steps\CategoriesStep;
use App\Services\TenantBootstrap\Steps\MenusStep;
use App\Services\TenantBootstrap\Steps\SettingsStep;
class RecipeRegistry
{
/**
* 레시피마다 실행 순서 정의
*/
public function steps(string $recipe = 'STANDARD'): array
{
return match ($recipe) {
'LITE' => [
new CapabilityProfilesStep(),
new CategoriesStep(),
],
default => [ // STANDARD
new CapabilityProfilesStep(),
new CategoriesStep(),
new MenusStep(),
new SettingsStep(),
],
};
}
public function version(string $recipe = 'STANDARD'): int
{
return match ($recipe) {
'LITE' => 1,
default => 1,
};
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Services\TenantBootstrap\Steps;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;
class CapabilityProfilesStep implements TenantBootstrapStep
{
public function key(): string { return 'capability_profiles'; }
public function run(int $tenantId): void
{
$profiles = [
['FINISHED_GOOD','완제품', ['is_sellable'=>1,'is_purchasable'=>0,'is_producible'=>1,'is_stock_managed'=>1]],
['SUB_ASSEMBLY','서브어셈블리', ['is_sellable'=>0,'is_purchasable'=>0,'is_producible'=>1,'is_stock_managed'=>1]],
['PURCHASED_PART','구매부품', ['is_sellable'=>0,'is_purchasable'=>1,'is_producible'=>0,'is_stock_managed'=>1]],
['PHANTOM','팬텀(가상)', ['is_sellable'=>0,'is_purchasable'=>0,'is_producible'=>1,'is_stock_managed'=>0]],
];
foreach ($profiles as [$code,$name,$attrs]) {
DB::table('common_codes')->updateOrInsert(
['tenant_id'=>$tenantId,'code_group'=>'capability_profile','code'=>$code],
[
'name'=>$name,
'attributes'=>json_encode($attrs, JSON_UNESCAPED_UNICODE),
'is_active'=>1, 'sort_order'=>0,
'description'=>'기본 프로필'
]
);
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Services\TenantBootstrap\Steps;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;
class CategoriesStep implements TenantBootstrapStep
{
public function key(): string { return 'categories_seed'; }
public function run(int $tenantId): void
{
$exists = DB::table('categories')->where([
'tenant_id'=>$tenantId, 'code_group'=>'product', 'code'=>'DEFAULT'
])->exists();
if (!$exists) {
DB::table('categories')->insert([
'tenant_id'=>$tenantId,
'parent_id'=>null,
'code_group'=>'product',
'code'=>'DEFAULT',
'name'=>'기본 카테고리',
'profile_code'=>'FINISHED_GOOD',
'is_active'=>1,
'sort_order'=>1,
'created_at'=>now(),
'updated_at'=>now(),
]);
}
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Services\TenantBootstrap\Steps;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;
class MenusStep implements TenantBootstrapStep
{
public function key(): string { return 'menus_seed'; }
public function run(int $tenantId): void
{
// 예시: menus 테이블이 있다고 가정한 최소 스텁 (스키마에 맞춰 수정)
if (!DB::getSchemaBuilder()->hasTable('menus')) return;
$exists = DB::table('menus')->where(['tenant_id'=>$tenantId, 'code'=>'ROOT'])->exists();
if (!$exists) {
DB::table('menus')->insert([
'tenant_id'=>$tenantId,
'name'=>'메인',
'parent_id'=>null, 'sort_order'=>0,
'is_active'=>1, 'created_at'=>now(), 'updated_at'=>now(),
]);
// 필요 시 하위 기본 메뉴들 추가…
}
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Services\TenantBootstrap\Steps;
use App\Services\TenantBootstrap\Contracts\TenantBootstrapStep;
use Illuminate\Support\Facades\DB;
class SettingsStep implements TenantBootstrapStep
{
public function key(): string { return 'settings_seed'; }
public function run(int $tenantId): void
{
if (!DB::getSchemaBuilder()->hasTable('settings')) return;
$pairs = [
['general.company_name', '회사명 미설정'],
['ui.theme', 'light'],
['inventory.auto_reserve', '0'],
];
foreach ($pairs as [$key,$val]) {
DB::table('settings')->updateOrInsert(
['tenant_id'=>$tenantId, 'key'=>$key],
['value'=>$val, 'updated_at'=>now(), 'created_at'=>now()]
);
}
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Services\TenantBootstrap\Support;
class TenantBootstrapLogger
{
private array $messages = [];
public function info(string $msg, array $ctx = []): void {
$this->messages[] = ['level'=>'info', 'msg'=>$msg, 'ctx'=>$ctx, 'ts'=>now()->toDateTimeString()];
}
public function error(string $msg, array $ctx = []): void {
$this->messages[] = ['level'=>'error', 'msg'=>$msg, 'ctx'=>$ctx, 'ts'=>now()->toDateTimeString()];
}
public function dump(): array { return $this->messages; }
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Services;
use App\Services\TenantBootstrap\RecipeRegistry;
use App\Services\TenantBootstrap\Support\TenantBootstrapLogger;
use Illuminate\Support\Facades\DB;
class TenantBootstrapper
{
public function __construct(private RecipeRegistry $registry) {}
public function bootstrap(int $tenantId, string $recipe = 'STANDARD'): void
{
$logger = new TenantBootstrapLogger();
$steps = $this->registry->steps($recipe);
$ver = $this->registry->version($recipe);
DB::transaction(function () use ($tenantId, $recipe, $steps, $ver, $logger) {
$runId = DB::table('tenant_bootstrap_runs')->insertGetId([
'tenant_id'=>$tenantId, 'recipe'=>$recipe, 'recipe_version'=>$ver,
'status'=>'RUNNING', 'steps'=>json_encode([]), 'log'=>json_encode([]),
'created_at'=>now(), 'updated_at'=>now(),
]);
$done = [];
try {
foreach ($steps as $step) {
/** @var \App\Services\TenantBootstrap\Contracts\TenantBootstrapStep $step */
$logger->info('start step', ['key'=>$step->key()]);
$step->run($tenantId);
$done[] = $step->key();
$logger->info('end step', ['key'=>$step->key()]);
DB::table('tenant_bootstrap_runs')->where('id',$runId)->update([
'steps'=>json_encode($done),
'log'=>json_encode($logger->dump(), JSON_UNESCAPED_UNICODE),
'updated_at'=>now(),
]);
}
DB::table('tenant_bootstrap_runs')->where('id',$runId)->update([
'status'=>'SUCCESS',
'log'=>json_encode($logger->dump(), JSON_UNESCAPED_UNICODE),
'updated_at'=>now(),
]);
} catch (\Throwable $e) {
$logger->error('step failed', ['exception'=>$e->getMessage()]);
DB::table('tenant_bootstrap_runs')->where('id',$runId)->update([
'status'=>'FAILED',
'log'=>json_encode($logger->dump(), JSON_UNESCAPED_UNICODE),
'updated_at'=>now(),
]);
throw $e;
}
});
}
}