14 Commits

Author SHA1 Message Date
Victor Gonzalez
000f1e3851 Merge pull request #27 from vicgonvt/update_travis_php
Updates the travis CI and bumps PHP version
2020-03-18 09:52:34 -04:00
Victor Gonzalex
7bcec7872f allow for custom namespacing 2020-03-18 09:50:03 -04:00
Victor Gonzalez
449dafcb89 Merge pull request #26 from vicgonvt/feature_namespace_custom
Allow for custom namespacing
2020-03-18 09:01:02 -04:00
Victor Gonzalex
af513debec allow for custom namespacing 2020-03-18 08:50:45 -04:00
Victor Gonzalez
5759629d34 Merge pull request #25 from vicgonvt/feature_storage_json
Adds the ability to have the json files stored remotely
2020-03-18 08:19:29 -04:00
Victor Gonzalex
0ab4a4f053 change tests name to be unique for remote test 2020-03-18 07:59:34 -04:00
Victor Gonzalex
163be72b86 fixes all tests 2020-03-18 07:57:45 -04:00
Victor Gonzalex
20b07aeea8 fix broken tests 2020-03-18 07:30:40 -04:00
Victor Gonzalex
168923d3da wip getting the Storage driver to work for remote json files 2020-03-17 16:31:17 -04:00
Victor Gonzalez
b177e8de2c Merge pull request #24 from vicgonvt/update_testbench
updates testbench to 5.0 and phpunit 9
2020-03-17 14:37:46 -04:00
Victor Gonzalex
cd3adf1a96 updates testbench to 5.0 and phpunit 9 2020-03-17 14:33:40 -04:00
nullthoughts
8a987b89c5 Update namespace 2020-03-17 14:01:56 -04:00
nullthoughts
552bc8fba8 Update namespace 2020-03-17 13:55:24 -04:00
nullthoughts
7b89add86e Update namespace from distinctm to nullthoughts 2020-03-17 13:51:26 -04:00
17 changed files with 1721 additions and 1622 deletions

View File

@@ -7,9 +7,6 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
- php: 7.1
env: setup=lowest
- php: 7.2
- php: 7.2
env: setup=lowest

View File

@@ -1,33 +1,34 @@
{
"name": "distinctm/laravel-data-sync",
"name": "nullthoughts/laravel-data-sync",
"description": "Laravel utility to keep records synced between environments through source control",
"license": "MIT",
"authors": [
{
"name": "distinctm",
"email": "jani@marketdistinctly.com"
"name": "nullthoughts",
"email": "jani@nullincorporated.com"
}
],
"require": {
"ext-json": "*"
"ext-json": "*",
"php": "^7.2"
},
"require-dev": {
"orchestra/testbench": "^3.7"
"orchestra/testbench": "^5.0"
},
"autoload": {
"psr-4" : {
"distinctm\\LaravelDataSync\\": "src/"
"nullthoughts\\LaravelDataSync\\": "src/"
}
},
"autoload-dev": {
"psr-4" : {
"distinctm\\LaravelDataSync\\Tests\\": "tests/"
"nullthoughts\\LaravelDataSync\\Tests\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
"distinctm\\LaravelDataSync\\DataSyncBaseServiceProvider"
"nullthoughts\\LaravelDataSync\\DataSyncBaseServiceProvider"
]
}
}

3002
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
<p align="center">
<a href="https://packagist.org/packages/distinctm/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/distinctm/laravel-data-sync/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/distinctm/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/distinctm/laravel-data-sync/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://packagist.org/packages/nullthoughts/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/nullthoughts/laravel-data-sync/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/nullthoughts/laravel-data-sync" target="_blank"><img src="https://poser.pugx.org/nullthoughts/laravel-data-sync/v/stable.svg" alt="Latest Stable Version"></a>
<a href="https://travis-ci.com/nullthoughts/laravel-data-sync"><img src="https://api.travis-ci.com/nullthoughts/laravel-data-sync.svg?branch=master" alt="Travis CI Build Status: Master"></a>
</p>
@@ -11,7 +11,7 @@ Laravel utility to keep records synchronized between environments through source
## Installation
You can install this package via composer:
```bash
composer require distinctm/laravel-data-sync
composer require nullthoughts/laravel-data-sync
```
Or add this line in your `composer.json`, inside of the `require` section:
@@ -19,14 +19,14 @@ Or add this line in your `composer.json`, inside of the `require` section:
``` json
{
"require": {
"distinctm/laravel-data-sync": "^1.0",
"nullthoughts/laravel-data-sync": "^1.0",
}
}
```
then run ` composer install `
## Usage
- Run `php artisan vendor:publish --provider="distinctm\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"` to publish config file. Specify directory for sync data files (default is a new sync directory in the project root)
- Run `php artisan vendor:publish --provider="nullthoughts\LaravelDataSync\DataSyncBaseServiceProvider" --tag="data-sync-config"` to publish config file. Specify directory for sync data files (default is a new sync directory in the project root)
- Create a JSON file for each model, using the model name as the filename. Example: Product.json would update the Product model
- Use nested arrays in place of hardcoded IDs for relationships
- Run `php artisan data:sync` (or `php artisan data:sync --model={model}` with the model flag to specify a model)

View File

@@ -1,8 +1,8 @@
<?php
namespace distinctm\LaravelDataSync\Console\Commands;
namespace nullthoughts\LaravelDataSync\Console\Commands;
use distinctm\LaravelDataSync\Updater;
use nullthoughts\LaravelDataSync\Updater;
use Illuminate\Console\Command;
class Sync extends Command

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync;
namespace nullthoughts\LaravelDataSync;
use Illuminate\Support\ServiceProvider;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
namespace nullthoughts\LaravelDataSync\Exceptions;
use Exception;
use Throwable;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
namespace nullthoughts\LaravelDataSync\Exceptions;
use Exception;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
namespace nullthoughts\LaravelDataSync\Exceptions;
use Exception;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Exceptions;
namespace nullthoughts\LaravelDataSync\Exceptions;
use Exception;
use Throwable;

View File

@@ -1,30 +1,72 @@
<?php
namespace distinctm\LaravelDataSync;
namespace nullthoughts\LaravelDataSync;
use distinctm\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
use distinctm\LaravelDataSync\Exceptions\NoCriteriaException;
use distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException;
use nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException;
use nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException;
use stdClass;
class Updater
{
/**
* @var string
*/
private $directory;
/**
* @var \Illuminate\Support\Collection
*/
private $files;
/**
* @var bool
*/
private $remote;
/**
* @var string
*/
private $disk;
/**
* @var string
*/
private $baseNamespace = '\\App\\';
/**
* Get files in sync directory.
*
* @param string|null $path
* @param string|null $model
* @param string|null $path
* @param string|null $model
*
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
* @param bool $remote
* @param string $disk
*
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
*/
public function __construct($path = null, $model = null)
public function __construct($path = null, $model = null, $remote = false, $disk = 's3')
{
$directory = $this->getDirectory($path);
$this->files = $this->getFiles($directory, $model);
$this->remote = $remote;
$this->disk = $disk;
$this->directory = $this->getDirectory($path);
$this->files = $this->getFiles($this->directory, $model);
}
/**
* Override the default namespace for the class.
*
* @param $namespace
*/
public function setNamespace($namespace)
{
$this->baseNamespace = $namespace;
}
/**
@@ -50,11 +92,11 @@ class Updater
/**
* Parse each record for criteria/values and update/create model.
*
* @param string $file
*
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
* @param string $file
*
* @return \Illuminate\Support\Collection
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
*/
protected function syncModel(string $file)
{
@@ -81,7 +123,7 @@ class Updater
*
* @param $path
*
* @throws \distinctm\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
* @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
*
* @return string
*/
@@ -89,7 +131,7 @@ class Updater
{
$directory = $path ?? config('data-sync.path', base_path('sync'));
if (!file_exists($directory)) {
if ($this->directoryMissingLocally($directory) || $this->directoryMissingRemotely($directory)) {
throw new FileDirectoryNotFoundException();
}
@@ -110,10 +152,17 @@ class Updater
return Collection::wrap($directory.'/'.$model.'.json');
}
return collect(File::files($directory))
$files = ($this->remote) ? Storage::disk($this->disk)->files($directory) : File::files($directory);
return collect($files)
->filter(function ($file) {
return pathinfo($file, PATHINFO_EXTENSION) == 'json';
})->map(function ($path) {
if (is_string($path)) {
return $path;
}
return $path->getPathname();
});
}
@@ -148,7 +197,7 @@ class Updater
*
* @param stdClass $record
*
* @throws \distinctm\LaravelDataSync\Exceptions\NoCriteriaException
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
*
* @return \Illuminate\Support\Collection
*/
@@ -198,21 +247,23 @@ class Updater
*/
protected function getModel(string $name)
{
return '\\App\\'.Str::studly(pathinfo($name, PATHINFO_FILENAME));
return $this->baseNamespace.Str::studly(pathinfo($name, PATHINFO_FILENAME));
}
/**
* Parses JSON from file and returns collection.
*
* @param string $file
*
* @throws \distinctm\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
* @param string $file
*
* @return \Illuminate\Support\Collection
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
*/
protected function getRecords(string $file)
{
$records = collect(json_decode(File::get($file)));
$fetchedFile = ($this->remote) ? Storage::disk($this->disk)->get($file) : File::get($file);
$records = collect(json_decode($fetchedFile));
if ($records->isEmpty()) {
throw new NoRecordsInvalidJSONException($file);
@@ -273,4 +324,24 @@ class Updater
return [$key => $value];
})->toArray();
}
/**
* @param \Illuminate\Config\Repository $directory
*
* @return bool
*/
protected function directoryMissingLocally($directory)
{
return !$this->remote && !file_exists($directory);
}
/**
* @param \Illuminate\Config\Repository $directory
*
* @return bool
*/
protected function directoryMissingRemotely($directory)
{
return $this->remote && !Storage::disk($this->disk)->exists($directory);
}
}

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Database\Eloquent\Model;

View File

@@ -1,6 +1,6 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

View File

@@ -0,0 +1,160 @@
<?php
namespace nullthoughts\LaravelDataSync\Tests;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
use Exception;
class UpdaterRemoteTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Storage::fake('s3');
Storage::disk('s3')->put('test-data/roles.json', File::get(__DIR__.'/../test-data/roles.json'));
foreach (File::directories(__DIR__.'/../test-data/') as $directory) {
$files = File::files($directory);
foreach ($files as $file) {
Storage::disk('s3')->put('test-data/'.basename($directory).'/'.$file->getRelativePathname(), File::get($file->getPathname()));
}
}
}
/** @test */
public function it_adds_roles_to_the_database_in_remote()
{
$updater = new UpdaterFake('test-data', 'roles', true, 's3');
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
}
/** @test */
public function it_can_default_to_configuration_in_remote()
{
config()->set('data-sync.path', 'test-data');
$updater = new UpdaterFake(null, null, true, 's3');
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
$this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
}
/** @test */
public function it_can_update_an_existing_record_in_remote()
{
config()->set('data-sync.path', 'test-data');
(new UpdaterFake(null, null, true, 's3'))->run();
config()->set('data-sync.path', 'test-data/valid');
(new UpdaterFake(null, null, true, 's3'))->run();
$this->assertDatabaseHas('roles', ['category' => 'changed']);
$this->assertDatabaseHas('roles', ['category' => 'changed']);
$this->assertDatabaseHas('roles', ['category' => 'changed']);
}
/** @test */
public function it_can_update_the_relationship_in_remote()
{
$supervisor = Supervisor::create([
'name' => 'CEO',
]);
config()->set('data-sync.path', 'test-data/relationship');
(new UpdaterFake(null, null, true, 's3'))->run();
$this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
$this->assertTrue($supervisor->is(Roles::first()->supervisor));
}
/**
* @test
* @group current
*/
public function exception_is_thrown_if_the_directory_does_not_exists()
{
try {
new UpdaterFake(null, null, true, 's3');
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
}
}
/** @test */
public function invalid_json_throws_an_exception_in_remote()
{
try {
$updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
$updater->run();
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
}
}
/** @test */
public function the_json_must_contain_a_key_with_an_underscore_in_remote()
{
try {
$updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
$updater->run();
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertEquals('No criteria/attributes detected', $e->getMessage());
}
}
/** @test */
public function order_of_imports_can_be_defined_in_config_in_remote()
{
config()->set('data-sync.order', [
'Supervisor',
'Roles',
]);
$updater = new UpdaterFake('test-data/ordered', null, true, 's3');
$updater->run();
$this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
$this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
}
/** @test */
public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote()
{
config()->set('data-sync.order', [
'Roles',
'Supervisor',
]);
$this->expectException(ErrorUpdatingModelException::class);
$updater = new UpdaterFake('test-data/ordered', null, true, 's3');
$updater->run();
}
/** @test */
public function it_ignores_non_json_files_in_remote()
{
$updater = new UpdaterFake('test-data/not-json', null, true, 's3');
$updater->run();
$this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
}
}

View File

@@ -1,9 +1,9 @@
<?php
namespace distinctm\LaravelDataSync\Tests;
namespace nullthoughts\LaravelDataSync\Tests;
use distinctm\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use distinctm\LaravelDataSync\Tests\fakes\UpdaterFake;
use nullthoughts\LaravelDataSync\Exceptions\ErrorUpdatingModelException;
use nullthoughts\LaravelDataSync\Tests\fakes\UpdaterFake;
use Exception;
class UpdaterTest extends TestCase
@@ -83,7 +83,7 @@ class UpdaterTest extends TestCase
$this->fail('exception was thrown');
} catch (Exception $e) {
$this->assertContains('No records or invalid JSON for', $e->getMessage());
$this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
}
}

View File

@@ -1,15 +1,15 @@
<?php
namespace distinctm\LaravelDataSync\Tests\Fakes;
namespace nullthoughts\LaravelDataSync\Tests\Fakes;
use distinctm\LaravelDataSync\Updater;
use nullthoughts\LaravelDataSync\Updater;
use Illuminate\Support\Str;
class UpdaterFake extends Updater
{
protected function getModel(string $name)
{
return '\\distinctm\\LaravelDataSync\\Tests\\'.Str::studly(
return '\\nullthoughts\\LaravelDataSync\\Tests\\'.Str::studly(
pathinfo($name, PATHINFO_FILENAME)
);
}