activerecord-postgres_enum 0.5.1 → 1.2.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: 0dc8b2da12bdecab45218daa75f6f4a6e6d560e87c20f3c20024a268757e609b
4
- data.tar.gz: ee367a630606e4dd7902d310179156afe5213d568a2507ccfed7ad96f2b82544
3
+ metadata.gz: 65fc162010507d18b3607853f938bc02f2106ba56764d020943a241c00af3f07
4
+ data.tar.gz: 42eb21dd6b8441e8ca5589b38a30da8cbc646de28c89ecf58f09df5c92c67f54
5
5
  SHA512:
6
- metadata.gz: fc9bf477c7369bd0753f728cea8ec8ae147ac5834dce7282f4289d18cbb5e3148e28bae8d0d286328864351afee588c73ade4d1b2e5b66a23f0547384aa0b224
7
- data.tar.gz: b48a4817f9ff6cfbbe8e5b36538c9ae900b9dfacd2dd0c8463f6c86bee4fbfaa33431dfa48ef8af66cd794e641b80acecbe2552ee78760ce185722e1128ad75c
6
+ metadata.gz: 753abb413ecf0ee2a32e68c3bf1be93c3678c75b1b578a9c3177426bb31a8bbeff9010dc7550f66a2b710394ca97eb11e17eaeedbd17d1f6f0ddb7ddd1ce5799
7
+ data.tar.gz: 76200a6b37e19b00066a133d5ae377f9a3396681ac7bda6031b1cb89a1c0316ea9ae2c1de69c0f0ea1eb0fab196abad4cd7f0be23f7e04ae0fb34de614a51216
data/README.md CHANGED
@@ -53,15 +53,36 @@ rename_enum :mood, :emotions
53
53
  To add a value into existing enum:
54
54
 
55
55
  ```ruby
56
- add_enum_value :mood, "crazy"
56
+ add_enum_value :mood, "pensive"
57
+ ```
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
+
67
+ To add a new enum column to an existing table:
68
+
69
+ ```ruby
70
+ def change
71
+ create_enum :product_type, %w[one-off subscription]
72
+
73
+ add_column :products, :type, :product_type
74
+ end
57
75
  ```
58
76
 
59
77
  To rename a value:
60
78
 
61
79
  ```ruby
62
- rename_enum_value :mood, "crazy", "nutty"
80
+ rename_enum_value :mood, "pensive", "wistful"
63
81
  ```
64
82
 
83
+ **NB:** To stop Postgres complaining about adding enum values inside a transaction, use [`disable_ddl_transaction!`](https://api.rubyonrails.org/classes/ActiveRecord/Migration.html#method-c-disable_ddl_transaction-21) in your migration.
84
+
85
+
65
86
  ## Development
66
87
 
67
88
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -9,6 +9,7 @@ require "active_support/lazy_load_hooks"
9
9
  require "active_record/postgres_enum/version"
10
10
  require "active_record/postgres_enum/postgresql_adapter"
11
11
  require "active_record/postgres_enum/schema_dumper"
12
+ require "active_record/postgres_enum/command_recorder"
12
13
  require "active_record/postgres_enum/enum_validator"
13
14
 
14
15
  ActiveSupport.on_load(:active_record) do
@@ -33,4 +34,6 @@ ActiveSupport.on_load(:active_record) do
33
34
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
34
35
  name: 'enum'
35
36
  }
37
+
38
+ ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
36
39
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ module CommandRecorder
6
+ def create_enum(name, values)
7
+ record(:create_enum, [name, values])
8
+ end
9
+
10
+ def invert_create_enum(args)
11
+ [:drop_enum, args.first]
12
+ end
13
+
14
+ def rename_enum(name, new_name)
15
+ record(:rename_enum, [name, new_name])
16
+ end
17
+
18
+ def invert_rename_enum(args)
19
+ [:rename_enum, args.reverse]
20
+ end
21
+
22
+ def rename_enum_value(name, existing_value, new_value)
23
+ record(:rename_enum_value, [name, existing_value, new_value])
24
+ end
25
+
26
+ def invert_rename_enum_value(args)
27
+ [:rename_enum_value, [args.first] + args.last(2).reverse]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -7,9 +7,9 @@ module ActiveRecord
7
7
  class Enum < Type::Value # :nodoc:
8
8
  attr_reader :enum_name
9
9
 
10
- def initialize(options = {})
11
- @enum_name = options.delete(:enum_name).to_sym
12
- super
10
+ def initialize(enum_name:, **kwargs)
11
+ @enum_name = enum_name.to_sym
12
+ super(**kwargs)
13
13
  end
14
14
  end
15
15
 
@@ -24,7 +24,11 @@ module ActiveRecord
24
24
  end
25
25
 
26
26
  DEFINED_ENUMS_QUERY = <<~SQL
27
- SELECT t.OID, t.typname, t.typtype, array_agg(e.enumlabel) as enumlabels
27
+ SELECT
28
+ t.OID,
29
+ t.typname,
30
+ t.typtype,
31
+ array_to_string(array_agg(e.enumlabel ORDER BY e.enumsortorder), '\t\t', '') as enumlabels
28
32
  FROM pg_type t
29
33
  LEFT JOIN pg_enum e ON e.enumtypid = t.oid
30
34
  WHERE typtype = 'e'
@@ -34,12 +38,12 @@ module ActiveRecord
34
38
 
35
39
  def enums
36
40
  select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
37
- memo[row["typname"].to_sym] = row['enumlabels'].gsub(/[{}]/, '').gsub('NULL', '').split(',').sort
41
+ memo[row["typname"].to_sym] = row['enumlabels'].split("\t\t")
38
42
  end
39
43
  end
40
44
 
41
45
  def create_enum(name, values)
42
- values = values.map { |v| "'#{v}'" }
46
+ values = values.map { |v| quote v }
43
47
  execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
44
48
  end
45
49
 
@@ -51,14 +55,31 @@ module ActiveRecord
51
55
  execute "ALTER TYPE #{name} RENAME TO #{new_name}"
52
56
  end
53
57
 
54
- def add_enum_value(name, value)
55
- execute "ALTER TYPE #{name} ADD VALUE '#{value}'"
58
+ def add_enum_value(name, value, after: nil, before: nil, if_not_exists: nil)
59
+ if_not_exists_statement = 'IF NOT EXISTS' if if_not_exists
60
+
61
+ sql = "ALTER TYPE #{name} ADD VALUE #{if_not_exists_statement} #{quote value}"
62
+ if after
63
+ sql += " AFTER #{quote after}"
64
+ elsif before
65
+ sql += " BEFORE #{quote before}"
66
+ end
67
+ execute sql
68
+ end
69
+
70
+ def remove_enum_value(name, value)
71
+ sql = %{
72
+ DELETE FROM pg_enum
73
+ WHERE enumlabel=#{quote value}
74
+ AND enumtypid=(SELECT oid FROM pg_type WHERE typname='#{name}')
75
+ }
76
+ execute sql
56
77
  end
57
78
 
58
79
  def rename_enum_value(name, existing_value, new_value)
59
80
  raise "Renaming enum values is only supported in PostgreSQL 10.0+" unless rename_enum_value_supported?
60
81
 
61
- execute "ALTER TYPE #{name} RENAME VALUE '#{existing_value}' TO '#{new_value}'"
82
+ execute "ALTER TYPE #{name} RENAME VALUE #{quote existing_value} TO #{quote new_value}"
62
83
  end
63
84
 
64
85
  def migration_keys
@@ -16,8 +16,8 @@ module ActiveRecord
16
16
  statements = []
17
17
 
18
18
  @connection.enums.each do |name, values|
19
- values = values.map(&:inspect).join(', ')
20
- statements << " create_enum #{name.inspect}, [#{values}]"
19
+ values = values.map { |v| " #{v.inspect}," }.join("\n")
20
+ statements << " create_enum #{name.inspect}, [\n#{values}\n ]"
21
21
  end
22
22
 
23
23
  stream.puts statements.join("\n")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "0.5.1"
5
+ VERSION = "1.2.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: 0.5.1
4
+ version: 1.2.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: 2019-02-12 00:00:00.000000000 Z
11
+ date: 2020-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,14 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '5'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.1'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: 4.2.0
29
+ version: '5'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.1'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: pg
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +78,14 @@ dependencies:
72
78
  requirements:
73
79
  - - "~>"
74
80
  - !ruby/object:Gem::Version
75
- version: '1.0'
81
+ version: '1.1'
76
82
  type: :development
77
83
  prerelease: false
78
84
  version_requirements: !ruby/object:Gem::Requirement
79
85
  requirements:
80
86
  - - "~>"
81
87
  - !ruby/object:Gem::Version
82
- version: '1.0'
88
+ version: '1.1'
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: pry-byebug
85
91
  requirement: !ruby/object:Gem::Requirement
@@ -100,42 +106,28 @@ dependencies:
100
106
  requirements:
101
107
  - - "~>"
102
108
  - !ruby/object:Gem::Version
103
- version: '10.0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '10.0'
111
- - !ruby/object:Gem::Dependency
112
- name: rspec
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '3.0'
109
+ version: '13.0'
118
110
  type: :development
119
111
  prerelease: false
120
112
  version_requirements: !ruby/object:Gem::Requirement
121
113
  requirements:
122
114
  - - "~>"
123
115
  - !ruby/object:Gem::Version
124
- version: '3.0'
116
+ version: '13.0'
125
117
  - !ruby/object:Gem::Dependency
126
118
  name: rspec-rails
127
119
  requirement: !ruby/object:Gem::Requirement
128
120
  requirements:
129
121
  - - "~>"
130
122
  - !ruby/object:Gem::Version
131
- version: '3.0'
123
+ version: '3.8'
132
124
  type: :development
133
125
  prerelease: false
134
126
  version_requirements: !ruby/object:Gem::Requirement
135
127
  requirements:
136
128
  - - "~>"
137
129
  - !ruby/object:Gem::Version
138
- version: '3.0'
130
+ version: '3.8'
139
131
  - !ruby/object:Gem::Dependency
140
132
  name: rubocop
141
133
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +152,7 @@ files:
160
152
  - LICENSE.txt
161
153
  - README.md
162
154
  - lib/active_record/postgres_enum.rb
155
+ - lib/active_record/postgres_enum/command_recorder.rb
163
156
  - lib/active_record/postgres_enum/enum_validator.rb
164
157
  - lib/active_record/postgres_enum/extensions.rb
165
158
  - lib/active_record/postgres_enum/postgresql_adapter.rb
@@ -186,8 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
179
  - !ruby/object:Gem::Version
187
180
  version: '0'
188
181
  requirements: []
189
- rubyforge_project:
190
- rubygems_version: 2.7.6
182
+ rubygems_version: 3.1.2
191
183
  signing_key:
192
184
  specification_version: 4
193
185
  summary: Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.