-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make JDatabaseDriver::splitSql() handle comments and remove trimSql functions #9369
Make JDatabaseDriver::splitSql() handle comments and remove trimSql functions #9369
Conversation
Make JDatabaseDriver::splitSql handle single line and multi line comments and end of line comments and trim the query and not return empty queries.
Remove local trimQuery functions and calls to them for queries obtained with JDatabaseDriver::splitSql(), since this strips off comments now.
$converted = 0; | ||
|
||
// Still render the error message from the Exception object | ||
JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the string I added last week here (check the database fixer thing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will not add it in the loop because there it will be repeated. I leave the sql error there for more info, and add the new message below as summary in case if some error happened before in the loop.
$converted = 0; | ||
|
||
// Show an error message telling to check database problems | ||
JFactory::getApplication()->enqueueMessage(JText::_('JLIB_DATABASE_ERROR_DATABASE_UPGRADE_FAILED'), 'error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wilsonge Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wilsonge Hmm, meanwhile I think this message here is not a good idea. Above it maybe makes sense because not in a look and handling with this special new table nobody should care about. But here in the loop if multiple errors we repeat it multiple times. We don't exit the loop in case of error, and we shouldn't, so it was ok with the exception text I think, it could be helpful information. And this new text we should use in addition at the end, after the loop, if $converted == 0 (means somethign failed) only 1 time. I will change this in that way now.
Corrected typo in testing instructions, code snippet for test 5 used $sql instead of $buffer in call to splitSql. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Change back previous commit but add the new mesage to the end for the case if something failed
// Show an error message telling to check database problems | ||
JFactory::getApplication()->enqueueMessage(JText::_('JLIB_DATABASE_ERROR_DATABASE_UPGRADE_FAILED'), 'error'); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it here instead, after the loop.
Please find me some testers. Not every tester has to perform all tests, but then he/she should not in coment to test result what was tested and what not, e.g. "Tested all except Test 5" or so. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@richard67 I am happy to test this afternoon if it hasn't been completed. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Great, thanks in advance. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
I have tested this item 🔴 unsuccessfully on 46fdca1 Test 1 Pass Test 2 Problem - Joomla 2.5.28 upgrade to J3.5-richard-version Test 3 I am getting an error when importing the database error is below
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS Test 4 Pass Joomla 2.5 upgraded and 3.4 upgraded (the table is inserted correctly both times) Test 5 Joomla 3.4.8 upgrade to J3.5-richard-version If you would like me to run any test again please let me know This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Thanks for testing, I'll have a look on it when I have time, later tonight or tomorrow. |
@Twincarb Did you upgrade with Joomla! Update component as descibed? Or did you use the extension installer to ugrade Joomla!? In this case please try again with Joomla! Update component. |
@richard67 I used the Joomla Update component each time, I am just going to do a job in the garage then I will take another look as well. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@Twincarb How did you import your database? When I use my test code snippet from Test 5 to show the content of installation/sql/mysql/joomla.sql from a 2.5 (where the defaults where '0' and not 0), the statement for creating the assets table is shown by moy code snipped as follows:
So as you can see there is not this syntax error with "... DEFAULT '0'COMMENT ...". How did you export and import your pre-updated 2.5 database? Did you run the joomla.sql? Or did you export / import with phpmyadmin? Or Akeba backup? Maybe they have a problem with old 2.5? I cannot replicate this here up to now. |
@Twincarb If you do the same test for updating with an unpatched 3.5.0 RC. Do you have the same problems? Can you check this? If so, they are not relatef tho this PR. |
@Twincarb Hmm, I've just processed the complete joomla.sql of a 2.5.28 with the patched splitSql function as shown in my test code snippet for Test 5 for the test scripts. Then I copied the complete sql from my frontpage into phpmyadmin and run the SQL. It all run without any error, and at the end all core tables of a 2.5.28 were created. So I have no idea what happened in your test. Please try what you did again with unpatched 3.5.0 RC and let me know if ok or same errors. Thanks in advance. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@richard67 Doing a test upgrade to 3.5rc was my next plan, although I am not sure where to find the xml file for it. With the sql, I did a quick export in phpmyadmin and then used the import to restore the dump. But it gave the error above This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@Twincarb Hmm, I also have a problem to update 2.5.28 to 3.5.0 RC using testing channel. Get told I am already at max version. I prepared a custom URL like for my patched version but with "3" instead of "1" in the file name: http://test5.richard-fath.de/list_test3_j25.xml. This points to an original 3.5.0 RC. But @wilsonge do you know why 2.5 cannot be updated to 3.5.0 RC via testing channel of Joomla! Update Component? Mistake? Or by purpose? This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@Twincarb The problem with syntax error in your export I cannot reproduce here. Did export from a 2.5.28 and it looks as it should, space between "DEFAULT '0'" and "COMMENT". Maybe this is a problem with your export/import? What I can reproduce is that editing and saving modules does not work after updating the 2.5.28 to my patched 3.5.0 RC, so I want to test if this is also the case with unpatched 3.5.0 RC. I let you know my result, but if you find time, pleas test, too. Thansk for your help. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
It's purposeful because 2.5 and 3.x share the same testing channel. So when we were pushing 2.5 and 3.x releases together, we couldn't put the beta/RC for 2.5 to 3.x upgrades on that testing channel because that would override testing 2.5 to 2.5 updates. Not as much of an issue now that 2.5's EOL I guess. In theory this change does it with the custom XML file we've got in the repo, or instead of the list_test_25to3x.xml change you change this line in the main testing channel's XML to use the 3.x releases. |
@mbabker Well, meanwhile I helpe myself with a separate custom URL on my domain serving the 3.5.0 RF for 2.5.28, http://test5.richard-fath.de/list_test3_j25.xml. If you want and have time you could help with testing this PR here. @Twincarb I tested with this URL and it serves the unpatched RC, and it has the same problem: Saving changes when editing modules does not work. So this problem is not related to this PR here. Can you test and confirm? This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@richard67 No worries will test with your xml there, I need to look into why I am not able to restore databases on my vps the only change on this server is I set db defaults to utf8mb4 , I have a second one rebuilding at the moment so will be able to test again when that is set up. In the meantime I will do the 2.5.28 upgrade using your xml. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Before upgrading the 2.5.28, export its db again and then check in the export file if for the very first table at the top, assets, you see "DEFAULT '0' COMMENT ..." (= ok) or "DEFAULT '0'COMMENT ..." (=error) for columns lft and rgt, and let me know the result. If it is wrong in your export, the problem is also not related to this PR. Thanks in advance. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
@richard67 Just had a look at the sql dump, the first line is missing the space which should be in here "DEFAULT '0'COMMENT ... This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Changed description and example sql script output back for c style comment handling, but with exceptions for mysql specials. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
richard, a question for the future: wouldn't it be easy to remove the comments with regular expressions? see for instance http://stackoverflow.com/questions/7100127/regex-to-match-mysql-comments/13823363#answer-13823363 (didn't test) |
@andrepereiradasilva I have thought about it and exactly had this stackoverflow answer in mind, but I remember from past discussions about comment strippings in other issues (with the "#" comments) that complex regexes are not so welcome for some of the maintainers, and I also find them not easy to maintain, even if well commented, and the solutoon from stackoverflow did not work as it was for our purpose. And so I decided to do it based on the existing solution. Regarding preformance I think the one is like the other ... the regex parser also has to loop and check backwards, so i think there is not a big difference in performance. So at the end it was a matter of taste, and what could be better to understand for others. PR is ready for test now by the way, with last commit I added back the handling of c style comments but with exceptions for the mysql specials. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
ok thanks for the awnser |
Would you have preferred the regex way, andre? |
always prefer to write less code :) |
I see .. well the problem with the stackoverflow solution was that it did not do what we need, split the complete filecontent into statement with borders at semicolons (which are not in quoted text) AND strip off the comments, inline, single line and multi line. This one you linked did not work for the complete file with multiple queries. I found another one for splitting but this then did not handle the comments. An all in one solution I did not find with regex, but found an answer in stackexchange telling that this would not be a trivial thing. |
yeah, with complex regex it never is simple but this PR is ready for test and that is what matters |
I will be able to test again this evening. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
At the moment we have this single standard for sql (which works somehow for lucky circumstances), and with this PR we will also have same standard for all kinds of databases (but this time working well), because it not makes a difference between the databas kinds. So I think this PR would be a good step in your direction. Or did you mean we shall support only 1 kind of comment for all kinds of files? I think this will not come true because sql guys not wanna miss their "--", while the shell scrip guys prefer their "#" ... not even to talk aout the c programmers. Thanks in avance for later testing. |
I was thinking of having a "standard" way for each of the file types.. Testing wise do I need to run all 5 tests? I presume you have updated your hosted files? This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Yes, I have updated my files, the test sql scripts and also the update container behind the custom url. I think about doing 1 more commit for making code better without changing functionality, but I can also do this later if time gets too tight. So if you or someone will test this before, I will not do that 1 more commit, in order not to need tests again, so it can go into 3.5.0 as it is now. Otherwise, if I see nobody tested yet when I have it prepared later, then I will check it in when my files are updated again. |
Make not append 1 char in each loop if not comment or comment end but save the start index as it was in old code without this PR and then copy to buffer only if necessary. Also restore old behavior of not ingnoring the complete statement if it has unclosed quote at file end, so that as before with this PR people will see the sql error and not see nothing like with this PR before this commit.
This PR has received new commits. This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Ready for test. Test update container is already updated, same custom URL. Comment to latest commit describes the changes. The test update container still is based on RC1 and not RC2 because with RC2 I had troubles with the changed handling of the manifest file. |
Code is even closer now to code before the PR, so diff (comparison) for the complete PR is better understandable now, even if diff for the last commit might be less clear. |
I didnt do test five - didnt quite understand This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
Thanks for testing, Brian. Test 5 is just using a code snippet to read in a test sql file full of comments and stuff and statements, use the splitSql function modified by this PR to split the file content into sql statements array, and then output the extracted sql statements to the web site's page and check if it looks like the shown output, i.e. only contains the queries. |
P.S.: The statements in test 5 are of course not executed. It is only to show what is result of reading the test sql file. So you can also do this test with sql file for postgresql when using a mysql. |
P.P.S.: @brianteeman Ah, I just see that the " |
I have tested this item ✅ successfully on 02462ed This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/9369. |
I finished the final test for @brianteeman successfully. So with 2 good tests i'm merging this |
Make JDatabaseDriver::splitSql() handle comments and remove trimSql functions
Thanks everybody for testing. |
Thankyou richard! |
Summary of Changes
This pull request makes JDatabaseDriver::splitSql() strip off comments beginning with "--" or "#" until the end of line and c style comments "/* ... */" (also inline and multi-line) from the sql statements, trim the query and not return empty queries.
For the c style comments there are exceptions for mysql specials, which will not be stripped off, see http://dev.mysql.com/doc/refman/5.7/en/comments.html.
At the moment those specials are allowed regardless of the database kind, but if this is desired I can with a later PR make overrides for the mysql drivers and remove these specials then from base driver here.
In addition this PR removes the local functions trimQuery() and their usage whereever they were recently added to work around the missing handling of comments (except single line "#") of JDatabaseDriver::splitSql().
The handling of comments in sql statements retrieved from sql files (e.g. for schema updates or extensions) is not correct in current staging because it does not respect if comment characters appear in quoted text (and so not are comments). This PR corrects also this.
Testing Instructions
Overview, preconditions
Because the changed splitSql function and the replacement of the local workarounds (trimSql functions) has impact on any kind of installation action, there are several tests necessary to make sure that all still works well:
All tests should be done for all supported kinds of databases, and in case of mysql databases also old without and new with utf8mb4 support.
Hint on utf8mb4 support of mysql: It is NOT supported, if database server version is lower than 5.5.3, or if mysqlnd client used with a version lower than 5.0.9, or if other database client used with a version lower than 5.5.3.
Test 1: New installation of Joomla!
Perform a new installation of Joomla! using the latest staging + this PR as installation source.
You can find it here: https://github.com/richard67/joomla-cms/archive/correct-split-sql-in-db-driver.zip.
Verify that all worked well and all Joomla! database tables have been created, and the database is shown as updated and having no problems in "Extensions -> Manager -> Database":
Test 2: Updating Joomla! with Joomla! Update component
Before you update, make an export of your database so you can later use it in Test 3.
Set "Custom URL" as test channel in the update component's options and enter following as custom URL:
Start the update and wait until it has finished.
Verify that all worked well and the database is shown as updated and having no problems in "Extensions -> Manager -> Database":
Test 3: Running Database Schema updates
After having made the previous Test 2, delete all database tables with and restored the old pre-update database using the export file made at the beginning of Test 2.
Goto "Extensions -> Manager -> Database" and verify that updated according to the version you have saved the old data from are shown as to be done, and also the utf8mb4 or utf8 conversion is shown as to be done as the last open problem.
Click the "Fix" button.
Result:
Test 4: Installing extensions which include sql scripts for updating database
With either the new installed 3.5.0 latest staging + this patch from Test 1 or the updated one from Test 2, verify that installing extensions works still well.
An example with an sql script containing a hashtag comment and a column with default value '#fff' can be found here: http://test5.richard-fath.de/sampleerror.zip. This file was once provided with issue #9251 and installs a table
#__bla
(replace#__
by your db prefix) if all goes well. If you use this example, check after installation that columnbla
of this new table has a default value of '#fff', so the '#' has not been stripped off by the splitSql function.Test 5: Parsing the test sql script.
If you have no 3.5.0 latest staging + this patch available from the previous tests 1 and 2, apply this patch on latest staging or a 3.5.0 RC.
Download following test script and store it on a location on your server which is accessibly for Joomla!:
Add following code snippet below the body tag in the index.php of your site template (e.g. protostar).
Replace the path by the path where you stored the test sql script, and in case of non-mysql db, change db type in the file name.
Now go to your site and verify that the sql statements have been correctly extracted from the sql script, removing all comments "--" and "#" for complete lines and for parts until line end.
The output should look as follows on mysql and similar on the other databases:
Names should be properly quoted (not shown properly here because name quotes are used here as markup).
You will see in my sql that I did not end the file with a new line and the last statement not with a semicolon.
This is compatible to the old behavior.
You can play around with adding the semicolon or the newline or both, the statements shown by the test should still be the same (all end with a semicolon).
You can also use the test sql script and the code snippet on an unpatched 3.5.0 RC or staging and see that the output of queries shows a mess which will very likely cause SQL syntax errors.