Mysql usage with Laravel and table items records are disappearing what might be?

Asked

Viewed 82 times

1

I have a mysql 5.7.29 database called "base_default" with collation and encoding according to the image below and the api uses Laravel 5.6.

Configurações do encoding e collation

on this basis I have the following table structure: data sheet[id, protocol], sub_data sheet[id, ficha_id, approved[bool 0 or 1]], items[id, name, description, approved], all of them have softdelete trait, so they have created_at, updated_at and deleted_at

the point is that errors started to appear in Sentry where it said it was not finding the item id, I went to investigate and really the id sought was no longer in the database.

then I did an investigation into the code that searched these items to see if it found something and nothing, as it was not finding anything that deleted or altered the item I thought it might be that the front was getting the wrong id, then I investigated this part and ran the tests with a new item in which I really knew the id ja was watching the bank and saw that I was picking up the item correctly

then I did the following, to disappear a mysql database item via code in Laravel you need to delete, Destroy or some kind of detach according to the documentation https://laravel.com/docs/5.6/eloquent-relationships or https://laravel.com/docs/5.6/eloquent, I searched for all methods and the only one I found was delete by model, which I did tests and since the model has softdelete only the deleted_at column is filled

I did not find the problem in the code, so I did the following, now to brabo then I will break this server, I put a log for any query that the system does in the Appserviceprovider.php system inside the boot method()

         DB::listen(function ($query) {
            $bindings = '';
            foreach ($query->bindings as $key => $bind) {
                if ($bind instanceof \DateTime)
                    $bindings .= date_format($bind, 'Y-m-d H:i:s');
                else if (is_bool($bind))
                    $bindings .= $bind ? '1' : '0';
                else
                    $bindings .= $bind;

                if ($key < count($query->bindings) - 1)
                    $bindings .= ', ';
            }
            File::append(
                storage_path('/logs/query.log'),
                 '[' . Carbon::now()->format('Y-m-d H:i:s') . '] => ' . $query->sql . ' [' . $bindings . '];' . PHP_EOL
            );
        });

after logging I went to the bank and put 2 Trigger one that prevents the item from being deleted and one that prevents the item id from being changed

DROP TRIGGER IF EXISTS items_prevent_delete; delimite $$ CREATE TRIGGER items_prevent_delete BEFORE DELETE ON items FOR EACH ROW BEGIN IF old.id > 0 THEN SIGNAL SQLSTATE '45000' SET MESAGE_TEXT = 'No items may be deleted'; END IF; END$$ delimite ;

DROP TRIGGER IF EXISTS items_prevent_update_id; delimite $$ CREATE TRIGGER items_prevent_update_id BEFORE UPDATE ON items FOR EACH ROW BEGIN IF old.id <> new.id THEN SIGNAL SQLSTATE '45000' SET MESAGE_TEXT = 'Cannot change item id'; END IF; END$$ delimite ;

after that I waited I made several tests, optimized codes, reviewed, made new Migrations, updated the layout of the front and then went to check the items, and guess the blessed disappeared even with the 2 Riggers running, so I thought, well there must be some clue in Ntry or in the log of the querys.

well there was nothing in Sentry other than the same error that said it was not finding the items and in the querys there was no deletion or update command of the item, the maximum it had was select looking for the missing item

Now the questions, has anyone else gone through it? Is my table too big because it has 1 million items? Is there any other way to erase a record in the database other than the ones I mentioned? need some clue to be able to find the problem because I have no idea why the items in the items table are disappearing randomly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.