├── .editorconfig
├── .github
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
└── workflows
│ └── phpunit.yml
├── .gitignore
├── .travis.yml
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── composer.json
├── config
└── data-sync.php
├── phpunit.xml
├── pint.json
├── psalm.xml
├── readme.md
├── rector.php
├── scratch.md
├── src
├── Console
│ └── Commands
│ │ └── Sync.php
├── DataSyncBaseServiceProvider.php
├── Exceptions
│ ├── ErrorUpdatingModelException.php
│ ├── FileDirectoryNotFoundException.php
│ ├── NoCriteriaException.php
│ └── NoRecordsInvalidJSONException.php
└── Updater.php
└── tests
├── Roles.php
├── Supervisor.php
├── TestCase.php
├── Unit
├── UpdaterRemoteTest.php
└── UpdaterTest.php
├── fakes
└── UpdaterFake.php
└── test-data
├── invalid-json
└── invalid.json
├── no-criteria
└── no-criteria.json
├── not-json
└── roles.txt
├── ordered
├── roles.json
└── supervisor.json
├── relationship
└── roles.json
├── roles.json
└── valid
└── roles.json
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig is awesome: https://editorconfig.org
2 |
3 | # top-most EditorConfig file
4 | root = true
5 |
6 | # Unix-style newlines with a newline ending every file
7 | [*]
8 | end_of_line = lf
9 | insert_final_newline = true
10 | charset = utf-8
11 | indent_style = space
12 | indent_size = 4
13 |
14 | [*.md]
15 | trim_trailing_whitespace = false
16 |
17 | [*.{yml,yaml}]
18 | indent_size = 2
19 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: bug
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 |
27 | **Laravel (please complete the following information):**
28 | - Host OS: [e.g. Linux]
29 | - Version [e.g. 6.1.0]
30 |
31 | **Desktop (please complete the following information):**
32 | - OS: [e.g. iOS]
33 | - Browser [e.g. chrome, safari]
34 | - Version [e.g. 22]
35 |
36 | **Smartphone (please complete the following information):**
37 | - Device: [e.g. iPhone6]
38 | - OS: [e.g. iOS8.1]
39 | - Browser [e.g. stock browser, safari]
40 | - Version [e.g. 22]
41 |
42 | **Additional context**
43 | Add any other context about the problem here.
44 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: enhancement
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/workflows/phpunit.yml:
--------------------------------------------------------------------------------
1 | ---
2 | # yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
3 | name: PHPUnit
4 |
5 | on:
6 | push:
7 | branches: [master, main]
8 | pull_request:
9 | branches: [master, main]
10 |
11 | jobs:
12 | build-test:
13 | runs-on: ubuntu-latest
14 |
15 | permissions:
16 | contents: write
17 | statuses: write
18 |
19 | strategy:
20 | fail-fast: true
21 | matrix:
22 | php: [ '8.0', '8.1', '8.2', '8.3' ]
23 |
24 | steps:
25 | - uses: actions/checkout@v4
26 |
27 | - name: Setup PHP
28 | uses: shivammathur/setup-php@v2
29 | with:
30 | php-version: ${{ matrix.php }}
31 | tools: composer
32 | extensions: json, dom, curl, libxml, mbstring
33 | coverage: xdebug
34 | env:
35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36 |
37 | - name: Get composer cache directory
38 | id: composer-cache-dir
39 | run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
40 |
41 | - name: Get composer.lock or composer.json hash for caching
42 | id: hash
43 | shell: bash
44 | run: |
45 | if [ -f composer.lock ]; then
46 | echo "lock=${{ hashFiles('**/composer.lock') }}" >> $GITHUB_OUTPUT
47 | else
48 | echo "lock=${{ hashFiles('**/composer.json') }}" >> $GITHUB_OUTPUT
49 | fi
50 |
51 | - name: Cache Composer packages
52 | id: composer-cache
53 | uses: actions/cache@v4
54 | with:
55 | path: ${{ steps.composer-cache-dir.outputs.dir }}
56 | key: ${{ runner.os }}-php-${{ matrix.php }}-${{ steps.hash.outputs.lock }}
57 | restore-keys: |
58 | ${{ runner.os }}-php-${{ matrix.php }}-${{ steps.hash.outputs.lock }}
59 | ${{ runner.os }}-php-${{ matrix.php }}-
60 | ${{ runner.os }}-php-
61 |
62 | - name: Install Dependencies (prefer-${{ matrix.stability }})
63 | run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
64 |
65 | - name: Configure matchers
66 | uses: mheap/phpunit-matcher-action@v1
67 |
68 | - name: Execute composer test (Unit and Feature tests)
69 | run: composer test:ci
70 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules
2 | /public/hot
3 | /public/storage
4 | /storage/*.key
5 | /vendor
6 | /.idea
7 | /.vscode
8 | /nbproject
9 | /.vagrant
10 | Homestead.json
11 | Homestead.yaml
12 | npm-debug.log
13 | yarn-error.log
14 | .env
15 | *.cache
16 | composer.lock
17 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: php
2 |
3 | env:
4 | global:
5 | - setup=stable
6 |
7 | matrix:
8 | fast_finish: true
9 | include:
10 | - php: 8.0
11 | - php: 8.0
12 | env: setup=lowest
13 | - php: 8.1
14 | - php: 8.1
15 | env: setup=lowest
16 | - php: 8.2
17 | - php: 8.2
18 | env: setup=lowest
19 |
20 | sudo: false
21 |
22 | cache:
23 | directories:
24 | - $HOME/.composer/cache
25 |
26 | install:
27 | - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
28 | - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi
29 |
30 | script: vendor/bin/phpunit
31 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, sex characteristics, gender identity and expression,
9 | level of experience, education, socio-economic status, nationality, personal
10 | appearance, race, religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at jani@nullincorporated.com. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 | For answers to common questions about this code of conduct, see
76 | https://www.contributor-covenant.org/faq
77 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contribution Guide
2 |
3 |
4 | ## Bug Reports
5 |
6 | To encourage active collaboration, we strongly encourages pull requests, not just bug reports. "Bug reports" may also be sent in the form of a pull request containing a failing test.
7 |
8 | However, if you file a bug report, your issue should contain a title and a clear description of the issue. You should also include as much relevant information as possible and a code sample that demonstrates the issue. The goal of a bug report is to make it easy for yourself - and others - to replicate the bug and develop a fix.
9 |
10 | Remember, bug reports are created in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the bug report will automatically see any activity or that others will jump to fix it. Creating a bug report serves to help yourself and others start on the path of fixing the problem.
11 |
12 |
13 | ## Coding Style
14 |
15 | Laravel follows the [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) coding standard and the [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) autoloading standard, so do we.
16 |
17 | ## Security Vulnerabilities
18 |
19 | If you discover a security vulnerability within the package, please send an e-mail to [@nullthoughts](https://github.com/nullthoughts) via [jani@nullincorporated.com](mailto:jani@nullincorporated.com). All security vulnerabilities will be promptly addressed.
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Jani Gyllenberg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nullthoughts/laravel-data-sync",
3 | "description": "Laravel utility to keep records synced between environments through source control",
4 | "license": "MIT",
5 | "authors": [
6 | {
7 | "name": "nullthoughts",
8 | "email": "jani@nullincorporated.com"
9 | }
10 | ],
11 | "scripts": {
12 | "lint": "pint --test",
13 | "lint:fix": "pint --repair",
14 | "psalm": "psalm",
15 | "psalm:fix": "psalm --alter --issues=MissingReturnType,MissingParamType",
16 | "test": "phpunit --coverage-text",
17 | "test:ci": "phpunit --teamcity"
18 | },
19 | "require": {
20 | "ext-json": "*",
21 | "php": "^8.0"
22 | },
23 | "require-dev": {
24 | "orchestra/testbench": "^7|^8",
25 | "rector/rector": "^1",
26 | "ergebnis/composer-normalize": "^2",
27 | "vimeo/psalm": "^5",
28 | "psalm/plugin-laravel": "^2",
29 | "laravel/pint": "^1"
30 | },
31 | "autoload": {
32 | "psr-4": {
33 | "nullthoughts\\LaravelDataSync\\": "src/"
34 | }
35 | },
36 | "autoload-dev": {
37 | "psr-4": {
38 | "nullthoughts\\LaravelDataSync\\Tests\\": "tests/",
39 | "nullthoughts\\LaravelDataSync\\Tests\\Fakes\\": "tests/fakes/"
40 | }
41 | },
42 | "extra": {
43 | "laravel": {
44 | "providers": [
45 | "nullthoughts\\LaravelDataSync\\DataSyncBaseServiceProvider"
46 | ]
47 | }
48 | },
49 | "config": {
50 | "allow-plugins": {
51 | "ergebnis/composer-normalize": true
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/config/data-sync.php:
--------------------------------------------------------------------------------
1 | base_path('sync'),
5 | 'order' => [
6 | //
7 | ],
8 | ];
9 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 | ./src
9 |
10 |
11 |
12 |
13 | ./tests/Unit
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/pint.json:
--------------------------------------------------------------------------------
1 | {
2 | "preset": "laravel"
3 | }
4 |
--------------------------------------------------------------------------------
/psalm.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | # Laravel Data Sync
8 |
9 | Laravel utility to keep records synchronized between environments through source control
10 |
11 | ## [V3.1 branch](https://github.com/nullthoughts/laravel-data-sync/tree/v3.1) Notes (Work in progress)
12 | - Adds support for Laravel 8+ models directory in `config/data-sync.php`:
13 | ```
14 | 'namespace' => '\\App\\Models\\',
15 | ```
16 |
17 | - Adds ability to export existing Models to data sync files:
18 | ```
19 | php artisan data:export User --criteria=name --criteria=email --except=id
20 | ```
21 |
22 | which generates
23 |
24 | ```json
25 | [
26 | {
27 | "_name": "Cameron Frye",
28 | "properties->title": "Best Friend",
29 | "phone_numbers->mobile": "555-555-5556",
30 | "_email": "noreply@buellerandco.com",
31 | }
32 | ]
33 | ```
34 |
35 | - Further work is required to support remote disks. For testing & development of new V3.1 features, use `composer require nullthoughts/laravel-data-sync:v3.1.x-dev`
36 |
37 | ---
38 |
39 | ## Installation
40 | You can install this package via composer:
41 | ```bash
42 | composer require nullthoughts/laravel-data-sync
43 | ```
44 |
45 | Or add this line in your `composer.json`, inside of the `require` section:
46 |
47 | ``` json
48 | {
49 | "require": {
50 | "nullthoughts/laravel-data-sync": "^1.0",
51 | }
52 | }
53 | ```
54 | then run ` composer install `
55 |
56 | ## Usage
57 | - 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)
58 | - Create a JSON file for each model, using the model name as the filename. Example: Product.json would update the Product model
59 | - Use nested arrays in place of hardcoded IDs for relationships
60 | - Run `php artisan data:sync` (or `php artisan data:sync --model={model}` with the model flag to specify a model)
61 |
62 | ### Optional
63 | If using Laravel Forge, you can have the data sync run automatically on deploy. Edit your deploy script in Site -> App to include:
64 | ```
65 | if [ -f artisan ]
66 | then
67 | php artisan migrate --force
68 | php artisan data:sync
69 | fi
70 | ```
71 |
72 | ## Notes
73 | - use studly case for model name relationships as JSON keys (example: 'option_group' => 'OptionGroup'). This is important for case sensitive file systems.
74 | - empty values are skipped
75 | - the criteria/attributes for updateOrCreate are identified with a leading underscore
76 | - nested values represent relationships and are returned using where($key, $value)->first()->id
77 | - order of import can be set in _config/data-sync.php_ with an array:
78 | ```
79 | return [
80 | 'path' => base_path('sync'),
81 | 'order' => [
82 | 'Role',
83 | 'Supervisor',
84 | ]
85 | ];
86 | ```
87 |
88 | ## Examples
89 | ### User.json:
90 | ```json
91 | [
92 | {
93 | "name": "Ferris Bueller",
94 | "properties->title": "Leisure Consultant",
95 | "phone_numbers->mobile": "555-555-5555",
96 | "phone_numbers->office": "",
97 | "_email": "ferris@buellerandco.com",
98 | "department": {
99 | "name": "Management",
100 | "location": {
101 | "name": "Chicago"
102 | }
103 | }
104 | }
105 | ]
106 | ```
107 |
108 | translates to...
109 |
110 | ```php
111 | User::updateOrCreate([
112 | 'email' => 'ferris@buellerandco.com',
113 | ],[
114 | 'name' => 'Ferris Bueller',
115 | 'properties->title' => 'Leisure Consultant',
116 | 'phone_numbers->mobile' => '555-555-5555',
117 | 'department_id' => Department::where('name', 'Management')
118 | ->where('location_id', Location::where('name', 'Chicago')->first()->id)
119 | ->first()
120 | ->id,
121 | ]);
122 | ```
123 |
124 | ### Role.json:
125 | ```json
126 | [
127 | {
128 | "_slug": "update-student-records"
129 | },
130 | {
131 | "_slug": "borrow-ferrari"
132 | },
133 | {
134 | "_slug": "destroy-ferrari"
135 | }
136 | ]
137 | ```
138 |
139 | translates to...
140 |
141 | ```php
142 | Role::updateOrCreate(['slug' => 'update-student-records']);
143 |
144 | Role::updateOrCreate(['slug' => 'borrow-ferrari']);
145 |
146 | Role::updateOrCreate(['slug' => 'destroy-ferrari']);
147 | ```
148 |
149 | ### RoleUser.json (pivot table with model):
150 | ```json
151 | [
152 | {
153 | "_user": {
154 | "email": "ferris@buellerandco.com"
155 | },
156 | "_role": {
157 | "slug": "update-student-records"
158 | }
159 | },
160 | {
161 | "_user": {
162 | "email": "ferris@buellerandco.com"
163 | },
164 | "_role": {
165 | "slug": "borrow-ferrari"
166 | }
167 | },
168 | {
169 | "_user": {
170 | "email": "ferris@buellerandco.com"
171 | },
172 | "_role": {
173 | "slug": "destroy-ferrari"
174 | }
175 | }
176 | ]
177 | ```
178 |
179 | translates to...
180 |
181 | ```php
182 | RoleUser::updateOrCreate([
183 | 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id,
184 | 'role_id' => Role::where('slug', 'update-student-records')->first()->id,
185 | ]);
186 |
187 | RoleUser::updateOrCreate([
188 | 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id,
189 | 'role_id' => Role::where('slug', 'borrow-ferrari')->first()->id,
190 | ]);
191 |
192 | RoleUser::updateOrCreate([
193 | 'user_id' => User::where('email', 'ferris@buellerandco.com')->first()->id,
194 | 'role_id' => Role::where('slug', 'destroy-ferrari')->first()->id,
195 | ]);
196 |
197 | ```
198 |
--------------------------------------------------------------------------------
/rector.php:
--------------------------------------------------------------------------------
1 | withPaths([
9 | __DIR__.'/config',
10 | __DIR__.'/src',
11 | __DIR__.'/tests',
12 | ])
13 | ->withPhpSets()
14 | ->withPreparedSets(deadCode: true, codeQuality: true)
15 | ->withImportNames(removeUnusedImports: true)
16 | ->withTypeCoverageLevel(0);
17 |
--------------------------------------------------------------------------------
/scratch.md:
--------------------------------------------------------------------------------
1 | # Dev
2 |
3 | - Check if null return from json decode (invalid JSON)
4 | - Check if updateOrCreate returns anything
5 |
6 | # Usage
7 |
8 | - Must have the columsn you are updating as fillable
9 | - In config, specify disk to use (string)
10 | - Document Forge example
11 |
12 | # Troubleshooting
13 |
14 | - Make sure columns are fillable in model
15 |
--------------------------------------------------------------------------------
/src/Console/Commands/Sync.php:
--------------------------------------------------------------------------------
1 | option('path');
18 | $model = $this->option('model');
19 |
20 | $this->info('Updating Models with sync data files');
21 |
22 | (new Updater($path, $model))->run();
23 |
24 | $this->comment('Data sync completed');
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/DataSyncBaseServiceProvider.php:
--------------------------------------------------------------------------------
1 | app->runningInConsole()) {
14 | $this->registerPublishing();
15 | }
16 | }
17 |
18 | public function register()
19 | {
20 | $this->commands([
21 | Sync::class,
22 | ]);
23 | }
24 |
25 | protected function registerPublishing(): void
26 | {
27 | $this->publishes([
28 | __DIR__.'/../config/data-sync.php' => config_path('data-sync.php'),
29 | ], 'data-sync-config');
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Exceptions/ErrorUpdatingModelException.php:
--------------------------------------------------------------------------------
1 | message = "Error updating the {$message} model.";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Exceptions/FileDirectoryNotFoundException.php:
--------------------------------------------------------------------------------
1 | message = "No records or invalid JSON for {$message} model.";
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/Updater.php:
--------------------------------------------------------------------------------
1 | remote = $remote;
40 | $this->disk = $disk;
41 |
42 | $this->directory = $this->getDirectory($path);
43 | $this->files = $this->getFiles($this->directory, $model);
44 | }
45 |
46 | /**
47 | * Override the default namespace for the class.
48 | *
49 | * @psalm-api
50 | */
51 | public function setNamespace(string $namespace): void
52 | {
53 | $this->baseNamespace = $namespace;
54 | }
55 |
56 | /**
57 | * Override the default directory for the class.
58 | *
59 | * @psalm-api
60 | */
61 | public function setDirectory(string $directory): void
62 | {
63 | $this->directory = $directory;
64 | }
65 |
66 | /**
67 | * Execute syncModel for each file.
68 | */
69 | public function run()
70 | {
71 | $files = $this->sortModels($this->files);
72 |
73 | return $files->map(function ($file) {
74 | try {
75 | return $this->syncModel($file);
76 | } catch (\ErrorException) {
77 | $model = pathinfo($file, PATHINFO_FILENAME);
78 |
79 | throw new ErrorUpdatingModelException(ucwords($model));
80 | }
81 | });
82 | }
83 |
84 | /**
85 | * Parse each record for criteria/values and update/create model.
86 | *
87 | *
88 | * @return \Illuminate\Support\Collection
89 | *
90 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
91 | * @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
92 | */
93 | protected function syncModel(string $file)
94 | {
95 | $model = $this->getModel($file);
96 | $records = $this->getRecords($file);
97 |
98 | $records->each(function ($record) use ($model) {
99 | $criteria = $this->resolveObjects(
100 | $this->getCriteria($record)
101 | );
102 |
103 | $values = $this->resolveObjects(
104 | $this->getValues($record)
105 | );
106 |
107 | $model::updateOrCreate($criteria, $values);
108 | });
109 |
110 | return $records;
111 | }
112 |
113 | /**
114 | * Get directory path for sync files.
115 | *
116 | *
117 | * @return string
118 | *
119 | * @throws \nullthoughts\LaravelDataSync\Exceptions\FileDirectoryNotFoundException
120 | */
121 | protected function getDirectory(?string $path)
122 | {
123 | $directory = $path ?? config('data-sync.path', base_path('sync'));
124 |
125 | if ($this->directoryMissingLocally($directory) || $this->directoryMissingRemotely($directory)) {
126 | throw new FileDirectoryNotFoundException;
127 | }
128 |
129 | $this->directory = $directory;
130 |
131 | return $this->directory;
132 | }
133 |
134 | /**
135 | * Get list of files in directory.
136 | *
137 | * @param string|null $model
138 | * @return \Illuminate\Support\Collection
139 | */
140 | protected function getFiles(string $directory, $model = null)
141 | {
142 | if ($model) {
143 | return Collection::wrap($directory.'/'.$model.'.json');
144 | }
145 |
146 | $files = ($this->remote) ? Storage::disk($this->disk)->files($directory) : File::files($directory);
147 |
148 | return collect($files)
149 | ->filter(function ($file) {
150 | return pathinfo($file, PATHINFO_EXTENSION) == 'json';
151 | })->map(function ($path) {
152 |
153 | if (is_string($path)) {
154 | return $path;
155 | }
156 |
157 | return $path->getPathname();
158 | });
159 | }
160 |
161 | /**
162 | * Sort Models by pre-configured order.
163 | *
164 | *
165 | * @return \Illuminate\Support\Collection
166 | */
167 | protected function sortModels(\Illuminate\Support\Collection $files)
168 | {
169 | if (empty(config('data-sync.order'))) {
170 | return $files;
171 | }
172 |
173 | return $files->sortBy(function ($file) use ($files) {
174 | $filename = pathinfo($file, PATHINFO_FILENAME);
175 |
176 | $order = array_search(
177 | Str::studly($filename),
178 | config('data-sync.order')
179 | );
180 |
181 | return $order !== false ? $order : (count($files) + 1);
182 | });
183 | }
184 |
185 | /**
186 | * Filter record criteria.
187 | *
188 | *
189 | * @return \Illuminate\Support\Collection
190 | *
191 | * @throws \nullthoughts\LaravelDataSync\Exceptions\NoCriteriaException
192 | */
193 | protected function getCriteria(stdClass $record)
194 | {
195 | $criteria = collect($record)->filter(function ($value, $key) {
196 | return $this->isCriteria($key);
197 | });
198 |
199 | if ($criteria->count() == 0) {
200 | throw new NoCriteriaException;
201 | }
202 |
203 | return $criteria->mapWithKeys(function ($value, $key) {
204 | return [substr($key, 1) => $value];
205 | });
206 | }
207 |
208 | /**
209 | * Filter record values.
210 | *
211 | *
212 | * @return \Illuminate\Support\Collection
213 | */
214 | protected function getValues(stdClass $record)
215 | {
216 | return collect($record)->reject(function ($value, $key) {
217 | if ($this->isCriteria($key)) {
218 | return true;
219 | }
220 |
221 | if (empty($value)) {
222 | return true;
223 | }
224 |
225 | return false;
226 | });
227 | }
228 |
229 | /**
230 | * Returns model name for file.
231 | *
232 | *
233 | * @return string
234 | */
235 | protected function getModel(string $name)
236 | {
237 | return $this->baseNamespace.Str::studly(pathinfo($name, PATHINFO_FILENAME));
238 | }
239 |
240 | /**
241 | * Parses JSON from file and returns collection.
242 | *
243 | *
244 | * @return \Illuminate\Support\Collection
245 | *
246 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
247 | * @throws \nullthoughts\LaravelDataSync\Exceptions\NoRecordsInvalidJSONException
248 | */
249 | protected function getRecords(string $file)
250 | {
251 | $fetchedFile = ($this->remote) ? Storage::disk($this->disk)->get($file) : File::get($file);
252 |
253 | $records = collect(json_decode($fetchedFile));
254 |
255 | if ($records->isEmpty()) {
256 | throw new NoRecordsInvalidJSONException($file);
257 | }
258 |
259 | return $records;
260 | }
261 |
262 | /**
263 | * Check if column is criteria for a condition match.
264 | *
265 | * @param string $key
266 | * @return bool
267 | */
268 | protected function isCriteria($key)
269 | {
270 | return substr($key, 0, 1) == '_';
271 | }
272 |
273 | /**
274 | * Return ID for nested key-value pairs.
275 | *
276 | *
277 | * @return array
278 | */
279 | protected function resolveId(string $key, stdClass $values)
280 | {
281 | $model = $this->getModel($key);
282 |
283 | $values = collect($values)->mapWithKeys(function ($value, $column) {
284 | if (is_object($value)) {
285 | return $this->resolveId($column, $value);
286 | }
287 |
288 | return [$column => $value];
289 | })->toArray();
290 |
291 | return [$key.'_id' => $model::where($values)->first()->id];
292 | }
293 |
294 | /**
295 | * Detect nested objects and resolve them.
296 | *
297 | *
298 | * @return array
299 | */
300 | protected function resolveObjects(Collection $record)
301 | {
302 | return $record->mapWithKeys(function ($value, $key) {
303 | if (is_object($value)) {
304 | return $this->resolveId($key, $value);
305 | }
306 |
307 | return [$key => $value];
308 | })->toArray();
309 | }
310 |
311 | /**
312 | * @param \Illuminate\Config\Repository $directory
313 | * @return bool
314 | */
315 | protected function directoryMissingLocally($directory)
316 | {
317 | return ! $this->remote && ! file_exists($directory);
318 | }
319 |
320 | /**
321 | * @param \Illuminate\Config\Repository $directory
322 | * @return bool
323 | */
324 | protected function directoryMissingRemotely($directory)
325 | {
326 | return $this->remote && ! Storage::disk($this->disk)->exists($directory);
327 | }
328 | }
329 |
--------------------------------------------------------------------------------
/tests/Roles.php:
--------------------------------------------------------------------------------
1 | belongsTo(Supervisor::class);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tests/Supervisor.php:
--------------------------------------------------------------------------------
1 | hasMany(Roles::class);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tests/TestCase.php:
--------------------------------------------------------------------------------
1 | set('database.default', 'testdb');
16 | $app['config']->set('database.connections.testdb', [
17 | 'driver' => 'sqlite',
18 | 'database' => ':memory:',
19 | ]);
20 |
21 | $this->testDataPath = __DIR__.'/test-data';
22 | }
23 |
24 | protected function setUp(): void
25 | {
26 | parent::setUp();
27 |
28 | Schema::create('supervisors', static function (Blueprint $table): void {
29 | $table->increments('id');
30 | $table->string('name');
31 | });
32 |
33 | Schema::create('roles', static function (Blueprint $table): void {
34 | $table->increments('id');
35 | $table->string('slug');
36 | $table->unsignedInteger('supervisor_id')->nullable();
37 | $table->string('category')->nullable();
38 | });
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/tests/Unit/UpdaterRemoteTest.php:
--------------------------------------------------------------------------------
1 | put('test-data/roles.json', File::get(__DIR__.'/../test-data/roles.json'));
22 | foreach (File::directories(__DIR__.'/../test-data/') as $directory) {
23 | $files = File::files($directory);
24 |
25 | foreach ($files as $file) {
26 | Storage::disk('s3')->put('test-data/'.basename($directory).'/'.$file->getRelativePathname(), File::get($file->getPathname()));
27 | }
28 | }
29 | }
30 |
31 | /** @test */
32 | public function it_adds_roles_to_the_database_in_remote()
33 | {
34 | $updater = new UpdaterFake('test-data', 'roles', true, 's3');
35 |
36 | $updater->run();
37 |
38 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
39 | $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
40 | $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
41 | }
42 |
43 | /** @test */
44 | public function it_can_default_to_configuration_in_remote()
45 | {
46 | config()->set('data-sync.path', 'test-data');
47 |
48 | $updater = new UpdaterFake(null, null, true, 's3');
49 |
50 | $updater->run();
51 |
52 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
53 | $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
54 | $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
55 | }
56 |
57 | /** @test */
58 | public function it_can_update_an_existing_record_in_remote()
59 | {
60 | config()->set('data-sync.path', 'test-data');
61 | (new UpdaterFake(null, null, true, 's3'))->run();
62 |
63 | config()->set('data-sync.path', 'test-data/valid');
64 | (new UpdaterFake(null, null, true, 's3'))->run();
65 |
66 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
67 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
68 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
69 | }
70 |
71 | /** @test */
72 | public function it_can_update_the_relationship_in_remote()
73 | {
74 | $supervisor = Supervisor::create([
75 | 'name' => 'CEO',
76 | ]);
77 |
78 | config()->set('data-sync.path', 'test-data/relationship');
79 | (new UpdaterFake(null, null, true, 's3'))->run();
80 |
81 | $this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
82 | $this->assertTrue($supervisor->is(Roles::first()->supervisor));
83 | }
84 |
85 | /**
86 | * @test
87 | *
88 | * @group current
89 | */
90 | public function exception_is_thrown_if_the_directory_does_not_exists(): void
91 | {
92 | try {
93 | new UpdaterFake(null, null, true, 's3');
94 |
95 | $this->fail('exception was thrown');
96 | } catch (Exception $e) {
97 | $this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
98 | }
99 | }
100 |
101 | /** @test */
102 | public function invalid_json_throws_an_exception_in_remote(): void
103 | {
104 | try {
105 | $updater = new UpdaterFake('test-data/invalid-json', null, true, 's3');
106 | $updater->run();
107 |
108 | $this->fail('exception was thrown');
109 | } catch (Exception $e) {
110 | $this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
111 | }
112 | }
113 |
114 | /** @test */
115 | public function the_json_must_contain_a_key_with_an_underscore_in_remote(): void
116 | {
117 | try {
118 | $updater = new UpdaterFake('test-data/no-criteria', null, true, 's3');
119 | $updater->run();
120 |
121 | $this->fail('exception was thrown');
122 | } catch (Exception $e) {
123 | $this->assertEquals('No criteria/attributes detected', $e->getMessage());
124 | }
125 | }
126 |
127 | /** @test */
128 | public function order_of_imports_can_be_defined_in_config_in_remote(): void
129 | {
130 | config()->set('data-sync.order', [
131 | 'Supervisor',
132 | 'Roles',
133 | ]);
134 |
135 | $updater = new UpdaterFake('test-data/ordered', null, true, 's3');
136 | $updater->run();
137 |
138 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
139 | $this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
140 | }
141 |
142 | /** @test */
143 | public function exception_is_thrown_if_imports_are_in_incorrect_order_in_remote(): void
144 | {
145 | config()->set('data-sync.order', [
146 | 'Roles',
147 | 'Supervisor',
148 | ]);
149 |
150 | $this->expectException(ErrorUpdatingModelException::class);
151 |
152 | $updater = new UpdaterFake('test-data/ordered', null, true, 's3');
153 | $updater->run();
154 | }
155 |
156 | /** @test */
157 | public function it_ignores_non_json_files_in_remote(): void
158 | {
159 | $updater = new UpdaterFake('test-data/not-json', null, true, 's3');
160 | $updater->run();
161 |
162 | $this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/tests/Unit/UpdaterTest.php:
--------------------------------------------------------------------------------
1 | testDataPath, 'roles');
18 |
19 | $updater->run();
20 |
21 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
22 | $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
23 | $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
24 | }
25 |
26 | /** @test */
27 | public function it_can_default_to_configuration()
28 | {
29 | config()->set('data-sync.path', $this->testDataPath);
30 |
31 | $updater = new UpdaterFake;
32 |
33 | $updater->run();
34 |
35 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
36 | $this->assertDatabaseHas('roles', ['slug' => 'borrow-ferrari']);
37 | $this->assertDatabaseHas('roles', ['slug' => 'destroy-ferrari']);
38 | }
39 |
40 | /** @test */
41 | public function it_can_update_an_existing_record()
42 | {
43 | config()->set('data-sync.path', $this->testDataPath);
44 | (new UpdaterFake)->run();
45 |
46 | config()->set('data-sync.path', $this->testDataPath.'/valid');
47 | (new UpdaterFake)->run();
48 |
49 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
50 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
51 | $this->assertDatabaseHas('roles', ['category' => 'changed']);
52 | }
53 |
54 | /** @test */
55 | public function it_can_update_the_relationship(): void
56 | {
57 | $supervisor = Supervisor::create([
58 | 'name' => 'CEO',
59 | ]);
60 |
61 | config()->set('data-sync.path', $this->testDataPath.'/relationship', 'roles');
62 | (new UpdaterFake)->run();
63 |
64 | $this->assertEquals($supervisor->id, Roles::first()->supervisor_id);
65 | $this->assertTrue($supervisor->is(Roles::first()->supervisor));
66 | }
67 |
68 | /** @test */
69 | public function exception_is_thrown_if_the_directory_does_not_exists(): void
70 | {
71 | try {
72 | new UpdaterFake;
73 |
74 | $this->fail('exception was thrown');
75 | } catch (Exception $e) {
76 | $this->assertEquals('Specified sync file directory does not exist', $e->getMessage());
77 | }
78 | }
79 |
80 | /** @test */
81 | public function invalid_json_throws_an_exception(): void
82 | {
83 | try {
84 | $updater = new UpdaterFake($this->testDataPath.'/invalid-json');
85 | $updater->run();
86 |
87 | $this->fail('exception was thrown');
88 | } catch (Exception $e) {
89 | $this->assertStringContainsString('No records or invalid JSON for', $e->getMessage());
90 | }
91 | }
92 |
93 | /** @test */
94 | public function the_json_must_contain_a_key_with_an_underscore(): void
95 | {
96 | try {
97 | $updater = new UpdaterFake($this->testDataPath.'/no-criteria');
98 | $updater->run();
99 |
100 | $this->fail('exception was thrown');
101 | } catch (Exception $e) {
102 | $this->assertEquals('No criteria/attributes detected', $e->getMessage());
103 | }
104 | }
105 |
106 | /** @test */
107 | public function order_of_imports_can_be_defined_in_config(): void
108 | {
109 | config()->set('data-sync.order', [
110 | 'Supervisor',
111 | 'Roles',
112 | ]);
113 |
114 | $updater = new UpdaterFake($this->testDataPath.'/ordered');
115 | $updater->run();
116 |
117 | $this->assertDatabaseHas('roles', ['slug' => 'update-student-records']);
118 | $this->assertDatabaseHas('supervisors', ['name' => 'CEO']);
119 | }
120 |
121 | public function exception_is_thrown_if_imports_are_in_incorrect_order(): void
122 | {
123 | config()->set('data-sync.order', [
124 | 'Roles',
125 | 'Supervisor',
126 | ]);
127 |
128 | $this->expectException(ErrorUpdatingModelException::class);
129 |
130 | $updater = new UpdaterFake($this->testDataPath.'/ordered');
131 | $updater->run();
132 | }
133 |
134 | /** @test */
135 | public function it_ignores_non_json_files(): void
136 | {
137 | $updater = new UpdaterFake($this->testDataPath.'/not-json');
138 | $updater->run();
139 |
140 | $this->assertDatabaseMissing('roles', ['slug' => 'update-student-records']);
141 | }
142 | }
143 |
--------------------------------------------------------------------------------
/tests/fakes/UpdaterFake.php:
--------------------------------------------------------------------------------
1 |