chore: update id order
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
// ============================================
|
||||
@@ -17,6 +20,7 @@ import (
|
||||
// Database encapsule la connexion à la base de données
|
||||
type Database struct {
|
||||
*sql.DB
|
||||
GDB *gorm.DB
|
||||
}
|
||||
|
||||
// DB est l'instance globale de la base de données
|
||||
@@ -58,8 +62,18 @@ func InitDB() *Database {
|
||||
|
||||
log.Println("✅ Connexion à PostgreSQL établie avec succès")
|
||||
|
||||
// Initialiser GORM en réutilisant la connexion sql.DB existante
|
||||
gormDB, err := gorm.Open(postgres.New(postgres.Config{
|
||||
Conn: db,
|
||||
}), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Erreur initialisation GORM: %v", err)
|
||||
}
|
||||
|
||||
// Créer l'instance Database
|
||||
database := &Database{db}
|
||||
database := &Database{db, gormDB}
|
||||
|
||||
// Assigner à la variable globale
|
||||
DB = database
|
||||
@@ -76,16 +90,6 @@ func InitDB() *Database {
|
||||
log.Fatalf("❌ Erreur migration must_change_password: %v", err)
|
||||
}
|
||||
|
||||
// Migration: ajouter colonne push_token pour les notifications push (clients)
|
||||
if _, err = database.Exec(`ALTER TABLE clients ADD COLUMN IF NOT EXISTS push_token TEXT`); err != nil {
|
||||
log.Fatalf("❌ Erreur migration push_token: %v", err)
|
||||
}
|
||||
|
||||
// Migration: ajouter colonne push_token pour les notifications push (livreurs/users)
|
||||
if _, err = database.Exec(`ALTER TABLE users ADD COLUMN IF NOT EXISTS push_token TEXT`); err != nil {
|
||||
log.Fatalf("❌ Erreur migration push_token users: %v", err)
|
||||
}
|
||||
|
||||
// Migration: proposition de modification d'adresse par admin/cabine
|
||||
if _, err = database.Exec(`ALTER TABLE commandes ADD COLUMN IF NOT EXISTS proposed_address TEXT`); err != nil {
|
||||
log.Fatalf("❌ Erreur migration proposed_address: %v", err)
|
||||
@@ -166,6 +170,23 @@ func InitDB() *Database {
|
||||
log.Fatalf("❌ Erreur migration commandes.payment_method: %v", err)
|
||||
}
|
||||
|
||||
// Migration: identifiant de commande par client (numérotation indépendante par client, commence à 1)
|
||||
if _, err = database.Exec(`ALTER TABLE commandes ADD COLUMN IF NOT EXISTS client_order_id INTEGER NOT NULL DEFAULT 0`); err != nil {
|
||||
log.Fatalf("❌ Erreur migration commandes.client_order_id: %v", err)
|
||||
}
|
||||
// Backfill: numéroter les commandes existantes par client dans l'ordre d'insertion
|
||||
if _, err = database.Exec(`
|
||||
UPDATE commandes c
|
||||
SET client_order_id = sub.rn
|
||||
FROM (
|
||||
SELECT id, ROW_NUMBER() OVER (PARTITION BY username ORDER BY id) AS rn
|
||||
FROM commandes
|
||||
) sub
|
||||
WHERE c.id = sub.id AND c.client_order_id = 0
|
||||
`); err != nil {
|
||||
log.Fatalf("❌ Erreur backfill commandes.client_order_id: %v", err)
|
||||
}
|
||||
|
||||
// Migration: table de suivi des paiements crypto
|
||||
if _, err = database.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS crypto_payments (
|
||||
|
||||
Reference in New Issue
Block a user