This is actually a little easier than you think.
First, you need to solve the "only source of truth."
That is, either the file system or the database is correct at any time, which one?
The reason for this is that it makes conflict resolution easier.
You must assume that the database is your Source and that the file system is the shadow part of the database. This seems counterintuitive, since how a record can exist in the database if it is not in the file system. Obviously this is not possible. But, basically, if the file is not in the database, then "it does not exist" in any case. Thus, the file system reflects the state of the database, and not vice versa.
Given these assumptions, you get these conflict resolution rules.
:
File Exists DB Entry Exists Action
Yes Yes No action, normal state
No Yes Error -- missing file, "should never happen"
No No No action, normal state
Yes No Delete the file, but no error.
- , .
, .
- , . , "in process" .
, "" . , . , , ( "" ). , , , , . , . , " ", "" ( ).
:
Start DB transaction
Rename file
Add DB record
Commit transaction
, , , . , ? № 4, . , .
, :
Start DB Transaction
Delete DB record
Commit transaction
Delete file from file system
, . , , № 4.
, , (, , ) , , . " ", .
, , " ". . , - .
- , .