wmde / Diff

䷂ Library for diffing, patching and representing differences between objects
BSD 3-Clause "New" or "Revised" License
200 stars 15 forks source link

Diff generation fails with call to undefined method getNewValue() #119

Closed innobrig closed 5 years ago

innobrig commented 5 years ago

I'm testing your tool and it seems that something is not quite right (I've tried both with v2 and v3 of diff/diff):

> PHP Fatal error:  Uncaught Error: Call to undefined method Diff\DiffOp\DiffOpRemove::getNewValue() in /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php:17
> Stack trace:
> #0 [internal function]: DBDiff\SQLGen\DiffToSQL\UpdateDataSQL->DBDiff\SQLGen\DiffToSQL\{closure}(Object(Diff\DiffOp\DiffOpRemove), 'new_test_field')
> #1 /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php(23): array_walk(Array, Object(Closure))
> #2 /var/www/html/DBDiff-master/src/SQLGen/MigrationGenerator.php(12): DBDiff\SQLGen\DiffToSQL\UpdateDataSQL->getUp()
> #3 /var/www/html/DBDiff-master/src/SQLGen/SQLGenerator.php(18): DBDiff\SQLGen\MigrationGenerator::generate(Array, 'getUp')
> #4 /var/www/html/DBDiff-master/src/DBDiff.php(33): DBDiff\SQLGen\SQLGenerator->getUp()
> #5 /var/www/html/DBDiff-master/dbdiff(8): DBDiff\DBDiff->run()
> #6 {main}
>   thrown in /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php on line 17
> 
> Fatal error: Uncaught Error: Call to undefined method Diff\DiffOp\DiffOpRemove::getNewValue() in /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php on line 17
> 
> Error: Call to undefined method Diff\DiffOp\DiffOpRemove::getNewValue() in /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php on line 17
> 
> Call Stack:
>     0.0001     388544   1. {main}() /var/www/html/DBDiff-master/dbdiff:0
>     0.0018     713696   2. DBDiff\DBDiff->run() /var/www/html/DBDiff-master/dbdiff:8
>     0.2702    2576176   3. DBDiff\SQLGen\SQLGenerator->getUp() /var/www/html/DBDiff-master/src/DBDiff.php:33
>     0.2703    2579216   4. DBDiff\SQLGen\MigrationGenerator::generate() /var/www/html/DBDiff-master/src/SQLGen/SQLGenerator.php:18
>     0.2705    2667792   5. DBDiff\SQLGen\DiffToSQL\UpdateDataSQL->getUp() /var/www/html/DBDiff-master/src/SQLGen/MigrationGenerator.php:12
>     0.2705    2668136   6. array_walk() /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php:23
>     0.2705    2668536   7. DBDiff\SQLGen\DiffToSQL\UpdateDataSQL->DBDiff\SQLGen\DiffToSQL\{closure}() /var/www/html/DBDiff-master/src/SQLGen/DiffToSQL/UpdateDataSQL.php:23

Thanks for any hints.

JeroenDeDauw commented 5 years ago

The issue is coming from thr DBDiff library, not from Diff itself.

These lines assume all diff operations in a diff have a getNewValue method, which is not the case: https://github.com/DBDiff/DBDiff/blob/master/src/SQLGen/DiffToSQL/UpdateDataSQL.php#L17-L18

Looks like someone already tried to fix this but failed to do so properly: https://github.com/DBDiff/DBDiff/commit/b2d4bf04b38fd2db552b66f7363c65ced1fbce43#diff-155365a800ce69d0660404707ac171d1

I don't know what that DBDiff code is doing, but it looks like you can at least avoid the PHP error by using method_exists or checking the type of DiffOp.

innobrig commented 5 years ago

I'm an idiot, I meant to post this as a DBDiff issue but had too many windows open and posted it here; my apologies.

In case someone else is running into this, I posted patch to fix this: https://github.com/DBDiff/DBDiff/pull/90