2026-05-04 19:32:14 +02:00
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 ) {
2026-05-05 09:30:03 +02:00
if ( b [ ' legacy_id ' ] ! = null ) {
2026-05-04 19:32:14 +02:00
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 ] ;
}
2026-05-05 09:30:03 +02:00
// 3. Controllo di sicurezza: se il brand non esiste su Supabase, saltiamo il record o mettiamo null?
2026-05-04 19:32:14 +02:00
// 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
2026-05-05 09:30:03 +02:00
' name ' : ( data [ ' nome ' ] as String ) . trim ( ) . toLowerCase ( ) ,
2026-05-04 19:32:14 +02:00
' 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 " ) ;
}
}
2026-05-05 09:30:03 +02:00
Future < void > migrateTicketsToSupabase ( ) async {
final String myRealCompanyId = ' 6c4b2323-2a60-4d33-bf21-c5a8eb6b4a5b ' ;
final String myRealStoreId = ' 782a4638-1c03-40af-9060-9ef214a3e238 ' ;
try {
print ( " Inizio migrazione Ticket... " ) ;
// ==========================================================
// 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 > customerResponse = await Supabase . instance . client
. from ( ' customer ' )
. select ( ' id, legacy_id ' ) ;
// Creiamo la mappa: la chiave è il vecchio ID, il valore è il nuovo UUID
Map < String , String > customerTranslationMap = { } ;
for ( var b in customerResponse ) {
if ( b [ ' legacy_id ' ] ! = null ) {
customerTranslationMap [ b [ ' legacy_id ' ] ] = b [ ' id ' ] ;
}
}
final List < dynamic > modelResponse = await Supabase . instance . client
. from ( ' model ' )
. select ( ' id, legacy_id ' ) ;
Map < String , String > modelTranslationMap = { } ;
for ( var b in modelResponse ) {
if ( b [ ' legacy_id ' ] ! = null ) {
modelTranslationMap [ b [ ' legacy_id ' ] ] = b [ ' id ' ] ;
}
}
print (
" Dizionario pronto! Trovati ${ customerTranslationMap . length } clienti e ${ modelTranslationMap . length } modelli. " ,
) ;
// ==========================================================
// FASE 2: SCARICAMENTO E TRADUZIONE DEI MODELLI
// ==========================================================
final snapshot = await FirebaseFirestore . instance
. collection ( ' schedeRiparazione ' )
. get ( ) ; // Controlla il nome esatto della collection!
if ( snapshot . docs . isEmpty ) {
print ( " Nessun scheda trovato su Firebase! " ) ;
return ;
}
List < Map < String , dynamic > > supabaseTickets = [ ] ;
for ( var doc in snapshot . docs ) {
final data = doc . data ( ) ;
// 1. Prendiamo il vecchio ID del brand salvato su Firebase
String ? oldFirebaseCustomerId = data [ ' idCliente ' ] ;
String ? oldFirebaseModelId = data [ ' idModello ' ] ;
// 2. TRADUZIONE ISTANTANEA! Cerchiamo il nuovo UUID nel nostro dizionario
String ? newSupabaseCustomerUuid ;
if ( oldFirebaseCustomerId ! = null ) {
newSupabaseCustomerUuid = customerTranslationMap [ oldFirebaseCustomerId ] ;
}
String ? newSupabaseModelUuid ;
if ( oldFirebaseModelId ! = null ) {
newSupabaseModelUuid = modelTranslationMap [ oldFirebaseModelId ] ;
}
// 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 ( newSupabaseCustomerUuid = = null & &
newSupabaseModelUuid = = null & &
oldFirebaseCustomerId ! = null & &
oldFirebaseModelId ! = null ) {
print (
" ATTENZIONE: La scheda di riparazione ${ data [ ' numeroScheda ' ] } ha un customer_id ( $ oldFirebaseCustomerId ) o un model_id ( $ oldFirebaseModelId ) che non esiste su Supabase. Salto o metto null. " ,
) ;
continue ; // Decommenta questo se vuoi saltare i modelli orfani
}
// Creiamo la riga per Supabase
supabaseTickets . add ( {
' legacy_id ' : doc . id ,
' customer_id ' : newSupabaseCustomerUuid ,
// Mappa gli altri campi
' created_at ' :
( data [ ' dataCreazione ' ] as Timestamp ? ) ? . toDate ( ) . toIso8601String ( ) ? ?
DateTime . now ( ) . toIso8601String ( ) ,
' company_id ' : myRealCompanyId ,
' store_id ' : myRealStoreId ,
' ticket_type ' : ' repair ' ,
' target_model_id ' : newSupabaseModelUuid ,
' returned_at ' : ( data [ ' dataRiconsegnaCliente ' ] as Timestamp ? )
? . toDate ( )
. toIso8601String ( ) ,
' request ' : ( data [ ' guasto ' ] as String ? ) ? . toLowerCase ( ) . trim ( ) ,
' public_notes ' : data [ ' note ' ] ,
' internal_notes ' : data [ ' noteInterne ' ] ,
' reference_number ' : data [ ' numeroScheda ' ] ,
} ) ;
}
// ==========================================================
// FASE 3: INVIO A SUPABASE
// ==========================================================
print ( " Sto per inviare ${ supabaseTickets . length } tickets a Supabase... " ) ;
await Supabase . instance . client
. from ( ' ticket ' )
. upsert ( supabaseTickets , onConflict: ' legacy_id ' ) ;
print ( " BOOM! Migrazione ticket completata con successo! 🚀 " ) ;
} catch ( e ) {
print ( " Errore durante la migrazione dei ticket: $ e " ) ;
}
}