activerecord-postgres_enum 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0d34e14ab6cb6bb3067632c5ea90dfa62a9e23be88330c30cb118ffe4f70e55d
4
+ data.tar.gz: 7b6dd1a8dbe8e7e2b08b616ca15894eb72e2040f7f0d691f02b3452d37a08542
5
+ SHA512:
6
+ metadata.gz: 19e8a1a1a6b55cca9ae200356c6bcff134809e1d17a3e3b37c0269a482a0f263925ae72ea467ae897133aa0d5e718536b8a693488716f45c9b1c8f5d68fa573e
7
+ data.tar.gz: 98cc761dec0bfa6248da5135e767d23ea54bcf36c91b604e0eee71c003f22cf79c32273a9bb3e9ec97a8afdc692389a7610665479356189484d569a2a9aa9a8c
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Michael Merkushin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
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)
3
+
4
+ # ActiveRecord::PostgresEnum
5
+
6
+ Adds migration and schema.rb support to PostgreSQL enum data types.
7
+
8
+ <a href="https://evilmartians.com/?utm_source=activerecord-postgres_enum">
9
+ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'activerecord-postgres_enum'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install activerecord-postgres_enum
26
+
27
+ ## Usage
28
+
29
+ ### Migrations
30
+
31
+ ```ruby
32
+ create_enum :mood, %w(happy great been_better)
33
+
34
+ create_table :person do
35
+ t.enum :person_mood, enum_name: :mood
36
+ end
37
+ ```
38
+
39
+ Running the above will create a table :person, with a column :person_mood of type :mood. This will also be saved on schema.rb so that `rake schema:load` works as expected.
40
+
41
+ To drop an existing enum:
42
+
43
+ ```ruby
44
+ drop_enum :mood
45
+ ```
46
+
47
+ To add a value into existing enum:
48
+
49
+ ```ruby
50
+ alter_enum :mood, "crazy"
51
+ ```
52
+
53
+ Renaming / updating enums is currently not supported, since it's quite tricky.
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bibendi/activerecord-postgres_enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the Activerecord::PostgresEnum project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bibendi/activerecord-postgres_enum/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "active_record/schema_dumper"
5
+ require "active_record/connection_adapters/postgresql/schema_statements"
6
+ require "active_record/connection_adapters/postgresql_adapter"
7
+ require "active_support/lazy_load_hooks"
8
+
9
+ require "active_record/postgres_enum/version"
10
+ require "active_record/postgres_enum/postgresql_adapter"
11
+ require "active_record/postgres_enum/schema_dumper"
12
+
13
+ ActiveSupport.on_load(:active_record) do
14
+ require "active_record/postgres_enum/extensions"
15
+
16
+ ActiveRecord::SchemaDumper.prepend ActiveRecord::PostgresEnum::SchemaDumper
17
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend ActiveRecord::PostgresEnum::PostgreSQLAdapter
18
+
19
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:enum] = {
20
+ name: 'enum'
21
+ }
22
+ end
@@ -0,0 +1,32 @@
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(options = {})
11
+ @enum_name = options.delete(:enum_name).to_sym
12
+ super
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
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ module PostgreSQLAdapter
6
+ def create_enum(name, values)
7
+ values = values.map { |v| "'#{v}'" }
8
+ execute "CREATE TYPE #{name} AS ENUM (#{values.join(', ')})"
9
+ end
10
+
11
+ def drop_enum(name)
12
+ execute "DROP TYPE #{name}"
13
+ end
14
+
15
+ def alter_enum(name, value)
16
+ execute "ALTER TYPE #{name} ADD VALUE '#{value}'"
17
+ end
18
+
19
+ def migration_keys
20
+ super + [:enum_name]
21
+ end
22
+
23
+ def prepare_column_options(column, types)
24
+ spec = super(column, types)
25
+ spec[:enum_name] = column.cast_type.enum_name.inspect if column.type == :enum
26
+ spec
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ # provide support for writing out the 'create_enum' calls in schema.rb
6
+ module SchemaDumper
7
+ def tables(stream)
8
+ enums(stream)
9
+ super(stream)
10
+ end
11
+
12
+ private
13
+
14
+ def defined_enums
15
+ query = <<~SQL
16
+ SELECT t.OID, t.typname, t.typtype, array_agg(e.enumlabel) as enumlabels
17
+ FROM pg_type t
18
+ INNER JOIN pg_enum e ON e.enumtypid = t.oid
19
+ WHERE typtype = 'e'
20
+ GROUP BY t.OID, t.typname, t.typtype
21
+ ORDER BY t.typname
22
+ SQL
23
+
24
+ @connection.select_all(query).each do |enum|
25
+ enum['enumlabels'] = enum['enumlabels'].gsub!(/[{}]/, '').split(',')
26
+ end
27
+ end
28
+
29
+ def enums(stream)
30
+ statements = []
31
+
32
+ defined_enums.each do |enum|
33
+ enum_name = enum['typname']
34
+ values = enum['enumlabels'].map(&:inspect).join(', ')
35
+ statements << " create_enum(#{enum_name.inspect}, [#{values}])"
36
+ end
37
+
38
+ stream.puts statements.join("\n")
39
+ stream.puts
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PostgresEnum
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/postgres_enum"
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-postgres_enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Merkushin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "<"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.58'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.58'
111
+ description:
112
+ email:
113
+ - merkushin.m.s@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - lib/active_record/postgres_enum.rb
121
+ - lib/active_record/postgres_enum/extensions.rb
122
+ - lib/active_record/postgres_enum/postgresql_adapter.rb
123
+ - lib/active_record/postgres_enum/schema_dumper.rb
124
+ - lib/active_record/postgres_enum/version.rb
125
+ - lib/activerecord/postgres_enum.rb
126
+ homepage: https://github.com/bibendi/activerecord-postgres_enum
127
+ licenses:
128
+ - MIT
129
+ metadata:
130
+ allowed_push_host: https://rubygems.org
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.7.6
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
151
+ test_files: []