diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e577942..feed843 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,10 +48,11 @@ jobs: strategy: matrix: php: [8.1, '8.0'] - laravel: [^10.0, ^9.0] + laravel: [^10.20, 10.19.*, ^9.0] db: [mysql, pgsql, sqlite, sqlsrv, 'odbc:sqlsrv'] exclude: - - { php: 8.0, laravel: ^10.0 } + - { php: 8.0, laravel: ^10.20 } + - { php: 8.0, laravel: 10.19.* } - { php: 8.0, laravel: ^11.0 } - { php: 8.1, laravel: ^11.0 } @@ -86,7 +87,9 @@ jobs: run: mkdir -p build/logs - name: Test - run: composer test -- --coverage-clover build/logs/clover.xml + run: | + composer test -- --migrate-configuration || : + composer test -- --coverage-clover build/logs/clover.xml env: DB: ${{ matrix.db }} diff --git a/src/UniqueViolationDetector.php b/src/UniqueViolationDetector.php index 7185541..fef1961 100644 --- a/src/UniqueViolationDetector.php +++ b/src/UniqueViolationDetector.php @@ -5,6 +5,7 @@ namespace Mpyw\LaravelUniqueViolationDetector; use Illuminate\Database\ConnectionInterface; +use Illuminate\Database\UniqueConstraintViolationException; use Mpyw\UniqueViolationDetector\UniqueViolationDetector as DetectorInterface; use PDOException; @@ -27,6 +28,10 @@ public function __construct(ConnectionInterface $connection) public function violated(PDOException $e): bool { + if ($e instanceof UniqueConstraintViolationException) { + return true; + } + return ( $this->detector ?: ($this->detector = (new DetectorDiscoverer())->discover($this->connection)) diff --git a/tests/Test.php b/tests/Test.php index d4ec3ae..944cdff 100644 --- a/tests/Test.php +++ b/tests/Test.php @@ -4,6 +4,8 @@ namespace Mpyw\LaravelUniqueViolationDetector\Tests; +use Illuminate\Database\Connection; +use Illuminate\Database\Connectors\SqlServerConnector; use Illuminate\Database\QueryException; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\SQLiteConnection; @@ -40,6 +42,19 @@ protected function setUp(): void { parent::setUp(); + // https://github.com/laravel/framework/issues/47937#issuecomment-1678200201 + $this->app->instance( + 'db.connector.sqlsrv', + new class() extends SqlServerConnector { + protected $options = [ + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, + PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true, + ]; + }, + ); + config(['database.connections' => require __DIR__ . '/config/database.php']); config(['database.default' => getenv('DB') ?: 'sqlite']);