feat: consolidar desenvolvimentos do portal InvestMoz

This commit is contained in:
2026-11-03 23:32:32 +02:00
parent 1da273f1f3
commit 36c8cadb49
345 changed files with 110714 additions and 2799 deletions
@@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('education_declaration_drafts', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignId('owner_user_id')->constrained('users')->restrictOnDelete();
$table->uuid('client_key');
$table->string('kind', 20);
$table->unsignedInteger('version_no')->default(1);
$table->text('payload');
$table->text('reference_snapshot')->nullable();
$table->string('state', 20)->default('draft');
$table->foreignUuid('demand_id')->nullable()->constrained('labour_demands')->restrictOnDelete();
$table->foreignUuid('capacity_id')->nullable()->constrained('training_capacities')->restrictOnDelete();
$table->timestampsTz();
$table->unique(['owner_user_id', 'client_key']);
$table->index(['owner_user_id', 'kind', 'updated_at']);
});
DB::statement("ALTER TABLE education_declaration_drafts ADD CONSTRAINT education_draft_state CHECK (version_no>0 AND kind IN ('demands','capacities') AND ((state='draft' AND demand_id IS NULL AND capacity_id IS NULL) OR (state='submitted' AND ((kind='demands' AND demand_id IS NOT NULL AND capacity_id IS NULL) OR (kind='capacities' AND capacity_id IS NOT NULL AND demand_id IS NULL)))))");
DB::statement("ALTER TABLE education_declaration_drafts ADD CONSTRAINT education_draft_snapshot CHECK ((state='draft' AND reference_snapshot IS NULL) OR (state='submitted' AND reference_snapshot IS NOT NULL))");
}
public function down(): void
{
if (DB::table('education_declaration_drafts')->exists()) {
throw new RuntimeException('Preserve education drafts and submission references before rollback.');
}
Schema::drop('education_declaration_drafts');
}
};
@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('existing_business_locations', function (Blueprint $table) {
$table->text('public_source_reference')->nullable();
$table->date('source_date')->nullable();
$table->string('source_system', 80)->nullable();
$table->unsignedInteger('source_payload_version')->nullable();
});
}
public function down(): void
{
if (DB::table('existing_business_locations')->whereNotNull('public_source_reference')->orWhereNotNull('source_date')->orWhereNotNull('source_system')->orWhereNotNull('source_payload_version')->exists()) {
throw new RuntimeException('Preserve business location sources before rollback.');
}
Schema::table('existing_business_locations', fn (Blueprint $table) => $table->dropColumn(['public_source_reference', 'source_date', 'source_system', 'source_payload_version']));
}
};
@@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('desk_service_versions', fn (Blueprint $table) => $table->date('effective_from')->nullable());
DB::statement('CREATE INDEX desk_service_effective_idx ON desk_service_versions (code,effective_from DESC,version DESC) WHERE status=\'published\'');
}
public function down(): void
{
if (DB::table('desk_service_versions')->whereNotNull('effective_from')->exists()) {
throw new RuntimeException('Preserve service effective dates before rollback.');
}
DB::statement('DROP INDEX desk_service_effective_idx');
Schema::table('desk_service_versions', fn (Blueprint $table) => $table->dropColumn('effective_from'));
}
};
@@ -0,0 +1,71 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('product_experiments', function (Blueprint $t) {
$t->uuid('id')->primary();
$t->unsignedInteger('version');
$t->string('state');
$t->foreignId('created_by')->constrained('users')->restrictOnDelete();
$t->foreignId('updated_by')->constrained('users')->restrictOnDelete();
$t->uuid('creation_key');
$t->timestampsTz();
$t->unique(['created_by', 'creation_key']);
$t->index(['state', 'updated_at']);
});
Schema::create('product_experiment_revisions', function (Blueprint $t) {
$t->uuid('id')->primary();
$t->foreignUuid('experiment_id')->constrained('product_experiments')->restrictOnDelete();
$t->unsignedInteger('version');
$t->foreignId('actor_id')->constrained('users')->restrictOnDelete();
$t->uuid('operation_key');
$t->char('request_hash', 64);
$t->char('snapshot_sha256', 64);
$t->text('snapshot');
$t->timestampTz('created_at');
$t->unique(['experiment_id', 'version']);
$t->unique(['experiment_id', 'actor_id', 'operation_key'], 'product_experiment_operation_unique');
});
DB::statement("ALTER TABLE product_experiments ADD CONSTRAINT product_experiment_state CHECK(state IN ('draft','planned','observing','reviewed','cancelled'))");
DB::statement('ALTER TABLE product_experiments ADD CONSTRAINT product_experiment_version CHECK(version>0)');
DB::statement('ALTER TABLE product_experiment_revisions ADD CONSTRAINT product_experiment_revision_version CHECK(version>0)');
DB::statement('ALTER TABLE product_experiments ADD CONSTRAINT product_experiment_current_revision FOREIGN KEY(id,version) REFERENCES product_experiment_revisions(experiment_id,version) DEFERRABLE INITIALLY DEFERRED');
DB::unprepared(<<<'SQL'
CREATE OR REPLACE FUNCTION public.product_experiment_reject_history_change() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'Product experiment history must be preserved; record a new revision' USING ERRCODE='23514';
END;
$$;
CREATE TRIGGER product_experiment_revisions_immutable BEFORE UPDATE OR DELETE OR TRUNCATE ON product_experiment_revisions FOR EACH STATEMENT EXECUTE FUNCTION public.product_experiment_reject_history_change();
CREATE TRIGGER product_experiments_preserve BEFORE DELETE OR TRUNCATE ON product_experiments FOR EACH STATEMENT EXECUTE FUNCTION public.product_experiment_reject_history_change();
CREATE OR REPLACE FUNCTION public.product_experiment_require_revision_advance() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
IF NEW.id IS DISTINCT FROM OLD.id OR NEW.created_by IS DISTINCT FROM OLD.created_by OR NEW.creation_key IS DISTINCT FROM OLD.creation_key OR NEW.created_at IS DISTINCT FROM OLD.created_at OR NEW.version <> OLD.version+1 THEN
RAISE EXCEPTION 'Product experiment updates must advance one revision and preserve origin' USING ERRCODE='23514';
END IF;
RETURN NEW;
END;
$$;
CREATE TRIGGER product_experiments_revision_advance BEFORE UPDATE ON product_experiments FOR EACH ROW EXECUTE FUNCTION public.product_experiment_require_revision_advance();
SQL);
}
public function down(): void
{
if (DB::table('product_experiments')->exists()) {
throw new RuntimeException('Preserve experiment records and audit history; reconcile a restore before rollback.');
}
DB::statement('ALTER TABLE product_experiments DROP CONSTRAINT product_experiment_current_revision');
Schema::dropIfExists('product_experiment_revisions');
Schema::dropIfExists('product_experiments');
DB::statement('DROP FUNCTION public.product_experiment_reject_history_change()');
DB::statement('DROP FUNCTION public.product_experiment_require_revision_advance()');
}
};
@@ -0,0 +1,50 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('product_search_definitions', function (Blueprint $t) {
$t->string('version', 100)->primary();
$t->char('sha256', 64);
$t->jsonb('definition');
$t->timestampTz('created_at', 6);
});
Schema::create('product_search_counts', function (Blueprint $t) {
$t->date('day');
$t->string('channel', 50);
$t->string('definition_version', 100);
$t->unsignedBigInteger('responses');
$t->unsignedBigInteger('zero_responses');
$t->timestampTz('first_recorded_at', 6);
$t->timestampTz('last_recorded_at', 6);
$t->primary(['day', 'channel', 'definition_version']);
$t->foreign('definition_version')->references('version')->on('product_search_definitions')->restrictOnDelete();
});
DB::statement('ALTER TABLE product_search_counts ADD CONSTRAINT product_search_counts_valid CHECK(responses>0 AND zero_responses>=0 AND zero_responses<=responses AND first_recorded_at<=last_recorded_at)');
DB::statement("ALTER TABLE product_search_counts ADD CONSTRAINT product_search_day_valid CHECK(day=(first_recorded_at AT TIME ZONE 'UTC')::date AND day=(last_recorded_at AT TIME ZONE 'UTC')::date)");
DB::unprepared(<<<'SQL'
CREATE OR REPLACE FUNCTION public.product_search_preserve_definition() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'Search measurement definitions are immutable; register another edition' USING ERRCODE='23514';
END;
$$;
CREATE TRIGGER product_search_definitions_immutable BEFORE UPDATE OR DELETE OR TRUNCATE ON product_search_definitions FOR EACH STATEMENT EXECUTE FUNCTION public.product_search_preserve_definition();
SQL);
}
public function down(): void
{
if (DB::table('product_search_counts')->exists() || DB::table('product_search_definitions')->exists()) {
throw new RuntimeException('Preserve collected search aggregates; reconcile a restore before rollback.');
}
Schema::dropIfExists('product_search_counts');
Schema::dropIfExists('product_search_definitions');
DB::statement('DROP FUNCTION public.product_search_preserve_definition()');
}
};
@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('desk_request_checkpoints', function (Blueprint $table) {
$table->foreignUuid('request_id')->constrained('desk_requests')->restrictOnDelete();
$table->foreignId('actor_id')->constrained('users')->restrictOnDelete();
$table->unsignedInteger('version');
$table->unsignedInteger('request_version');
$table->uuid('operation_key');
$table->char('operation_sha256', 64);
$table->text('snapshot');
$table->timestampsTz();
$table->primary(['request_id', 'actor_id']);
});
DB::statement('ALTER TABLE desk_request_checkpoints ADD CONSTRAINT desk_checkpoint_versions_positive CHECK(version > 0 AND request_version > 0)');
}
public function down(): void
{
if (DB::table('desk_request_checkpoints')->exists()) {
throw new RuntimeException('Preserve recoverable private drafts before rolling back this migration.');
}
Schema::dropIfExists('desk_request_checkpoints');
}
};
@@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
DB::statement('CREATE UNIQUE INDEX desk_documents_scope_unique ON desk_documents(case_id, document_id, request_id) WHERE request_id IS NOT NULL');
DB::statement('CREATE UNIQUE INDEX desk_documents_dossier_unique ON desk_documents(case_id, document_id) WHERE request_id IS NULL');
Schema::table('desk_documents', fn (Blueprint $table) => $table->dropUnique(['case_id', 'document_id']));
}
public function down(): void
{
if (DB::table('desk_documents')->select(['case_id', 'document_id'])->groupBy('case_id', 'document_id')->havingRaw('count(*) > 1')->exists()) {
throw new RuntimeException('Preserve scoped document uses before restoring the old uniqueness rule.');
}
Schema::table('desk_documents', fn (Blueprint $table) => $table->unique(['case_id', 'document_id']));
DB::statement('DROP INDEX desk_documents_scope_unique');
DB::statement('DROP INDEX desk_documents_dossier_unique');
}
};
@@ -0,0 +1,17 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
DB::statement("CREATE UNIQUE INDEX crm_task_creation_commands_unique ON audit_events (actor_user_id, (after_data->'task_creation'->>'lead_id'), (after_data->'task_creation'->>'key')) WHERE source_system='portal-crm-task-creation' AND entity_type='tasks' AND action='created'");
}
public function down(): void
{
DB::statement('DROP INDEX crm_task_creation_commands_unique');
}
};
@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('crm_task_preparations', function (Blueprint $table) {
$table->foreignUuid('lead_id')->constrained('leads')->restrictOnDelete();
$table->foreignId('actor_id')->constrained('users')->restrictOnDelete();
$table->unsignedInteger('version');
$table->uuid('operation_key');
$table->char('operation_sha256', 64);
$table->text('snapshot');
$table->timestampsTz();
$table->primary(['lead_id', 'actor_id']);
});
DB::statement('ALTER TABLE crm_task_preparations ADD CONSTRAINT crm_task_preparation_version_positive CHECK(version > 0)');
}
public function down(): void
{
if (DB::table('crm_task_preparations')->exists()) {
throw new RuntimeException('Preserve private task preparations before rolling back this migration.');
}
Schema::dropIfExists('crm_task_preparations');
}
};
@@ -0,0 +1,26 @@
<?php
use App\Support\IsolatedQaDatabase;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
if (app()->environment('testing')) {
throw_unless(IsolatedQaDatabase::allowsSchemaChanges(), LogicException::class, 'Isolated profile QA required.');
} else {
throw_unless(config('portal_profiles.authenticated_installation') === true, LogicException::class, 'Authenticated profile installation required.');
}
DB::statement("ALTER TABLE portal_role_assignments DROP CONSTRAINT portal_role_name, ADD CONSTRAINT portal_role_name CHECK (role IN ('promoter','validator','publisher','auditor','technical_support'))");
}
public function down(): void
{
if (DB::table('portal_role_assignments')->where('role', 'technical_support')->exists()) {
throw new LogicException('Preserve existing and historical support assignments before downgrade.');
}
DB::statement("ALTER TABLE portal_role_assignments DROP CONSTRAINT portal_role_name, ADD CONSTRAINT portal_role_name CHECK (role IN ('promoter','validator','publisher','auditor'))");
}
};