activerecord-postgres_enum 1.0.0 → 1.5.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: e97d08a739660755211e03868a76e345eba7546be13b374619a644dfa048ab11
4
- data.tar.gz: d37271b37da014e68ad570a071e8841e3e851552d001084ac5668b4bd43b58f6
3
+ metadata.gz: ac2154f798abecd00d17ad2ddb22307b9567113d10bb6e46b93ac2696a166afe
4
+ data.tar.gz: 75925162b95e2e293bf6f6834dbc05bea188ad40b35aac7d7c5b3f0ac36caf59
5
5
  SHA512:
6
- metadata.gz: b1486eeb1f2621cc0f2625fffb52ccbb56c272c95238587a93a7ced95ec743aeb333ee441cfc566d9a6d346e896ef3300dbef8f5348632d0b9e3c527adedbcb4
7
- data.tar.gz: 338a9914209f6797286e6e90658a867682289909539cadb4c3d8e4de5cf7ebd6ad6d803c2e1ed7e197f0fbe0e2134c8dc9542da937708d5640bcaf6c5930a2a5
6
+ metadata.gz: 8a8599019f7412e927bce0a25c5ae26de5a7b0e197a7a4c5da54f53edcb888e4bb6b7e0414d20f461fa61b2f33f2927e7edd67542eb8e968ed4766d25a15a811
7
+ data.tar.gz: 1042bdcdd209e900527348265f906da95773635053118806ea7107f2496f6da39c3b885024d28b7fdcb292fb0868a11ed1aa277e37e9aeea8cab4389ba835599
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/activerecord-postgres_enum.svg)](https://badge.fury.io/rb/activerecord-postgres_enum)
2
- [![Build Status](https://travis-ci.org/bibendi/activerecord-postgres_enum.svg?branch=master)](https://travis-ci.org/bibendi/activerecord-postgres_enum)
2
+ [![Build Status](https://github.com/bibendi/activerecord-postgres_enum/workflows/Ruby/badge.svg?branch=master)](https://github.com/bibendi/activerecord-postgres_enum/actions?query=branch%3Amaster)
3
3
 
4
4
  # ActiveRecord::PostgresEnum
5
5
 
@@ -56,7 +56,23 @@ To add a value into existing enum:
56
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.
59
+ To remove a value from existing enum:
60
+
61
+ > :warning: Make sure that value is not used anywhere in the database.
62
+
63
+ ```ruby
64
+ remove_enum_value :mood, "pensive"
65
+ ```
66
+
67
+ To add a new enum column to an existing table:
68
+
69
+ ```ruby
70
+ def change
71
+ create_enum :product_type, %w[one-off subscription]
72
+
73
+ add_column :products, :type, :product_type
74
+ end
75
+ ```
60
76
 
61
77
  To rename a value:
62
78
 
@@ -64,6 +80,9 @@ To rename a value:
64
80
  rename_enum_value :mood, "pensive", "wistful"
65
81
  ```
66
82
 
83
+ **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.
84
+
85
+
67
86
  ## Development
68
87
 
69
88
  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.
@@ -32,7 +32,7 @@ ActiveSupport.on_load(:active_record) do
32
32
  end
33
33
 
34
34
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
35
- name: 'enum'
35
+ name: "enum"
36
36
  }
37
37
 
38
38
  ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
@@ -16,7 +16,7 @@ module ActiveRecord
16
16
  class TypeMapInitializer
17
17
  # We need to know the column name, and the default implementation discards it
18
18
  def register_enum_type(row)
19
- register row['oid'], OID::Enum.new(enum_name: row['typname'])
19
+ register row["oid"], OID::Enum.new(enum_name: row["typname"])
20
20
  end
21
21
  end
22
22
  end
@@ -38,25 +38,35 @@ module ActiveRecord
38
38
 
39
39
  def enums
40
40
  select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
41
- memo[row["typname"].to_sym] = row['enumlabels'].split("\t\t")
41
+ memo[row["typname"].to_sym] = row["enumlabels"].split("\t\t")
42
42
  end
43
43
  end
44
44
 
45
- def create_enum(name, values)
45
+ def create_enum(name, values, force: false, if_not_exists: nil)
46
+ return if if_not_exists && enums.include?(name.to_sym)
47
+
48
+ drop_enum(name, cascade: force == :cascade, if_exists: true) if force
49
+
46
50
  values = values.map { |v| quote v }
47
- execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
51
+ execute "CREATE TYPE #{name} AS ENUM (#{values.join(", ")})"
48
52
  end
49
53
 
50
- def drop_enum(name)
51
- execute "DROP TYPE #{name}"
54
+ def drop_enum(name, cascade: nil, if_exists: nil)
55
+ if_exists_statement = "IF EXISTS" if if_exists
56
+ cascade_statement = "CASCADE" if cascade
57
+
58
+ sql = "DROP TYPE #{if_exists_statement} #{name} #{cascade_statement}"
59
+ execute sql
52
60
  end
53
61
 
54
62
  def rename_enum(name, new_name)
55
63
  execute "ALTER TYPE #{name} RENAME TO #{new_name}"
56
64
  end
57
65
 
58
- def add_enum_value(name, value, after: nil, before: nil)
59
- sql = "ALTER TYPE #{name} ADD VALUE #{quote value}"
66
+ def add_enum_value(name, value, after: nil, before: nil, if_not_exists: nil)
67
+ if_not_exists_statement = "IF NOT EXISTS" if if_not_exists
68
+
69
+ sql = "ALTER TYPE #{name} ADD VALUE #{if_not_exists_statement} #{quote value}"
60
70
  if after
61
71
  sql += " AFTER #{quote after}"
62
72
  elsif before
@@ -65,6 +75,15 @@ module ActiveRecord
65
75
  execute sql
66
76
  end
67
77
 
78
+ def remove_enum_value(name, value)
79
+ sql = %{
80
+ DELETE FROM pg_enum
81
+ WHERE enumlabel=#{quote value}
82
+ AND enumtypid=(SELECT oid FROM pg_type WHERE typname='#{name}')
83
+ }
84
+ execute sql
85
+ end
86
+
68
87
  def rename_enum_value(name, existing_value, new_value)
69
88
  raise "Renaming enum values is only supported in PostgreSQL 10.0+" unless rename_enum_value_supported?
70
89
 
@@ -17,10 +17,10 @@ module ActiveRecord
17
17
 
18
18
  @connection.enums.each do |name, values|
19
19
  values = values.map { |v| " #{v.inspect}," }.join("\n")
20
- statements << " create_enum #{name.inspect}, [\n#{values}\n ]"
20
+ statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
21
21
  end
22
22
 
23
- stream.puts statements.join("\n")
23
+ stream.puts statements.join("\n\n")
24
24
  stream.puts
25
25
  end
26
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "1.0.0"
5
+ VERSION = "1.5.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: 1.0.0
4
+ version: 1.5.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: 2020-05-29 00:00:00.000000000 Z
11
+ date: 2020-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.1'
22
+ version: '6.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.1'
32
+ version: '6.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: pg
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -120,28 +120,28 @@ dependencies:
120
120
  requirements:
121
121
  - - "~>"
122
122
  - !ruby/object:Gem::Version
123
- version: '3.8'
123
+ version: '4.0'
124
124
  type: :development
125
125
  prerelease: false
126
126
  version_requirements: !ruby/object:Gem::Requirement
127
127
  requirements:
128
128
  - - "~>"
129
129
  - !ruby/object:Gem::Version
130
- version: '3.8'
130
+ version: '4.0'
131
131
  - !ruby/object:Gem::Dependency
132
- name: rubocop
132
+ name: standard
133
133
  requirement: !ruby/object:Gem::Requirement
134
134
  requirements:
135
135
  - - "~>"
136
136
  - !ruby/object:Gem::Version
137
- version: '0.58'
137
+ version: '0.10'
138
138
  type: :development
139
139
  prerelease: false
140
140
  version_requirements: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - "~>"
143
143
  - !ruby/object:Gem::Version
144
- version: '0.58'
144
+ version: '0.10'
145
145
  description:
146
146
  email:
147
147
  - merkushin.m.s@gmail.com
@@ -172,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
172
  requirements:
173
173
  - - ">="
174
174
  - !ruby/object:Gem::Version
175
- version: '0'
175
+ version: '2.5'
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  requirements:
178
178
  - - ">="