90 lines
5.3 KiB
PHP
90 lines
5.3 KiB
PHP
<?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('people_processes', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
|
$table->uuid('operation_key');
|
|
$table->string('creation_hash', 64);
|
|
$table->string('kind', 20);
|
|
$table->string('title', 200);
|
|
$table->text('purpose');
|
|
$table->date('closes_on');
|
|
$table->json('rules');
|
|
$table->string('status', 30)->default('draft');
|
|
$table->unsignedInteger('version')->default(1);
|
|
$table->foreignId('opened_by')->nullable()->constrained('users')->restrictOnDelete();
|
|
$table->foreignId('selected_application_id')->nullable();
|
|
$table->json('selection')->nullable();
|
|
$table->foreignId('confirmed_by')->nullable()->constrained('users')->restrictOnDelete();
|
|
$table->json('contract')->nullable();
|
|
$table->timestamps();
|
|
$table->unique(['tenant_id', 'operation_key']);
|
|
});
|
|
Schema::create('people_applications', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('people_process_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
|
$table->uuid('operation_key');
|
|
$table->string('name', 200);
|
|
$table->string('reference', 200);
|
|
$table->text('summary');
|
|
$table->date('received_on');
|
|
$table->timestamps();
|
|
$table->unique(['people_process_id', 'reference']);
|
|
$table->unique(['tenant_id', 'operation_key']);
|
|
});
|
|
Schema::create('people_reviews', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('people_application_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('reviewed_by')->constrained('users')->restrictOnDelete();
|
|
$table->json('answers');
|
|
$table->text('reason');
|
|
$table->unsignedInteger('score_minor');
|
|
$table->timestamps();
|
|
$table->unique(['people_application_id', 'reviewed_by']);
|
|
});
|
|
if (DB::getDriverName() === 'pgsql') {
|
|
DB::unprepared("CREATE FUNCTION sgc_preserve_people_evidence() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN
|
|
RAISE EXCEPTION 'Preservar candidatura e avaliacao'; END $$;
|
|
CREATE TRIGGER preserve_people_applications BEFORE UPDATE OR DELETE ON people_applications FOR EACH ROW EXECUTE FUNCTION sgc_preserve_people_evidence();
|
|
CREATE TRIGGER preserve_people_reviews BEFORE UPDATE OR DELETE ON people_reviews FOR EACH ROW EXECUTE FUNCTION sgc_preserve_people_evidence();
|
|
CREATE FUNCTION sgc_preserve_people_process() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN
|
|
IF TG_OP = 'DELETE' OR OLD.status = 'contract_draft' THEN RAISE EXCEPTION 'Preservar processo e minuta'; END IF;
|
|
IF ROW(NEW.tenant_id, NEW.created_by, NEW.kind, NEW.title, NEW.purpose, NEW.closes_on, NEW.rules::text, NEW.operation_key, NEW.creation_hash)
|
|
IS DISTINCT FROM ROW(OLD.tenant_id, OLD.created_by, OLD.kind, OLD.title, OLD.purpose, OLD.closes_on, OLD.rules::text, OLD.operation_key, OLD.creation_hash)
|
|
THEN RAISE EXCEPTION 'Preservar regras e ambito do processo'; END IF;
|
|
RETURN NEW; END $$;
|
|
CREATE TRIGGER preserve_people_process BEFORE UPDATE OR DELETE ON people_processes FOR EACH ROW EXECUTE FUNCTION sgc_preserve_people_process();");
|
|
} elseif (DB::getDriverName() === 'sqlite') {
|
|
foreach (['people_applications', 'people_reviews'] as $table) {
|
|
foreach (['UPDATE', 'DELETE'] as $operation) {
|
|
DB::unprepared("CREATE TRIGGER preserve_{$table}_{$operation} BEFORE {$operation} ON {$table} BEGIN SELECT RAISE(ABORT, 'Preservar evidencias'); END;");
|
|
}
|
|
}
|
|
DB::unprepared("CREATE TRIGGER preserve_people_process_delete BEFORE DELETE ON people_processes BEGIN SELECT RAISE(ABORT, 'Preservar processo'); END;");
|
|
DB::unprepared("CREATE TRIGGER preserve_people_process_update BEFORE UPDATE ON people_processes WHEN OLD.status = 'contract_draft'
|
|
OR NEW.tenant_id IS NOT OLD.tenant_id OR NEW.created_by IS NOT OLD.created_by OR NEW.kind IS NOT OLD.kind OR NEW.title IS NOT OLD.title
|
|
OR NEW.purpose IS NOT OLD.purpose OR NEW.closes_on IS NOT OLD.closes_on OR NEW.rules IS NOT OLD.rules
|
|
OR NEW.operation_key IS NOT OLD.operation_key OR NEW.creation_hash IS NOT OLD.creation_hash
|
|
BEGIN SELECT RAISE(ABORT, 'Preservar regras e processo'); END;");
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
throw new LogicException('Preservar processos de contratação e evidências.');
|
|
}
|
|
};
|