fix(migrations): handle information_schema on pgsql and column checks

This commit is contained in:
michele 2026-09-11 18:57:01 +02:00
parent ff3ceb9947
commit 946e6c70e4
4 changed files with 28 additions and 4 deletions

View File

@ -56,10 +56,12 @@ public function up(): void
// Aggiornare i record esistenti che non hanno codice_univoco
try {
// Per amministratori
$amministratori = DB::table('amministratori')->whereNull('codice_univoco')->orWhere('codice_univoco', '')->get();
foreach ($amministratori as $admin) {
$codice = $this->generateCodiceUnivocoPhp('amministratori');
DB::table('amministratori')->where('id', $admin->id)->update(['codice_univoco' => $codice]);
if (DB::getSchemaBuilder()->hasColumn('amministratori', 'codice_univoco')) {
$amministratori = DB::table('amministratori')->whereNull('codice_univoco')->orWhere('codice_univoco', '')->get();
foreach ($amministratori as $admin) {
$codice = $this->generateCodiceUnivocoPhp('amministratori');
DB::table('amministratori')->where('id', $admin->id)->update(['codice_univoco' => $codice]);
}
}
// Per users (se la colonna esiste)

View File

@ -253,6 +253,9 @@ public function up(): void
$hasFk = false;
if ($conn->getDriverName() === 'sqlite') {
$hasFk = false;
} elseif ($conn->getDriverName() === 'pgsql') {
$exists = $conn->selectOne("SELECT COUNT(*) AS c FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = 'unita_immobiliari' AND kcu.column_name = 'palazzina_id'");
$hasFk = isset($exists->c) ? ((int) $exists->c > 0) : false;
} else {
$exists = $conn->selectOne("SELECT COUNT(*) AS c FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'unita_immobiliari' AND COLUMN_NAME = 'palazzina_id' AND REFERENCED_TABLE_NAME IS NOT NULL", [$dbName]);
$hasFk = isset($exists->c) ? ((int) $exists->c > 0) : false;
@ -270,6 +273,9 @@ public function up(): void
if ($conn->getDriverName() === 'sqlite') {
$indexes = collect($conn->select("PRAGMA index_list(unita_immobiliari)"));
$hasIdx = $indexes->pluck('name')->contains('unita_immobiliari_codice_unita_unique');
} elseif ($conn->getDriverName() === 'pgsql') {
$indexes = collect($conn->select("SELECT indexname FROM pg_indexes WHERE tablename = 'unita_immobiliari'"));
$hasIdx = $indexes->pluck('indexname')->contains('unita_immobiliari_codice_unita_unique');
} else {
$idx = $conn->selectOne("SELECT COUNT(*) AS c FROM information_schema.statistics WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'unita_immobiliari' AND INDEX_NAME = 'unita_immobiliari_codice_unita_unique'", [$dbName]);
$hasIdx = isset($idx->c) ? ((int) $idx->c > 0) : false;

View File

@ -23,6 +23,14 @@ private function indexExists(string $table, string $indexName): bool
return false;
}
if (DB::connection()->getDriverName() === 'pgsql') {
$row = DB::selectOne(
"SELECT 1 AS ok FROM pg_indexes WHERE tablename = ? AND indexname = ? LIMIT 1",
[$table, $indexName]
);
return (bool) ($row?->ok ?? false);
}
$row = DB::selectOne(
'SELECT 1 AS ok FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ? LIMIT 1',
[$table, $indexName],

View File

@ -57,6 +57,14 @@ private function indexExists(string $table, string $index): bool
return false;
}
if (DB::connection()->getDriverName() === 'pgsql') {
$row = DB::selectOne(
"SELECT COUNT(*) AS c FROM pg_indexes WHERE tablename = ? AND indexname = ?",
[$table, $index]
);
return isset($row->c) && (int) $row->c > 0;
}
$database = Schema::getConnection()->getDatabaseName();
$row = DB::selectOne(
'SELECT COUNT(*) AS c FROM information_schema.statistics WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?',