# typed: strict
class MigrateE621ScannedFavsAtToJsonColumn < ActiveRecord::Migration[7.2]
  extend T::Sig

  sig { void }
  def change
    reversible do |dir|
      dir.up do
        execute <<~SQL
          UPDATE domain_e621_users
          SET json_attributes = jsonb_set(json_attributes, '{scanned_favs_at}', to_json(scanned_favs_at)::jsonb)
          WHERE scanned_favs_at IS NOT NULL
        SQL
        remove_column :domain_e621_users, :scanned_favs_at
      end
      dir.down do
        add_column :domain_e621_users, :scanned_favs_at, :datetime
        execute <<~SQL
          UPDATE domain_e621_users
          SET scanned_favs_at = (json_attributes->>'scanned_favs_at')::text::timestamp
          WHERE json_attributes->>'scanned_favs_at' IS NOT NULL
        SQL
      end
    end
  end
end
