fix(migrations): ensure cross-database compatibility with PostgreSQL and SQLite
This commit is contained in:
parent
4362f04047
commit
368077449e
|
|
@ -96,7 +96,7 @@ public function up(): void
|
|||
$table->foreign('unita_immobiliare_id')->references('id')->on('unita_immobiliari')->onDelete('cascade');
|
||||
$table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
|
||||
|
||||
$indexName = DB::connection()->getDriverName() === 'sqlite' ? 'unique_tabella_unita_dm' : 'unique_tabella_unita';
|
||||
$indexName = DB::connection()->getDriverName() === 'mysql' ? 'unique_tabella_unita' : 'unique_tabella_unita_dm';
|
||||
$table->unique(['tabella_millesimale_id', 'unita_immobiliare_id'], $indexName);
|
||||
$table->index('millesimi');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public function up(): void
|
|||
// Indici
|
||||
$table->index(['tabella_id']);
|
||||
$table->index(['unita_immobiliare_id']);
|
||||
$indexName = DB::connection()->getDriverName() === 'sqlite' ? 'unique_tabella_unita_dett' : 'unique_tabella_unita';
|
||||
$indexName = DB::connection()->getDriverName() === 'mysql' ? 'unique_tabella_unita' : 'unique_tabella_unita_dett';
|
||||
$table->unique(['tabella_id', 'unita_immobiliare_id'], $indexName);
|
||||
|
||||
// Foreign keys
|
||||
|
|
|
|||
|
|
@ -167,6 +167,8 @@ public function up()
|
|||
Schema::table('documenti', function (Blueprint $table) {
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
$indexNames = collect(\DB::select("PRAGMA index_list(documenti)"))->pluck('name');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexNames = collect(\DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'documenti'"))->pluck('indexname');
|
||||
} else {
|
||||
$indexNames = collect(\DB::select("SHOW INDEX FROM documenti"))->pluck('Key_name');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,9 +19,15 @@ public function up(): void
|
|||
});
|
||||
}
|
||||
|
||||
Schema::table('rate', function (Blueprint $table) {
|
||||
if (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement('ALTER TABLE rate ADD COLUMN importo DECIMAL(10,2) GENERATED ALWAYS AS (importo_rata) STORED');
|
||||
} elseif (DB::connection()->getDriverName() === 'mysql') {
|
||||
DB::statement('ALTER TABLE rate ADD COLUMN importo DECIMAL(10,2) GENERATED ALWAYS AS (importo_rata) VIRTUAL');
|
||||
});
|
||||
} else {
|
||||
Schema::table('rate', function (Blueprint $table) {
|
||||
$table->decimal('importo', 10, 2)->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (!Schema::hasTable('soggetti')) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (!Schema::hasTable('soggetti')) {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
*/
|
||||
public function up(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (!Schema::hasTable('dati_bancari')) {
|
||||
|
|
@ -25,7 +25,7 @@ public function up(): void
|
|||
*/
|
||||
public function down(): void
|
||||
{
|
||||
if (!Schema::hasTable('dati_bancari')) {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,10 +13,19 @@ public function up(): void
|
|||
return;
|
||||
}
|
||||
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_codice_univoco_unique');
|
||||
if (in_array(DB::connection()->getDriverName(), ['sqlite', 'pgsql'], true)) {
|
||||
try {
|
||||
if (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement('ALTER TABLE fornitori DROP CONSTRAINT IF EXISTS fornitori_codice_univoco_unique');
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_codice_univoco_unique');
|
||||
} else {
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_codice_univoco_unique');
|
||||
}
|
||||
} catch (\Throwable) {}
|
||||
if (Schema::hasColumn('fornitori', 'amministratore_id')) {
|
||||
DB::statement('CREATE UNIQUE INDEX IF NOT EXISTS fornitori_admin_codice_univoco_unique ON fornitori(amministratore_id, codice_univoco)');
|
||||
try {
|
||||
DB::statement('CREATE UNIQUE INDEX IF NOT EXISTS fornitori_admin_codice_univoco_unique ON fornitori(amministratore_id, codice_univoco)');
|
||||
} catch (\Throwable) {}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
@ -58,9 +67,18 @@ public function down(): void
|
|||
return;
|
||||
}
|
||||
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_admin_codice_univoco_unique');
|
||||
DB::statement('CREATE UNIQUE INDEX IF NOT EXISTS fornitori_codice_univoco_unique ON fornitori(codice_univoco)');
|
||||
if (in_array(DB::connection()->getDriverName(), ['sqlite', 'pgsql'], true)) {
|
||||
try {
|
||||
if (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement('ALTER TABLE fornitori DROP CONSTRAINT IF EXISTS fornitori_admin_codice_univoco_unique');
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_admin_codice_univoco_unique');
|
||||
} else {
|
||||
DB::statement('DROP INDEX IF EXISTS fornitori_admin_codice_univoco_unique');
|
||||
}
|
||||
} catch (\Throwable) {}
|
||||
try {
|
||||
DB::statement('CREATE UNIQUE INDEX IF NOT EXISTS fornitori_codice_univoco_unique ON fornitori(codice_univoco)');
|
||||
} catch (\Throwable) {}
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,12 @@ public function up(): void
|
|||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexNames = collect(DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'fornitori'"))
|
||||
->map(fn($row) => (string) ($row->indexname ?? ''))
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
} else {
|
||||
$indexNames = collect(DB::select('SHOW INDEX FROM fornitori'))
|
||||
->map(fn($row) => (string) ($row->Key_name ?? ''))
|
||||
|
|
@ -50,6 +56,12 @@ public function down(): void
|
|||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexNames = collect(DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'fornitori'"))
|
||||
->map(fn($row) => (string) ($row->indexname ?? ''))
|
||||
->filter()
|
||||
->unique()
|
||||
->values();
|
||||
} else {
|
||||
$indexNames = collect(DB::select('SHOW INDEX FROM fornitori'))
|
||||
->map(fn($row) => (string) ($row->Key_name ?? ''))
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (! Schema::hasTable('rubrica_universale') || ! Schema::hasColumn('rubrica_universale', 'categoria')) {
|
||||
|
|
@ -22,7 +22,7 @@ public function up(): void
|
|||
|
||||
public function down(): void
|
||||
{
|
||||
if (! Schema::hasTable('rubrica_universale') || ! Schema::hasColumn('rubrica_universale', 'categoria')) {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,10 @@ public function up(): void
|
|||
// Now drop the old unique index (raw statement to avoid Blueprint deferred-execution issues).
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement('DROP INDEX IF EXISTS unique_tabella_unita_ruolo');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
try {
|
||||
DB::statement('ALTER TABLE "dettaglio_importi_tabella" DROP CONSTRAINT IF EXISTS "unique_tabella_unita_ruolo"');
|
||||
} catch (\Throwable) {}
|
||||
} else {
|
||||
DB::statement('ALTER TABLE `dettaglio_importi_tabella` DROP INDEX `unique_tabella_unita_ruolo`');
|
||||
}
|
||||
|
|
@ -55,6 +59,10 @@ public function down(): void
|
|||
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement('DROP INDEX IF EXISTS unique_tabella_unita_ruolo_anno');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
try {
|
||||
DB::statement('ALTER TABLE "dettaglio_importi_tabella" DROP CONSTRAINT IF EXISTS "unique_tabella_unita_ruolo_anno"');
|
||||
} catch (\Throwable) {}
|
||||
} else {
|
||||
DB::statement('ALTER TABLE `dettaglio_importi_tabella` DROP INDEX `unique_tabella_unita_ruolo_anno`');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ public function up(): void
|
|||
Schema::table('documenti', function (Blueprint $table): void {
|
||||
if (\DB::connection()->getDriverName() === 'sqlite') {
|
||||
$indexNames = collect((array) \DB::select("PRAGMA index_list('documenti')"))->pluck('name');
|
||||
} elseif (\DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexNames = collect((array) \DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'documenti'"))->pluck('indexname');
|
||||
} else {
|
||||
$indexNames = collect((array) \DB::select('SHOW INDEX FROM documenti'))->pluck('Key_name');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ public function up(): void
|
|||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
$indexes = collect(DB::select("PRAGMA index_list('fornitori')"))
|
||||
->pluck('name');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexes = collect(DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'fornitori'"))
|
||||
->pluck('indexname');
|
||||
} else {
|
||||
$indexes = collect(DB::select("SHOW INDEX FROM fornitori"))
|
||||
->pluck('Key_name');
|
||||
|
|
@ -48,6 +51,9 @@ public function down(): void
|
|||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
$indexes = collect(DB::select("PRAGMA index_list('fornitori')"))
|
||||
->pluck('name');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
$indexes = collect(DB::select("SELECT indexname FROM pg_indexes WHERE tablename = 'fornitori'"))
|
||||
->pluck('indexname');
|
||||
} else {
|
||||
$indexes = collect(DB::select("SHOW INDEX FROM fornitori"))
|
||||
->pluck('Key_name');
|
||||
|
|
|
|||
|
|
@ -22,7 +22,12 @@ public function up(): void
|
|||
|
||||
Schema::table('dettaglio_millesimi', function (Blueprint $table): void {
|
||||
try {
|
||||
DB::statement('ALTER TABLE `dettaglio_millesimi` DROP INDEX `unique_tabella_unita`');
|
||||
if (DB::connection()->getDriverName() === 'mysql') {
|
||||
DB::statement('ALTER TABLE `dettaglio_millesimi` DROP INDEX `unique_tabella_unita`');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement('ALTER TABLE "dettaglio_millesimi" DROP CONSTRAINT IF EXISTS "unique_tabella_unita_dm"');
|
||||
DB::statement('ALTER TABLE "dettaglio_millesimi" DROP CONSTRAINT IF EXISTS "unique_tabella_unita"');
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// Already dropped or not present.
|
||||
}
|
||||
|
|
@ -36,12 +41,17 @@ public function down(): void
|
|||
}
|
||||
|
||||
Schema::table('dettaglio_millesimi', function (Blueprint $table): void {
|
||||
$table->unique(['tabella_millesimale_id', 'unita_immobiliare_id'], 'unique_tabella_unita');
|
||||
$indexName = DB::connection()->getDriverName() === 'mysql' ? 'unique_tabella_unita' : 'unique_tabella_unita_dm';
|
||||
$table->unique(['tabella_millesimale_id', 'unita_immobiliare_id'], $indexName);
|
||||
});
|
||||
|
||||
Schema::table('dettaglio_millesimi', function (Blueprint $table): void {
|
||||
try {
|
||||
DB::statement('ALTER TABLE `dettaglio_millesimi` DROP INDEX `unique_tabella_unita_ruolo_legacy`');
|
||||
if (DB::connection()->getDriverName() === 'mysql') {
|
||||
DB::statement('ALTER TABLE `dettaglio_millesimi` DROP INDEX `unique_tabella_unita_ruolo_legacy`');
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement('ALTER TABLE "dettaglio_millesimi" DROP CONSTRAINT IF EXISTS "unique_tabella_unita_ruolo_legacy"');
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
// Already dropped or not present.
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,14 @@ public function down(): void
|
|||
private function dropIndex(string $table, string $index): void
|
||||
{
|
||||
try {
|
||||
DB::statement(sprintf('ALTER TABLE `%s` DROP INDEX `%s`', $table, $index));
|
||||
if (DB::connection()->getDriverName() === 'mysql') {
|
||||
DB::statement(sprintf('ALTER TABLE `%s` DROP INDEX `%s`', $table, $index));
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement(sprintf('ALTER TABLE "%s" DROP CONSTRAINT IF EXISTS "%s"', $table, $index));
|
||||
DB::statement(sprintf('DROP INDEX IF EXISTS "%s"', $index));
|
||||
} elseif (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement(sprintf('DROP INDEX IF EXISTS `%s`', $index));
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,9 @@ private function dropIndex(string $table, string $index): void
|
|||
try {
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
DB::statement(sprintf('DROP INDEX IF EXISTS `%s`', $index));
|
||||
} elseif (DB::connection()->getDriverName() === 'pgsql') {
|
||||
DB::statement(sprintf('ALTER TABLE "%s" DROP CONSTRAINT IF EXISTS "%s"', $table, $index));
|
||||
DB::statement(sprintf('DROP INDEX IF EXISTS "%s"', $index));
|
||||
} else {
|
||||
DB::statement(sprintf('ALTER TABLE `%s` DROP INDEX `%s`', $table, $index));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (! Schema::hasTable('arera_settori_attivita')) {
|
||||
|
|
@ -27,7 +27,7 @@ public function up(): void
|
|||
|
||||
public function down(): void
|
||||
{
|
||||
if (DB::connection()->getDriverName() === 'sqlite') {
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
return;
|
||||
}
|
||||
if (! Schema::hasTable('arera_settori_attivita')) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user