开发者

Moving encoded data with Rails

I'm trying to move data from one database to another from within a rake task.

However, I'm getting some fruity encoding issues on some of the data:

rake aborted!
PGError: ERROR:  invalid byte sequence for encoding "UTF8": 0x92
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client开发者_如何学JAVA_encoding".

What can I do to resolve this error and get the data in? As far as I can tell (not knowing anything about encoding), the source DB is latin1.


if both databases are PG then you can export and import the whole database using the pg_dump options to change the encoding... that would probably the most performant way to do it

if you do this via a rake task you can do the transcoding inside your rake-task... that actually means you will have to touch every attribute and reencode it...

as it seems your new database is utf8 whereas the old is latin1

you could do it by having every string/text/text-like value encoded using... checking for respond_to?(:encoding) makes sure the data is encoded only if it has some encoding information attached, i. e. numeric values wont be transcoded

def transcode(data, toEnc = 'utf8')
  if data.respond_to?(:encoding) && data.encoding.name != toEnc
    return data.dup.force_encoding toEnc
  end
  data
end      

now you can just read a record from the old db, run it through this method and then write it to the new database

u = OldDBUser.first
u.attribute_names.each { |x|
  u[x.to_sym] = transcode u[x.to_sym]
}
#... whatever you do with the transcoded u

... well I have not tested those, but please do, maybe its all you need

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜