I am changing the encoding of all my tables from latin1
to utf8mb4
using a rails rake task.
I want to:
I can grab all the tables that have the wrong enconding with
results = ActiveRecord::Base.connection.execute <<-STRING
SHOW TABLE STATUS WHERE collation <> 'utf8mb4'
STRING
I'm stuck with how to pass the table name
for each of those tables to the mysql query so that the table encoding can be altered.
results.each do |table|
ActiveRecord::Base.connection.execute <<-STRING
ALTER TABLE #{table.table_name} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
STRING
end
Each result is an array:
results.first.class
=> Array
And the first value returned by SHOW TABLE STATUS
is the table name. So you should be able to use something like:
results.each do |table|
ActiveRecord::Base.connection.execute <<-STRING
ALTER TABLE #{table[0]} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
STRING
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.