Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed Feb 15, 2024
1 parent c4079cf commit 35b3389
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 45 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"require-dev": {
"larastan/larastan": "*",
"laravel/octane": "^2.3",
"laravel/pint": "*",
"nunomaduro/collision": "*",
"orchestra/testbench": "^9.0",
Expand Down
30 changes: 30 additions & 0 deletions database/factories/DatastoreFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Envor\Datastore\Database\Factories;

use Envor\Datastore\Driver;
use Illuminate\Database\Eloquent\Factories\Factory;

class DatastoreFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = \Envor\Datastore\Tests\Fixtures\Datastore::class;

Check failure on line 15 in database/factories/DatastoreFactory.php

View workflow job for this annotation

GitHub Actions / phpstan

PHPDoc type string of property Envor\Datastore\Database\Factories\DatastoreFactory::$model is not covariant with PHPDoc type class-string<Illuminate\Database\Eloquent\Model> of overridden property Illuminate\Database\Eloquent\Factories\Factory<Illuminate\Database\Eloquent\Model>::$model.

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'uuid' => $this->faker->uuid,
'name' => ':memory:',
'driver' => Driver::SQLite,
];
}
}
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ use Illuminate\Support\Facades\Schema;

return new class extends Migration
{

public function getConnection()
{
return config('database.platform', 'testing');
}

public function up()
{
Schema::create('datastores', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->index()->unique();
$table->string('name')->index()->unique();
$table->string('driver');
$table->morphs('owner');
$table->nullableMorphs('owner');
$table->timestamps();
});
}
Expand Down
23 changes: 14 additions & 9 deletions src/Concerns/HasDatastoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ trait HasDatastoreDriver
use HasPlatformUuids;
use UsesPlatformConnection;

protected $guarded = [];

protected $casts = [
'driver' => Driver::class,
];

public function owner()
{
return $this->morphTo();
Expand All @@ -28,7 +22,7 @@ protected static function bootHasDatastoreDriver()
static::created(function (Model $model) {
$model->driver = $model->driver ?? Driver::SQLite;

$model->createDatabase();
$model->createDatabase()->migrate();
});
}

Expand All @@ -42,14 +36,25 @@ public function use()
public function createDatabase()
{
$this->database()->create();

return $this;
}

public function configure()
{
return $this->database()->configure();
$this->database()->configure();

return $this;
}

public function migrate()
{
$this->database()->migrate();

return $this;
}

protected function database()
public function database()
{
return $this->driver->toNewDatabase($this->name);
}
Expand Down
15 changes: 14 additions & 1 deletion src/Databases/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class SQLite extends Datastore
{

protected function createDatabase(): void
{
if ($this->name === ':memory:') {
return;
}

parent::createDatabase();
}

protected function makeAdminConfig() : mixed
{
return config('database.connections.sqlite');
Expand All @@ -29,8 +39,11 @@ protected function configureDatabase() : void
$connection = basename($this->name, '.sqlite');

config([
'database.default' => $connection,
"database.connections.{$connection}" => $this->config,
]);

config([
'database.default' => $connection,
]);
}
}
20 changes: 9 additions & 11 deletions src/Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public function __construct(string $name, string $disk = 'local')
{
$this->name = $this->makeName($name);

$this->config = $this->makeConfig();

$this->adminName = $this->makeAdminName($name);
$this->adminConfig = $this->makeAdminConfig();

$this->adminName = $this->makeAdminName($name);
$this->config = $this->makeConfig();

}

public function migrateOptions(array $options): self
Expand All @@ -50,12 +51,7 @@ public function __toString()
{
return $this->name;
}

public function __destruct()
{
$this->cleanup();
}


public function clearConfigs(): void
{
$isDatastoreConfig = function ($value, $key) {
Expand Down Expand Up @@ -96,7 +92,6 @@ public function migrate(): void
{
$this->configure();
$this->callMigrateCommand();
$this->cleanup();
}

public function cleanup(): void
Expand Down Expand Up @@ -211,9 +206,12 @@ protected function configureDatabase() : void
$connection = $this->name;

config([
'database.default' => $connection,
"database.connections.{$connection}" => $this->config,
]);

config([
'database.default' => $connection,
]);
}

public function configure() : void
Expand Down
14 changes: 14 additions & 0 deletions src/DatastoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Envor\Datastore;

use Envor\Datastore\Commands\DatastoreCommand;
use Envor\Datastore\Databases\SQLite;
use Illuminate\Support\Facades\Event;
use Laravel\Octane\Events\RequestTerminated;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -22,4 +25,15 @@ public function configurePackage(Package $package): void
->hasMigration('create_laravel-datastore_table')
->hasCommand(DatastoreCommand::class);
}

public function packageBooted()
{

if (! isset($_SERVER['LARAVEL_OCTANE'])) {

return;
}

Event::listen(fn (RequestTerminated $requestTerminated) => (new SQLite(':memory:'))->cleanup());
}
}
20 changes: 20 additions & 0 deletions tests/Fixtures/Datastore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Envor\Datastore\Tests\Fixtures;

use Envor\Datastore\Concerns\HasDatastoreDriver;
use Envor\Datastore\Driver;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Datastore extends Model
{
use HasDatastoreDriver;
use HasFactory;

protected $guarded = [];

protected $casts = [
'driver' => Driver::class,
];
}
22 changes: 22 additions & 0 deletions tests/HasDatastoreDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Envor\Datastore\Tests\Fixtures\Datastore;
use Illuminate\Support\Facades\Schema;

it('will configure the datastore', function () {
$datastore = Datastore::factory()->create();

$datastore->configure();

Check failure on line 9 in tests/HasDatastoreDriverTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model::configure().

expect(config('database.default'))->toBe($datastore->database()->name);

Check failure on line 11 in tests/HasDatastoreDriverTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model::database().

expect(config("database.connections.{$datastore->database()->name}"))->toBe($datastore->database()->config);

Check failure on line 13 in tests/HasDatastoreDriverTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model::database().

Check failure on line 13 in tests/HasDatastoreDriverTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model::database().
});

it('will create and migrate the datastore', function () {
$datastore = Datastore::factory()->create();

$datastore->migrate();

Check failure on line 19 in tests/HasDatastoreDriverTest.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Collection<int, Illuminate\Database\Eloquent\Model>|Illuminate\Database\Eloquent\Model::migrate().

expect(Schema::connection($datastore->database()->name)->hasTable('migrations'))->toBeTrue();
});
9 changes: 5 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Envor\Datastore\Tests;

use Envor\Datastore\DatastoreServiceProvider;
use Envor\SchemaMacros\SchemaMacrosServiceProvider;
use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -21,16 +22,16 @@ protected function getPackageProviders($app)
{
return [
DatastoreServiceProvider::class,
SchemaMacrosServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
config()->set('database.platform', 'testing');

/*
$migration = include __DIR__.'/../database/migrations/create_laravel-datastore_table.php.stub';
$migration = include __DIR__.'/../database/migrations/create_datastores_table.php.stub';
$migration->up();
*/

}
}

0 comments on commit 35b3389

Please sign in to comment.