Sobes.tech
Senior

There are orders that transition through statuses during their lifecycle. You need to implement a database layer: - Save order status history - Consider partitioning In the future, beyond the current task, other methods will be implemented: retrieving statuses by time, full order history, etc.

sobes.tech AI

Answer from AI

To implement a database layer that stores order status history and accounts for partitioning, you can use the following architecture:

  1. Status history table — stores records with fields: order_id, status, changed_at (change time), possibly changed_by (who changed), and other metadata.

  2. Partitioning — for scalability and quick data access, partition the table by time (e.g., monthly) or by order_id range, depending on load and data volume.

  3. Example table structure (PostgreSQL):

CREATE TABLE order_status_history (
    order_id UUID NOT NULL,
    status VARCHAR(50) NOT NULL,
    changed_at TIMESTAMP NOT NULL,
    changed_by UUID,
    PRIMARY KEY (order_id, changed_at)
) PARTITION BY RANGE (changed_at);

CREATE TABLE order_status_history_2024_01 PARTITION OF order_status_history
    FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
  1. In Go you can implement a layer with methods for adding records to history and querying by various parameters. For recording status:
func SaveOrderStatus(ctx context.Context, db *sql.DB, orderID string, status string, changedBy string) error {
    query := `INSERT INTO order_status_history (order_id, status, changed_at, changed_by) VALUES ($1, $2, NOW(), $3)`
    _, err := db.ExecContext(ctx, query, orderID, status, changedBy)
    return err
}
  1. Further methods (retrieving statuses by time, full history) can be implemented using filters on changed_at and order_id.

This approach will allow efficient storage and scaling of order status history with flexible data querying capabilities.

There are orders that transition through statuses… - sobes.tech