activerecord-postgres_enum 1.1.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11be6863582ca701d4d5d01e48fef1c95c6654c5889d6c82823e7eeb7e548667
4
- data.tar.gz: c7324a3933d4345cb222d3378dcd74815a7860ec5d4c90e01bf4cb213c986b61
3
+ metadata.gz: 9e3664d257be105bae903d0b4183683768247f9a441afbcd6afc9290036ed8a5
4
+ data.tar.gz: ec213e9dbc12ed859ef762c3f8ef1982e0eb072da67d1d11e921d0e17f8bd560
5
5
  SHA512:
6
- metadata.gz: 4a65ec7099464c6f1fd5863efdad17316702cc4a0427c2ed6008a42e059f72c22d094b79a564c6880da5072c3cd3f0db3bd36ddf4bde1e8a847fa5391623c1e9
7
- data.tar.gz: '099ee04842e690d703ff3e859067c5b07c22bc52a03166caedce11f03060365b11f6140d7e614d83cd692eef8b4a8bc06f3ace730d17a68a5913953d91c5c11a'
6
+ metadata.gz: 7f6dd1f45d8168aed7199e1e0d8381be40e9a745c421aca1e1f39bf4f2d45400e943d4ed955acf12ed9f489a70662b8b126b2a0b9ef6ceef98b96115227cf08f
7
+ data.tar.gz: aaaad9f196e5ff222481bed5c732d596e6474566b8508e171e8fc5c6c6647b4d6c0d0a8f76060f7c6633004244d59e01689d68c7f2ce1bd84f9dfabb4e443cae
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/activerecord-postgres_enum.svg)](https://badge.fury.io/rb/activerecord-postgres_enum)
2
- [![Build Status](https://travis-ci.org/bibendi/activerecord-postgres_enum.svg?branch=master)](https://travis-ci.org/bibendi/activerecord-postgres_enum)
2
+ [![Build Status](https://github.com/bibendi/activerecord-postgres_enum/workflows/Ruby/badge.svg?branch=master)](https://github.com/bibendi/activerecord-postgres_enum/actions?query=branch%3Amaster)
3
3
 
4
4
  # ActiveRecord::PostgresEnum
5
5
 
@@ -56,6 +56,14 @@ To add a value into existing enum:
56
56
  add_enum_value :mood, "pensive"
57
57
  ```
58
58
 
59
+ To remove a value from existing enum:
60
+
61
+ > :warning: Make sure that value is not used anywhere in the database.
62
+
63
+ ```ruby
64
+ remove_enum_value :mood, "pensive"
65
+ ```
66
+
59
67
  To add a new enum column to an existing table:
60
68
 
61
69
  ```ruby
@@ -32,7 +32,7 @@ ActiveSupport.on_load(:active_record) do
32
32
  end
33
33
 
34
34
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
35
- name: 'enum'
35
+ name: "enum"
36
36
  }
37
37
 
38
38
  ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
@@ -16,15 +16,15 @@ module ActiveRecord
16
16
  class TypeMapInitializer
17
17
  # We need to know the column name, and the default implementation discards it
18
18
  def register_enum_type(row)
19
- register row['oid'], OID::Enum.new(enum_name: row['typname'])
19
+ register row["oid"], OID::Enum.new(enum_name: row["typname"])
20
20
  end
21
21
  end
22
22
  end
23
23
 
24
24
  module ColumnMethods # :nodoc:
25
25
  # Enables `t.enum :my_field, enum_name: :my_enum_name` on migrations
26
- def enum(name, options = {})
27
- column(name, options.delete(:enum_name), options.except(:enum_name))
26
+ def enum(name, enum_name:, **options)
27
+ column(name, enum_name, **options)
28
28
  end
29
29
  end
30
30
  end
@@ -38,25 +38,35 @@ module ActiveRecord
38
38
 
39
39
  def enums
40
40
  select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
41
- memo[row["typname"].to_sym] = row['enumlabels'].split("\t\t")
41
+ memo[row["typname"].to_sym] = row["enumlabels"].split("\t\t")
42
42
  end
43
43
  end
44
44
 
45
- def create_enum(name, values)
45
+ def create_enum(name, values, force: false, if_not_exists: nil)
46
+ return if if_not_exists && enums.include?(name.to_sym)
47
+
48
+ drop_enum(name, cascade: force == :cascade, if_exists: true) if force
49
+
46
50
  values = values.map { |v| quote v }
47
- execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
51
+ execute "CREATE TYPE #{name} AS ENUM (#{values.join(", ")})"
48
52
  end
49
53
 
50
- def drop_enum(name)
51
- execute "DROP TYPE #{name}"
54
+ def drop_enum(name, cascade: nil, if_exists: nil)
55
+ if_exists_statement = "IF EXISTS" if if_exists
56
+ cascade_statement = "CASCADE" if cascade
57
+
58
+ sql = "DROP TYPE #{if_exists_statement} #{name} #{cascade_statement}"
59
+ execute sql
52
60
  end
53
61
 
54
62
  def rename_enum(name, new_name)
55
63
  execute "ALTER TYPE #{name} RENAME TO #{new_name}"
56
64
  end
57
65
 
58
- def add_enum_value(name, value, after: nil, before: nil)
59
- sql = "ALTER TYPE #{name} ADD VALUE #{quote value}"
66
+ def add_enum_value(name, value, after: nil, before: nil, if_not_exists: nil)
67
+ if_not_exists_statement = "IF NOT EXISTS" if if_not_exists
68
+
69
+ sql = "ALTER TYPE #{name} ADD VALUE #{if_not_exists_statement} #{quote value}"
60
70
  if after
61
71
  sql += " AFTER #{quote after}"
62
72
  elsif before
@@ -65,6 +75,15 @@ module ActiveRecord
65
75
  execute sql
66
76
  end
67
77
 
78
+ def remove_enum_value(name, value)
79
+ sql = %{
80
+ DELETE FROM pg_enum
81
+ WHERE enumlabel=#{quote value}
82
+ AND enumtypid=(SELECT oid FROM pg_type WHERE typname='#{name}')
83
+ }
84
+ execute sql
85
+ end
86
+
68
87
  def rename_enum_value(name, existing_value, new_value)
69
88
  raise "Renaming enum values is only supported in PostgreSQL 10.0+" unless rename_enum_value_supported?
70
89
 
@@ -17,10 +17,10 @@ module ActiveRecord
17
17
 
18
18
  @connection.enums.each do |name, values|
19
19
  values = values.map { |v| " #{v.inspect}," }.join("\n")
20
- statements << " create_enum #{name.inspect}, [\n#{values}\n ]"
20
+ statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
21
21
  end
22
22
 
23
- stream.puts statements.join("\n")
23
+ stream.puts statements.join("\n\n")
24
24
  stream.puts
25
25
  end
26
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "1.1.0"
5
+ VERSION = "1.6.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Merkushin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-19 00:00:00.000000000 Z
11
+ date: 2021-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.1'
22
+ version: '6.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.1'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: pg
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -120,28 +120,28 @@ dependencies:
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '3.8'
123
+ version: '4.0'
124
124
  type: :development
125
125
  prerelease: false
126
126
  version_requirements: !ruby/object:Gem::Requirement
127
127
  requirements:
128
128
  - - "~>"
129
129
  - !ruby/object:Gem::Version
130
- version: '3.8'
130
+ version: '4.0'
131
131
  - !ruby/object:Gem::Dependency
132
- name: rubocop
132
+ name: standard
133
133
  requirement: !ruby/object:Gem::Requirement
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: '0.58'
137
+ version: '0.10'
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '0.58'
144
+ version: '0.10'
145
145
  description:
146
146
  email:
147
147
  - merkushin.m.s@gmail.com
@@ -172,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
172
  requirements:
173
173
  - - ">="
174
174
  - !ruby/object:Gem::Version
175
- version: '0'
175
+ version: '2.5'
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - ">="