activerecord-postgres_enum 1.7.0 → 2.0.1

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: 3d36ad353392d74fe6bdd5aaeb03cc682cb0f84205b5502ecff4681dd5833afb
4
+ data.tar.gz: 3c854bb369675b9a188dc8b102d35053fbe942f5939ea8675ea0fb3cfe3f9848
5
5
  SHA512:
6
- metadata.gz: 3df55af7787c9fb43356c5dfbbef7bac50165e579fbef7964b0f764de2683836b496eb7bc9f36e3db64db2bf9d5d9565feb23a784ab6ce07caf5f688e57e96d2
7
- data.tar.gz: 19dff3e375b92c55ac056b4d4d1d789b016b49b13c208e1368df623187068eb51884cc7832226124aed69289d5fab67878ee8d31445979bdbb264a275390757a
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,18 +4,20 @@ 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 = []
17
- if @connection.respond_to?(:enums)
18
- @connection.enums.each do |name, values|
19
+ if @connection.respond_to?(:enum_types)
20
+ @connection.enum_types.each do |name, values|
19
21
  values = values.map { |v| " #{v.inspect}," }.join("\n")
20
22
  statements << " create_enum #{name.inspect}, [\n#{values}\n ], force: :cascade"
21
23
  end
@@ -24,6 +26,14 @@ module ActiveRecord
24
26
  stream.puts
25
27
  end
26
28
  end
29
+
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
36
+ end
27
37
  end
28
38
  end
29
39
  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.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.7.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Merkushin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-26 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,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
@@ -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
@@ -173,7 +175,7 @@ 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
178
+ rubygems_version: 3.2.32
177
179
  signing_key:
178
180
  specification_version: 4
179
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, enum_name:, **options)
27
- column(name, enum_name, **options)
28
- end
29
- end
30
- end
31
- end
32
- end