https://github.com/medilies/rm-q
Queue and avoid disastrous file deletions.
https://github.com/medilies/rm-q
deletion file laravel rollback safe transaction
Last synced: about 1 month ago
JSON representation
Queue and avoid disastrous file deletions.
- Host: GitHub
- URL: https://github.com/medilies/rm-q
- Owner: medilies
- License: mit
- Created: 2024-08-24T21:25:48.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-02T20:38:04.000Z (almost 2 years ago)
- Last Synced: 2025-01-24T10:47:36.666Z (over 1 year ago)
- Topics: deletion, file, laravel, rollback, safe, transaction
- Language: PHP
- Homepage:
- Size: 72.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Queue and avoid disastrous file deletions
[](https://packagist.org/packages/medilies/rm-q)
[](https://github.com/medilies/rm-q/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/medilies/rm-q/actions?query=workflow%3A"phpstan"+branch%3Amain)
Since file deletion is often irreversible, this Laravel package queues file deletions within a database transaction, allowing for rollback in case of errors.
## The problem
Let's say you have a use case that resembles this:
```php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
DB::transaction(function () use ($images) {
/** @var \App\Models\Image $image */
foreach ($images as $image) {
$image->delete();
Storage::delete($image->path);
// more logic ...
}
});
```
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.
## The solution
```php
use Illuminate\Support\Facades\Storage;
use Medilies\RmQ\Facades\RmQ;
RmQ::transaction(function () use ($images) {
/** @var \App\Models\Image $image */
foreach ($images as $image) {
$image->delete();
RmQ::stage($image->path);
// more logic ...
}
});
RmQ::delete();
```
This way, the file deletion is queued and the deletion can be fully rolled back.
## Installation
Requirements:
- PHP >= 8.2
- Laravel >= 10 (not tested on older versions).
Install the package via composer:
```bash
composer require medilies/rm-q
```
Publish and run the migrations with:
```bash
php artisan vendor:publish --tag="rm-q-migrations"
php artisan migrate
```
You can publish the config file with:
```bash
php artisan vendor:publish --tag="rm-q-config"
```
## Usage
### Phase 1: Staging the files
```php
use Medilies\RmQ\Facades\RmQ;
RmQ::transaction(function () {
// ...
$files = '/path/to/file';
// or
$files = [
'/path/to/file1',
'/path/to/file2',
];
// ...
RmQ::stage($files);
});
```
> [!IMPORTANT]
> If you use `DB::transaction` instead of `RmQ::transaction` make sure to not call `Rmq::stage` within a loop since each call will perform a database insertion.
>
> Using the middleware will optimize the performance further more by not doing any query until the end of the request performing a total of 1 to 3 queries.
### Phase 2: Deleting the files
Delete the files staged by the singleton:
```php
use Medilies\RmQ\Facades\RmQ;
RmQ::delete();
```
Delete all the staged files:
```php
use Medilies\RmQ\Facades\RmQ;
RmQ::deleteAll();
```
Delete all the staged files using a command (you can also [schedule](https://laravel.com/docs/11.x/scheduling#scheduling-artisan-commands) it):
```shell
php artisan rm-q:delete
```
> `deleteAll` takes into consideration the `after` config to fetch staged entries.
Automatically delete the staged files at the end of the request using the middleware:
```php
use Medilies\RmQ\Middleware\RmqMiddleware;
Route::put('edit-user-details', function (Request $request) {
// ...
})->middleware(RmqMiddleware::class);
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [medilies](https://github.com/medilies)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.