2017 © Pedro Peláez
 

library tablediff

Comparing DB tables data for Laravel

image

edujugon/tablediff

Comparing DB tables data for Laravel

  • Friday, March 16, 2018
  • by edujugon
  • Repository
  • 1 Watchers
  • 0 Stars
  • 168 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 0 % Grown

The README.md

TableDiff

Comparing DB tables data for Laravel., (*1)

Installation

type in console:, (*2)

composer require edujugon/tablediff

Register TableDiff service by adding it to the providers array., (*3)

'providers' => array(
        ...
        Edujugon\TableDiff\Providers\TableDiffServiceProvider::class
    )

Let's add the Alias facade, add it to the aliases array., (*4)

'aliases' => array(
        ...
        'TableDiff' => Edujugon\TableDiff\Facades\TableDiff::class,
    )

Usage samples

Instance the main TableDiff class:, (*5)

$diff = new \Edujugon\TableDiff\TableDiff();

Add tables to be compared, (*6)

$diff->tables('base_table', 'merge_table');

Set the pivots, (*7)

$diff->pivots('base_table_pivot', 'merge_table_pivot');

If the columns have the same name you could do it like follows:, (*8)

$diff->pivot('pivot_name');

Now, we can run the comparison and get the report, (*9)

$report = $diff->run()->withReport();

Of course, all those methods can be chained, (*10)

$report = $diff->tables('base_table','merge_table')
            ->pivot('pivot_name')
            ->run()
            ->withReport();

Notice if you don't use column method, it will look for all columns with same name in both tables., (*11)

Merging

The simplest way yo do a merge is like follows, (*12)

$diff->tables('base_table','merge_table')
    ->pivot('id')
    ->column('column_to_update')
    ->merge();

The above code snippet will update the column_to_update column values from base_table with the column_to_update column values of merge_table in matched ids., (*13)

Notice that merge method will update the matched records and also add those records that are new for base table., (*14)

Just merging matched records, (*15)

diff->tables('base_table','merge_table')
    ->pivot('id')
    ->column('column_to_update')
    ->mergeMatched();

Now, let's insert the new records., (*16)

diff->tables('base_table','merge_table')
    ->pivot('id')
    ->column('column_to_update')
    ->mergeUnMatched();

merge, mergeMatched and mergeUnMatched methods accept callbacks before doing the merge and just after each update., (*17)

Before callback is perfect for casting data like. It takes the data which will be added., (*18)

$ddiff->tables('base_table','merge_table')
            ->pivot('id')
            ->column('column_to_update')
            ->merge(null,function(&$data){
                // HERE your code
                $data->column_to_update = (float) $data->column_to_update;
            });

The first callback is call each time the db is updated. It takes the collection to be updated and the data with the new values, (*19)

$diff->tables('base_table','merge_table')
    ->pivot('id')
    ->column('column_to_update')
    ->merge(function($collection,$data){
        //HERE your code
    });

In case of mergeUnMatched, the first callback takes the new elements to be added and is called for each chunk (By default 10), (*20)

$diff->tables('base_table','merge_table')
    ->pivot('id')
    ->column('column_to_update')
    ->mergeUnMatched(function($list){
        //HERE your code
    });

Event

Once the merge is done the package fires an event call Edujugon\TableDiff\Events\MergeDone with some useful parameters, (*21)

Parameters:, (*22)

  • Merge type
  • Report
  • Extra payload

Extra payload can be anything you want to add the the event, (*23)

$diff->eventPayload(['user' => 'John Doe']);

Kepp in mind that eventPayload method have to be called before calling merge method., (*24)

That is perfect when need to do some actions after merging like store it in db, send notification, etc.., (*25)

The Versions

16/03 2018

dev-master

9999999-dev

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

03/07 2017

dev-dev

dev-dev

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

30/06 2017

1.0.9

1.0.9.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

20/06 2017

1.0.8

1.0.8.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

07/06 2017

1.0.7

1.0.7.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

07/06 2017

1.0.6

1.0.6.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

06/06 2017

1.0.5

1.0.5.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

05/06 2017

1.0.4

1.0.4.0

Comparing DB tables data for Laravel

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

05/06 2017

1.0.3

1.0.3.0

Compare the values of two tables and either returns the differences or update the data with the new values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

05/06 2017

1.0.2

1.0.2.0

Compare the values of two tables and either returns the differences or update the data with the new values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

05/06 2017

1.0.1

1.0.1.0

Compare the values of two tables and either returns the differences or update the data with the new values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff

05/06 2017

1.0.0

1.0.0.0

Compare the values of two tables and either returns the differences or update the data with the new values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Eduardo Marcos

laravel package differences tablediff