fix(migrations): handle information_schema on pgsql and column checks
This commit is contained in:
parent
ff3ceb9947
commit
946e6c70e4
|
|
@ -56,10 +56,12 @@ public function up(): void
|
||||||
// Aggiornare i record esistenti che non hanno codice_univoco
|
// Aggiornare i record esistenti che non hanno codice_univoco
|
||||||
try {
|
try {
|
||||||
// Per amministratori
|
// Per amministratori
|
||||||
$amministratori = DB::table('amministratori')->whereNull('codice_univoco')->orWhere('codice_univoco', '')->get();
|
if (DB::getSchemaBuilder()->hasColumn('amministratori', 'codice_univoco')) {
|
||||||
foreach ($amministratori as $admin) {
|
$amministratori = DB::table('amministratori')->whereNull('codice_univoco')->orWhere('codice_univoco', '')->get();
|
||||||
$codice = $this->generateCodiceUnivocoPhp('amministratori');
|
foreach ($amministratori as $admin) {
|
||||||
DB::table('amministratori')->where('id', $admin->id)->update(['codice_univoco' => $codice]);
|
$codice = $this->generateCodiceUnivocoPhp('amministratori');
|
||||||
|
DB::table('amministratori')->where('id', $admin->id)->update(['codice_univoco' => $codice]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Per users (se la colonna esiste)
|
// Per users (se la colonna esiste)
|
||||||
|
|
|
||||||
|
|
@ -253,6 +253,9 @@ public function up(): void
|
||||||
$hasFk = false;
|
$hasFk = false;
|
||||||
if ($conn->getDriverName() === 'sqlite') {
|
if ($conn->getDriverName() === 'sqlite') {
|
||||||
$hasFk = false;
|
$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 {
|
} 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]);
|
$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;
|
$hasFk = isset($exists->c) ? ((int) $exists->c > 0) : false;
|
||||||
|
|
@ -270,6 +273,9 @@ public function up(): void
|
||||||
if ($conn->getDriverName() === 'sqlite') {
|
if ($conn->getDriverName() === 'sqlite') {
|
||||||
$indexes = collect($conn->select("PRAGMA index_list(unita_immobiliari)"));
|
$indexes = collect($conn->select("PRAGMA index_list(unita_immobiliari)"));
|
||||||
$hasIdx = $indexes->pluck('name')->contains('unita_immobiliari_codice_unita_unique');
|
$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 {
|
} 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]);
|
$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;
|
$hasIdx = isset($idx->c) ? ((int) $idx->c > 0) : false;
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,14 @@ private function indexExists(string $table, string $indexName): bool
|
||||||
return false;
|
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(
|
$row = DB::selectOne(
|
||||||
'SELECT 1 AS ok FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ? LIMIT 1',
|
'SELECT 1 AS ok FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ? LIMIT 1',
|
||||||
[$table, $indexName],
|
[$table, $indexName],
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,14 @@ private function indexExists(string $table, string $index): bool
|
||||||
return false;
|
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();
|
$database = Schema::getConnection()->getDatabaseName();
|
||||||
$row = DB::selectOne(
|
$row = DB::selectOne(
|
||||||
'SELECT COUNT(*) AS c FROM information_schema.statistics WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?',
|
'SELECT COUNT(*) AS c FROM information_schema.statistics WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = ?',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user