science-ation/scripts/update_encoding.php
2025-03-13 23:01:51 +00:00

49 lines
2.7 KiB
PHP

<?
/*
* This file is part of the Science-ation project
* Science-ation Website: https://science-ation.ca
*
* This file was part of the 'Science Fair In A Box' project
*
*
* Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
* Copyright (C) 2005 James Grant <james@lightbox.org>
* Copyright (C) 2024 AlgoLibre Inc. <science-ation@algolibre.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
require_once ('./data/config.inc.php');
$dsn = "mysql:host=db;dbname=$DBNAME;charset=utf8mb4";
$pdo = new PDO($dsn, $DBUSER, $DBPASS);
try {
//echo "SELECT DISTINCT CONCAT('ALTER TABLE ', TABLE_NAME,' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;') as queries FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$DBNAME' AND TABLE_TYPE='BASE TABLE' UNION SELECT DISTINCT CONCAT('ALTER TABLE ', C.TABLE_NAME, ' CHANGE ', C.COLUMN_NAME, ' ', C.COLUMN_NAME, ' ', C.COLUMN_TYPE, ' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;') as queries FROM INFORMATION_SCHEMA.COLUMNS as C LEFT JOIN INFORMATION_SCHEMA.TABLES as T ON C.TABLE_NAME = T.TABLE_NAME WHERE C.COLLATION_NAME is not null AND C.TABLE_SCHEMA='$DBNAME' AND T.TABLE_TYPE='BASE TABLE'";
$stmt = $pdo->prepare("SELECT DISTINCT CONCAT('ALTER TABLE ', TABLE_NAME,' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;') as queries FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='$DBNAME' AND TABLE_TYPE='BASE TABLE' UNION SELECT DISTINCT CONCAT('ALTER TABLE ', C.TABLE_NAME, ' CHANGE ', C.COLUMN_NAME, ' ', C.COLUMN_NAME, ' ', C.COLUMN_TYPE, ' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;') as queries FROM INFORMATION_SCHEMA.COLUMNS as C LEFT JOIN INFORMATION_SCHEMA.TABLES as T ON C.TABLE_NAME = T.TABLE_NAME WHERE C.COLLATION_NAME is not null AND C.TABLE_SCHEMA='$DBNAME' AND T.TABLE_TYPE='BASE TABLE'");
$stmt->execute();
while ($r = $stmt->fetch(PDO::FETCH_OBJ)) {
foreach ($r as $query) {
print_r($r);
$stmt = $pdo->prepare($query);
$stmt->execute();
}
}
} catch (PDOException $exception) {
error_log('Err: ' . $exception);
}
?>