summaryrefslogtreecommitdiff
path: root/lib/postgres_set.rb
blob: dfad4cc6741dd8809309f1b9f54708f6e6525ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# frozen_string_literal: true

module PostgresSet
  class << self
    def add_to_set(relation, column, value)
      sql = Tag.send :sanitize_sql, ["#{column} = ARRAY(SELECT DISTINCT unnest(array_append(#{column}, :val)) ORDER BY 1)", val: value]
      relation.in_batches.update_all(sql)
    end

    def replace(relation, column, old_value, new_value)
      sql = Tag.send :sanitize_sql, ["#{column} = ARRAY(SELECT DISTINCT unnest(array_replace(#{column}, :old, :new)) ORDER BY 1)", old: old_value, new: new_value]
      relation.in_batches.update_all(sql)
    end

    def pull(relation, column, value)
      sql = Tag.send :sanitize_sql, ["#{column} = array_remove(#{column}, :val)", val: value]
      relation.in_batches.update_all(sql)
    end
  end
end