All Projects → nullthoughts → laravel-data-sync

nullthoughts / laravel-data-sync

Licence: MIT License
Laravel utility to keep records synced between enviroments through source control

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-data-sync

mytosis
🔀 A peer-to-peer data sync framework
Stars: ✭ 19 (-42.42%)
Mutual labels:  data-sync
maxwell-sink
consume maxwell generated message from kafka,export it to another mysql.
Stars: ✭ 16 (-51.52%)
Mutual labels:  data-sync
mydataharbor
🇨🇳 MyDataHarbor是一个致力于解决任意数据源到任意数据源的分布式、高扩展性、高性能、事务级的数据同步中间件。帮助用户可靠、快速、稳定的对海量数据进行准实时增量同步或者定时全量同步,主要定位是为实时交易系统服务,亦可用于大数据的数据同步(ETL领域)。
Stars: ✭ 28 (-15.15%)
Mutual labels:  data-sync
DataBridge.NET
Configurable data bridge for permanent ETL jobs
Stars: ✭ 16 (-51.52%)
Mutual labels:  data-sync
Flinkx
Based on Apache Flink. support data synchronization/integration and streaming SQL computation.
Stars: ✭ 2,651 (+7933.33%)
Mutual labels:  data-sync
bigbrother-specs
Research and specification for Big Brother protocol
Stars: ✭ 13 (-60.61%)
Mutual labels:  data-sync

Total Downloads Latest Stable Version Travis CI Build Status: Master

Laravel Data Sync

Laravel utility to keep records synchronized between environments through source control

Installation

You can install this package via composer:

composer require nullthoughts/laravel-data-sync

Or add this line in your composer.json, inside of the require section:

{
    "require": {
        "nullthoughts/laravel-data-sync": "^1.0",
    }
}

then run composer install

Usage

  • 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)

Optional

If using Laravel Forge, you can have the data sync run automatically on deploy. Edit your deploy script in Site -> App to include:

if [ -f artisan ]
then
    php artisan migrate --force
    php artisan data:sync
fi

Notes

  • use studly case for model name relationships as JSON keys (example: 'option_group' => 'OptionGroup'). This is important for case sensitive file systems.
  • empty values are skipped
  • the criteria/attributes for updateOrCreate are identified with a leading underscore
  • nested values represent relationships and are returned using where($key, $value)->first()->id
  • order of import can be set in config/data-sync.php with an array:
return [
    'path' => base_path('sync'),
    'order' => [
        'Role',
        'Supervisor',
    ]
];

Examples

User.json:

[
    {
        "name": "Ferris Bueller",
        "properties->title": "Leisure Consultant",
        "phone_numbers->mobile": "555-555-5555",
        "phone_numbers->office": "",
        "_email": "[email protected]",
        "department": {
            "name": "Management",
            "location": {
                "name": "Chicago"
            }
        }
    }
]

translates to...

User::updateOrCreate([
    'email' => '[email protected]',
],[
    'name' => 'Ferris Bueller',
    'properties->title' => 'Leisure Consultant',
    'phone_numbers->mobile' => '555-555-5555',
    'department_id' => Department::where('name', 'Management')
                        ->where('location_id', Location::where('name', 'Chicago')->first()->id)
                        ->first()
                        ->id,
]);

Role.json:

[
    {
        "_slug": "update-student-records"
    },
    {
        "_slug": "borrow-ferrari"
    },
    {
        "_slug": "destroy-ferrari"
    }
]

translates to...

    Role::updateOrCreate(['slug' => 'update-student-records']);

    Role::updateOrCreate(['slug' => 'borrow-ferrari']);

    Role::updateOrCreate(['slug' => 'destroy-ferrari']);

RoleUser.json (pivot table with model):

[
    {
        "_user": {
            "email": "[email protected]"
        },
        "_role": {
            "slug": "update-student-records"
        }
    },
    {
        "_user": {
            "email": "[email protected]"
        },
        "_role": {
            "slug": "borrow-ferrari"
        }
    },
    {
        "_user": {
            "email": "[email protected]"
        },
        "_role": {
            "slug": "destroy-ferrari"
        }
    }
]

translates to...

    RoleUser::updateOrCreate([
        'user_id' => User::where('email', '[email protected]')->first()->id,
        'role_id' => Role::where('slug', 'update-student-records')->first()->id,
    ]);

    RoleUser::updateOrCreate([
        'user_id' => User::where('email', '[email protected]')->first()->id,
        'role_id' => Role::where('slug', 'borrow-ferrari')->first()->id,
    ]);

    RoleUser::updateOrCreate([
        'user_id' => User::where('email', '[email protected]')->first()->id,
        'role_id' => Role::where('slug', 'destroy-ferrari')->first()->id,
    ]);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].