summaryrefslogtreecommitdiff
path: root/lib/gitlab/database/type/json_pg_safe.rb
blob: bbc207bd0d979220f3bae371250aa392ad581725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true

module Gitlab
  module Database
    module Type
      # Extends Rails' ActiveRecord::Type::Json data type to remove JSON
      # encooded nullbytes `\u0000` to prevent PostgreSQL errors like
      # `PG::UntranslatableCharacter: ERROR: unsupported Unicode escape
      # sequence`.
      #
      # Example:
      #
      #   class SomeModel < ApplicationRecord
      #     # some_model.a_field is of type `jsonb`
      #     attribute :a_field, Gitlab::Database::Type::JsonPgSafe.new
      #   end
      class JsonPgSafe < ActiveRecord::Type::Json
        def serialize(value)
          super&.gsub('\u0000', '')
        end
      end
    end
  end
end