MIT License

项目介绍

spatie/laravel-db-snapshots 是一个托管在 GitHub 上的开源项目,主要使用 PHP 编写,采用 MIT License 许可证,目前获得 1.2k 个 Star、100 次 Fork。

仓库全名
spatie/laravel-db-snapshots
开源许可证
MIT License
Star 数
1.2k
Fork 数
100

README 摘要

Quickly dump and load databases This package provides Artisan commands to quickly dump and load databases in a Laravel application. This package supports MySQL, PostgreSQL and SQLite. Support us We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our…

完整内容见 GitHub 原始 README 或本页 。

常见问题

laravel-db-snapshots 用什么语言开发?

主要使用 PHP 编写,采用 MIT License 许可证,目前有 1.2k 个 Star。

laravel-db-snapshots 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 laravel-db-snapshots 的源码?

可以直接克隆仓库:git clone https://github.com/spatie/laravel-db-snapshots.git,默认分支为 main;安装与运行步骤见仓库 README。

laravel-db-snapshots 最近还在维护吗?

仓库最近一次更新为 4月1日,当前没有未关闭的 Issue;Star 数为 1.2k,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

Quickly dump and load databases

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
2618
价值分
876
活跃状态
活跃

语言占比

PHP

Release

README

Quickly dump and load databases

Latest Version on Packagist Software License run-tests Total Downloads

This package provides Artisan commands to quickly dump and load databases in a Laravel application.

# Create a dump
php artisan snapshot:create my-first-dump

# Make some changes to your db
# ...

# Create another dump
php artisan snapshot:create my-second-dump

# Load up the first dump
php artisan snapshot:load my-first-dump

# Load up the latest dump
php artisan snapshot:load --latest

# List all snapshots
php artisan snapshot:list

# Remove old snapshots. Keeping only the most recent
php artisan snapshot:cleanup --keep=2

This package supports MySQL, PostgreSQL and SQLite.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You’ll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You’ll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

This package requires PHP 8.3 or higher and Laravel 12 or 13.

For PHP 7.x and/or Laravel 6.x, use v1.x of this package.

You can install the package via Composer:

composer require spatie/laravel-db-snapshots

You should add a disk named snapshots to config/filesystems.php on which the snapshots will be saved. This would be a typical configuration:

// ...
'disks' => [
    // ...
    'snapshots' => [
        'driver' => 'local',
        'root' => database_path('snapshots'),
    ],
// ...

Optionally, you may publish the configuration file with:

php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"

This is the content of the published file:

return [

    /*
     * The name of the disk on which the snapshots are stored.
     */
    'disk' => 'snapshots',

    /*
     * The connection to be used to create snapshots. Set this to null
     * to use the default configured in `config/databases.php`
     */
    'default_connection' => null,

    /*
     * The directory where temporary files will be stored.
     */
    'temporary_directory_path' => storage_path('app/laravel-db-snapshots/temp'),

    /*
     * Create dump files that are gzipped
     */
    'compress' => false,

    /*
     * Only these tables will be included in the snapshot. Set to `null` to include all tables.
     *
     * Default: `null`
     */
    'tables' => null,

    /*
     * All tables will be included in the snapshot expect this tables. Set to `null` to include all tables.
     *
     * Default: `null`
     */
    'exclude' => null,
];

Usage

To create a snapshot (which is just a dump from the database) run:

php artisan snapshot:create my-first-dump

Giving your snapshot a name is optional. If you don’t pass a name the current date time will be used:

# Creates a snapshot named something like `2017-03-17 14:31`
php artisan snapshot:create

If you pass a connection but do not declare a name for the snapshot, the connection will be prepended

# Creates a snapshot named something like `logging_2017-03-17 14:31`
php artisan snapshot:create --connection=logging

Maybe you only want to snapshot a couple of tables. You can do this by passing the --table multiple times or as a comma separated list:

# Both commands create a snapshot containing only the posts and users tables:
php artisan snapshot:create --table=posts,users
php artisan snapshot:create --table=posts --table=users

You may want to exclude some tables from snapshot. You can do this by passing the --exclude multiple times or as a comma separated list:

# create snapshot from all tables excluding the users and posts
php artisan snapshot:create --exclude=posts,users
php artisan snapshot:create --exclude=posts --exclude=users

Note: if you pass --table and --exclude in the same time it will use --table to create the snapshot and it’s ignore the --exclude

When creating snapshots, you can optionally create compressed snapshots. To do this either pass the --compress option on the command line, or set the db-snapshots.compress configuration option to true:

# Creates a snapshot named my-compressed-dump.sql.gz
php artisan snapshot:create my-compressed-dump --compress

After you’ve made some changes to the database you can create another snapshot:

php artisan snapshot:create my-second-dump

To load a previous dump issue this command:

php artisan snapshot:load my-first-dump

To load a previous dump to another DB connection:

php artisan snapshot:load my-first-dump --connection=connectionName

By default, snapshot:load will drop all existing tables in the database. If you don’t want this behaviour, you can pass the --drop-tables=0 option:

php artisan snapshot:load my-first-dump --drop-tables=0

By default, snapshot:load will load the entire snapshot into memory which may cause problems when using large files. To avoid this, you can pass the --stream option to stream the snapshot to the database one statement at a time:

php artisan snapshot:load my-first-dump --stream

A final SQL statement does not need a trailing semicolon. If a streamed snapshot ends inside an unterminated string literal, the load throws a Spatie\DbSnapshots\Exceptions\CannotLoadSnapshot exception instead of skipping the rest of the file. Statements already loaded are not rolled back.

To list all the dumps run:

php artisan snapshot:list

A dump can be deleted with:

php artisan snapshot:delete my-first-dump

To remove all backups except the most recent 2

php artisan snapshot:cleanup --keep=2

If you need to pass extra options to the underlying db-dumper, add a dump key to the database connection with a key of addExtraOption and a value of the option. For example, to prevent the Postgres db dumper from setting the owner, you’d add:

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

To the pgsql connection in database.php

Events

There are several events fired which can be used to perform some logic of your own:

  • Spatie\DbSnapshots\Events\CreatingSnapshot: will be fired before a snapshot is created
  • Spatie\DbSnapshots\Events\CreatedSnapshot: will be fired after a snapshot has been created
  • Spatie\DbSnapshots\Events\LoadingSnapshot: will be fired before a snapshot is loaded
  • Spatie\DbSnapshots\Events\LoadedSnapshot: will be fired after a snapshot has been loaded
  • Spatie\DbSnapshots\Events\DeletingSnapshot: will be fired before a snapshot is deleted
  • Spatie\DbSnapshots\Events\DeletedSnapshot: will be fired after a snapshot has been deleted

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

评论

暂无评论