ar-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: 7fafa2cb564dcb6f21264144628705d9c7d68af50a13ea21638ef448a447de8a
4
+ data.tar.gz: 607ed017f7d41594e0c1c48d89a854b3921607236a859160076491c8091118dc
5
+ SHA512:
6
+ metadata.gz: 57fceb9b862e7bb38e953bedd08843af834fe5f35e8665388441a712b520eebd4991f6520275d5a1f71f4f186058f35eb3180e139c03456f027af89b78ad2e61
7
+ data.tar.gz: ebf8f91d84c179226b6303faaad634059336bc1da8edd8ff641d8ffc6c234fa2f7bf002bfadf90fc24f05d3677ade918555bc1f642c31a2171328459b320735c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ *.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,33 @@
1
+ language: ruby
2
+ addons:
3
+ postgresql: "10"
4
+ apt:
5
+ packages:
6
+ - postgresql-10
7
+ - postgresql-client-10
8
+ cache: bundler
9
+ before_install:
10
+ - sudo service postgresql stop
11
+ - sed -e 's/^port.*/port = 5432/' /etc/postgresql/10/main/postgresql.conf > postgresql.conf
12
+ - sudo chown postgres postgresql.conf
13
+ - sudo mv postgresql.conf /etc/postgresql/10/main
14
+ - sudo service postgresql start 10
15
+ before_script:
16
+ - createdb test
17
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
18
+ - chmod +x ./cc-test-reporter
19
+ - "./cc-test-reporter before-build"
20
+ after_script:
21
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
22
+ gemfile:
23
+ - Gemfile
24
+ - gemfiles/activerecord_6_0.gemfile
25
+ - gemfiles/activerecord_5_2.gemfile
26
+ rvm:
27
+ - 2.6.1
28
+ - 2.5.3
29
+ notifications:
30
+ email: false
31
+ env:
32
+ global:
33
+ secure: ItWMub2Jbp/Xey50SAPWeYzZcKoEFORj716NmbQI1NbnOzqEDOF3pDZd9HHCxHoBA6gYYDIGWFCa15ymfugzBJ/eGaL7qzEF098xU7hwgG0DOlU31RAPOwn7ov3merGVJOttWOgYYmTR7pxyyg5iMc7/IzU5ag1As+Sz8s7vq9uR2HRdRALvuRFAS1Vty5lV1dINVNIs2vSHMIdgwWCaraWno8R3WupchXgkoA4WGwK6+n99ZUfuCv6kLwAJhlH3rWXEuwT49ILGRCYUp3rRZ/wawclUviYjxgVvS5xeEGzsh3X9AMkbq68up9zxcG1KYeL+EKxLDL6ieC2OtaqMst3k7UariK5QWro00TdGdNpw2M83d0ca81PUW8lpyZVvOJ8STDFR3Dtt8emaz/F0rHiBJlMP1vKWYxgQgpIEIv+WQV2YcVJBg0NNjQ8Ei6ujGG2s8pJNvJBudK/jL3okpXy/uPd5djQfoJcLnZk0xHOqJtcr1+Xg/9TUo/vh81U8cIgGJDtwssoyMastqv1U9Gm2AfsRKmjDyRoIhvwbF9V3dpdks4tRtmnEoYo91K9y2nv45tyO0R6PSEhNL8wDGlvgqUk4XlaaQEs2yNPgpX07ivs47+/x8ufkcyzAf0nHXN/FDzqLnHQBHqdk8a/BTY9wWQP1d5iIuojr30vR4X4=
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # ar-check
2
+
3
+ #### v0.1.0
4
+
5
+ - Initial release
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ar-check.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nando Vieira
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,100 @@
1
+ # AR::Enum
2
+
3
+ [![Travis-CI](https://travis-ci.org/fnando/ar-enum.svg)](https://travis-ci.org/fnando/ar-enum)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/ar-enum/badges/gpa.svg)](https://codeclimate.com/github/fnando/ar-enum)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/ar-enum/badges/coverage.svg)](https://codeclimate.com/github/fnando/ar-enum/coverage)
6
+ [![Gem](https://img.shields.io/gem/v/ar-enum.svg)](https://rubygems.org/gems/ar-enum)
7
+ [![Gem](https://img.shields.io/gem/dt/ar-enum.svg)](https://rubygems.org/gems/ar-enum)
8
+
9
+ Add support for creating `ENUM` types in PostgreSQL with ActiveRecord.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "ar-enum"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ar-enum
26
+
27
+ ## Usage
28
+
29
+ To create a `enum` type, use the method `create_enum`.
30
+
31
+ ```ruby
32
+ # The type is created independently from the table.
33
+ create_enum :article_status, %w[draft published]
34
+
35
+ create_table :articles do |t|
36
+ # Use the type `article_status` when defining your column.
37
+ t.column :status, :article_status, null: false, default: "draft"
38
+ end
39
+ ```
40
+
41
+ To remove the enum, use `drop_enum`.
42
+
43
+ ```ruby
44
+ drop_enum :article_status
45
+ ```
46
+
47
+ You'll receive a `ActiveRecord::StatementInvalid` if any of your tables have a dependency on the type; i.e. you're have a column using that enum. You can drop these columns by specifying `cascade: true`.
48
+
49
+ ```ruby
50
+ drop_enum :article_status, cascade: true
51
+ ```
52
+
53
+ If you don't want to remove the column, change the column type before dropping the enum.
54
+
55
+ ```ruby
56
+ change_column :articles, :status, :text
57
+ drop_enum :article_status
58
+ ```
59
+
60
+ Use `add_enum_label` to add new values to the enum type. You can control where the value will be inserted by using `before: label` and `after: label`.
61
+
62
+ ```ruby
63
+ create_enum :article_status, %w[draft published]
64
+
65
+ # labels will be sorted as [draft, published, unlisted]
66
+ add_enum_label :article_status, "unlisted"
67
+
68
+ # labels will be sorted as [draft, unlisted, published]
69
+ add_enum_value :article_status, "unlisted", after: "draft"
70
+
71
+ # labels will be sorted as [draft, unlisted, published]
72
+ add_enum_value :article_status, "unlisted", before: "published"
73
+ ```
74
+
75
+ Use `rename_enum_label` to rename a value.
76
+
77
+ ```ruby
78
+ create_enum :article_status, %w[draft unlisted published]
79
+
80
+ # labels will be [draft, hidden, unlisted]
81
+ rename_enum_label :article_status, "unlisted", "hidden"
82
+ ```
83
+
84
+ **WARNING:** PostgreSQL does not have a way of removing values from enum types.
85
+
86
+ ## Development
87
+
88
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
89
+
90
+ 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).
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/ar-enum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
95
+
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
100
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ t.verbose = false
11
+ t.warning = false
12
+ end
13
+
14
+ task default: :test
data/ar-enum.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "./lib/ar/enum/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ar-enum"
7
+ spec.version = AR::Enum::VERSION
8
+ spec.authors = ["Nando Vieira"]
9
+ spec.email = ["fnando.vieira@gmail.com"]
10
+
11
+ spec.summary = "Add support for creating `ENUM` types in PostgreSQL with ActiveRecord"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://rubygems.org/gems/ar-enum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`
17
+ .split("\x0")
18
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activerecord"
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "minitest-utils"
27
+ spec.add_development_dependency "pg"
28
+ spec.add_development_dependency "pry-meta"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "simplecov"
31
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec path: ".."
5
+
6
+ gem "activerecord", "~> 5.2.0"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec path: ".."
5
+
6
+ gem "activerecord", "~> 6.0.0.beta2"
data/lib/ar-enum.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ar/enum"
data/lib/ar/enum.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/all"
4
+ require "active_record"
5
+ require "active_record/connection_adapters/postgresql_adapter"
6
+ require "active_record/schema_dumper"
7
+ require "ar/enum/version"
8
+
9
+ module AR
10
+ module Enum
11
+ require "ar/enum/adapter"
12
+ require "ar/enum/schema_dumper"
13
+ end
14
+ end
15
+
16
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Enum::Adapter
17
+ ActiveRecord::SchemaDumper.prepend AR::Enum::SchemaDumper
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AR
4
+ module Enum
5
+ module Adapter
6
+ def enum_types
7
+ execute <<~SQL
8
+ with
9
+ sorted_enums as (
10
+ select
11
+ t.typname,
12
+ e.enumlabel
13
+ from
14
+ pg_type t,
15
+ pg_enum e
16
+ where
17
+ t.oid = e.enumtypid
18
+ order by
19
+ e.enumsortorder
20
+ )
21
+
22
+ select
23
+ typname as name,
24
+ string_agg(enumlabel, ',') as labels
25
+ from
26
+ sorted_enums
27
+ group by
28
+ typname
29
+ SQL
30
+ end
31
+
32
+ def create_enum(name, values)
33
+ values = values.map do |value|
34
+ quote(value.to_s)
35
+ end
36
+
37
+ sql = <<-SQL
38
+ CREATE TYPE #{name}
39
+ AS ENUM (#{values.join(', ')})
40
+ SQL
41
+
42
+ execute(sql)
43
+ end
44
+
45
+ def add_enum_label(name, value, options = {})
46
+ sql = "ALTER TYPE #{name} ADD VALUE #{quote(value.to_s)}"
47
+
48
+ if options[:before]
49
+ sql = "#{sql} BEFORE #{quote(options[:before].to_s)}"
50
+ elsif options[:after]
51
+ sql = "#{sql} AFTER #{quote(options[:after].to_s)}"
52
+ end
53
+
54
+ execute(sql)
55
+ end
56
+
57
+ def rename_enum_label(name, from, to)
58
+ sql = "ALTER TYPE #{name} RENAME VALUE #{quote(from.to_s)} TO #{quote(to.to_s)}"
59
+ execute(sql)
60
+ end
61
+
62
+ def drop_enum(name, options = {})
63
+ sql = "DROP TYPE IF EXISTS #{name}"
64
+ sql = "#{sql} CASCADE" if options[:cascade]
65
+
66
+ execute(sql)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AR
4
+ module Enum
5
+ module SchemaDumper
6
+ def header(stream)
7
+ super
8
+ enum_types(stream)
9
+ end
10
+
11
+ def enum_types(stream)
12
+ list = @connection.enum_types.to_a
13
+
14
+ stream.puts(" # These are enum types created on this database") if list.any?
15
+
16
+ list.each do |row|
17
+ labels = row["labels"].split(",")
18
+ name = row["name"]
19
+
20
+ statement = [
21
+ " create_enum",
22
+ "#{name.to_sym.inspect},",
23
+ labels.inspect
24
+ ].join(" ")
25
+
26
+ stream.puts(statement)
27
+ end
28
+
29
+ stream.puts
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AR
4
+ module Enum
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar-enum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-10 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-utils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-meta
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Add support for creating `ENUM` types in PostgreSQL with ActiveRecord
112
+ email:
113
+ - fnando.vieira@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - ar-enum.gemspec
127
+ - gemfiles/activerecord_5_2.gemfile
128
+ - gemfiles/activerecord_6_0.gemfile
129
+ - lib/ar-enum.rb
130
+ - lib/ar/enum.rb
131
+ - lib/ar/enum/adapter.rb
132
+ - lib/ar/enum/schema_dumper.rb
133
+ - lib/ar/enum/version.rb
134
+ homepage: https://rubygems.org/gems/ar-enum
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.0.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Add support for creating `ENUM` types in PostgreSQL with ActiveRecord
157
+ test_files: []