activerecord-postgres_enum 1.5.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac2154f798abecd00d17ad2ddb22307b9567113d10bb6e46b93ac2696a166afe
4
- data.tar.gz: 75925162b95e2e293bf6f6834dbc05bea188ad40b35aac7d7c5b3f0ac36caf59
3
+ metadata.gz: 3cef504021e0ff35024356c599daaed71ce72fe3be8d1c3b24fc038fa2b0b5ff
4
+ data.tar.gz: 7fbbe81954d2bdaeef201f1cc5abb4cdaefa216c3603ba7a8dd949ec06d8ea8a
5
5
  SHA512:
6
- metadata.gz: 8a8599019f7412e927bce0a25c5ae26de5a7b0e197a7a4c5da54f53edcb888e4bb6b7e0414d20f461fa61b2f33f2927e7edd67542eb8e968ed4766d25a15a811
7
- data.tar.gz: 1042bdcdd209e900527348265f906da95773635053118806ea7107f2496f6da39c3b885024d28b7fdcb292fb0868a11ed1aa277e37e9aeea8cab4389ba835599
6
+ metadata.gz: 847fc44c04be544a22d468e73dbba779324cfc3a32cbb615dc664090c45a870f963881d875c5b83f72e7808fae878861580ce031a8f105a8f4a34c90e3ea9d36
7
+ data.tar.gz: a87a1a3250627e1e0112c5fc3e8d4b0ad27a33d5099ca4b7f5bf65bc169e19ee505b43df63176c2f12df45f4eba061bf5b6b10abcb6a7338cd63ed4776032cf0
data/README.md CHANGED
@@ -32,7 +32,7 @@ Or install it yourself as:
32
32
  create_enum :mood, %w(happy great been_better)
33
33
 
34
34
  create_table :person do
35
- t.enum :person_mood, enum_name: :mood
35
+ t.enum :person_mood, enum_type: :mood
36
36
  end
37
37
  ```
38
38
 
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # frozen_string_literal: true
4
+
5
+ module ActiveRecord
6
+ module PostgresEnum
7
+ module Column
8
+ def enum?
9
+ type == :enum
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ module ColumnMethods
6
+ # Enables `t.enum :my_field, enum_type: :my_enum_name` on migrations
7
+ def enum(name, enum_type:, **options)
8
+ column(name, enum_type, **options)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  @enums ||= {}
8
8
  return @enums[attr_id] if @enums.key?(attr_id)
9
9
 
10
- @enums[attr_id] ||= connection.enums.fetch(type.to_sym) do
10
+ @enums[attr_id] ||= connection.enum_types.fetch(type.to_sym) do
11
11
  raise "Enum `#{type}` not found in a database #{connection}"
12
12
  end
13
13
  end
@@ -3,26 +3,6 @@
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
-
26
6
  DEFINED_ENUMS_QUERY = <<~SQL
27
7
  SELECT
28
8
  t.OID,
@@ -36,14 +16,14 @@ module ActiveRecord
36
16
  ORDER BY t.typname
37
17
  SQL
38
18
 
39
- def enums
19
+ def enum_types
40
20
  select_all(DEFINED_ENUMS_QUERY).each_with_object({}) do |row, memo|
41
21
  memo[row["typname"].to_sym] = row["enumlabels"].split("\t\t")
42
22
  end
43
23
  end
44
24
 
45
25
  def create_enum(name, values, force: false, if_not_exists: nil)
46
- return if if_not_exists && enums.include?(name.to_sym)
26
+ return if if_not_exists && enum_types.include?(name.to_sym)
47
27
 
48
28
  drop_enum(name, cascade: force == :cascade, if_exists: true) if force
49
29
 
@@ -90,16 +70,6 @@ module ActiveRecord
90
70
  execute "ALTER TYPE #{name} RENAME VALUE #{quote existing_value} TO #{quote new_value}"
91
71
  end
92
72
 
93
- def migration_keys
94
- super + [:enum_name]
95
- end
96
-
97
- def prepare_column_options(column, types)
98
- spec = super(column, types)
99
- spec[:enum_name] = column.cast_type.enum_name.inspect if column.type == :enum
100
- spec
101
- end
102
-
103
73
  def rename_enum_value_supported?
104
74
  ActiveRecord::Base.connection.send(:postgresql_version) >= 100_000
105
75
  end
@@ -5,23 +5,30 @@ 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
- dump_enums(stream)
8
+ types(stream)
9
9
 
10
10
  super
11
11
  end
12
12
 
13
13
  private
14
14
 
15
- def dump_enums(stream)
15
+ def types(stream)
16
16
  statements = []
17
+ if @connection.respond_to?(:enum_types)
18
+ @connection.enum_types.each do |name, values|
19
+ values = values.map { |v| " #{v.inspect}," }.join("\n")
20
+ statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
21
+ end
17
22
 
18
- @connection.enums.each do |name, values|
19
- values = values.map { |v| " #{v.inspect}," }.join("\n")
20
- statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
23
+ stream.puts statements.join("\n\n")
24
+ stream.puts
21
25
  end
26
+ end
22
27
 
23
- stream.puts statements.join("\n\n")
24
- stream.puts
28
+ def prepare_column_options(column)
29
+ spec = super
30
+ spec[:enum_type] ||= "\"#{column.sql_type}\"" if column.enum?
31
+ spec
25
32
  end
26
33
  end
27
34
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ module SchemaStatements
6
+ def type_to_sql(type, enum_type: nil, **kwargs)
7
+ if type.to_s == "enum"
8
+ enum_type
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module PostgresEnum
5
- VERSION = "1.5.0"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
@@ -9,31 +9,56 @@ 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/schema_statements"
13
+ require "active_record/postgres_enum/column"
14
+ require "active_record/postgres_enum/column_methods"
12
15
  require "active_record/postgres_enum/command_recorder"
13
16
  require "active_record/postgres_enum/enum_validator"
14
17
 
15
- ActiveSupport.on_load(:active_record) do
16
- require "active_record/postgres_enum/extensions"
18
+ module ActiveRecord
19
+ module PostgresEnum
20
+ def self.rails_7?
21
+ Rails::VERSION::MAJOR == 7
22
+ end
17
23
 
18
- ActiveRecord::SchemaDumper.prepend ActiveRecord::PostgresEnum::SchemaDumper
24
+ def self.rails_5?
25
+ Rails::VERSION::MAJOR == 5
26
+ end
27
+ end
28
+ end
19
29
 
30
+ ActiveSupport.on_load(:active_record) do
20
31
  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend ActiveRecord::PostgresEnum::PostgreSQLAdapter
21
32
 
22
- if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper)
23
- ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper.prepend(
24
- ActiveRecord::PostgresEnum::PostgreSQLAdapter::ColumnDumperExt
25
- )
26
- end
33
+ ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
34
+
35
+ unless ActiveRecord::PostgresEnum.rails_7?
36
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {}
27
37
 
28
- if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper)
29
38
  ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(
30
- ActiveRecord::PostgresEnum::PostgreSQLAdapter::SchemaDumperExt
39
+ ActiveRecord::PostgresEnum::SchemaDumper
31
40
  )
32
- end
33
41
 
34
- ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
35
- name: "enum"
36
- }
42
+ if ActiveRecord::PostgresEnum.rails_5?
43
+ ActiveRecord::ConnectionAdapters::PostgreSQLColumn.prepend(
44
+ ActiveRecord::PostgresEnum::Column
45
+ )
46
+ else
47
+ ActiveRecord::ConnectionAdapters::PostgreSQL::Column.prepend(
48
+ ActiveRecord::PostgresEnum::Column
49
+ )
50
+ end
37
51
 
38
- ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
52
+ ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include(
53
+ ActiveRecord::PostgresEnum::ColumnMethods
54
+ )
55
+
56
+ ActiveRecord::ConnectionAdapters::PostgreSQL::Table.include(
57
+ ActiveRecord::PostgresEnum::ColumnMethods
58
+ )
59
+
60
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements.prepend(
61
+ ActiveRecord::PostgresEnum::SchemaStatements
62
+ )
63
+ end
39
64
  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.5.0
4
+ version: 2.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: 2020-12-11 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,20 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '6.2'
19
+ version: '5.2'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '5'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '6.2'
26
+ version: '5.2'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: pg
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -120,28 +114,28 @@ dependencies:
120
114
  requirements:
121
115
  - - "~>"
122
116
  - !ruby/object:Gem::Version
123
- version: '4.0'
117
+ version: '5.0'
124
118
  type: :development
125
119
  prerelease: false
126
120
  version_requirements: !ruby/object:Gem::Requirement
127
121
  requirements:
128
122
  - - "~>"
129
123
  - !ruby/object:Gem::Version
130
- version: '4.0'
124
+ version: '5.0'
131
125
  - !ruby/object:Gem::Dependency
132
126
  name: standard
133
127
  requirement: !ruby/object:Gem::Requirement
134
128
  requirements:
135
129
  - - "~>"
136
130
  - !ruby/object:Gem::Version
137
- version: '0.10'
131
+ version: '1.1'
138
132
  type: :development
139
133
  prerelease: false
140
134
  version_requirements: !ruby/object:Gem::Requirement
141
135
  requirements:
142
136
  - - "~>"
143
137
  - !ruby/object:Gem::Version
144
- version: '0.10'
138
+ version: '1.1'
145
139
  description:
146
140
  email:
147
141
  - merkushin.m.s@gmail.com
@@ -152,11 +146,13 @@ files:
152
146
  - LICENSE.txt
153
147
  - README.md
154
148
  - lib/active_record/postgres_enum.rb
149
+ - lib/active_record/postgres_enum/column.rb
150
+ - lib/active_record/postgres_enum/column_methods.rb
155
151
  - lib/active_record/postgres_enum/command_recorder.rb
156
152
  - lib/active_record/postgres_enum/enum_validator.rb
157
- - lib/active_record/postgres_enum/extensions.rb
158
153
  - lib/active_record/postgres_enum/postgresql_adapter.rb
159
154
  - lib/active_record/postgres_enum/schema_dumper.rb
155
+ - lib/active_record/postgres_enum/schema_statements.rb
160
156
  - lib/active_record/postgres_enum/version.rb
161
157
  - lib/activerecord/postgres_enum.rb
162
158
  homepage: https://github.com/bibendi/activerecord-postgres_enum
@@ -179,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
175
  - !ruby/object:Gem::Version
180
176
  version: '0'
181
177
  requirements: []
182
- rubygems_version: 3.1.2
178
+ rubygems_version: 3.0.3.1
183
179
  signing_key:
184
180
  specification_version: 4
185
181
  summary: Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module ConnectionAdapters
5
- module PostgreSQL
6
- module OID # :nodoc:
7
- class Enum < Type::Value # :nodoc:
8
- attr_reader :enum_name
9
-
10
- def initialize(enum_name:, **kwargs)
11
- @enum_name = enum_name.to_sym
12
- super(**kwargs)
13
- end
14
- end
15
-
16
- class TypeMapInitializer
17
- # We need to know the column name, and the default implementation discards it
18
- def register_enum_type(row)
19
- register row["oid"], OID::Enum.new(enum_name: row["typname"])
20
- end
21
- end
22
- end
23
-
24
- module ColumnMethods # :nodoc:
25
- # Enables `t.enum :my_field, enum_name: :my_enum_name` on migrations
26
- def enum(name, options = {})
27
- column(name, options.delete(:enum_name), options.except(:enum_name))
28
- end
29
- end
30
- end
31
- end
32
- end