58 lines
3.2 KiB
PHP
58 lines
3.2 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::table('people_processes', fn (Blueprint $table) => $table->unique(['tenant_id', 'id']));
|
|
Schema::create('candidate_opportunity_publications', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->unsignedBigInteger('tenant_id');
|
|
$table->unsignedBigInteger('people_process_id');
|
|
$table->foreign(['tenant_id', 'people_process_id'])->references(['tenant_id', 'id'])->on('people_processes')->restrictOnDelete();
|
|
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
|
$table->unsignedInteger('version');
|
|
$table->boolean('active');
|
|
$table->text('description');
|
|
$table->text('reason');
|
|
$table->timestamp('created_at');
|
|
$table->unique(['people_process_id', 'version']);
|
|
});
|
|
Schema::create('candidate_submissions', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->unsignedBigInteger('tenant_id');
|
|
$table->unsignedBigInteger('people_process_id');
|
|
$table->unsignedBigInteger('candidate_account_id');
|
|
$table->foreign(['tenant_id', 'people_process_id'])->references(['tenant_id', 'id'])->on('people_processes')->restrictOnDelete();
|
|
$table->foreign(['tenant_id', 'candidate_account_id'])->references(['tenant_id', 'id'])->on('candidate_accounts')->restrictOnDelete();
|
|
$table->uuid('operation_key');
|
|
$table->string('reference', 50)->unique();
|
|
$table->string('input_hash', 64);
|
|
$table->json('snapshot');
|
|
$table->timestamp('created_at');
|
|
$table->unique(['people_process_id', 'candidate_account_id']);
|
|
$table->unique(['candidate_account_id', 'operation_key']);
|
|
});
|
|
if (DB::getDriverName() === 'sqlite') {
|
|
foreach (['candidate_opportunity_publications', 'candidate_submissions'] as $table) {
|
|
DB::unprepared("CREATE TRIGGER preserve_{$table}_update BEFORE UPDATE ON {$table} BEGIN SELECT RAISE(ABORT, 'Preservar submissao e publicacao'); END;
|
|
CREATE TRIGGER preserve_{$table}_delete BEFORE DELETE ON {$table} BEGIN SELECT RAISE(ABORT, 'Preservar submissao e publicacao'); END;");
|
|
}
|
|
} elseif (DB::getDriverName() === 'pgsql') {
|
|
DB::unprepared("CREATE FUNCTION sgc_preserve_candidate_submissions() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'Preservar submissao e publicacao'; END $$;
|
|
CREATE TRIGGER preserve_candidate_publications BEFORE UPDATE OR DELETE ON candidate_opportunity_publications FOR EACH ROW EXECUTE FUNCTION sgc_preserve_candidate_submissions();
|
|
CREATE TRIGGER preserve_candidate_submissions BEFORE UPDATE OR DELETE ON candidate_submissions FOR EACH ROW EXECUTE FUNCTION sgc_preserve_candidate_submissions();");
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
throw new LogicException('Preservar candidaturas e publicações.');
|
|
}
|
|
};
|