activerecord-postgres_enum 0.1.0 → 0.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: 0d34e14ab6cb6bb3067632c5ea90dfa62a9e23be88330c30cb118ffe4f70e55d
4
- data.tar.gz: 7b6dd1a8dbe8e7e2b08b616ca15894eb72e2040f7f0d691f02b3452d37a08542
3
+ metadata.gz: 38f91da5be0b65f63986102e8c44d6d77980e3b232fa6b9abfffa8702a874f40
4
+ data.tar.gz: 248ad3810cca88eb9c5b1caa6de339d439d821faea6bd8377b51092483b13573
5
5
  SHA512:
6
- metadata.gz: 19e8a1a1a6b55cca9ae200356c6bcff134809e1d17a3e3b37c0269a482a0f263925ae72ea467ae897133aa0d5e718536b8a693488716f45c9b1c8f5d68fa573e
7
- data.tar.gz: 98cc761dec0bfa6248da5135e767d23ea54bcf36c91b604e0eee71c003f22cf79c32273a9bb3e9ec97a8afdc692389a7610665479356189484d569a2a9aa9a8c
6
+ metadata.gz: 3d29a3a97a34ea5c2b82f08470a5689b837be302560dda5923127bf570603574e2a0d2377ef49c8c7e93af4f7f98e5bbc7adcd8f6bcafd46b17fd71ebab9f60b
7
+ data.tar.gz: 253db867e758c6560b7893a09084b209e585cf53e388bf0a8a9050af8b3c38edf217364b4ee0c9c745877e2778cbd7cf7dc3ba167339b609cccc88d98dd2a6f1
data/README.md CHANGED
@@ -44,13 +44,23 @@ To drop an existing enum:
44
44
  drop_enum :mood
45
45
  ```
46
46
 
47
+ To rename an existing enum:
48
+
49
+ ```ruby
50
+ rename_enum :mood, :emotions
51
+ ```
52
+
47
53
  To add a value into existing enum:
48
54
 
49
55
  ```ruby
50
- alter_enum :mood, "crazy"
56
+ add_enum_value :mood, "crazy"
51
57
  ```
52
58
 
53
- Renaming / updating enums is currently not supported, since it's quite tricky.
59
+ To rename a value:
60
+
61
+ ```ruby
62
+ rename_enum_value :mood, "crazy", "nutty"
63
+ ```
54
64
 
55
65
  ## Development
56
66
 
@@ -3,6 +3,21 @@
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
5
  module PostgreSQLAdapter
6
+ DEFINED_ENUMS_QUERY = <<~SQL
7
+ SELECT t.OID, t.typname, t.typtype, array_agg(e.enumlabel) as enumlabels
8
+ FROM pg_type t
9
+ INNER JOIN pg_enum e ON e.enumtypid = t.oid
10
+ WHERE typtype = 'e'
11
+ GROUP BY t.OID, t.typname, t.typtype
12
+ ORDER BY t.typname
13
+ SQL
14
+
15
+ def enums
16
+ select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
17
+ memo[row["typname"].to_sym] = row['enumlabels'].gsub(/[{}]/, '').split(',')
18
+ end
19
+ end
20
+
6
21
  def create_enum(name, values)
7
22
  values = values.map { |v| "'#{v}'" }
8
23
  execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
@@ -12,10 +27,18 @@ module ActiveRecord
12
27
  execute "DROP TYPE #{name}"
13
28
  end
14
29
 
15
- def alter_enum(name, value)
30
+ def rename_enum(name, new_name)
31
+ execute "ALTER TYPE #{name} RENAME TO #{new_name}"
32
+ end
33
+
34
+ def add_enum_value(name, value)
16
35
  execute "ALTER TYPE #{name} ADD VALUE '#{value}'"
17
36
  end
18
37
 
38
+ def rename_enum_value(name, existing_value, new_value)
39
+ execute "ALTER TYPE #{name} RENAME VALUE '#{existing_value}' TO '#{new_value}'"
40
+ end
41
+
19
42
  def migration_keys
20
43
  super + [:enum_name]
21
44
  end
@@ -5,34 +5,19 @@ module ActiveRecord
5
5
  # provide support for writing out the 'create_enum' calls in schema.rb
6
6
  module SchemaDumper
7
7
  def tables(stream)
8
- enums(stream)
9
- super(stream)
8
+ dump_enums(stream)
9
+
10
+ super
10
11
  end
11
12
 
12
13
  private
13
14
 
14
- def defined_enums
15
- query = <<~SQL
16
- SELECT t.OID, t.typname, t.typtype, array_agg(e.enumlabel) as enumlabels
17
- FROM pg_type t
18
- INNER JOIN pg_enum e ON e.enumtypid = t.oid
19
- WHERE typtype = 'e'
20
- GROUP BY t.OID, t.typname, t.typtype
21
- ORDER BY t.typname
22
- SQL
23
-
24
- @connection.select_all(query).each do |enum|
25
- enum['enumlabels'] = enum['enumlabels'].gsub!(/[{}]/, '').split(',')
26
- end
27
- end
28
-
29
- def enums(stream)
15
+ def dump_enums(stream)
30
16
  statements = []
31
17
 
32
- defined_enums.each do |enum|
33
- enum_name = enum['typname']
34
- values = enum['enumlabels'].map(&:inspect).join(', ')
35
- statements << " create_enum(#{enum_name.inspect}, [#{values}])"
18
+ @connection.enums.each do |name, values|
19
+ values = values.map(&:inspect).join(', ')
20
+ statements << " create_enum('#{name}', [#{values}])"
36
21
  end
37
22
 
38
23
  stream.puts statements.join("\n")
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-postgres_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.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: 2018-10-30 00:00:00.000000000 Z
11
+ date: 2018-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.2.0
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '5.2'
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
29
  version: 4.2.0
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '5.2'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: pg
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +44,20 @@ dependencies:
38
44
  - - "<"
39
45
  - !ruby/object:Gem::Version
40
46
  version: '1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: appraisal
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.2'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.2'
41
61
  - !ruby/object:Gem::Dependency
42
62
  name: bundler
43
63
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +72,20 @@ dependencies:
52
72
  - - "~>"
53
73
  - !ruby/object:Gem::Version
54
74
  version: '1.16'
75
+ - !ruby/object:Gem::Dependency
76
+ name: combustion
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.0'
55
89
  - !ruby/object:Gem::Dependency
56
90
  name: pry-byebug
57
91
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +128,20 @@ dependencies:
94
128
  - - "~>"
95
129
  - !ruby/object:Gem::Version
96
130
  version: '3.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rspec-rails
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '3.0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '3.0'
97
145
  - !ruby/object:Gem::Dependency
98
146
  name: rubocop
99
147
  requirement: !ruby/object:Gem::Requirement