Skip to content

Commit

Permalink
Merge pull request #8 from envor/main
Browse files Browse the repository at this point in the history
add support for in memory database
  • Loading branch information
inmanturbo authored Feb 12, 2024
2 parents 26b220f + 3405f3b commit b346efc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-managed-databases` will be documented in this file.

## v1.1.1 - 2024-02-11

**Full Changelog**: https://github.com/envor/laravel-managed-databases/commits/v1.1.1

## v1.1.0 - 2024-02-08

### What's Changed
Expand Down
22 changes: 20 additions & 2 deletions src/ManagedDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public static function createsConnectionConfig(string|Stringable $database, $man
return $database;
}

public static function createsInMemoryConnectionConfig(string|Stringable $database): string
{

config(['database.connections.'.$database => array_merge(
config('database.connections.manager_sqlite'), [
'database' => ':memory:',
])]);

return $database;
}

public static function restoreCurrentConnection()
{
DB::purge('current');
Expand Down Expand Up @@ -142,9 +153,16 @@ public static function runOnDatabase($database, $callback, $managerConnection =
return $result;
}

public static function configureDatabase($database, $managerConnection = 'manager_sqlite')
public static function configureDatabase($database, $managerConnection = 'manager_sqlite', $disk = 'local')
{
$database = static::createsConnectionConfig($database, $managerConnection, $disk);

static::setDefaultConnection($database);
}

public static function configureInMemoryDatabase($database)
{
$database = static::createsConnectionConfig($database, $managerConnection);
$database = static::createsInMemoryConnectionConfig($database);

static::setDefaultConnection($database);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ConfigureDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Envor\ManagedDatabases\ManagedDatabases;

it('can configure an in memory database for the testing', function () {
ManagedDatabases::configureInMemoryDatabase('testing_123');

expect(config('database.connections.testing_123.database'))->toBe(':memory:');

expect(config('database.default'))->toBe('testing_123');
});
1 change: 0 additions & 1 deletion tests/CreateDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@
expect(ManagedDatabases::runOnDatabase('md_test', function () {
return Schema::hasTable('migrations');
}))->toBeTrue();

});

0 comments on commit b346efc

Please sign in to comment.