Announcement

#1 2020-11-12 03:53:18

memecode
Member
2020-11-12
7

Sql Errors

Hi,

I'm getting these SQL errors trying to add a large DB of images via sync:


Warning:  [mysql error 1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'piccies/Album/104-0437_IMG.JPG',NULL,'9312','1')
  , ('23910','104-0438_IMG.J...' at line 23910

INSERT  INTO `piwigo_images`
  (`id`,`file`,`name`,`date_available`,`path`,`representative_ext`,`storage_category_id`,`added_by`)
  VALUES('2','IMG_2650.JPG','IMG 2650','2020-11-12 13:26:31','./galleries/Photos/2009/2009_08_03/IMG_2650.JPG',NULL,'1115','1')
  , ('3','IMG_2651.JPG','IMG 2651','2020-11-12 13:26:31','./galleries/Photos/2009/2009_08_03/IMG_2651.JPG',NULL,'1115','1')
  , ('4','2009_08_04.zip','2009 08 04','2020-11-12 13:26:31','./galleries/Photos/2009/2009_08_04.zip',NULL,'1114','1')
  , ('5','2009_08_05.zip','2009 08 05','2020-11-12 13:26:31','./galleries/Photos/2009/2009_08_05.zip',NULL,'1114','1')
  , ('6','IMG_2629.JPG','IMG 2629','2020-11-12 13:26:31','./galleries/Photos/2009/Daddy_kids/IMG_2629.JPG',NULL,'1116','1')
  , ('7','IMG_2630.JPG','IM in /var/www/html/piwigo/include/dblayer/functions_mysqli.inc.php on line 856



Warning:  [mysql error 1062] Duplicate entry '2-1115' for key 'PRIMARY'

INSERT  INTO `piwigo_image_category`
  (`image_id`,`category_id`)
  VALUES('2','1115')
  , ('3','1115')
  , ('4','1114')
  , ('5','1114')
  , ('6','1116')
  , ('7','1116')
  , ('8','1116')
  , ('9','1116')
  , ('10','1116')
  , ('11','1116')
  , ('12','1116')
  , ('13','1116')
  , ('14','1116')
  , ('15','1116')
  , ('16','1116')
  , ('17','1116')
  , ('18','1116')
  , ('19','1116')
  , ('20','1116')
  , ('21','1117')
  , ('22','1117')
  , ('23','1117')
  , ('24','1117')
  , ('25','1117')
  , ('26','1117')
  , ('27','1118')
  , ('28','1118')
  , ('29','1118')
  , ('30','1118')
  , ('31','1118')
  , ('32','1118')
  , ('33','1118')
  , ('34','1119')
  , ('35','1119')
  , ('36','1120')
  , ('37','1120')
  , ('38','1122')
  , ('39','1124')
  , ('40','1124')
  , ('41','1124')
  , ('42','1124')
  , ('43','1124')
  , ('44','1124')
  , ('45','1124')
  , ('46','1124')
  , ('47','1124')
  , ('48','1124')
  , ('49','1124')
  , ('50','1124')
  , ('51','1125') in /var/www/html/piwigo/include/dblayer/functions_mysqli.inc.php on line 856


    Piwigo 2.10.2 Check for upgrade
    Operating system: Linux
    PHP: 7.4.12 (Show info) [2020-11-12 13:52:13]
    MySQL: 5.5.5-10.3.25-MariaDB-0ubuntu0.20.04.1 [2020-11-12 13:52:13]
    Graphics Library: GD 2.3.0

Piwigo URL: (not public)

Is there a better log of the actual full SQL somewhere?

Offline

 

#2 2020-11-12 12:16:19

erAck
Only trying to help
2015-09-06
2029

Re: Sql Errors

Obviously in table `piwigo_image_category` there is already an entry where `image_id` = 2 and `category_id` = 1115, which apparently was to be added for the IMG_2650.JPG of table `piwigo_images` with `id` = 2 and `storage_category_id` = 1115. However those image id and category id pair duplicates were calculated first hand (the following images entries also seem to be duplicates). The piwigo_images table, where the next id value is calculated from, may had been cleared without clearing also piwigo_image_category. Probably best to check database integrity, [extension by rvelices] RV DB Integrity might help to detect odds.


Running Piwigo at https://erack.net/gallery/

Offline

 

#3 2020-11-12 23:24:13

memecode
Member
2020-11-12
7

Re: Sql Errors

In further investigations to this issue, it seems that piwigo is completely unprepared for the amount of data I'm throwing at it:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4929792 bytes) in /var/www/html/piwigo/include/functions_html.inc.php on line 380

And also it's hitting the default 30 second script timeout too. This in trying to sync a large folder of photos and videos. I've increase both the memory limit and the script timeout to try and work around it, but it seems to me that some sort of chunking of the sync algorithm is needed in the long term.

Have the sync script run for 20seconds and then update the user's display with a ajax message or something. Then run the script again with the next batch of data... and keep the numbers of photos processed at once to a reasonable size... rather than just run out of memory.

Honestly I though this software was going to be a bit better than this.

Back to the SQL error itself. Seems it's not properly handling quotes. e.g.:

  , ('24498','104-0437_IMG.JPG','104-0437 IMG','2020-11-13 09:15:46','./galleries/Photos/Image Library One/Mags' piccies/Album/104-0437_IMG.JPG',NULL,'20470','1')
  , ('24499','104-0438_IMG.JPG','104-0438 IMG','2020-11-13 09:15:46','./galleries/Photos/Image Library One/Mags' piccies/Album/104-0438_IMG.JPG',NULL,'20470','1')

When the folder path has a single quote in the name, it needs to escape that correctly. Rookie error. *shakes head slowly*

Last edited by memecode (2020-11-12 23:28:23)

Offline

 

#4 2020-11-12 23:45:18

memecode
Member
2020-11-12
7

Re: Sql Errors

In site_update.php there is:

    $insert = array(
      'id'             => $next_element_id++,
      'file'           => $filename,
      'name'           => get_name_from_file($filename),
      'date_available' => CURRENT_DATE,
      'path'           => str_replace("\'", "\'\'", $fulldir),
      'representative_ext'  => $fs[$path]['representative_ext'],
      'storage_category_id' => $db_fulldirs[$dirname],
      'added_by'       => $user['id'],
      );


By adding the str_replace call to '$fulldir' the query succeeds. Ta da!

Offline

 

#5 2020-11-12 23:51:57

memecode
Member
2020-11-12
7

Re: Sql Errors

To actually answer my original question about getting the full SQL log, in include/config_default.inc.php turn on:

$conf['show_queries'] = true;
$conf['die_on_sql_error'] = true;

Seems to do the trick.

Offline

 

#6 2020-11-13 00:00:59

Aest
Member
2020-11-12
6

Re: Sql Errors

memecode wrote:

In further investigations to this issue, it seems that piwigo is completely unprepared for the amount of data I'm throwing at it:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4929792 bytes) in /var/www/html/piwigo/include/functions_html.inc.php on line 380

And also it's hitting the default 30 second script timeout too. This in trying to sync a large folder of photos and videos. I've increase both the memory limit and the script timeout to try and work around it, but it seems to me that some sort of chunking of the sync algorithm is needed in the long term.

Have the sync script run for 20seconds and then update the user's display with a ajax message or something. Then run the script again with the next batch of data... and keep the numbers of photos processed at once to a reasonable size... rather than just run out of memory.

Honestly I though this software was going to be a bit better than this.

Back to the SQL error itself. Seems it's not properly handling quotes. e.g.:

  , ('24498','104-0437_IMG.JPG','104-0437 IMG','2020-11-13 09:15:46','./galleries/Photos/Image Library One/Mags' piccies/Album/104-0437_IMG.JPG',NULL,'20470','1')
  , ('24499','104-0438_IMG.JPG','104-0438 IMG','2020-11-13 09:15:46','./galleries/Photos/Image Library One/Mags' piccies/Album/104-0438_IMG.JPG',NULL,'20470','1')

When the folder path has a single quote in the name, it needs to escape that correctly. Rookie error. *shakes head slowly*

Faced same problems.
I've solved issues related to memory exhaust and long running operations by adjusting following parameters in php.ini:
max_execution_time = 1200 (10minutes)
max_input_time = 1200 (same)
memory_limit = 2048M (it is good idea to turn swap on if you have a virtual server with ssd and limited ram)

Found VERY strange thing. During synchronization, if a filename contains a 'rank' substring (i.e battleship-rank-1.jpg)  then filename, picture name and picture path will be written in database if following manner: battleship-`rank`-1. This leads to checksum misses, and various upleasant things.

Can someone point where to dig for solving this issue?

Offline

 

#7 2020-11-13 00:48:54

memecode
Member
2020-11-12
7

Re: Sql Errors

Aest wrote:

Found VERY strange thing. During synchronization, if a filename contains a 'rank' substring (i.e battleship-rank-1.jpg)  then filename, picture name and picture path will be written in database if following manner: battleship-`rank`-1. This leads to checksum misses, and various upleasant things.

Can someone point where to dig for solving this issue?

Oh that's easy:

In include/dblayer/functions_mysqli.inc.php
Beginning of function pwg_query($query):

    if (preg_match('/\brank\b/', $query))

Does the 'rank' mangling.

Offline

 

#8 2020-11-13 00:58:49

erAck
Only trying to help
2015-09-06
2029

Re: Sql Errors

memecode wrote:

When the folder path has a single quote in the name, it needs to escape that correctly.

Which running into should be possible only if the apostrophe was explicitly added to $conf['sync_chars_regex'].

'path'           => str_replace("\'", "\'\'", $fulldir),

Though storing that form may lead to trouble later if the value is read from the database and used as is.


Aest wrote:

if a filename contains a 'rank' substring (i.e battleship-rank-1.jpg)  then filename, picture name and picture path will be written in database if following manner: battleship-`rank`-1

Not any substring 'rank', but a word 'rank', due to include/dblayer/functions_mysqli.inc.php function pwg_query() line 132.


Running Piwigo at https://erack.net/gallery/

Offline

 

#9 2020-11-13 01:02:13

memecode
Member
2020-11-12
7

Re: Sql Errors

erAck wrote:

memecode wrote:

When the folder path has a single quote in the name, it needs to escape that correctly.

Which running into should be possible only if the apostrophe was explicitly added to $conf['sync_chars_regex'].

How that's not the default is awful.

erAck wrote:

'path'           => str_replace("\'", "\'\'", $fulldir),

Though storing that form may lead to trouble later if the value is read from the database and used as is.

Ay, more bugs. So many.

Last edited by memecode (2020-11-13 01:08:39)

Offline

 

#10 2020-11-13 01:09:01

erAck
Only trying to help
2015-09-06
2029

Re: Sql Errors

Please submit bug reports at https://github.com/Piwigo/Piwigo/issues, or even better pull requests with proper fixes..


Running Piwigo at https://erack.net/gallery/

Offline

 

#11 2020-11-13 09:22:17

Aest
Member
2020-11-12
6

Re: Sql Errors

Already posted an issue. Have no idea how to fix this.

Thanks all for help.

Last edited by Aest (2020-11-13 09:34:23)

Offline

 

#12 2020-11-13 09:43:04

Aest
Member
2020-11-12
6

Re: Sql Errors

I've read some discussions at piwigo's gihtub and other places, so have a couple ideas about that 'rank' issue.
As mentioned by @memecode, exactly separate word "rank", not any substring leads to probems.
That happens because rank is reserved keyword in MySQL.
BUT, i suppose it have meaning to field names of a table, or to parts of SQL clauses, but not to value of a field.
So, we need to add some distinction between "rank" used in a sql syntax and "rank" used as a value of a field.

UPD. made a quick and dirty workaround:

Code:

  if (!preg_match('/(?<=\s|^|\W)values(?=\s|$|\W).*(?=\b)rank(?=\b)/i', $query))
  {
    // starting with MySQL 8, rank becomes a reserved keyword, we need to escape it
    if (preg_match('/\brank\b/', $query))
    {
      // first we unescape what's already escaped (to avoid double escaping)
      $query = preg_replace('/`rank`/', 'rank', $query);
      // then we escape the keyword
      $query = preg_replace('/\brank\b/', '`rank`', $query);
    }
  }

  if (!preg_match('/(?<=\s|^|\W)values(?=\s|$|\W).*(?=\b)groups(?=\b)/i', $query))
  {
    if (preg_match('/\bgroups\b/', $query))
    {
      // first we unescape what's already escaped (to avoid double escaping)
      $query = preg_replace('/`groups`/', 'groups', $query);
      // then we escape the keyword
      $query = preg_replace('/\bgroups\b/', '`groups`', $query);
    }
  }

Opinions?

Last edited by Aest (2020-11-13 13:39:06)

Offline

 

#13 2020-11-14 00:52:34

memecode
Member
2020-11-12
7

Re: Sql Errors

erAck wrote:

Please submit bug reports at https://github.com/Piwigo/Piwigo/issues or even better pull requests with proper fixes..

If the [Github] Piwigo issue #250 about that exact issue hasn't been actioned... what is the point of me putting in a duplicate?

sudo rm -rf /var/www/html/piwigo

Ok we're done here.

Offline

 

#14 2020-11-14 01:34:51

jnashpiwigo
Piwigo Team
2014-10-21
254

Re: Sql Errors

Don't forget to remove the database as well!

Offline

 

#15 2020-11-14 02:37:37

erAck
Only trying to help
2015-09-06
2029

Re: Sql Errors

Why did I even waste my time on that.


Running Piwigo at https://erack.net/gallery/

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact