activerecord-postgres_enum 1.6.0 → 2.0.1

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: 9e3664d257be105bae903d0b4183683768247f9a441afbcd6afc9290036ed8a5
4
- data.tar.gz: ec213e9dbc12ed859ef762c3f8ef1982e0eb072da67d1d11e921d0e17f8bd560
3
+ metadata.gz: 3d36ad353392d74fe6bdd5aaeb03cc682cb0f84205b5502ecff4681dd5833afb
4
+ data.tar.gz: 3c854bb369675b9a188dc8b102d35053fbe942f5939ea8675ea0fb3cfe3f9848
5
5
  SHA512:
6
- metadata.gz: 7f6dd1f45d8168aed7199e1e0d8381be40e9a745c421aca1e1f39bf4f2d45400e943d4ed955acf12ed9f489a70662b8b126b2a0b9ef6ceef98b96115227cf08f
7
- data.tar.gz: aaaad9f196e5ff222481bed5c732d596e6474566b8508e171e8fc5c6c6647b4d6c0d0a8f76060f7c6633004244d59e01689d68c7f2ce1bd84f9dfabb4e443cae
6
+ metadata.gz: 8a95d1362c9bd42e9201c386e26908d3cb0ea7162554a2fd81e5d1b102ff4087d6b47dd5966d3770e00efea5d18e7e8438947f55118057b6dc223d4a070d8ccd
7
+ data.tar.gz: 827677270881d6e14b394fa043d8c40c6ac2f789f791d484ddf7f0d7f1cbdba365840b3c8e370002c3fb56db46a7f1ce493cb1a9d27fd2f99c4ccae9ad27cfb8
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
@@ -4,24 +4,35 @@ module ActiveRecord
4
4
  module PostgresEnum
5
5
  # provide support for writing out the 'create_enum' calls in schema.rb
6
6
  module SchemaDumper
7
- def tables(stream)
8
- dump_enums(stream)
7
+ unless ActiveRecord::PostgresEnum.rails_7?
8
+ def tables(stream)
9
+ types(stream)
9
10
 
10
- super
11
+ super
12
+ end
11
13
  end
12
14
 
13
15
  private
14
16
 
15
- def dump_enums(stream)
17
+ def types(stream)
16
18
  statements = []
19
+ if @connection.respond_to?(:enum_types)
20
+ @connection.enum_types.each do |name, values|
21
+ values = values.map { |v| " #{v.inspect}," }.join("\n")
22
+ statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
23
+ end
17
24
 
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"
25
+ stream.puts statements.join("\n\n")
26
+ stream.puts
21
27
  end
28
+ end
22
29
 
23
- stream.puts statements.join("\n\n")
24
- stream.puts
30
+ unless ActiveRecord::PostgresEnum.rails_7?
31
+ def prepare_column_options(column)
32
+ spec = super
33
+ spec[:enum_type] ||= "\"#{column.sql_type}\"" if column.enum?
34
+ spec
35
+ end
25
36
  end
26
37
  end
27
38
  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.6.0"
5
+ VERSION = "2.0.1"
6
6
  end
7
7
  end
@@ -6,34 +6,59 @@ require "active_record/connection_adapters/postgresql/schema_statements"
6
6
  require "active_record/connection_adapters/postgresql_adapter"
7
7
  require "active_support/lazy_load_hooks"
8
8
 
9
+ module ActiveRecord
10
+ module PostgresEnum
11
+ def self.rails_7?
12
+ ActiveRecord::VERSION::MAJOR == 7
13
+ end
14
+
15
+ def self.rails_5?
16
+ ActiveRecord::VERSION::MAJOR == 5
17
+ end
18
+ end
19
+ end
20
+
9
21
  require "active_record/postgres_enum/version"
10
22
  require "active_record/postgres_enum/postgresql_adapter"
11
23
  require "active_record/postgres_enum/schema_dumper"
24
+ require "active_record/postgres_enum/schema_statements"
25
+ require "active_record/postgres_enum/column"
26
+ require "active_record/postgres_enum/column_methods"
12
27
  require "active_record/postgres_enum/command_recorder"
13
28
  require "active_record/postgres_enum/enum_validator"
14
29
 
15
30
  ActiveSupport.on_load(:active_record) do
16
- require "active_record/postgres_enum/extensions"
31
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend ActiveRecord::PostgresEnum::PostgreSQLAdapter
17
32
 
18
- ActiveRecord::SchemaDumper.prepend ActiveRecord::PostgresEnum::SchemaDumper
33
+ ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(
34
+ ActiveRecord::PostgresEnum::SchemaDumper
35
+ )
19
36
 
20
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend ActiveRecord::PostgresEnum::PostgreSQLAdapter
37
+ ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
21
38
 
22
- if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper)
23
- ActiveRecord::ConnectionAdapters::PostgreSQL::ColumnDumper.prepend(
24
- ActiveRecord::PostgresEnum::PostgreSQLAdapter::ColumnDumperExt
25
- )
26
- end
39
+ unless ActiveRecord::PostgresEnum.rails_7?
40
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {}
41
+
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
27
51
 
28
- if defined?(ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper)
29
- ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(
30
- ActiveRecord::PostgresEnum::PostgreSQLAdapter::SchemaDumperExt
52
+ ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition.include(
53
+ ActiveRecord::PostgresEnum::ColumnMethods
31
54
  )
32
- end
33
55
 
34
- ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
35
- name: "enum"
36
- }
56
+ ActiveRecord::ConnectionAdapters::PostgreSQL::Table.include(
57
+ ActiveRecord::PostgresEnum::ColumnMethods
58
+ )
37
59
 
38
- ActiveRecord::Migration::CommandRecorder.prepend ActiveRecord::PostgresEnum::CommandRecorder
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.6.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Merkushin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-17 00:00:00.000000000 Z
11
+ date: 2021-12-24 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,29 +114,29 @@ 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'
145
- description:
138
+ version: '1.1'
139
+ description:
146
140
  email:
147
141
  - merkushin.m.s@gmail.com
148
142
  executables: []
@@ -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
@@ -164,7 +160,7 @@ licenses:
164
160
  - MIT
165
161
  metadata:
166
162
  allowed_push_host: https://rubygems.org
167
- post_install_message:
163
+ post_install_message:
168
164
  rdoc_options: []
169
165
  require_paths:
170
166
  - lib
@@ -179,8 +175,8 @@ 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
183
- signing_key:
178
+ rubygems_version: 3.2.32
179
+ signing_key:
184
180
  specification_version: 4
185
181
  summary: Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
186
182
  test_files: []
@@ -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, enum_name:, **options)
27
- column(name, enum_name, **options)
28
- end
29
- end
30
- end
31
- end
32
- end