spatie/laravel-db-snapshots 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spatie/laravel-db-snapshots

Composer 安装命令:

composer require spatie/laravel-db-snapshots

包简介

Quickly dump and load databases

README 文档

README

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

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

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.

spatie/laravel-db-snapshots 适用场景与选型建议

spatie/laravel-db-snapshots 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.1M 次下载、GitHub Stars 达 1.21k, 最近一次更新时间为 2017 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「spatie」 「laravel-db-snapshots」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 spatie/laravel-db-snapshots 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 spatie/laravel-db-snapshots 我们能提供哪些服务?
定制开发 / 二次开发

基于 spatie/laravel-db-snapshots 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.1M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1211
  • 点击次数: 24
  • 依赖项目数: 15
  • 推荐数: 2

GitHub 信息

  • Stars: 1205
  • Watchers: 11
  • Forks: 101
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-22