resolvemos o problema dos ZEE e ZFI
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-10-07 09:08:43 +02:00
parent e65cb26901
commit 59d108a576
37 changed files with 1514 additions and 102 deletions
@@ -15,6 +15,8 @@ return new class extends Migration
DB::statement("ALTER TABLE portal_resource_links DROP CONSTRAINT portal_resource_links_target_type_check");
DB::statement("ALTER TABLE portal_resource_links ADD CONSTRAINT portal_resource_links_target_type_check CHECK (target_type IN ('theme','module','activity','stage','procedure','entity','territory','document','investment_type','legal_source','requirement'))");
DB::statement("ALTER TABLE portal_resource_links ADD CONSTRAINT portal_resource_links_review_status_check CHECK (review_status IN ('pending','confirmed'))");
DB::statement('ALTER TABLE portal_resource_revisions DROP CONSTRAINT portal_resource_revisions_change_type_check');
DB::statement("ALTER TABLE portal_resource_revisions ADD CONSTRAINT portal_resource_revisions_change_type_check CHECK (change_type IN ('created','metadata_updated','published','unpublished','evidence_linked'))");
}
public function down(): void
@@ -22,6 +24,11 @@ return new class extends Migration
if (DB::table('portal_resource_links')->where('target_type', 'requirement')->exists()) {
throw new RuntimeException('Requirement evidence links exist; reconcile them before rollback.');
}
if (DB::table('portal_resource_revisions')->where('change_type', 'evidence_linked')->exists()) {
throw new RuntimeException('Evidence association revisions exist; reconcile them before rollback.');
}
DB::statement('ALTER TABLE portal_resource_revisions DROP CONSTRAINT portal_resource_revisions_change_type_check');
DB::statement("ALTER TABLE portal_resource_revisions ADD CONSTRAINT portal_resource_revisions_change_type_check CHECK (change_type IN ('created','metadata_updated','published','unpublished'))");
DB::statement('ALTER TABLE portal_resource_links DROP CONSTRAINT portal_resource_links_target_type_check, DROP CONSTRAINT portal_resource_links_review_status_check');
DB::statement("ALTER TABLE portal_resource_links ADD CONSTRAINT portal_resource_links_target_type_check CHECK (target_type IN ('theme','module','activity','stage','procedure','entity','territory','document','investment_type','legal_source'))");
Schema::table('portal_resource_links', fn (Blueprint $table) => $table->dropColumn('review_status'));
@@ -0,0 +1,23 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
DB::statement('ALTER TABLE investment_zones DROP CONSTRAINT investment_zones_publication_requirements_check');
DB::statement("ALTER TABLE investment_zones ADD CONSTRAINT investment_zones_publication_requirements_check CHECK (publication_status <> 'published' OR (validation_status='validated' AND category <> 'pending' AND reviewer_id IS NOT NULL AND published_at IS NOT NULL AND length(trim(name)) > 0 AND physical_category IS NOT NULL AND physical_category <> 'unknown' AND (legal_source_id IS NOT NULL OR jsonb_array_length(source_evidence)>0)))");
}
public function down(): void
{
if (DB::table('investment_zones')->where('publication_status', 'published')->whereColumn('reviewer_id', 'editor_id')->exists()) {
throw new RuntimeException('A published zone uses the same author and publisher. Reconcile it before restoring the two-person requirement.');
}
DB::statement('ALTER TABLE investment_zones DROP CONSTRAINT investment_zones_publication_requirements_check');
DB::statement("ALTER TABLE investment_zones ADD CONSTRAINT investment_zones_publication_requirements_check CHECK (publication_status <> 'published' OR (validation_status='validated' AND category <> 'pending' AND reviewer_id IS NOT NULL AND reviewer_id <> editor_id AND published_at IS NOT NULL AND length(trim(name)) > 0 AND physical_category IS NOT NULL AND physical_category <> 'unknown' AND ((category IN ('zee','zfi') AND legal_source_id IS NOT NULL) OR jsonb_array_length(source_evidence)>0)))");
}
};
@@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
private const MODALITIES = [
'company_sale', 'company_purchase', 'participation_sale', 'capital_increase', 'financial_investor', 'strategic_partner',
'buyer_search', 'position_assignment', 'franchise', 'asset_sale', 'asset_lease', 'business_unit_transfer', 'joint_venture',
'succession', 'recovery', 'expansion', 'internationalization', 'real_estate', 'agricultural_investment', 'land_right',
'mining_license', 'forestry_license', 'investment_project', 'business_partnership', 'financing_request',
'concession_opportunity', 'supply_opportunity',
];
public function up(): void
{
$this->replaceConstraint(self::MODALITIES);
}
public function down(): void
{
if (DB::table('existing_businesses')->where('modality', 'company_purchase')->exists()) {
throw new RuntimeException('Cannot remove company_purchase while records use it.');
}
$this->replaceConstraint(array_values(array_filter(self::MODALITIES, fn (string $value): bool => $value !== 'company_purchase')));
}
/** @param list<string> $values */
private function replaceConstraint(array $values): void
{
if (DB::getDriverName() !== 'pgsql') {
throw new RuntimeException('This modality constraint migration requires PostgreSQL.');
}
$constraints = DB::select("SELECT conname FROM pg_constraint WHERE conrelid = 'existing_businesses'::regclass AND contype = 'c' AND conkey @> ARRAY[(SELECT attnum FROM pg_attribute WHERE attrelid = 'existing_businesses'::regclass AND attname = 'modality')]::smallint[] AND pg_get_constraintdef(oid) ILIKE '%company_sale%' AND pg_get_constraintdef(oid) ILIKE '%supply_opportunity%'");
if (count($constraints) !== 1) {
throw new RuntimeException('Expected exactly one business modality constraint; inspect the schema before migrating.');
}
$name = str_replace('"', '""', $constraints[0]->conname);
DB::statement('ALTER TABLE existing_businesses DROP CONSTRAINT "'.$name.'"');
$quotedValues = implode(',', array_map(fn (string $value): string => "'".$value."'", $values));
DB::statement('ALTER TABLE existing_businesses ADD CONSTRAINT imz_existing_businesses_modality_check CHECK (modality IN ('.$quotedValues.'))');
}
};