定制 envor/laravel-database-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

envor/laravel-database-manager

最新稳定版本:v2.0.2

Composer 安装命令:

composer require envor/laravel-database-manager

包简介

A small library for managing databases.

README 文档

README

A small library for managing databases

Create and delete mysql and sqlite databases. Soft deletes, or "recycles" databases by default. Also it can clean up old recycled databases.

Installation

You can install the package via composer:

composer require envor/laravel-database-manager

Warning

If you are using laravel 10, you will have to manually install doctrine/dbal
doctrine/dbal is not a requirement for Laravel 11

composer require doctrine/dbal

You can publish the config file with:

php artisan vendor:publish --tag="database-manager-config"

This is the contents of the published config file:

// config for Envor/DatabaseManager
return [
    /***
     * The disk where the sqlite database files will be stored.
     */
    'sqlite_disk' => 'local',

    /***
     * Available drivers that can be managed.
     */
    'managers' => [
        'sqlite' => \Envor\DatabaseManager\SQLiteDatabaseManager::class,
        'mysql' => \Envor\DatabaseManager\MySQLDatabaseManager::class,
    ]
];

Usage

Faking

This package tests things like creating and deleting physical databases so that you don't have to. You simply call DatabaseManager::fake() then test your application's feature logic (validation, etc)

// controller
public function store(Request $request)
{
    $this->validate($request->all());

    $databaseManager = (new Envor\DatabaseManager)
        ->manage($request->database_driver)
        ->createDatabase($request->database_name);
    
    if($databaseManager){
        $request->user()->databases()->create([
            'name' => $request->database_name,
            'driver' => $request->database_driver,
        ]);
    }
}
    // test
    public function test_it_can_create_a_database(): void
    {
        $this->actingAs($user = User::factory()->create());

        Envor\DatabaseManager\Facades\DatabaseManager::fake();

        $this->post(route('database.create'), [
            'database_driver' => 'sqlite',
            'database_name' => 'test_database',
        ]);

        $this->assertDatabaseHas('databases', [
            'user_id' => $user->id,
            'driver' => 'sqlite',
            'name' => 'test_database',
        ]);
    }

SQLite

Creates an sqlite database at storage/app/my-new-database.sqlite

$databaseManager = (new Envor\DatabaseManager)
    ->manage('sqlite')
    ->createDatabase('my-new-database');

Soft deletes the database and moves it to storage/app/.trash/2023/03/02/07_04_38_my-new-database.sqlite:

The package appends the .sqlite file extension on its own, and expects managed sqlite database files to have the extension

echo now()->format('Y/m/d_h_i_s_');
// 2023/3/2/7_04_38_
$databaseManager->deleteDatabase(
    databaseName: 'my-new-database', 
    deletedAt: now(), // optional: defaults to now() (Carbon date)
);

Erases the database permanently from disk:

// erase the database permanently from disk
$databaseManager->eraseDatabase('.trash/2023/03/02/07_04_38_my-new-database');

Erases all the database files in the .trash folder with mtime more than one day old:

$databaseManager->cleanupOldDatabases(
    daysOld: 1, // optional, defaults to one
);

MYSQL

Sets the connection then creates a new database.

Note

The sqlite driver to doesn't need a connection because it uses the Illuminate\Support\Storage helper under the hood.

$databaseManager = (new Envor\DatabaseManager)
    ->manage('mysql')
    ->setConnection('any-mysql-connection')
    ->createDatabase('my_new_database');

Soft deletes the database and moves it to deleted_2023_3_2_7_04_38_my_new_database

echo now()->format('Y_m_d_h_i_s_');
// 2023/3/2/7_04_38_
$databaseManager->deleteDatabase(
    databaseName: 'my_new_database', 
    deletedAt: now(), // optional: defaults to now(). Uses Carbon. 
);

No mtime for mysql, simply compares $daysOld against the formated time segment in the deleted name 2023_3_2_7_04_38_. This is done by using Carbon::createFromFormat('Y_m_H_h_i_s_').

$databaseManager->cleanupOldDatabases(
    daysOld: 1, // optional, defaults to one
);

Creating Managers

Out of the box this package includes managers for sqlite and mysql. You can create your own managers and add them to the managers array in the database-manager config. Feel free to submit a PR for any additional custom managers that use standard laravel drivers, such as postgres.

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.

envor/laravel-database-manager 适用场景与选型建议

envor/laravel-database-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 envor/laravel-database-manager 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

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