Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ protected function bindValues(PDOStatement $stmt, array $bindings): void
*
* @throws QueryException
*/
protected function handleException(PDOException $e, string $sql, array $bindings): false
protected function handleException(PDOException $e, string $sql, array $bindings): bool
{
if ($this->throwExceptions) {
throw new QueryException(
Expand Down Expand Up @@ -695,4 +695,4 @@ protected function setMysqlCharset(PDO $pdo, array $config): void

$pdo->exec("SET NAMES '{$charset}' COLLATE '{$collation}'");
}
}
}
27 changes: 26 additions & 1 deletion tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ protected static function boolType(): string
};
}

/**
* Returns a boolean default value appropriate for the active driver.
* PostgreSQL requires TRUE/FALSE for BOOLEAN columns, not 1/0.
*/
protected static function boolDefault(bool $value): string
{
$driver = strtolower((string) (getenv('DB_DRIVER') ?: 'sqlite'));
if ($driver === 'pgsql') {
return $value ? 'TRUE' : 'FALSE';
}
return $value ? '1' : '0';
}

/**
* Returns a datetime column type appropriate for the active driver.
* PostgreSQL uses TIMESTAMP, MySQL/SQLite use DATETIME.
*/
protected static function datetimeType(): string
{
return match (strtolower((string) (getenv('DB_DRIVER') ?: 'sqlite'))) {
'pgsql' => 'TIMESTAMP',
default => 'DATETIME',
};
}

/**
* Quote an identifier for the active driver.
*/
Expand All @@ -125,4 +150,4 @@ protected static function truncate(string $table): void
default => DB::statement("TRUNCATE TABLE {$q}"),
};
}
}
}
10 changes: 6 additions & 4 deletions tests/Integration/ModelCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected static function createSchema(): void
{
$ai = static::autoIncrement();
$bool = static::boolType();
$dt = static::datetimeType();
$boolDefault = static::boolDefault(true);

DB::statement("DROP TABLE IF EXISTS " . static::q('users'));
DB::statement("
Expand All @@ -31,11 +33,11 @@ protected static function createSchema(): void
" . static::q('name') . " VARCHAR(255) NOT NULL,
" . static::q('email') . " VARCHAR(255) NOT NULL,
" . static::q('age') . " INTEGER DEFAULT 0,
" . static::q('is_active') . " {$bool} DEFAULT 1,
" . static::q('is_active') . " {$bool} DEFAULT {$boolDefault},
" . static::q('score') . " DOUBLE PRECISION DEFAULT 0,
" . static::q('settings') . " TEXT DEFAULT NULL,
" . static::q('created_at') . " DATETIME DEFAULT NULL,
" . static::q('updated_at') . " DATETIME DEFAULT NULL
" . static::q('created_at') . " {$dt} DEFAULT NULL,
" . static::q('updated_at') . " {$dt} DEFAULT NULL
)
");
}
Expand Down Expand Up @@ -400,4 +402,4 @@ public function scopeActive(Builder $q): Builder
{
return $q->where('is_active', 1);
}
}
}
7 changes: 4 additions & 3 deletions tests/Integration/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class QueryTest extends IntegrationTestCase

protected static function createSchema(): void
{
$ai = static::autoIncrement();
$bool = static::boolType();
$ai = static::autoIncrement();
$bool = static::boolType();
$boolDefault = static::boolDefault(true);

DB::statement("DROP TABLE IF EXISTS " . static::q('orders'));
DB::statement("DROP TABLE IF EXISTS " . static::q('query_users'));
Expand All @@ -29,7 +30,7 @@ protected static function createSchema(): void
" . static::q('name') . " VARCHAR(255),
" . static::q('email') . " VARCHAR(255),
" . static::q('age') . " INTEGER DEFAULT 0,
" . static::q('active') . " {$bool} DEFAULT 1,
" . static::q('active') . " {$bool} DEFAULT {$boolDefault},
" . static::q('score') . " DOUBLE PRECISION DEFAULT 0,
" . static::q('role') . " VARCHAR(50) DEFAULT 'user'
)
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class SoftDeleteTest extends IntegrationTestCase
protected static function createSchema(): void
{
$ai = static::autoIncrement();
$dt = static::datetimeType();

DB::statement("DROP TABLE IF EXISTS " . static::q('soft_posts'));
DB::statement("
CREATE TABLE " . static::q('soft_posts') . " (
" . static::q('id') . " {$ai},
" . static::q('title') . " VARCHAR(255),
" . static::q('deleted_at') . " DATETIME DEFAULT NULL
" . static::q('deleted_at') . " {$dt} DEFAULT NULL
)
");
}
Expand Down
Loading