Moodle 4.5
Plugin version: 2025080700
While using the CLI find/replace tool (replace.php), I found that the script would crash if the replacement text is too long to fit into the database field. I had to work around it by running the script until such a record was encountered, then manually removing it from my input CSV file, and then re-running the replace script.
This was unexpected because the script has such a great facility for handling other kinds of problems gracefully and then reporting them at the end. So, I'd like to suggest that this problem should also be caught and reported, rather than crashing the script.
To be really fancy I suppose it could try to predetermine the size of the DB field (from install.xml files, or database inspection?), but I think it would be enough to just try/catch for dmlwriteexception. That would also catch anything else that might make the UPDATE query fail, like uniqueness constraints.
Here's an example log output including the stack trace, encountered when doing a find/replace on the column mdl_resource.name, which has a character limit of 255. (I've edited out some client-specific information)
$ sudo -u www-data php ./admin/tool/advancedreplace/cli/replace.php --input=inputfile.csv
[??????????????????????????????????????????????? ] 84.6% - 16 secs
14804 Replaced, 0 Skipped, 4523 Already replaced, 169 Errors.
Default exception handler: Error writing to database Debug: ERROR: value too long for type character varying(255)
UPDATE mdl_resource SET name = $2 WHERE id = $1
[array (
0 => 1004038,
1 => '(REPLACEMENT TEXT LONGER THAN 255 characters)',
)]
Error code: dmlwriteexception
* line 500 of /lib/dml/moodle_database.php: dml_write_exception thrown
* line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end()
* line 358 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->read_slave_query_end()
* line 1431 of /lib/dml/pgsql_native_moodle_database.php: call to pgsql_native_moodle_database->query_end()
* line 1924 of /lib/dml/moodle_database.php: call to pgsql_native_moodle_database->set_field_select()
* line 704 of /admin/tool/advancedreplace/classes/helper.php: call to moodle_database->set_field()
* line 830 of /admin/tool/advancedreplace/classes/helper.php: call to tool_advancedreplace\helper::replace_text_in_a_record()
* line 86 of /admin/tool/advancedreplace/cli/replace.php: call to tool_advancedreplace\helper::handle_replace_csv()
!!! Error writing to database !!!
Thanks!
Moodle 4.5
Plugin version: 2025080700
While using the CLI find/replace tool (
replace.php), I found that the script would crash if the replacement text is too long to fit into the database field. I had to work around it by running the script until such a record was encountered, then manually removing it from my input CSV file, and then re-running the replace script.This was unexpected because the script has such a great facility for handling other kinds of problems gracefully and then reporting them at the end. So, I'd like to suggest that this problem should also be caught and reported, rather than crashing the script.
To be really fancy I suppose it could try to predetermine the size of the DB field (from install.xml files, or database inspection?), but I think it would be enough to just try/catch for
dmlwriteexception. That would also catch anything else that might make the UPDATE query fail, like uniqueness constraints.Here's an example log output including the stack trace, encountered when doing a find/replace on the column
mdl_resource.name, which has a character limit of 255. (I've edited out some client-specific information)Thanks!