Module: EnumKit::ActiveRecordExtensions::ConnectionAdapters::PostgreSQLAdapter

Defined in:
lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb

Overview

:nodoc:

Constant Summary collapse

ENUM_QUERY =

Returns An SQL query that returns all available enum types in the database.

Returns:

  • (String)

    An SQL query that returns all available enum types in the database.

<<~SQL
  SELECT
  pg_type.OID,
  pg_type.typname,
  pg_type.typtype,
  array_to_string(array_agg(pg_enum.enumlabel ORDER BY pg_enum.enumsortorder), '\t\t', '') as values
  FROM pg_type
  LEFT JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid
  WHERE pg_type.typtype = 'e'
  GROUP BY pg_type.OID, pg_type.typname, pg_type.typtype
  ORDER BY pg_type.typname
SQL

Instance Method Summary collapse

Instance Method Details

#add_enum_value(name, value, after: nil, before: nil) ⇒ Object

Add a new value to an enum type in the database.

Note that you can’t specify both :before and :after.

Parameters:

  • name (Symbol)

    The enum’s name.

  • value (String|Symbol)

    The value to add.

  • after (String|Symbol) (defaults to: nil)

    An existing value after which the new value should be inserted.

  • before (String|Symbol) (defaults to: nil)

    An existing value before which the new value should be inserted.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 87

def add_enum_value(name, value, after: nil, before: nil)
  name  = EnumKit.sanitize_name!(name)
  value = EnumKit.sanitize_value!(value)

  statement = "ALTER TYPE #{name} ADD VALUE #{EnumKit.sqlize(value)}"

  raise ArgumentError, "You can't specify both :before and :after" if before && after

  statement += " AFTER #{EnumKit.sqlize(EnumKit.sanitize_value!(after))}"   if after
  statement += " BEFORE #{EnumKit.sqlize(EnumKit.sanitize_value!(before))}" if before

  enum_execute(statement)
end

#clear_enum_cache!Object

Clear the cached enums to force a refresh the next time they are accessed.



32
33
34
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 32

def clear_enum_cache!
  @enums = nil
end

#create_enum(name, values) ⇒ Object

Create a new enum type in the database.

Parameters:

  • name (Symbol)

    The enum’s name.

  • values (Array)

    The enum’s acceptable values.



49
50
51
52
53
54
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 49

def create_enum(name, values)
  name   = EnumKit.sanitize_name!(name)
  values = EnumKit.sanitize_values!(values)

  enum_execute "CREATE TYPE #{name} AS ENUM #{EnumKit.sqlize(values)}"
end

#drop_enum(name) ⇒ Object

Drop an existing enum type from the database.

Parameters:

  • name (Symbol)

    The enum’s name.



72
73
74
75
76
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 72

def drop_enum(name)
  name = EnumKit.sanitize_name!(name)

  enum_execute "DROP TYPE #{name}"
end

#ensure_renaming_enum_values_is_supported!Object

Raise an exception if the active PostgreSQL version doesn’t support renaming enum values.

Raises:

  • (NotImplementedError)


133
134
135
136
137
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 133

def ensure_renaming_enum_values_is_supported!
  return if ActiveRecord::Base.connection.postgresql_version >= 100_000

  raise NotImplementedError, 'PostgreSQL 10.0+ is required to enable renaming of enum values.'
end

#enum_execute(*args, &block) ⇒ Object

Execute a SQL statement, then clear the enum cache.



141
142
143
144
145
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 141

def enum_execute(*args, &block)
  result = execute(*args, &block)
  clear_enum_cache!
  result
end

#enumsHash

Returns The enum types available in the database.

Returns:

  • (Hash)

    The enum types available in the database.



38
39
40
41
42
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 38

def enums
  @enums ||= select_all(ENUM_QUERY.tr("\n", ' ').strip).each_with_object({}) do |row, enums|
    enums[row['typname'].to_sym] = row['values'].split("\t\t")
  end
end

#migration_keysObject

:nodoc:



119
120
121
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 119

def migration_keys
  super + [:enum_type]
end

#prepare_column_options(column) ⇒ Object

:nodoc:



125
126
127
128
129
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 125

def prepare_column_options(column)
  spec = super
  spec[:enum_type] = column.sql_type.inspect if column.type == :enum
  spec
end

#rename_enum(current_name, new_name) ⇒ Object

Rename an existing enum type.

Parameters:

  • current_name (Symbol)

    The enum’s current name.

  • new_name (Symbol)

    The enum’s new name.



61
62
63
64
65
66
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 61

def rename_enum(current_name, new_name)
  current_name = EnumKit.sanitize_name!(current_name)
  new_name     = EnumKit.sanitize_name!(new_name)

  enum_execute "ALTER TYPE #{current_name} RENAME TO #{new_name}"
end

#rename_enum_value(name, current_name, new_name) ⇒ Object

Rename a value within an enum type in the database.

Parameters:

  • name (Symbol)

    The enum’s name.

  • current_value (String|Symbol)

    The enum value’s current name.

  • new_value (String|Symbol)

    The enum value’s new name.



107
108
109
110
111
112
113
114
115
# File 'lib/enum_kit/active_record_extensions/connection_adapters/postgresql_adapter.rb', line 107

def rename_enum_value(name, current_name, new_name)
  ensure_renaming_enum_values_is_supported!

  name         = EnumKit.sanitize_name!(name)
  current_name = EnumKit.sanitize_value!(current_name)
  new_name     = EnumKit.sanitize_value!(new_name)

  enum_execute "ALTER TYPE #{name} RENAME VALUE #{EnumKit.sqlize(current_name)} TO #{EnumKit.sqlize(new_name)}"
end