activerecord-postgres_enum 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -2
- data/lib/active_record/postgres_enum.rb +16 -0
- data/lib/active_record/postgres_enum/command_recorder.rb +31 -0
- data/lib/active_record/postgres_enum/extensions.rb +3 -3
- data/lib/active_record/postgres_enum/postgresql_adapter.rb +36 -6
- data/lib/active_record/postgres_enum/schema_dumper.rb +2 -2
- data/lib/active_record/postgres_enum/version.rb +1 -1
- metadata +18 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e97d08a739660755211e03868a76e345eba7546be13b374619a644dfa048ab11
|
4
|
+
data.tar.gz: d37271b37da014e68ad570a071e8841e3e851552d001084ac5668b4bd43b58f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1486eeb1f2621cc0f2625fffb52ccbb56c272c95238587a93a7ced95ec743aeb333ee441cfc566d9a6d346e896ef3300dbef8f5348632d0b9e3c527adedbcb4
|
7
|
+
data.tar.gz: 338a9914209f6797286e6e90658a867682289909539cadb4c3d8e4de5cf7ebd6ad6d803c2e1ed7e197f0fbe0e2134c8dc9542da937708d5640bcaf6c5930a2a5
|
data/README.md
CHANGED
@@ -53,13 +53,15 @@ rename_enum :mood, :emotions
|
|
53
53
|
To add a value into existing enum:
|
54
54
|
|
55
55
|
```ruby
|
56
|
-
add_enum_value :mood, "
|
56
|
+
add_enum_value :mood, "pensive"
|
57
57
|
```
|
58
58
|
|
59
|
+
**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.
|
60
|
+
|
59
61
|
To rename a value:
|
60
62
|
|
61
63
|
```ruby
|
62
|
-
rename_enum_value :mood, "
|
64
|
+
rename_enum_value :mood, "pensive", "wistful"
|
63
65
|
```
|
64
66
|
|
65
67
|
## Development
|
@@ -9,15 +9,31 @@ 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
|
15
16
|
require "active_record/postgres_enum/extensions"
|
16
17
|
|
17
18
|
ActiveRecord::SchemaDumper.prepend ActiveRecord::PostgresEnum::SchemaDumper
|
19
|
+
|
18
20
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend ActiveRecord::PostgresEnum::PostgreSQLAdapter
|
19
21
|
|
22
|
+
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper)
|
23
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper.prepend(
|
24
|
+
ActiveRecord::PostgresEnum::PostgreSQLAdapter::ColumnDumperExt
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper)
|
29
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(
|
30
|
+
ActiveRecord::PostgresEnum::PostgreSQLAdapter::SchemaDumperExt
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
20
34
|
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
|
21
35
|
name: 'enum'
|
22
36
|
}
|
37
|
+
|
38
|
+
ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
|
23
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(
|
11
|
-
@enum_name =
|
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
|
|
@@ -3,8 +3,32 @@
|
|
3
3
|
module ActiveRecord
|
4
4
|
module PostgresEnum
|
5
5
|
module PostgreSQLAdapter
|
6
|
+
# For Rails >= 5.2
|
7
|
+
# https://github.com/rails/rails/blob/5-2-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb
|
8
|
+
module SchemaDumperExt
|
9
|
+
def prepare_column_options(column)
|
10
|
+
spec = super
|
11
|
+
spec[:enum_name] = column.sql_type.inspect if column.type == :enum
|
12
|
+
spec
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# For Rails <5.2
|
17
|
+
# https://github.com/rails/rails/blob/5-1-stable/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb
|
18
|
+
module ColumnDumperExt
|
19
|
+
def prepare_column_options(column)
|
20
|
+
spec = super
|
21
|
+
spec[:enum_name] = column.sql_type.inspect if column.type == :enum
|
22
|
+
spec
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
6
26
|
DEFINED_ENUMS_QUERY = <<~SQL
|
7
|
-
SELECT
|
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
|
8
32
|
FROM pg_type t
|
9
33
|
LEFT JOIN pg_enum e ON e.enumtypid = t.oid
|
10
34
|
WHERE typtype = 'e'
|
@@ -14,12 +38,12 @@ module ActiveRecord
|
|
14
38
|
|
15
39
|
def enums
|
16
40
|
select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
|
17
|
-
memo[row["typname"].to_sym] = row['enumlabels'].
|
41
|
+
memo[row["typname"].to_sym] = row['enumlabels'].split("\t\t")
|
18
42
|
end
|
19
43
|
end
|
20
44
|
|
21
45
|
def create_enum(name, values)
|
22
|
-
values = values.map { |v|
|
46
|
+
values = values.map { |v| quote v }
|
23
47
|
execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
|
24
48
|
end
|
25
49
|
|
@@ -31,14 +55,20 @@ module ActiveRecord
|
|
31
55
|
execute "ALTER TYPE #{name} RENAME TO #{new_name}"
|
32
56
|
end
|
33
57
|
|
34
|
-
def add_enum_value(name, value)
|
35
|
-
|
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
|
36
66
|
end
|
37
67
|
|
38
68
|
def rename_enum_value(name, existing_value, new_value)
|
39
69
|
raise "Renaming enum values is only supported in PostgreSQL 10.0+" unless rename_enum_value_supported?
|
40
70
|
|
41
|
-
execute "ALTER TYPE #{name} RENAME VALUE
|
71
|
+
execute "ALTER TYPE #{name} RENAME VALUE #{quote existing_value} TO #{quote new_value}"
|
42
72
|
end
|
43
73
|
|
44
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
|
20
|
-
statements << " create_enum
|
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")
|
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.
|
4
|
+
version: 1.0.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:
|
11
|
+
date: 2020-05-29 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:
|
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:
|
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.
|
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.
|
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: '
|
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: '
|
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.
|
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.
|
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
|
-
|
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.
|