e0dfc3ec82
API CI/CD / Validate (composer + pint) (push) Successful in 3m15s
API CI/CD / Test (PHPUnit) (push) Failing after 5m4s
API CI/CD / Build frontend assets (push) Successful in 1m3s
API CI/CD / Security audit (push) Failing after 49s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
90 lines
3.1 KiB
PHP
90 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SqliteCompat
|
|
{
|
|
public static function statement(string $sql): bool
|
|
{
|
|
$connection = DB::connection();
|
|
$driver = $connection->getDriverName();
|
|
|
|
if ($driver !== 'sqlite') {
|
|
return $connection->statement($sql);
|
|
}
|
|
|
|
$trimmed = ltrim($sql);
|
|
if (stripos($trimmed, 'ALTER TABLE') === 0) {
|
|
// SQLite cannot add primary keys or change auto_increment via ALTER.
|
|
return true;
|
|
}
|
|
|
|
if (stripos($trimmed, 'CREATE TABLE') === 0) {
|
|
$sql = self::normalizeCreateTable($sql);
|
|
} else {
|
|
$sql = self::normalizeGeneric($sql);
|
|
}
|
|
|
|
return $connection->statement($sql);
|
|
}
|
|
|
|
private static function normalizeCreateTable(string $sql): string
|
|
{
|
|
$sql = self::normalizeGeneric($sql);
|
|
|
|
// Promote `id` column to PRIMARY KEY if not already.
|
|
$sql = preg_replace(
|
|
'/`id`\\s+integer\\s+NOT\\s*NULL/i',
|
|
'`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL',
|
|
$sql,
|
|
1
|
|
);
|
|
|
|
if (stripos($sql, 'CREATE TABLE `additional_charges`') !== false) {
|
|
$sql = preg_replace(
|
|
'/`title`\\s+varchar\\(255\\)\\s+NOT\\s+NULL/i',
|
|
'`title` varchar(255) DEFAULT NULL',
|
|
$sql
|
|
);
|
|
}
|
|
|
|
if (stripos($sql, 'CREATE TABLE `score_comments`') !== false) {
|
|
$sql = preg_replace(
|
|
'/`class_section_id`\\s+integer\\s+NOT\\s+NULL/i',
|
|
'`class_section_id` integer DEFAULT NULL',
|
|
$sql
|
|
);
|
|
}
|
|
|
|
return $sql;
|
|
}
|
|
|
|
private static function normalizeGeneric(string $sql): string
|
|
{
|
|
// Remove engine/charset/collation.
|
|
$sql = preg_replace('/\\)\\s*ENGINE=.*$/is', ')', $sql);
|
|
$sql = preg_replace('/\\)\\s*DEFAULT CHARSET=.*$/is', ')', $sql);
|
|
$sql = preg_replace('/\\)\\s*CHARSET=.*$/is', ')', $sql);
|
|
$sql = preg_replace('/\\)\\s*COLLATE=.*$/is', ')', $sql);
|
|
|
|
// Replace MySQL-specific types.
|
|
$sql = preg_replace('/int\\(\\d+\\)\\s+unsigned/i', 'integer', $sql);
|
|
$sql = preg_replace('/int\\(\\d+\\)/i', 'integer', $sql);
|
|
$sql = preg_replace('/\\bint\\b/i', 'integer', $sql);
|
|
$sql = preg_replace('/smallint\\(\\d+\\)/i', 'integer', $sql);
|
|
$sql = preg_replace('/tinyint\\(\\d+\\)/i', 'integer', $sql);
|
|
$sql = preg_replace('/decimal\\(\\d+\\s*,\\s*\\d+\\)/i', 'numeric', $sql);
|
|
$sql = preg_replace('/enum\\s*\\([^\\)]*\\)/i', 'text', $sql);
|
|
$sql = preg_replace('/\\bunsigned\\b/i', '', $sql);
|
|
$sql = preg_replace('/\\s+COMMENT\\s+\'[^\']*\'/i', '', $sql);
|
|
$sql = preg_replace('/\\s+ON\\s+UPDATE\\s+CURRENT_TIMESTAMP/i', '', $sql);
|
|
$sql = preg_replace('/\\s+COLLATE\\s+\\w+/i', '', $sql);
|
|
$sql = preg_replace('/\\s+CHARACTER\\s+SET\\s+\\w+/i', '', $sql);
|
|
$sql = preg_replace('/set\\s*\\([^\\)]*\\)/i', 'text', $sql);
|
|
|
|
return rtrim($sql, " \t\n\r\0\x0B;");
|
|
}
|
|
}
|