envor/laravel-database-manager

This package is abandoned and no longer maintained. The author suggests using the envor/laravel-schema-macros package instead.

A small library for managing databases.

v2.0.2 2024-02-03 08:45 UTC

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.