ar-check 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e593c8ca44038db60d6124950ca496009d4c535c
4
+ data.tar.gz: 65c8729717f59a687299c2f8652fde3d2ee8c7e4
5
+ SHA512:
6
+ metadata.gz: b7fc749d55e12d40f441a6f9e91af4d146c0c7ad243b88d28981ce5f64b4df4af9719f58aed4fe8d4b74cee39cb76edde5aebfc6b57c6b929a9c8f7e773e7598
7
+ data.tar.gz: 33ef1bb7aed53bbfc9006ba73abbe099281670aa1165c2d3b930d51062e994025f4244cca436726dcb6e0d64c3513f5e81ad575114a3b62995a2dda19720cdb2
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache: bundler
4
+ before_install:
5
+ - gem install bundler
6
+ before_script:
7
+ - createdb test
8
+ rvm:
9
+ - '2.3.0'
10
+ - '2.2'
11
+ - '2.1'
12
+ notifications:
13
+ email: false
14
+ env:
15
+ global:
16
+ secure: OvcOmU3tTrRrgXAAZpZVMbgwtxL7NtF35hf1sBhR8K+x1zZvcJkO1ADc60zzcXsMSIfavsD6uUPE0V92OJ+AgJPJq85skXr0UaYWmCDsEcnCqCl3HQYph1d6ntzZ2ZVC7Oz/nwuk2oaTL02wXuh/2XsztvDSTHWsNAWye787u4mPU4n4YXggsSCIU6zHSz5orfkKR4DkXT7jhrVP6lIn+an+j4RyZJ2Y7HE32vtV93bq1bdSiyJNs0fOY6iGpPRoD86mO2dRSdpQPPI4y0ArnPH6dFiWTIy94xWNyb64vvzDCqsJdj1A/4h13AUNqK5rG8nuWllWCe455o17fTPJchqM71ImjrOyeHFPkGvkID2UL0tgIApNl3bBdANWDITXLeJiAtDHIlFcisxda2qJs3pe4YM+UkspInofHvO80YFT4/IBfYB1bbWwcP8+bisvpUoStjZXO7VPAilxVC+RimGK1vp74WLHm1LQGkINpeEhFyhAxYyuOGHhTLlfquVAhQbCy6bV78NHeoxpTBjt6u0ZpBlDe9yJXO0NsRqyhCSKf+4LNpafROpxCU4BvT4WYi7QM3cJgO7Mlia67YzVrl3IYKkOctvcfj3pdlxTWlcd0RzEBj8Xicq+3tCgJ1mkMqExIylXQwYGflP77ErCw4hcC6wsWYhBl/wQMC/+VWE=
@@ -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,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ar-check.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,67 @@
1
+ # AR::Check
2
+
3
+ [![Travis-CI](https://travis-ci.org/fnando/ar-check.png)](https://travis-ci.org/fnando/ar-check)
4
+ [![Code Climate](https://codeclimate.com/github/fnando/ar-check/badges/gpa.svg)](https://codeclimate.com/github/fnando/ar-check)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/ar-check/badges/coverage.svg)](https://codeclimate.com/github/fnando/ar-check/coverage)
6
+ [![Gem](https://img.shields.io/gem/v/ar-check.svg)](https://rubygems.org/gems/ar-check)
7
+ [![Gem](https://img.shields.io/gem/dt/ar-check.svg)](https://rubygems.org/gems/ar-check)
8
+
9
+ Enable PostgreSQL's CHECK constraints on ActiveRecord migrations.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "ar-check"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ar-check
26
+
27
+ ## Usage
28
+
29
+ To create a `CHECK` constraint, just use the method `add_check`.
30
+
31
+ ```ruby
32
+ create_table :employees do |t|
33
+ t.integer :salary, null: false
34
+ end
35
+
36
+ add_check :employees, :positive_salary, "salary > 0"
37
+ ```
38
+
39
+ This will generate a new constraint using the following SQL statement:
40
+
41
+ ```sql
42
+ ALTER TABLE employees
43
+ ADD CONSTRAINT positive_salary_on_things
44
+ CHECK (salary > 0)
45
+ ```
46
+
47
+ To remove it, just using `remove_check`.
48
+
49
+ ```ruby
50
+ remove_check :employees, :positive_salary
51
+ ```
52
+
53
+ ## Development
54
+
55
+ 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.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/ar-check. 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.
62
+
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ t.verbose = false
9
+ t.warning = false
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,29 @@
1
+ require "./lib/ar/check/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ar-check"
5
+ spec.version = AR::Check::VERSION
6
+ spec.authors = ["Nando Vieira"]
7
+ spec.email = ["fnando.vieira@gmail.com"]
8
+
9
+ spec.summary = "Enable PostgreSQL's CHECK constraints on ActiveRecord migrations"
10
+ spec.description = spec.summary
11
+ spec.homepage = "https://rubygems.org/gems/ar-check"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`
15
+ .split("\x0")
16
+ .reject {|f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest-utils"
26
+ spec.add_development_dependency "pg"
27
+ spec.add_development_dependency "pry-meta"
28
+ spec.add_development_dependency "codeclimate-test-reporter"
29
+ end
@@ -0,0 +1 @@
1
+ require "ar/check"
@@ -0,0 +1,18 @@
1
+ require "active_support/all"
2
+ require "active_record"
3
+ require "active_record/connection_adapters/postgresql_adapter"
4
+ require "active_record/migration/command_recorder"
5
+ require "active_record/schema_dumper"
6
+ require "ar/check/version"
7
+
8
+ module AR
9
+ module Check
10
+ require "ar/check/command_recorder"
11
+ require "ar/check/adapter"
12
+ require "ar/check/schema_dumper"
13
+ end
14
+ end
15
+
16
+ ActiveRecord::Migration::CommandRecorder.include AR::Check::CommandRecorder
17
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Check::Adapter
18
+ ActiveRecord::SchemaDumper.prepend AR::Check::SchemaDumper
@@ -0,0 +1,40 @@
1
+ module AR
2
+ module Check
3
+ module Adapter
4
+ def check_constraints(table)
5
+ result = select_all <<-SQL.strip_heredoc
6
+ SELECT c.conname AS name,
7
+ c.consrc AS expression,
8
+ t1.relname AS table
9
+ FROM pg_constraint c
10
+ JOIN pg_class t1 ON c.conrelid = t1.oid
11
+ JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
12
+ JOIN pg_namespace t2 ON c.connamespace = t2.oid
13
+ WHERE c.contype = 'c'
14
+ AND c.convalidated = TRUE
15
+ AND t1.relname = '#{table}'
16
+ AND t2.nspname = ANY (current_schemas(false))
17
+ SQL
18
+
19
+ result.to_a
20
+ end
21
+
22
+ def add_check(table, constraint_name, expression)
23
+ sql = <<-SQL
24
+ ALTER TABLE #{table}
25
+ ADD CONSTRAINT #{quote_column_name("#{constraint_name}_on_#{table}")}
26
+ CHECK (#{expression})
27
+ SQL
28
+ execute(sql)
29
+ end
30
+
31
+ def remove_check(table, constraint_name)
32
+ sql = <<-SQL
33
+ ALTER TABLE #{table}
34
+ DROP CONSTRAINT #{quote_column_name("#{constraint_name}_on_#{table}")}
35
+ SQL
36
+ execute(sql)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ module AR
2
+ module Check
3
+ module CommandRecorder
4
+ # Usage:
5
+ #
6
+ # add_check :users, :check_user_age, "age > 18"
7
+ #
8
+ def add_check(table, constraint_name, expression)
9
+ record(__method__, [table, constraint_name, expression])
10
+ end
11
+
12
+ # Usage:
13
+ #
14
+ # remove_check :users, :check_user_age
15
+ #
16
+ def remove_check(table, constraint_name)
17
+ record(__method__, [table, constraint_name])
18
+ end
19
+
20
+ def invert_add_check(args)
21
+ table, constraint_name, _ = args
22
+ [:remove_check, [table, constraint_name]]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ module AR
2
+ module Check
3
+ module SchemaDumper
4
+ def indexes(table, stream)
5
+ super
6
+ check_constraints(table, stream)
7
+ end
8
+
9
+ def check_constraints(table, stream)
10
+ constraints = @connection.check_constraints(table)
11
+ return if constraints.empty?
12
+
13
+ constraints.each do |constraint|
14
+ statement = [
15
+ "add_check",
16
+ ":#{constraint["table"]},",
17
+ ":#{constraint["name"].gsub("_on_#{table}", "")},",
18
+ constraint["expression"][1..-2].inspect
19
+ ].join(" ")
20
+
21
+ stream.puts " #{statement}"
22
+ end
23
+
24
+ stream.puts
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ module AR
2
+ module Check
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar-check
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: 2016-04-09 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: rake
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: minitest-utils
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: pg
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: pry-meta
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: codeclimate-test-reporter
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: Enable PostgreSQL's CHECK constraints on ActiveRecord migrations
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-check.gemspec
127
+ - lib/ar-check.rb
128
+ - lib/ar/check.rb
129
+ - lib/ar/check/adapter.rb
130
+ - lib/ar/check/command_recorder.rb
131
+ - lib/ar/check/schema_dumper.rb
132
+ - lib/ar/check/version.rb
133
+ homepage: https://rubygems.org/gems/ar-check
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.5.1
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Enable PostgreSQL's CHECK constraints on ActiveRecord migrations
157
+ test_files: []