Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Db Snapshots Laravel Package

spatie/laravel-db-snapshots

Artisan commands to quickly create, load, list, and clean up database snapshots in Laravel. Dump and restore MySQL, PostgreSQL, and SQLite databases with named or latest snapshots—ideal for local/dev workflows and fast resets.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package:
    composer require spatie/laravel-db-snapshots
    
  2. Configure storage disk in config/filesystems.php:
    'snapshots' => [
        'driver' => 'local',
        'root' => database_path('snapshots'),
    ],
    
  3. Publish config (optional):
    php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"
    

First Use Case: Creating a Snapshot

Create a baseline database state for testing or rollback:

php artisan snapshot:create "initial-state"

Verify the snapshot exists:

php artisan snapshot:list

Implementation Patterns

Workflow: Feature Development with Snapshots

  1. Baseline snapshot before starting a feature:
    php artisan snapshot:create "feature-x-start"
    
  2. Restore snapshot after testing:
    php artisan snapshot:load "feature-x-start"
    
  3. Cleanup old snapshots (keep last 3):
    php artisan snapshot:cleanup --keep=3
    

Integration with CI/CD

  • Pre-test snapshot: Load a known state before running tests.
  • Post-test snapshot: Capture test results for debugging.
    php artisan snapshot:create "test-run-$(date +%s)"
    

Selective Table Snapshots

Target specific tables (e.g., for large databases):

php artisan snapshot:create "users-only" --table=users,roles

Multi-Connection Snapshots

Useful for testing migrations across environments:

php artisan snapshot:create "staging-db" --connection=staging

Event-Driven Extensions

Listen for snapshot events to trigger custom logic (e.g., notifications):

// In EventServiceProvider
protected $listen = [
    \Spatie\DbSnapshots\Events\CreatedSnapshot::class => [
        \App\Listeners\LogSnapshotCreation::class,
    ],
];

Gotchas and Tips

Pitfalls

  1. Large Snapshots:

    • Use --stream for loading large files to avoid memory issues:
      php artisan snapshot:load "big-dump" --stream
      
    • Enable compression for storage efficiency:
      php artisan snapshot:create "compressed" --compress
      
  2. Table Exclusions:

    • --exclude is ignored if --table is specified. Use --table for inclusion-only logic.
  3. Connection Conflicts:

    • Ensure the target connection exists in config/database.php before loading snapshots.
  4. PostgreSQL Ownership:

    • Add dump.addExtraOption to skip owner settings:
      'pgsql' => [
          'dump' => ['addExtraOption' => '--no-owner'],
      ],
      

Debugging

  • Verify snapshot integrity: Manually inspect the .sql file in storage/app/snapshots.
  • Check disk permissions: Ensure the snapshots disk is writable.

Extension Points

  1. Custom Dump Logic: Override the dumper by binding a custom Spatie\DbSnapshots\Dumpers\Dumper implementation.

  2. Pre/Post-Load Hooks: Use events (LoadingSnapshot, LoadedSnapshot) to validate or transform data before loading.

  3. Remote Storage: Extend the snapshots disk to use S3 or other cloud storage for shared snapshots.

Pro Tips

  • Tag Snapshots: Include branch names or commit hashes in snapshot names for traceability:
    php artisan snapshot:create "feature-x-$(git rev-parse --short HEAD)"
    
  • Automate Cleanup: Schedule snapshot:cleanup in Laravel’s app/Console/Kernel.php:
    protected function schedule(Schedule $schedule) {
        $schedule->command('snapshot:cleanup --keep=5')->daily();
    }
    
  • Exclude Temporary Tables: Use the exclude config to skip migrations, failed_jobs, etc.:
    'exclude' => ['migrations', 'failed_jobs'],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai