enum_kit 0.2.1 → 0.2.2
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: a34de5e74d0221b789a2b6f032fbb9925ac5514d9543107346afdcaedb040c69
|
4
|
+
data.tar.gz: 0cce23fa1f546f4f23cdd7e7b124e7eb081a66ab599cbf2a16fe14295fe9cb49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de6d95058b1386969164de088d9f12d5ec69798d87696bc1ef20e8c88289856e1a25a9266b9489dd6618d3c95296870ffc971feea44f0c0ed27f403f199dded3
|
7
|
+
data.tar.gz: 5d47f9663a4249c66e7624753c14d7ae46c77f7da38cb068809a74ac7f6c0bb6b36be6b6b604e3367a56b2cd732cbdf2f7fec4ed4462d0e61351ceefdcc433b1
|
@@ -27,10 +27,16 @@ module EnumKit
|
|
27
27
|
ORDER BY pg_type.typname
|
28
28
|
SQL
|
29
29
|
|
30
|
+
# Clear the cached enums to force a refresh the next time they are accessed.
|
31
|
+
#
|
32
|
+
def clear_enum_cache!
|
33
|
+
@enums = nil
|
34
|
+
end
|
35
|
+
|
30
36
|
# @return [Hash] The enum types available in the database.
|
31
37
|
#
|
32
38
|
def enums
|
33
|
-
select_all(ENUM_QUERY.tr("\n", ' ').strip).each_with_object({}) do |row, enums|
|
39
|
+
@enums ||= select_all(ENUM_QUERY.tr("\n", ' ').strip).each_with_object({}) do |row, enums|
|
34
40
|
enums[row['typname'].to_sym] = row['values'].split("\t\t")
|
35
41
|
end
|
36
42
|
end
|
@@ -44,7 +50,7 @@ module EnumKit
|
|
44
50
|
name = EnumKit.sanitize_name!(name)
|
45
51
|
values = EnumKit.sanitize_values!(values)
|
46
52
|
|
47
|
-
|
53
|
+
enum_execute "CREATE TYPE #{name} AS ENUM #{EnumKit.sqlize(values)}"
|
48
54
|
end
|
49
55
|
|
50
56
|
# Rename an existing enum type.
|
@@ -56,7 +62,7 @@ module EnumKit
|
|
56
62
|
current_name = EnumKit.sanitize_name!(current_name)
|
57
63
|
new_name = EnumKit.sanitize_name!(new_name)
|
58
64
|
|
59
|
-
|
65
|
+
enum_execute "ALTER TYPE #{current_name} RENAME TO #{new_name}"
|
60
66
|
end
|
61
67
|
|
62
68
|
# Drop an existing enum type from the database.
|
@@ -66,7 +72,7 @@ module EnumKit
|
|
66
72
|
def drop_enum(name)
|
67
73
|
name = EnumKit.sanitize_name!(name)
|
68
74
|
|
69
|
-
|
75
|
+
enum_execute "DROP TYPE #{name}"
|
70
76
|
end
|
71
77
|
|
72
78
|
# Add a new value to an enum type in the database.
|
@@ -89,7 +95,7 @@ module EnumKit
|
|
89
95
|
statement += " AFTER #{EnumKit.sqlize(EnumKit.sanitize_value!(after))}" if after
|
90
96
|
statement += " BEFORE #{EnumKit.sqlize(EnumKit.sanitize_value!(before))}" if before
|
91
97
|
|
92
|
-
|
98
|
+
enum_execute(statement)
|
93
99
|
end
|
94
100
|
|
95
101
|
# Rename a value within an enum type in the database.
|
@@ -105,7 +111,7 @@ module EnumKit
|
|
105
111
|
current_name = EnumKit.sanitize_value!(current_name)
|
106
112
|
new_name = EnumKit.sanitize_value!(new_name)
|
107
113
|
|
108
|
-
|
114
|
+
enum_execute "ALTER TYPE #{name} RENAME VALUE #{EnumKit.sqlize(current_name)} TO #{EnumKit.sqlize(new_name)}"
|
109
115
|
end
|
110
116
|
|
111
117
|
# :nodoc:
|
@@ -129,6 +135,14 @@ module EnumKit
|
|
129
135
|
|
130
136
|
raise NotImplementedError, 'PostgreSQL 10.0+ is required to enable renaming of enum values.'
|
131
137
|
end
|
138
|
+
|
139
|
+
# Execute a SQL statement, then clear the enum cache.
|
140
|
+
#
|
141
|
+
def enum_execute(*args, &block)
|
142
|
+
result = execute(*args, &block)
|
143
|
+
clear_enum_cache!
|
144
|
+
result
|
145
|
+
end
|
132
146
|
end
|
133
147
|
end
|
134
148
|
end
|
data/lib/enum_kit/constants.rb
CHANGED
@@ -6,6 +6,9 @@ RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :unit do
|
|
6
6
|
before do
|
7
7
|
# Sanity check to ensure ActiveRecord is configured to use a PostgreSQL database.
|
8
8
|
expect(connection).to be_a(described_class)
|
9
|
+
|
10
|
+
# Clear the cached enums before each test to ensure clean tests.
|
11
|
+
connection.clear_enum_cache!
|
9
12
|
end
|
10
13
|
|
11
14
|
%i[create_enum rename_enum drop_enum add_enum_value rename_enum_value].each do |method|
|
@@ -14,6 +17,44 @@ RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :unit do
|
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
20
|
+
# TODO: Investigate possible alternatives as this test is a bit odd.
|
21
|
+
describe '#clear_enum_cache!' do
|
22
|
+
it 'sets @enums to nil' do
|
23
|
+
connection.instance_eval do
|
24
|
+
@enums = { sizes: ['small', 'medium', 'large', 'extra large'] }
|
25
|
+
end
|
26
|
+
|
27
|
+
connection.clear_enum_cache!
|
28
|
+
|
29
|
+
expect(connection.instance_eval { @enums }).to be nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#enums' do
|
34
|
+
before { connection.execute "CREATE TYPE sizes AS ENUM ('small', 'medium', 'large', 'extra large')" }
|
35
|
+
after { connection.execute 'DROP TYPE IF EXISTS sizes' }
|
36
|
+
|
37
|
+
subject { connection.enums }
|
38
|
+
|
39
|
+
it 'retrieves the enums' do
|
40
|
+
expect(subject).to include(sizes: ['small', 'medium', 'large', 'extra large'])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#enum_execute' do
|
45
|
+
subject { connection.enum_execute('SELECT 1=1') }
|
46
|
+
|
47
|
+
it 'invokes #execute with the specified arguments' do
|
48
|
+
expect(connection).to receive(:execute).with('SELECT 1=1').once
|
49
|
+
subject
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'invokes #clear_enum_cache!' do
|
53
|
+
expect(connection).to receive(:clear_enum_cache!).once
|
54
|
+
subject
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
17
58
|
describe '#create_enum' do
|
18
59
|
after { connection.execute 'DROP TYPE IF EXISTS sizes' }
|
19
60
|
|