31 lines
1.6 KiB
PHP
31 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Shared\Installation;
|
|
|
|
use Illuminate\Database\Schema\PostgresBuilder;
|
|
|
|
/** Reuse only the four explicitly verified foundation tables; never skip arbitrary DDL. */
|
|
final class CompositionSchemaBuilder extends PostgresBuilder
|
|
{
|
|
public array $reused=[];
|
|
|
|
public function create($table,\Closure $callback)
|
|
{
|
|
if(in_array($table,['a_clients','a_services','a_service_drafts','a_billing_drafts'],true)&&$this->hasTable($table)){
|
|
$expected=['id','tenant_id','company_id','unit_id','name','version','data','created_at','updated_at'];
|
|
if(in_array($table,['a_service_drafts','a_billing_drafts'],true))$expected=[...$expected,'client_id','state'];
|
|
if($table==='a_billing_drafts')$expected[]='source_id';
|
|
$columns=$this->getColumns($table);$names=array_column($columns,'name');sort($expected);sort($names);
|
|
if($names!==$expected)throw new \LogicException('COMPOSITION_EXISTING_TABLE_INCOMPATIBLE: '.$table);
|
|
foreach($columns as $column){
|
|
$type=$column['type_name'];$name=$column['name'];
|
|
$valid=match($name){'data'=>$type==='jsonb','name','state'=>$type==='varchar','created_at','updated_at'=>$type==='timestamptz','version'=>in_array($type,['int4','integer'],true),default=>in_array($type,['int8','bigint'],true)};
|
|
if(!$valid)throw new \LogicException('COMPOSITION_EXISTING_COLUMN_INCOMPATIBLE: '.$table.'.'.$name);
|
|
}
|
|
$this->reused[]=$table;
|
|
return;
|
|
}
|
|
parent::create($table,$callback);
|
|
}
|
|
}
|