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

Laravel package that adds Artisan commands to quickly create, load, list, and clean up database snapshots (dumps) for your app. Works with MySQL, PostgreSQL, and SQLite—ideal for resetting state in development or tests.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package:
    composer require spatie/laravel-db-snapshots
    
  2. Configure storage disk: Add a snapshots 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

Create your first snapshot:

php artisan snapshot:create "initial-state"

Verify it exists:

php artisan snapshot:list

Implementation Patterns

Daily Workflow

  1. Pre-feature snapshot:
    php artisan snapshot:create "before-feature-x"
    
  2. Post-feature snapshot:
    php artisan snapshot:create "after-feature-x"
    
  3. Rollback testing:
    php artisan snapshot:load "before-feature-x"
    

Integration with CI/CD

  • Pre-test snapshot:
    php artisan snapshot:create "ci-test-state-$(date +%s)"
    
  • Post-test cleanup:
    php artisan snapshot:cleanup --keep=1
    

Selective Snapshots

  • Include only specific tables:
    php artisan snapshot:create "users-only" --table=users,roles
    
  • Exclude tables:
    php artisan snapshot:create "exclude-temp" --exclude=temp_data
    

Database-Specific Config

Configure database.php for custom dump options (e.g., PostgreSQL):

'pgsql' => [
    'driver' => 'pgsql',
    'dump' => [
        'addExtraOption' => '--no-owner',
    ],
],

Event-Driven Extensions

Listen to snapshot events in EventServiceProvider:

protected $listen = [
    \Spatie\DbSnapshots\Events\CreatingSnapshot::class => [
        \App\Listeners\LogSnapshotCreation::class,
    ],
];

Gotchas and Tips

Common Pitfalls

  1. Large snapshots:

    • Use --stream for loading large files:
      php artisan snapshot:load "big-snapshot" --stream
      
    • Avoid memory issues with --drop-tables=0 for incremental loads.
  2. Connection mismatches:

    • Explicitly specify --connection when loading:
      php artisan snapshot:load "snapshot-name" --connection=staging
      
  3. Compression quirks:

    • Ensure .gz files are handled correctly in CI (e.g., GitHub Actions):
      php artisan snapshot:load "compressed-snapshot" --stream
      

Debugging Tips

  • Verify disk permissions:
    chmod -R 775 database_path('snapshots')
    
  • Check dump contents:
    php artisan snapshot:load "debug-snapshot" --stream --verbose
    

Extension Points

  1. Custom snapshot logic: Extend Spatie\DbSnapshots\Commands\CreateSnapshotCommand for pre/post hooks.

  2. Custom storage: Implement Spatie\DbSnapshots\Contracts\SnapshotRepository for S3/remote storage.

  3. Database-specific tweaks: Override Spatie\DbSnapshots\Dumpers\MysqlDumper for custom SQL generation.

Configuration Quirks

  • Default connection: Set default_connection in config/db-snapshots.php to avoid ambiguity.
  • Table filtering: --table overrides --exclude; use --exclude only when no --table is specified.

Performance Considerations

  • Compression tradeoff: Enable compress: true in config for storage savings, but benchmark load times.
  • Selective snapshots: Use --table to reduce snapshot size for CI environments.

Security Note

  • Snapshot cleanup: Automate cleanup in CI to prevent storage bloat:
    php artisan snapshot:cleanup --keep=5
    
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.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata