weslinkde/laravel-postgres-tools

PostgreSQL数据库的一些实用辅助函数

v0.3.0 2024-09-05 09:36 UTC

This package is auto-updated.

Last update: 2024-09-05 09:38:51 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

此包提供了一些工具,使使用PostgreSQL数据库更加方便。它包括创建数据库快照和恢复快照的命令。我们创建此包以处理大型数据库(+16GB)。感谢Spatie提供的优秀包,特别是laravel-db-snapshots包,我们使用它来创建快照。您还可以创建新数据库、删除现有数据库或克隆它们。

安装

您可以通过composer安装此包

composer require weslinkde/laravel-postgres-tools

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="laravel-postgres-tools-config"

这是已发布配置文件的内容

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' => 'pgsql',

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

    /*
     * 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,

    /*
     * These are the options that will be passed to `pg_dump`. See `man pg_dump` for more information.
     */
    'addExtraOption' => '--no-owner --no-acl --no-privileges -Z 9 -Fc',
];

用法

要创建快照(它只是数据库的导出),运行

php artisan weslink:snapshot:create my-first-dump

给快照命名是可选的。如果您不传递名称,将使用当前日期和时间

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

也许您只想快照几个表。您可以通过多次传递--table或以逗号分隔的列表来实现

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

您可能想排除某些表。您可以通过多次传递--exclude或以逗号分隔的列表来实现

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

注意:如果您同时传递--table--exclude,它将使用--table创建快照,并忽略--exclude

在您对数据库进行一些更改后,可以创建另一个快照

php artisan weslink:snapshot:create my-second-dump

要加载以前的导出问题,使用此命令

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

要将以前的导出加载到另一个数据库连接(但驱动程序必须是pgsql)

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

可以使用以下方式删除导出

php artisan weslink:snapshot:delete my-first-dump

您可以使用以下方式创建新数据库

php artisan weslink:database:create my-new-database

您可以使用以下方式删除现有数据库

php artisan weslink:database:drop my-old-database

注意:此操作是不可逆的。它将删除数据库,在生产环境中将要求您确认。

还可以使用以下方式克隆现有数据库

php artisan weslink:database:clone my-old-database my-new-database

它将为您创建一个新数据库。如果新数据库已存在,则不会执行任何操作。

事件

为了方便,我们使用了Spaties包中的事件。有多个事件被触发,可以用来执行一些自定义逻辑

  • Spatie\DbSnapshots\Events\CreatingSnapshot:在创建快照之前触发
  • Spatie\DbSnapshots\Events\CreatedSnapshot:在创建快照之后触发
  • Spatie\DbSnapshots\Events\LoadingSnapshot:在加载快照之前触发
  • Spatie\DbSnapshots\Events\LoadedSnapshot:在加载快照之后触发
  • Spatie\DbSnapshots\Events\DeletingSnapshot:在删除快照之前触发
  • Spatie\DbSnapshots\Events\DeletedSnapshot:在删除快照之后触发

更新日志

请参阅更新日志以获取有关最近更改的更多信息。

贡献

请参阅贡献以获取详细信息。

安全漏洞

请查看我们的安全策略,了解如何报告安全漏洞。

致谢

许可协议

MIT许可协议(MIT)。请参阅许可文件获取更多信息。