activerecord-postgres_enum 0.5.0 → 1.1.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: 6a1b82a6fcd24c4c9811b824bbe1dc8db8043dc7285a6481a029d9b88fe92410
4
- data.tar.gz: f7989568c9a5db1a33fac706e4baf98ffdbde8b6d23f9625182def8b1370b19f
3
+ metadata.gz: 11be6863582ca701d4d5d01e48fef1c95c6654c5889d6c82823e7eeb7e548667
4
+ data.tar.gz: c7324a3933d4345cb222d3378dcd74815a7860ec5d4c90e01bf4cb213c986b61
5
5
  SHA512:
6
- metadata.gz: ce4d78fdc5af2ed94e53c749f365a1c1ef9c5a1a43c5aa2a56861db99381ee991749f6e51d5114182058c0b9936eef6a96cbbb390cda7b05760169312244cc57
7
- data.tar.gz: aaed102da13bc6f8ccd8bd5994ea9d6c9ec88ff72cafd537ea225126f9ccb29ab570e5ab7958eecb747365f67b7d870386c1642264cfedec10a3887dd501c6b3
6
+ metadata.gz: 4a65ec7099464c6f1fd5863efdad17316702cc4a0427c2ed6008a42e059f72c22d094b79a564c6880da5072c3cd3f0db3bd36ddf4bde1e8a847fa5391623c1e9
7
+ data.tar.gz: '099ee04842e690d703ff3e859067c5b07c22bc52a03166caedce11f03060365b11f6140d7e614d83cd692eef8b4a8bc06f3ace730d17a68a5913953d91c5c11a'
data/README.md CHANGED
@@ -53,15 +53,28 @@ 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 add a new enum column to an existing table:
60
+
61
+ ```ruby
62
+ def change
63
+ create_enum :product_type, %w[one-off subscription]
64
+
65
+ add_column :products, :type, :product_type
66
+ end
57
67
  ```
58
68
 
59
69
  To rename a value:
60
70
 
61
71
  ```ruby
62
- rename_enum_value :mood, "crazy", "nutty"
72
+ rename_enum_value :mood, "pensive", "wistful"
63
73
  ```
64
74
 
75
+ **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.
76
+
77
+
65
78
  ## Development
66
79
 
67
80
  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,20 @@ 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)
59
+ sql = "ALTER TYPE #{name} ADD VALUE #{quote value}"
60
+ if after
61
+ sql += " AFTER #{quote after}"
62
+ elsif before
63
+ sql += " BEFORE #{quote before}"
64
+ end
65
+ execute sql
56
66
  end
57
67
 
58
68
  def rename_enum_value(name, existing_value, new_value)
59
69
  raise "Renaming enum values is only supported in PostgreSQL 10.0+" unless rename_enum_value_supported?
60
70
 
61
- execute "ALTER TYPE #{name} RENAME VALUE '#{existing_value}' TO '#{new_value}'"
71
+ execute "ALTER TYPE #{name} RENAME VALUE #{quote existing_value} TO #{quote new_value}"
62
72
  end
63
73
 
64
74
  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}', [#{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.0"
5
+ VERSION = "1.1.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.0
4
+ version: 1.1.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-06-19 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.