Sha256: 2d20d7fd85e9ea2dceda41b9326ca71d9c8596a249e9292d59f9b4afe63c9577
Contents?: true
Size: 1.53 KB
Versions: 4
Compression:
Stored size: 1.53 KB
Contents
# frozen_string_literal: true module AR module Enum module Adapter def enum_types execute <<~SQL with sorted_enums as ( select t.typname, e.enumlabel from pg_type t, pg_enum e where t.oid = e.enumtypid order by e.enumsortorder ) select typname as name, string_agg(enumlabel, ',') as labels from sorted_enums group by typname SQL end def create_enum(name, values) values = values.map do |value| quote(value.to_s) end sql = <<-SQL CREATE TYPE #{name} AS ENUM (#{values.join(', ')}) SQL execute(sql) end def add_enum_label(name, value, options = {}) sql = "ALTER TYPE #{name} ADD VALUE #{quote(value.to_s)}" if options[:before] sql = "#{sql} BEFORE #{quote(options[:before].to_s)}" elsif options[:after] sql = "#{sql} AFTER #{quote(options[:after].to_s)}" end execute(sql) end def rename_enum_label(name, from, to) sql = "ALTER TYPE #{name} RENAME VALUE #{quote(from.to_s)} TO #{quote(to.to_s)}" execute(sql) end def drop_enum(name, options = {}) sql = "DROP TYPE IF EXISTS #{name}" sql = "#{sql} CASCADE" if options[:cascade] execute(sql) end end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
ar-enum-0.2.2 | lib/ar/enum/adapter.rb |
ar-enum-0.2.1 | lib/ar/enum/adapter.rb |
ar-enum-0.2.0 | lib/ar/enum/adapter.rb |
ar-enum-0.1.0 | lib/ar/enum/adapter.rb |