activerecord-postgres_enum 1.7.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: 1bb4e436ffccf3e85046e307bfb7e677c99169128cc524dcd96feb235ee367b3
4
- data.tar.gz: c7c9c1a8c8d207371208382a3c13f33fba0c9181cb217d327112922eb4eec90a
3
+ metadata.gz: 3cef504021e0ff35024356c599daaed71ce72fe3be8d1c3b24fc038fa2b0b5ff
4
+ data.tar.gz: 7fbbe81954d2bdaeef201f1cc5abb4cdaefa216c3603ba7a8dd949ec06d8ea8a
5
5
  SHA512:
6
- metadata.gz: 3df55af7787c9fb43356c5dfbbef7bac50165e579fbef7964b0f764de2683836b496eb7bc9f36e3db64db2bf9d5d9565feb23a784ab6ce07caf5f688e57e96d2
7
- data.tar.gz: 19dff3e375b92c55ac056b4d4d1d789b016b49b13c208e1368df623187068eb51884cc7832226124aed69289d5fab67878ee8d31445979bdbb264a275390757a
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,17 +5,17 @@ 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?(:enums)
18
- @connection.enums.each do |name, values|
17
+ if @connection.respond_to?(:enum_types)
18
+ @connection.enum_types.each do |name, values|
19
19
  values = values.map { |v| " #{v.inspect}," }.join("\n")
20
20
  statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
21
21
  end
@@ -24,6 +24,12 @@ module ActiveRecord
24
24
  stream.puts
25
25
  end
26
26
  end
27
+
28
+ def prepare_column_options(column)
29
+ spec = super
30
+ spec[:enum_type] ||= "\"#{column.sql_type}\"" if column.enum?
31
+ spec
32
+ end
27
33
  end
28
34
  end
29
35
  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.7.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.7.0
4
+ version: 2.0.0
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-10-26 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,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5'
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: pg
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -136,7 +136,7 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '1.1'
139
- description:
139
+ description:
140
140
  email:
141
141
  - merkushin.m.s@gmail.com
142
142
  executables: []
@@ -146,11 +146,13 @@ files:
146
146
  - LICENSE.txt
147
147
  - README.md
148
148
  - lib/active_record/postgres_enum.rb
149
+ - lib/active_record/postgres_enum/column.rb
150
+ - lib/active_record/postgres_enum/column_methods.rb
149
151
  - lib/active_record/postgres_enum/command_recorder.rb
150
152
  - lib/active_record/postgres_enum/enum_validator.rb
151
- - lib/active_record/postgres_enum/extensions.rb
152
153
  - lib/active_record/postgres_enum/postgresql_adapter.rb
153
154
  - lib/active_record/postgres_enum/schema_dumper.rb
155
+ - lib/active_record/postgres_enum/schema_statements.rb
154
156
  - lib/active_record/postgres_enum/version.rb
155
157
  - lib/activerecord/postgres_enum.rb
156
158
  homepage: https://github.com/bibendi/activerecord-postgres_enum
@@ -158,7 +160,7 @@ licenses:
158
160
  - MIT
159
161
  metadata:
160
162
  allowed_push_host: https://rubygems.org
161
- post_install_message:
163
+ post_install_message:
162
164
  rdoc_options: []
163
165
  require_paths:
164
166
  - lib
@@ -173,8 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
173
175
  - !ruby/object:Gem::Version
174
176
  version: '0'
175
177
  requirements: []
176
- rubygems_version: 3.1.2
177
- signing_key:
178
+ rubygems_version: 3.0.3.1
179
+ signing_key:
178
180
  specification_version: 4
179
181
  summary: Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
180
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