https://github.com/envor/laravel-managed-databases
Manage multiple databases and their connections at runtime using laravel tools
https://github.com/envor/laravel-managed-databases
database database-management laravel
Last synced: about 2 months ago
JSON representation
Manage multiple databases and their connections at runtime using laravel tools
- Host: GitHub
- URL: https://github.com/envor/laravel-managed-databases
- Owner: envor
- License: mit
- Created: 2024-02-05T18:01:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-04T09:50:26.000Z (over 2 years ago)
- Last Synced: 2024-03-12T18:21:29.052Z (over 2 years ago)
- Topics: database, database-management, laravel
- Language: PHP
- Homepage:
- Size: 44.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
## A small package for managing multiple databases and their connections at runtime using laravel tools
[](https://packagist.org/packages/envor/laravel-managed-databases)
[](https://github.com/envor/laravel-managed-databases/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/envor/laravel-managed-databases/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/envor/laravel-managed-databases)
## Installation
You can install the package via composer:
```bash
composer require envor/laravel-managed-databases
```
## Usage
[createDatabase()](#manageddatabasescreatedatabase)
[runOnDatabase()](#manageddatabasesrunondatabase)
[configureDatabase()](#manageddatabasesconfiguredatabase)
### #`ManagedDatabases::createDatabase()`
The `createDatabase()` method will
- Cache the current default database connection config
- Set the connection to the `$managerConnection`
- Purge the connection and reconnect
- Create the physical database
- Purge the connection
- Restore original default connection
- Purge and reconnect
> [!TIP]
> The `$managerConnection` must exist and be a configured database connection.
> This package creates a few defaults: `manager_sqlite`, `manager_mysql` and `manager_mariadb`.
> They are bootstrapped into memory by cloning the default configs for `sqlite`, `mysql` and `mariadb`.
```php
use Envor\ManagedDatabases\ManagedDatabases;
$managerConnection = 'manager_sqlite';
$name = 'database'
ManagedDatabases::createDatabase($name, $managerConnection);
// database
```
### #`ManagedDatabases::runOnDatabase()`
The `runOnDatabase()` method will connect the given `$database` using a new connection created with the credentials and options from the given `$managerConnection`, execute the given `$callback`, then finally, restore the original default database connection.
- Cache the current default database connection config
- Create a new connection config for the database by cloning the `$managerConnection` config
- Set the database as default and connect to it
- Run the given callback
- Purge the connection
- Restore original default connection
- Purge and reconnect
```php
use Envor\ManagedDatabases\ManagedDatabases;
ManagedDatabases::runOnDatabase(
$database = 'database',
$callback = fn() => Artisan::call('migrate', ['--force' => true]),
$managerConnection = 'manager_sqlite'
);
```
The package also includes an `artisan` wrapper for the `runOnDatabase()` method called `managed-databases:run`.
The simplest and most harmless way to check it out is by pasting the following command into your terminal:
```bash
php artisan managed-databases:run "migrate:fresh --seed" --database=":memory:" --managerConnection="sqlite"
```
This will run your migrations and seeders harmlessly against an in-memory sqlite database. A great way to quickly check if they can run without errors.
### #`ManagedDatabases::configureDatabase()`
The `configureDatabase()` method will set the given database as the default on on a brand new connection modeled after the given `$managerConnection`
```php
use Envor\ManagedDatabases\ManagedDatabases;
ManagedDatabases::createDatabase('database2', 'sqlite');
ManagedDatabases::useDatabase('database2', 'sqlite');
config('database.default');
// database2
config('database.connections.database2')
// [
// "driver" => "sqlite",
// "url" => null,
// "database" => "/home/forge/mysite.com/storage/app/managed_database2.sqlite",
// "prefix" => "",
// "foreign_key_constraints" => true,
// ]
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [inmanturbo](https://github.com/envor)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.