laravel-er-diagram-generator icon indicating copy to clipboard operation
laravel-er-diagram-generator copied to clipboard

Running the command cleared my Algolia Indices

Open jamesmills opened this issue 4 years ago • 2 comments

Hi,

Yesterday I tried to run the command. I got the following error

[2019-09-10 14:53:40] local.ERROR: An error occurred while creating the graph; GraphViz returned: sh: dot: command not found {"exception":"[object] (phpDocumentor\\GraphViz\\Exception(code: 0): An error occurred while creating the graph; GraphViz returned: sh: dot: command not found at /Users/jamesmills/Projects/flights/vendor/phpdocumentor/graphviz/src/phpDocumentor/GraphViz/Graph.php:375)

I didn't think much of it and I just decided to look at it again another time. Then a few hours later I was alerted to the fact that our search was not working on our website. I looked at the Algolia logs and it looks like my application send a 'clear indices' command at the same time I ran the generate:erd command. Granted, I should have my admin Algolia key commented out in development to prevent anything odd happening like this but I thought it was odd that this would happen.

I can only think that when the package loads up and looks at the Model the interaction that it had made it look like it was clearing, deleting or that there were no records for the Model and then Scout/Algolia cleared my live index.

Just wanted to make you aware of this. I have not had the time to dig into it any further.

James

jamesmills avatar Sep 11 '19 05:09 jamesmills

I've been seeing this issue as well. I tracked it down to the RelationFinder class. The problem is with how relations are collected. Every 0-parameter public method available on the model is invoked in the second line of getRelationshipFromMethodAndModel.

$return = $method->invoke(app($model));

For models that use the Searchable trait, this includes a static public method: removeAllFromSearch.

Thus, every time the code tries to find relationships for a model that uses Searchable, all of the entries are removed from the search index.

spleenboy avatar Jul 21 '21 16:07 spleenboy

One proposal for a solution to this issue would be to reject static methods before invocation to test for a relationship. That should eliminate most of the potentially destructive methods on models.

        $methods = Collection::make($class->getMethods(ReflectionMethod::IS_PUBLIC))
            ->merge($traitMethods)
            ->reject(function (ReflectionMethod $method) use ($model) {
                return $method->class !== $model || $method->getNumberOfParameters() > 0 || $method->isStatic();
            });

spleenboy avatar Jul 21 '21 16:07 spleenboy