db-patch-mysql-utf8.pl 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use FindBin '$Bin';
  7. use lib "/opt/Eye/scripts";
  8. use Data::Dumper;
  9. use eyelib::config;
  10. use eyelib::main;
  11. use eyelib::mysql;
  12. use strict;
  13. use warnings;
  14. my @tables = get_recrods_sql($dbh,"SHOW TABLES");
  15. print "Migrate tables to UTF8 format\n";
  16. for $table (@tables) {
  17. print "Apply table $table\n";
  18. do_sql($dbh,"SET foreign_key_checks = 0; ALTER TABLE `".$table."` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; SET foreign_key_checks = 1; ");
  19. print "Done\n";
  20. }
  21. print "Migrate database\n"
  22. do_sql($dbh,"ALTER DATABASE ".$DBNAME." CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
  23. print "Done\n";
  24. print "Revert filed type to TEXT\n";
  25. for $table (@tables) {
  26. my $create_table = do_sql($dbh,"show create table $table");
  27. print Dumper($create_table);
  28. # | sed 's/\\n/\n/g' | egrep -i "[[:space:]]MEDIUMTEXT[[:space:]]" | awk '{ print $1 }' | while read c_field; do
  29. # echo "ALTER TABLE $table MODIFY $c_field TEXT;" >>migration_utf8
  30. }
  31. print "Done!\n";
  32. exit;