We are migrating a wiki system from MediaWiki v1.25 to v1.35 while using a cloned database in the same MySQL server, but in a new Apache server. The issue is that, after doing all the steps mentioned here and here, all the BLOB columns that have a UTF-8 encoded character like ??, ?�, ?� got somehow polluted with the UTF-8 encoded character U+0083 just before the afromentioned characters.
Fortunatelly, we found out that it can be solved by doing the followingUPDATE
.
UPDATE clone_wiki_db.some_table
SET blob_col = REPLACE(blob_col, CONCAT(CHAR(0x83), CHAR(0xC2)), '');
The problem here is that there are a lot of BLOB columns in the DB. Is there a way to massUPDATE
all BLOB columns in the cloned DB? Is there a better approach to do this "cleaning"?
Here are some details of both systems:
Legacy system (no issues here)
New system (in test stage, with issues)
Self answering if anyone falls in the same situation.
Based in this answer, I used a stored procedure, a cursor and a prepared statement to do the job. First, I retrieve all the columns in the DB with aBLOB
,TINYBLOB
orVARBINARY
datatype along with the respective table. Then, by using aCURSOR
, I fetch every row and build a dynamicUPDATE
string in order to execute it by using a prepared statement/dynamic SQL.
DELIMITER //
DROP PROCEDURE IF EXISTS BLOB_CLEANER//
CREATE PROCEDURE BLOB_CLEANER() BEGIN
DECLARE var_final INTEGER DEFAULT 0;
DECLARE var_column VARCHAR(25);
DECLARE var_table VARCHAR(25);
DECLARE cursor1 CURSOR FOR
SELECT TABLE_NAME,
COLUMN_NAME
FROM INFORMATION_SCHEMA.columns
WHERE TABLE_SCHEMA = 'clone_wiki_db'
AND DATA_TYPE IN ('TINYBLOB', 'BLOB', 'VARBINARY');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET var_final = 1;
OPEN cursor1;
bucle: LOOP
FETCH cursor1
INTO var_table,
var_column;
IF (var_final = 1) THEN
LEAVE bucle;
END IF;
SET @sql = CONCAT(
'UPDATE ',
var_table,
' SET ',
var_column,
' = REPLACE(',
var_column,
', CONCAT(CHAR(0x83), CHAR(0xC2)), '''')'
);
PREPARE stmt
FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP bucle;
CLOSE cursor1;
END//
DELIMITER ;
After executing the SP, I changed the charset of the DB from latin1 to utf8_swedish_ci.
ALTER SCHEMA `clone_wiki_db` DEFAULT COLLATE utf8_swedish_ci;
Finally, restarted the Apache service.
As always, be cautious when using dynamic SQL (or a prepared statements like this) in order to avoid a SQL injection.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.