@@ -0,0 +1,64 @@
|
||||
<?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
|
||||
{
|
||||
$identity = DB::selectOne('SELECT current_database() AS database,inet_server_port() AS port');
|
||||
if (! (app()->environment('testing') && str_ends_with($identity->database, '_test') && (int) $identity->port === 55441) && ! config('opportunity_resources.authenticated_installation')) {
|
||||
throw new RuntimeException('QA isolada ou instalação autenticada necessária.');
|
||||
}
|
||||
// Metadata belongs to the existing opportunity. Binaries and territories stay in their existing repositories.
|
||||
Schema::table('documents', function (Blueprint $t) {
|
||||
$t->string('storage_encryption', 30)->nullable();
|
||||
});
|
||||
foreach (['documents', 'images', 'location_drafts'] as $kind) {
|
||||
Schema::create('opportunity_'.$kind, function (Blueprint $t) use ($kind) {
|
||||
$t->uuid('id')->primary();
|
||||
$t->foreignId('opportunity_id')->constrained('opportunities')->restrictOnDelete();
|
||||
if ($kind !== 'location_drafts') {
|
||||
$t->foreignUuid('document_id')->constrained('documents')->restrictOnDelete();
|
||||
}
|
||||
$t->jsonb('payload');
|
||||
$t->unsignedInteger('version')->default(1);
|
||||
$t->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$t->timestampsTz();
|
||||
$t->softDeletesTz();
|
||||
$t->index(['opportunity_id', 'deleted_at']);
|
||||
});
|
||||
}
|
||||
DB::unprepared(<<<'SQL'
|
||||
CREATE OR REPLACE FUNCTION guard_opportunity_resource_delete() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION 'Preservar recursos, versões e histórico; usar exclusão lógica';
|
||||
END $$;
|
||||
CREATE TRIGGER protect_opportunity_documents BEFORE DELETE ON opportunity_documents FOR EACH ROW EXECUTE FUNCTION guard_opportunity_resource_delete();
|
||||
CREATE TRIGGER protect_opportunity_images BEFORE DELETE ON opportunity_images FOR EACH ROW EXECUTE FUNCTION guard_opportunity_resource_delete();
|
||||
CREATE TRIGGER protect_opportunity_location_drafts BEFORE DELETE ON opportunity_location_drafts FOR EACH ROW EXECUTE FUNCTION guard_opportunity_resource_delete();
|
||||
SQL);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
foreach (['documents', 'images', 'location_drafts'] as $kind) {
|
||||
if (DB::table('opportunity_'.$kind)->exists()) {
|
||||
throw new RuntimeException('Preservar recursos antes de reversão.');
|
||||
}
|
||||
}
|
||||
foreach (['documents', 'images', 'location_drafts'] as $kind) {
|
||||
Schema::drop('opportunity_'.$kind);
|
||||
}
|
||||
DB::statement('DROP FUNCTION guard_opportunity_resource_delete()');
|
||||
if (DB::table('documents')->whereNotNull('storage_encryption')->exists()) {
|
||||
throw new RuntimeException('Preservar originais cifrados antes de reversão.');
|
||||
}
|
||||
Schema::table('documents', function (Blueprint $t) {
|
||||
$t->dropColumn('storage_encryption');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user