fix: add missing tables and correct order insertion logic in lean seeder

This commit is contained in:
Tiago Yamamoto 2025-12-22 11:11:00 -03:00
parent 061930a604
commit 30b8395639

View file

@ -90,6 +90,7 @@ func SeedLean(dsn string) (string, error) {
log.Println("🧹 [Lean] Resetting database...")
// Re-create tables
mustExec(db, `DROP TABLE IF EXISTS shipments CASCADE`)
mustExec(db, `DROP TABLE IF EXISTS inventory_adjustments CASCADE`)
mustExec(db, `DROP TABLE IF EXISTS order_items CASCADE`)
mustExec(db, `DROP TABLE IF EXISTS orders CASCADE`)
@ -141,6 +142,69 @@ func SeedLean(dsn string) (string, error) {
updated_at TIMESTAMPTZ NOT NULL
)`)
mustExec(db, `CREATE TABLE orders (
id UUID PRIMARY KEY,
buyer_id UUID NOT NULL REFERENCES companies(id),
seller_id UUID NOT NULL REFERENCES companies(id),
status TEXT NOT NULL,
total_cents BIGINT NOT NULL,
shipping_recipient_name TEXT,
shipping_street TEXT,
shipping_number TEXT,
shipping_complement TEXT,
shipping_district TEXT,
shipping_city TEXT,
shipping_state TEXT,
shipping_zip_code TEXT,
shipping_country TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
)`)
mustExec(db, `CREATE TABLE order_items (
id UUID PRIMARY KEY,
order_id UUID NOT NULL REFERENCES orders(id),
product_id UUID NOT NULL REFERENCES products(id),
quantity BIGINT NOT NULL,
unit_cents BIGINT NOT NULL,
batch TEXT NOT NULL,
expires_at DATE NOT NULL
)`)
mustExec(db, `CREATE TABLE cart_items (
id UUID PRIMARY KEY,
buyer_id UUID NOT NULL REFERENCES companies(id),
product_id UUID NOT NULL REFERENCES products(id),
quantity BIGINT NOT NULL,
unit_cents BIGINT NOT NULL,
batch TEXT,
expires_at DATE,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
UNIQUE (buyer_id, product_id)
)`)
mustExec(db, `CREATE TABLE reviews (
id UUID PRIMARY KEY,
order_id UUID NOT NULL UNIQUE REFERENCES orders(id),
buyer_id UUID NOT NULL REFERENCES companies(id),
seller_id UUID NOT NULL REFERENCES companies(id),
rating INT NOT NULL CHECK (rating BETWEEN 1 AND 5),
comment TEXT,
created_at TIMESTAMPTZ NOT NULL
)`)
mustExec(db, `CREATE TABLE shipments (
id UUID PRIMARY KEY,
order_id UUID NOT NULL UNIQUE REFERENCES orders(id),
carrier TEXT NOT NULL,
tracking_code TEXT,
external_tracking TEXT,
status TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
)`)
// Helper for hashing
hashPwd := func(pwd string) string {
h, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
@ -274,6 +338,11 @@ func SeedLean(dsn string) (string, error) {
status := statuses[rng.Intn(len(statuses))]
totalCents := int64(0)
mustExec(db, fmt.Sprintf(`INSERT INTO orders (id, buyer_id, seller_id, status, total_cents, created_at, updated_at)
VALUES ('%s', '%s', '%s', '%s', %d, NOW(), NOW())`,
orderID, buyerID, sellerID, status, totalCents,
))
// Create order items
numItems := 1 + rng.Intn(3)
for j := 0; j < numItems && j < len(sellerProducts); j++ {
@ -289,10 +358,8 @@ func SeedLean(dsn string) (string, error) {
))
}
mustExec(db, fmt.Sprintf(`INSERT INTO orders (id, buyer_id, seller_id, status, total_cents, created_at, updated_at)
VALUES ('%s', '%s', '%s', '%s', %d, NOW(), NOW())`,
orderID, buyerID, sellerID, status, totalCents,
))
// Update total cents on order because it we calculated it inside the loop
mustExec(db, fmt.Sprintf(`UPDATE orders SET total_cents = %d WHERE id = '%s'`, totalCents, orderID))
// Create Review if Delivered
if status == "Entregue" {
@ -326,9 +393,10 @@ func SeedLean(dsn string) (string, error) {
}
cartItemID := uuid.Must(uuid.NewV7())
qty := int64(1 + rng.Intn(3))
mustExec(db, fmt.Sprintf(`INSERT INTO cart_items (id, buyer_id, product_id, quantity, created_at)
VALUES ('%s', '%s', '%s', %d, NOW()) ON CONFLICT DO NOTHING`,
cartItemID, buyerID, prod["id"].(uuid.UUID), qty,
priceCents := prod["price_cents"].(int64)
mustExec(db, fmt.Sprintf(`INSERT INTO cart_items (id, buyer_id, product_id, quantity, unit_cents, created_at, updated_at)
VALUES ('%s', '%s', '%s', %d, %d, NOW(), NOW()) ON CONFLICT DO NOTHING`,
cartItemID, buyerID, prod["id"].(uuid.UUID), qty, priceCents,
))
}
log.Printf("✅ [Lean] Added cart items")