elvisdosreis's avatar

Extend Grammars and add new function

in laravel i need to extend the postgresql grammar to add a functionality, to create a conditional unique index

in debugging I was only able to reach the uniqueSoftDelete milestone, it does not enter the extended function compileUniqueSoftDelete

PostgresGrammar extended

<?php

namespace App\Database\Schema\Grammars;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as BasePostgresGrammar;
use Illuminate\Support\Fluent;

class PostgresGrammar extends BasePostgresGrammar
{
    /**
     * Compile a unique key command.
     *
     * @param Blueprint $blueprint
     * @param Fluent $command
     * @return string
     */
    public function compileUniqueSoftDelete(Blueprint $blueprint, Fluent $command): string
    {
        // Extrai as colunas e o nome do índice
        $columns = $this->columnize($command->columns);
        $indexName = $this->wrap($command->indexName);

        // Retorna o SQL que cria o índice único condicional
        return "CREATE UNIQUE INDEX {$indexName} ON {$this->wrapTable($blueprint)} ({$columns}) WHERE deleted_at IS NULL";
    }

}

Provider

Migration

0 likes
0 replies

Please or to participate in this conversation.