Skip to content

Added tests, fixed a couple of bugs, added new feature #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
/vendor
/coverage
composer.lock
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ By default the query builder will fetch the latest version (e. g. `User::find(1)

* `allVersions()` returns all versions of the queried items<br>Example: `User::allVersions()->get()` will return all versions of all users

* `moment(Carbon)` returns a specific version, closest but lower than the input date<br>Example: `User::moment(Carbon::now()->subWeek()->find(1)` will return the version at that point in time.

#### Create, update and delete records

All these operations can be performed normally. The package will automatically generate a version 1 on create, the next version on update and will remove all versions on delete.
Expand Down
25 changes: 23 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
{
"name": "proai/eloquent-versioning",
"description": "An extension for the Eloquent ORM to support versioning.",
"keywords": ["laravel","orm","eloquent","revision","version","versioned","versioning","audit"],
"keywords": [
"laravel",
"orm",
"eloquent",
"revision",
"version",
"versioned",
"versioning",
"audit",
"audits"
],
"homepage": "http://github.com/ProAI/eloquent-versioning",
"license": "MIT",
"authors": [
Expand All @@ -11,14 +21,25 @@
}
],
"require": {
"php": ">=5.4.0",
"php": ">=7.1",
"illuminate/database": "5.*"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^3.6",
"orchestra/database": "^3.6"
},
"autoload": {
"psr-4": {
"ProAI\\Versioning\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"ProAI\\Versioning\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
<testsuite name="Versioning Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
15 changes: 11 additions & 4 deletions src/BuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ public function insert(array $values)

// set version, ref_id and latest_version
$values[$this->model->getLatestVersionColumn()] = 1;
$versionValues[$this->model->getVersionKeyName()] = $this->model->getKey();
$versionValues[$this->model->getVersionColumn()] = 1;

// insert main table record
if (! $this->query->insert($values)) {
if (! $id = $this->query->insertGetId($values)) {
return false;
}

$versionValues[$this->model->getVersionKeyName()] = $id;
$versionValues[$this->model->getVersionColumn()] = 1;

// insert version table record
$db = $this->model->getConnection();
return $db->table($this->model->getVersionTable())->insert($versionValues);
Expand Down Expand Up @@ -87,6 +88,9 @@ public function insertGetId(array $values, $sequence = null)
return false;
}

// fill the latest version value
$this->model->{$this->model->getLatestVersionColumn()} = 1;

return $id;
}

Expand Down Expand Up @@ -126,14 +130,17 @@ public function update(array $values)

// set version and ref_id
$recordVersionValues[$this->model->getVersionKeyName()] = $record->{$this->model->getKeyName()};
$recordVersionValues[$this->model->getVersionColumn()] = $record->{$this->model->getVersionColumn()}+1;
$recordVersionValues[$this->model->getVersionColumn()] = $record->{$this->model->getLatestVersionColumn()}+1;

// insert new version
if(! $db->table($this->model->getVersionTable())->insert($recordVersionValues)) {
return false;
}
}

// fill the latest version value
$this->model->{$this->model->getLatestVersionColumn()} += 1;

return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/SoftDeletes.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ public function getQualifiedDeletedAtColumn()

return $this->getTable().'.'.$deletedAt;
}

}
46 changes: 41 additions & 5 deletions src/VersioningScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Query\JoinClause;
use Carbon\Carbon;

class VersioningScope implements Scope
{
Expand All @@ -14,7 +15,7 @@ class VersioningScope implements Scope
*
* @var array
*/
protected $extensions = ['Version', 'AllVersions'];
protected $extensions = ['Version', 'AllVersions', 'Moment'];

/**
* Apply the scope to a given Eloquent query builder.
Expand All @@ -25,10 +26,12 @@ class VersioningScope implements Scope
*/
public function apply(Builder $builder, Model $model)
{
$builder->join($model->getVersionTable(), function($join) use ($model) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
$join->on($model->getQualifiedVersionColumn(), '=', $model->getQualifiedLatestVersionColumn());
});
if (!$this->hasVersionJoin($builder, $model->getVersionTable())) {
$builder->join($model->getVersionTable(), function($join) use ($model) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
$join->on($model->getQualifiedVersionColumn(), '=', $model->getQualifiedLatestVersionColumn());
});
}

$this->extend($builder);
}
Expand Down Expand Up @@ -109,6 +112,28 @@ protected function addAllVersions(Builder $builder)
});
}

/**
* Add the moment extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addMoment(Builder $builder)
{
$builder->macro('moment', function(Builder $builder, Carbon $moment) {
$model = $builder->getModel();

$this->remove($builder, $builder->getModel());

$builder->join($model->getVersionTable(), function($join) use ($model, $moment) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
$join->where('updated_at', '<=', $moment)->orderBy('updated_at', 'desc')->limit(1);
})->orderBy('updated_at', 'desc')->limit(1);

return $builder;
});
}

/**
* Determine if the given join clause is a version constraint.
*
Expand All @@ -121,4 +146,15 @@ protected function isVersionJoinConstraint(JoinClause $join, $table)
return $join->type == 'inner' && $join->table == $table;
}

/**
* Determine if the given builder contains a join with the given table
*
* @param Builder $builder
* @param string $table
* @return bool
*/
protected function hasVersionJoin(Builder $builder, string $table)
{
return collect($builder->getQuery()->joins)->pluck('table')->contains($table);
}
}
26 changes: 26 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ProAI\Versioning\Tests\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

use ProAI\Versioning\Versionable;
use ProAI\Versioning\SoftDeletes;

class Post extends Authenticatable
{
use Versionable, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title', 'content'
];

public $timestamps = true;

public $versioned = ['content', 'updated_at'];
}
35 changes: 35 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace ProAI\Versioning\Tests\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

use ProAI\Versioning\Versionable;
use ProAI\Versioning\SoftDeletes;

class User extends Authenticatable
{
use Versionable, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'username', 'city', 'latest_version',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

public $timestamps = true;

public $versioned = ['email', 'city', 'updated_at', 'deleted_at'];
}
33 changes: 33 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ProAI\Versioning\Tests;

use Orchestra\Database\ConsoleServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
/**
* {@inheritdoc}
*/
protected function getEnvironmentSetUp($app)
{
// Database
$app['config']->set('database.default', 'testing');
$app['config']->set('database.connections.testing', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}

/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
$this->loadMigrationsFrom(__DIR__.'/database/migrations');
$this->withFactories(__DIR__.'/database/factories');
}
}
Loading