Sobes.tech
Senior

/* There are orders that transition through statuses during their lifecycle. It is required to implement a database layer: - Save order status history - Consider partitioning In the future, outside the current task, other methods will be implemented: getting statuses by time, full order history, etc. */

sobes.tech AI

Answer from AI

To implement a database layer that stores order status history with partitioning, the following architecture can be used:

  1. A table order_status_history with columns:

    • order_id (order identifier)
    • status (current status)
    • changed_at (time of change)
    • other metadata (e.g., who changed)
  2. Partitioning by time (e.g., monthly) or by order_id — depending on volume and query frequency.

  3. In Go, you can implement a layer with methods:

    • SaveStatus(orderID string, status string, changedAt time.Time) error — to save a new record
    • Later — methods for fetching history by time or by order.

Example code in Go using database/sql:

import (
    "database/sql"
    "time"
)

type OrderStatusHistory struct {
    DB *sql.DB
}

func (osh *OrderStatusHistory) SaveStatus(orderID string, status string, changedAt time.Time) error {
    query := `INSERT INTO order_status_history (order_id, status, changed_at) VALUES (?, ?, ?)`
    _, err := osh.DB.Exec(query, orderID, status, changedAt)
    return err
}

Partitioning at the database level (e.g., in PostgreSQL) is configured separately to optimize storage and queries.