15 lines
3.1 KiB
PHP
15 lines
3.1 KiB
PHP
<?php
|
|
$book=$argv[1]??'regression';require __DIR__.'/sgf-single-bootstrap.php';
|
|
$tables=['ws_proposals','ws_claims','ws_allocations','ws_returns','ws_closures'];
|
|
if($native->getSchemaBuilder()->hasTable('ws_proposals')){foreach($tables as $table)if(!$native->getSchemaBuilder()->hasTable($table))throw new LogicException('Incomplete existing physical provenance schema');echo json_encode(['passed'=>true,'replayed'=>true,'business_commands'=>0]).PHP_EOL;exit;}
|
|
$native->transaction(function()use($native,$tables){
|
|
$native->unprepared('CREATE TABLE ws_proposals(id bigserial PRIMARY KEY,tenant_id bigint NOT NULL,company_id bigint NOT NULL,unit_id bigint NOT NULL,entry_id bigint NOT NULL,entry_version int NOT NULL,preparer_id bigint NOT NULL,command_key uuid NOT NULL,request_hash text NOT NULL,reason text NOT NULL,evidence text NOT NULL,source_hash text NOT NULL,snapshot jsonb NOT NULL,created_at timestamp NOT NULL,UNIQUE(tenant_id,company_id,unit_id,preparer_id,command_key))');
|
|
$common='tenant_id bigint NOT NULL,company_id bigint NOT NULL,unit_id bigint NOT NULL,origin_key text UNIQUE NOT NULL,proposal_id bigint UNIQUE NOT NULL REFERENCES ws_proposals(id),entry_id bigint UNIQUE NOT NULL,entry_version int NOT NULL,operation_id bigint NOT NULL REFERENCES stock_operations(id),movement_id bigint NOT NULL REFERENCES stock_movements(id),preparer_id bigint NOT NULL,confirmer_id bigint NOT NULL CHECK(preparer_id<>confirmer_id),reason text NOT NULL,before_milli text NOT NULL,after_milli text NOT NULL,source_hash text NOT NULL,created_at timestamp NOT NULL';
|
|
$native->unprepared('CREATE TABLE ws_claims(id bigserial PRIMARY KEY,'.$common.',UNIQUE(operation_id),UNIQUE(movement_id))');
|
|
$native->unprepared('CREATE TABLE ws_allocations(id bigserial PRIMARY KEY,'.$common.',quantity_milli bigint NOT NULL CHECK(quantity_milli>0))');
|
|
$native->unprepared('CREATE TABLE ws_returns(id bigserial PRIMARY KEY,'.$common.',parent_movement_id bigint NOT NULL REFERENCES stock_movements(id),quantity_milli bigint NOT NULL CHECK(quantity_milli>0),UNIQUE(operation_id),UNIQUE(movement_id))');
|
|
$native->unprepared("CREATE TABLE ws_closures(id bigserial PRIMARY KEY,tenant_id bigint NOT NULL,company_id bigint NOT NULL,unit_id bigint NOT NULL,proposal_id bigint NOT NULL UNIQUE REFERENCES ws_proposals(id),actor_id bigint NOT NULL,source_hash text NOT NULL,reason text NOT NULL,before_state text NOT NULL CHECK(before_state='pending'),state text NOT NULL CHECK(state IN ('withdrawn','rejected')),state_version int NOT NULL CHECK(state_version=2),created_at timestamp NOT NULL)");
|
|
foreach($tables as $table){$native->unprepared('CREATE TRIGGER ws_immutable BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION gl_append_only()');$native->statement('REVOKE ALL ON TABLE '.$table.' FROM PUBLIC');$native->statement('REVOKE ALL ON SEQUENCE '.$table.'_id_seq FROM PUBLIC');}
|
|
});
|
|
file_put_contents($work.'/workshop-stock-schema.json',json_encode(['passed'=>true,'tables'=>$tables,'business_commands'=>0,'physical_balance_tables_created'=>0,'public_rights'=>false],JSON_PRETTY_PRINT));echo json_encode(['passed'=>true,'proof'=>$work.'/workshop-stock-schema.json']).PHP_EOL;
|