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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38f91da5be0b65f63986102e8c44d6d77980e3b232fa6b9abfffa8702a874f40
|
4
|
+
data.tar.gz: 248ad3810cca88eb9c5b1caa6de339d439d821faea6bd8377b51092483b13573
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
56
|
+
add_enum_value :mood, "crazy"
|
51
57
|
```
|
52
58
|
|
53
|
-
|
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
|
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
|
-
|
9
|
-
|
8
|
+
dump_enums(stream)
|
9
|
+
|
10
|
+
super
|
10
11
|
end
|
11
12
|
|
12
13
|
private
|
13
14
|
|
14
|
-
def
|
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
|
-
|
33
|
-
|
34
|
-
|
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")
|
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.
|
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-
|
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
|