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:
-
Status history table — stores records with fields:
order_id,status,changed_at(change time), possiblychanged_by(who changed), and other metadata. -
Partitioning — for scalability and quick data access, partition the table by time (e.g., monthly) or by
order_idrange, depending on load and data volume. -
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');
- 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
}
- Further methods (retrieving statuses by time, full history) can be implemented using filters on
changed_atandorder_id.
This approach will allow efficient storage and scaling of order status history with flexible data querying capabilities.