[Feature Request] Soft Delete
Open
@billy1624 is already working on this.
Since Oct 28, 2021.
- Dominant language
- Rust
- Stars
- 9.9k
- Forks
- 734
- Avg merge
- 6h 36m
- Merged PRs (30d)
- 8
Description
Use case
- Sometimes we can't always perform hard deletions in production.
- This is what most ORMs do
The Ideal way
- Respect the
deleted_atcolumn (pub deleted_at: Option<DateTimeWithTimeZone>) - All
deleted_atcheckings should run behind the scenes instead manually addingAND deleted_at IS NULL deleted_atcolumns should be indexed for performance reasons
Delete
let res: DeleteResult = fruit.delete(db).await?; // ID = 10
Should generate the SQL below
-- Soft Delete - If the column deleted_at exists
UPDATE `fruit` SET `deleted_at`="2020-10-29 10:23" WHERE id = 10
-- Hard Delete - If the column deleted_at doesn't exist
DELETE FROM `fruit` WHERE `fruit`.`id` = 10'
Select
let cheese: Option<cake::Model> = Cake::find_by_id(1).one(db).await?;
Should generate the SQL below
-- If the column deleted_at exists
SELECT * FROM cake WHERE id = 1 AND deleted_at IS NULL
-- If the column deleted_at doesn't exist
SELECT * FROM cake WHERE id = 1
Golang GORM
PS: I am a Go developer most of the time.
GORM, the popular ORM for Go, has defined a default struct called gorm.Model.
// gorm.Model definition
type Model struct {
ID uint `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
And any struct with gorm.Model in it will automatically become soft-delete aware.
type User struct {
gorm.Model
Name string
Phone string
}
If one really needs to perform hard deletion, here's how in GORM:
db.Delete(&order)
// UPDATE orders SET deleted_at="2020-10-29 10:23" WHERE id = 10;
db.Unscoped().Delete(&order)
// DELETE FROM orders WHERE id=10;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.