Use raw MySQL REPLACE to improve migrate performance

18 Jul 2019

When I migrate rails from 4.2 to 5.2, I meet Strong Parameters issue. I have saved many strong parameters in MySQL database. I thought it would just be some json like string in database. But after I chekced the datbase, I see three formats:

--- !map:ActiveSupport::HashWithIndifferentAccess {}
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess {}
--- !ruby/hash:ActionController::Parameters {}

Atually, the firt two formats can success parsed under 5.2. But the ActionController::Parameters can not parse under 5.2.

So I create a migration to fix this. But I have 7 million records of this table. It will cost so long time to finish the migrate.

After some research, I find a method - MySQL REPLACE

ActiveRecord::Base.connection.execute(
	"UPDATE usages SET params = REPLACE(params, 'ActionController::Parameters', 'HashWithIndifferentAccess')"
)

It only cost dozen of seconds to finish this migrate.