Class: Gitlab::Database::Type::JsonPgSafe

Inherits:
ActiveRecord::Type::Json
  • Object
show all
Defined in:
lib/gitlab/database/type/json_pg_safe.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(replace_with: '') ⇒ JsonPgSafe



18
19
20
21
22
# File 'lib/gitlab/database/type/json_pg_safe.rb', line 18

def initialize(replace_with: '')
  super()

  @replace_with = replace_with
end

Instance Method Details

#serialize(value) ⇒ Object



24
25
26
27
# File 'lib/gitlab/database/type/json_pg_safe.rb', line 24

def serialize(value)
  # Replace unicode null character(\u0000) that isn't escaped (not preceded by odd number of backslashes)
  super&.gsub(/(?<!\\)(?:\\\\)*\\u0000/, @replace_with)
end