Skip to content

Commit

Permalink
Paving the way for Metadata access via Eloquent.
Browse files Browse the repository at this point in the history
Make the id the primary key on the filter_metadata table.
Add unique constraint to disk, path columns.
Add BackingData cast for filer_metadata->backing_data DB column.
Update the Database repository to format the timestamp correctly.
Add Uuid trait to support creation of Metadata through the Eloquent model.
Update the base TestCase to support config changes.
  • Loading branch information
nvahalik committed Apr 15, 2021
1 parent be1bb18 commit fd3c357
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 4 deletions.
4 changes: 2 additions & 2 deletions database/migrations/2021_04_08_105954_create_filer_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function up()
$table->enum('visibility', ['public', 'private'])->comment('Visibiilty of the file.');
$table->json('backing_data')->comment('The information about where the file is stored and how.');
$table->timestamp('timestamp')->useCurrent();
$table->primary([
$table->unique([
'disk', 'path',
]);
$table->unique('id');
$table->primary('id');
});
}

Expand Down
19 changes: 19 additions & 0 deletions src/Casts/BackingData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Nvahalik\Filer\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class BackingData implements CastsAttributes
{

public function get($model, string $key, $value, array $attributes)
{
return \Nvahalik\Filer\BackingData::unserialize($value);
}

public function set($model, string $key, $value, array $attributes)
{
return $value->toJson();
}
}
8 changes: 6 additions & 2 deletions src/MetadataRepository/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Nvahalik\Filer\MetadataRepository;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Nvahalik\Filer\BackingData;
use Nvahalik\Filer\Contracts\MetadataRepository;
use Nvahalik\Filer\Metadata;
Expand Down Expand Up @@ -89,6 +91,7 @@ public function listContents(string $directory = '', bool $recursive = false)
->where('path', 'NOT LIKE', "$directory%/%");
})->cursor()
->map(function ($record) {
$record->timestamp = Carbon::parse($record->timestamp)->format('U');
$record->backing_data = BackingData::unserialize($record->backing_data);

return $record;
Expand All @@ -115,11 +118,12 @@ public function record(Metadata $metadata)

$updates = Arr::where(array_keys($updatePayload), fn ($a) => $a !== 'path');

$updatePayload['timestamp'] = Carbon::parse($updatePayload['timestamp'])->toDateTimeString();
$updatePayload['disk'] = $this->storageId;
$updatePayload['id'] = $updatePayload['id'] ?? Uuid::uuid4();
$updatePayload['id'] = $updatePayload['id'] ?? Str::uuid();

$this->newQuery()
->upsert($updatePayload, ['path', 'disk'], $updates);
->upsert($updatePayload, ['id'], $updates);
}

public function delete(string $path)
Expand Down
57 changes: 57 additions & 0 deletions src/Models/Metadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Nvahalik\Filer\Models;

use Illuminate\Database\Eloquent\Model;
use Nvahalik\Filer\Casts\BackingData;
use Nvahalik\Filer\Traits\Uuid;

class Metadata extends Model
{
use Uuid;

protected $table = 'filer_metadata';

/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;

/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'uuid';

protected $fillable = [
'id',
'disk',
'path',
'size',
'mimetype',
'etag',
'visibility',
'backing_data',
'timestamp',
];

protected $casts = [
'size' => 'integer',
'backing_data' => BackingData::class
];

protected $dates = [
'timestamp',
];

public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->setConnection(config('filer.database.connection'));
}

}
17 changes: 17 additions & 0 deletions src/Traits/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Nvahalik\Filer\Traits;

use Illuminate\Support\Str;

trait Uuid
{
protected static function boot()
{
parent::boot();

static::creating(function ($model) {
$model->id = (string) Str::uuid();
});
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ protected function defineEnvironment($app)
// Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench');

$app['config']->set('filer.metadata', 'database');
$app['config']->set('filer.database.connection', 'testbench');

$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
Expand Down

0 comments on commit fd3c357

Please sign in to comment.