refactor: Convert country timezones to JSON and handle errors

This commit refactors the database migration to convert the
timezones for each country in the database to a JSON format.
It addresses an issue where the timezone data was not properly
formatted. It provides a default value if there's an error to avoid
data loss. It also includes error handling to catch and log exceptions
during the migration process, preventing the entire migration from failing.
This commit is contained in:
songtianlun 2025-02-15 16:54:59 +08:00
parent ca3691004f
commit 2ffb1a4248

View File

@ -13,11 +13,11 @@ class ConvertCountryTimezonesToJson < ActiveRecord::Migration[8.0]
rescue => e
puts "Error converting timezones for country #{country.id}: #{e.message}"
# 设置默认值
country.update_column(:timezones_json, [{"zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00"}])
country.update_column(:timezones_json, [ { "zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00" } ])
end
else
# 设置默认值
country.update_column(:timezones_json, [{"zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00"}])
country.update_column(:timezones_json, [ { "zoneName" => "UTC", "gmtOffsetName" => "UTC+00:00" } ])
end
end