Skip to content

Commit add1a95

Browse files
committed
update readme
1 parent c49be5d commit add1a95

File tree

2 files changed

+108
-11
lines changed

2 files changed

+108
-11
lines changed

README.md

+104-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
1-
# Delete files within a transaction
1+
# Delete files when time is right
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/medilies/rm-q.svg?style=flat-square)](https://packagist.org/packages/medilies/rm-q)
44
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/medilies/rm-q/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/medilies/rm-q/actions?query=workflow%3Arun-tests+branch%3Amain)
55
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/medilies/rm-q/phpstan.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/medilies/rm-q/actions?query=workflow%3A"phpstan"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/medilies/rm-q.svg?style=flat-square)](https://packagist.org/packages/medilies/rm-q)
6+
<!-- [![Total Downloads](https://img.shields.io/packagist/dt/medilies/rm-q.svg?style=flat-square)](https://packagist.org/packages/medilies/rm-q) -->
77

8-
...
8+
Since file deletion is often irreversible, this Laravel package queues file deletions within a database transaction, allowing for rollback in case of errors.
9+
10+
## The problem
11+
12+
Let's say you have a use case that resembles this:
13+
14+
```php
15+
use App\Models\Image;
16+
use Illuminate\Support\Facades\DB;
17+
use Illuminate\Support\Facades\Storage;
18+
19+
DB::transaction(function () use ($fileIds) {
20+
$images = Image::whereIn('id', $fileIds)->get();
21+
22+
foreach ($images as $image) {
23+
if (Storage::exists($image->path)) {
24+
Storage::delete($image->path);
25+
}
26+
27+
// more logic ...
28+
29+
$image->delete();
30+
}
31+
});
32+
```
33+
34+
If an error occurs while handling the second image, the database rows for both the first and second images will be rolled back by the transaction, but the actual file for the first image will be gone forever.
35+
36+
## The solution
37+
38+
```php
39+
use App\Models\Image;
40+
use Illuminate\Support\Facades\DB;
41+
use Illuminate\Support\Facades\Storage;
42+
use Medilies\RmQ\Facades\RmQ;
43+
44+
DB::transaction(function () use ($fileIds) {
45+
$images = Image::whereIn('id', $fileIds)->get();
46+
47+
foreach ($images as $image) {
48+
if (Storage::exists($image->path)) {
49+
RmQ::stage($image->path);
50+
}
51+
52+
// more logic ...
53+
54+
$image->delete();
55+
}
56+
});
57+
```
58+
59+
This way, the file deletion is queued and the deletion can be fully rolled back.
960

1061
## Installation
1162

@@ -15,7 +66,7 @@ Install the package via composer:
1566
composer require medilies/rm-q
1667
```
1768

18-
You can publish and run the migrations with:
69+
Publish and run the migrations with:
1970

2071
```bash
2172
php artisan vendor:publish --tag="rm-q-migrations"
@@ -28,20 +79,65 @@ You can publish the config file with:
2879
php artisan vendor:publish --tag="rm-q-config"
2980
```
3081

31-
This is the contents of the published config file:
82+
## Usage
83+
84+
### Phase 1: Staging the files
3285

3386
```php
34-
return [
87+
use Medilies\RmQ\Facades\RmQ;
88+
89+
DB::transaction(function () {
90+
// ...
3591

36-
];
92+
$files = '/path/to/file';
93+
// or
94+
$files = [
95+
'/path/to/file1',
96+
'/path/to/file2',
97+
];
98+
99+
// ...
100+
101+
RmQ::stage($files);
102+
});
37103
```
38104

39-
## Usage
105+
### Phase 2: Deleting the files
106+
107+
Delete the files staged by the singleton:
108+
109+
```php
110+
use Medilies\RmQ\Facades\RmQ;
111+
112+
RmQ::delete();
113+
```
114+
115+
Delete all the staged files:
116+
117+
```php
118+
use Medilies\RmQ\Facades\RmQ;
119+
120+
RmQ::deleteAll();
121+
```
122+
123+
Delete all the staged files using a command:
124+
125+
```shell
126+
php artisan rm-q:delete
127+
```
128+
129+
Automatically delete the staged files at the end of the request using the middleware:
40130

41131
```php
132+
use Medilies\RmQ\Middleware\RmqMiddleware;
42133

134+
Route::put('edit-user-details', function (Request $request) {
135+
// ...
136+
})->middleware(RmqMiddleware::class);
43137
```
44138

139+
Using a Queued Job (TODO).
140+
45141
## Changelog
46142

47143
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

composer.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "medilies/rm-q",
3-
"description": "Delete files within a transaction",
3+
"description": "Delete files when time is right",
44
"keywords": [
55
"files",
66
"delete",
77
"rm",
88
"safe",
99
"rollback",
10-
"transaction"
10+
"transaction",
11+
"laravel"
1112
],
1213
"homepage": "https://github.com/medilies/rm-q",
1314
"license": "MIT",
@@ -84,4 +85,4 @@
8485
},
8586
"minimum-stability": "stable",
8687
"prefer-stable": true
87-
}
88+
}

0 commit comments

Comments
 (0)