149 lines
5.3 KiB
Dart
149 lines
5.3 KiB
Dart
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||
|
|
|
||
|
|
Future<void> migrateCustomersToSupabase() async {
|
||
|
|
// 1. IL TUO COMPANY ID REALE SU SUPABASE
|
||
|
|
// Vai nel database Supabase, copia l'UUID della tua azienda e incollalo qui
|
||
|
|
final String myRealCompanyId = '6c4b2323-2a60-4d33-bf21-c5a8eb6b4a5b';
|
||
|
|
|
||
|
|
try {
|
||
|
|
print("Inizio download modello da Firebase...");
|
||
|
|
|
||
|
|
// 2. Scarichiamo TUTTI i clienti da Firebase
|
||
|
|
final snapshot = await FirebaseFirestore.instance.collection('marca').get();
|
||
|
|
|
||
|
|
if (snapshot.docs.isEmpty) {
|
||
|
|
print("Nessun marca trovato su Firebase!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Questa lista conterrà i dati formattati pronti per Supabase
|
||
|
|
List<Map<String, dynamic>> supabaseBrands = [];
|
||
|
|
|
||
|
|
// 3. Cicliamo i documenti di Firebase e li trasformiamo
|
||
|
|
for (var doc in snapshot.docs) {
|
||
|
|
final data = doc.data();
|
||
|
|
|
||
|
|
// Creiamo la riga per Supabase
|
||
|
|
supabaseBrands.add({
|
||
|
|
'legacy_id': doc.id, // L'ID vecchio di Firebase
|
||
|
|
//'company_id': myRealCompanyId, // ECCO IL TUO COMPANY ID!
|
||
|
|
// Mappa i campi (attento a far combaciare i nomi esatti delle colonne Supabase!)
|
||
|
|
'name': (data['nome'] as String).trim().toLowerCase(),
|
||
|
|
|
||
|
|
'company_id': myRealCompanyId,
|
||
|
|
|
||
|
|
// Se avevi una data di creazione su Firebase, convertila, altrimenti ignorala
|
||
|
|
// e Supabase userà il suo 'default now()'
|
||
|
|
// 'created_at': (data['createdAt'] as Timestamp?)?.toDate().toIso8601String(),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
print("Sto per inviare ${supabaseBrands.length} brand a Supabase...");
|
||
|
|
|
||
|
|
// 4. Invio a Supabase con UPSERT
|
||
|
|
await Supabase.instance.client
|
||
|
|
.from('brand')
|
||
|
|
.upsert(
|
||
|
|
supabaseBrands,
|
||
|
|
onConflict:
|
||
|
|
'legacy_id', // Se il legacy_id c'è già, aggiorna invece di duplicare
|
||
|
|
);
|
||
|
|
|
||
|
|
print("BOOM! Migrazione brand completata con successo! 🚀");
|
||
|
|
} catch (e) {
|
||
|
|
print("Porca miseria, errore durante la migrazione: $e");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> migrateModelsToSupabase() async {
|
||
|
|
final String myRealCompanyId = '6c4b2323-2a60-4d33-bf21-c5a8eb6b4a5b';
|
||
|
|
|
||
|
|
try {
|
||
|
|
print("Inizio migrazione Modelli...");
|
||
|
|
|
||
|
|
// ==========================================================
|
||
|
|
// FASE 1: CREAZIONE DEL DIZIONARIO DI TRADUZIONE (LA MAGIA)
|
||
|
|
// ==========================================================
|
||
|
|
print("Scarico i Brand da Supabase per tradurre gli ID...");
|
||
|
|
|
||
|
|
// Chiediamo a Supabase solo 2 colonne: il nuovo UUID e il vecchio ID di Firebase
|
||
|
|
final List<dynamic> brandResponse = await Supabase.instance.client
|
||
|
|
.from('brand')
|
||
|
|
.select('id, legacy_id');
|
||
|
|
|
||
|
|
// Creiamo la mappa: la chiave è il vecchio ID, il valore è il nuovo UUID
|
||
|
|
Map<String, String> brandTranslationMap = {};
|
||
|
|
for (var b in brandResponse) {
|
||
|
|
if (b['legacy_firestore_id'] != null) {
|
||
|
|
brandTranslationMap[b['legacy_id']] = b['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
print("Dizionario pronto! Trovati ${brandTranslationMap.length} Brand.");
|
||
|
|
|
||
|
|
// ==========================================================
|
||
|
|
// FASE 2: SCARICAMENTO E TRADUZIONE DEI MODELLI
|
||
|
|
// ==========================================================
|
||
|
|
final snapshot = await FirebaseFirestore.instance
|
||
|
|
.collection('modello')
|
||
|
|
.get(); // Controlla il nome esatto della collection!
|
||
|
|
|
||
|
|
if (snapshot.docs.isEmpty) {
|
||
|
|
print("Nessun modello trovato su Firebase!");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
List<Map<String, dynamic>> supabaseModels = [];
|
||
|
|
|
||
|
|
for (var doc in snapshot.docs) {
|
||
|
|
final data = doc.data();
|
||
|
|
|
||
|
|
// 1. Prendiamo il vecchio ID del brand salvato su Firebase
|
||
|
|
String? oldFirebaseBrandId = data['idMarca'];
|
||
|
|
|
||
|
|
// 2. TRADUZIONE ISTANTANEA! Cerchiamo il nuovo UUID nel nostro dizionario
|
||
|
|
String? newSupabaseBrandUuid;
|
||
|
|
if (oldFirebaseBrandId != null) {
|
||
|
|
newSupabaseBrandUuid = brandTranslationMap[oldFirebaseBrandId];
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. Controllo di sicurezza: se il brand non esiste su Supabase, saltiamo il record o mettiamo null?
|
||
|
|
// Se nella tua tabella 'model' il 'brand_id' NON PUÒ essere null, devi per forza avere un match!
|
||
|
|
if (newSupabaseBrandUuid == null && oldFirebaseBrandId != null) {
|
||
|
|
print(
|
||
|
|
"ATTENZIONE: Il modello ${data['nome']} ha un brand_id ($oldFirebaseBrandId) che non esiste su Supabase. Salto o metto null.",
|
||
|
|
);
|
||
|
|
continue; // Decommenta questo se vuoi saltare i modelli orfani
|
||
|
|
}
|
||
|
|
|
||
|
|
// Creiamo la riga per Supabase
|
||
|
|
supabaseModels.add({
|
||
|
|
'legacy_id': doc.id,
|
||
|
|
|
||
|
|
// ECCO LA CHIAVE ESTERNA TRADOTTA!
|
||
|
|
'brand_id': newSupabaseBrandUuid,
|
||
|
|
|
||
|
|
// Mappa gli altri campi
|
||
|
|
'name': (data['name'] as String).trim().toLowerCase(),
|
||
|
|
'name_with_brand': (data['nomeConMarca'] as String)
|
||
|
|
.toLowerCase()
|
||
|
|
.trim(),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// ==========================================================
|
||
|
|
// FASE 3: INVIO A SUPABASE
|
||
|
|
// ==========================================================
|
||
|
|
print("Sto per inviare ${supabaseModels.length} modelli a Supabase...");
|
||
|
|
|
||
|
|
await Supabase.instance.client
|
||
|
|
.from('model')
|
||
|
|
.upsert(supabaseModels, onConflict: 'legacy_id');
|
||
|
|
|
||
|
|
print("BOOM! Migrazione modelli completata con successo! 🚀");
|
||
|
|
} catch (e) {
|
||
|
|
print("Errore durante la migrazione dei modelli: $e");
|
||
|
|
}
|
||
|
|
}
|