Setup
(schema simplified for clarity)
Suppose I have a table called resource
:
+---------------+--------------+------+-----+
| Field | Type | Null | Key |
+---------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| name | varchar(255) | NO | |
| owner_id | int(11) | NO | |
+---------------+--------------+------+-----+
Each resource record is "owned" by a user defined in the user
table:
+---------------+--------------+------+-----+
| Field | Type | Null | Key |
+---------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| name | varchar(255) | NO | |
+---------------+--------------+------+-----+
A resources ownership can be transferred to other users by two different methods:
- a resource owner can request that another user takes ownership of the resource, and ownership is transferred as soon as that user accepts ownership.
- A user can request ownership from the current owner of a resource, ownership is transferred as soon as the current owner accepts the request.
In both cases if the request is denied by the approver, than nothing is done.
My naive implementation of the tables required to implement these concepts.
transferOwnershipRequest
+------------------------+--------------+------+-----+
| Field | Type | Null | Key |
+------------------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| resource_id | int(11) | NO | |
| requested_by_owner_id | int(11) | NO | |
| approver_owner_id | int(11) | NO | |
| requested_new_owner_id | int(11) | NO | |
| status_id | int(11) | NO | |
+------------------------+--------------+------+-----+
transferOwnershipRequestStatus
+---------------+--------------+------+-----+
| Field | Type | Null | Key |
+---------------+--------------+------+-----+
| id | int(11) | NO | PRI |
| status | varchar(10) | NO | |
+---------------+--------------+------+-----+
// status example data
+----+----------+
| id | status |
+----+----------+
| 1 | pending |
| 2 | approved |
| 3 | denied |
+----+----------+
My business logic would update the transferOwnershipRequest
.status_id
as a request makes its way through the approval process.
Problem
I immediately see multiple issues with this implementation.
- There is no way to track ownership changes over time.
- Any timestamp fields in
transferOwnershipRequest
(likelast_updated
,request_time
,approver_response_time
etc) feel cumbersome - Any kind of fallback functionality (like reverting to a previous owner if the current owner is hit by a bus) would be difficult to implement.
Question
What are my options as far as relational database design patterns for this type of situation? Have you implemented something similar to this?