pg_index_where 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWEzMzNmMGFhZjg4ZDkzNjdmODhmNjk1ZGM4NDNkZmU2NzJjNjdmOA==
5
+ data.tar.gz: !binary |-
6
+ YjJkMzFkZDhiYzAyZjYxOTQ0YTk2Nzc1YWEyZDNjMzc2ODBmZjdkMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YThkNzVhZTQxMWNiMTM2NTVkOWI0OGQwZGVhY2RlZmUyM2YyODYxZDdmMGM5
10
+ ZTcwN2QyNWY3M2I4Y2U4YzdiNzAzNDc3YWU1YmVjNzk4M2Y5MjQ3YmU0MTk1
11
+ NTEzYzRhY2U5MDQzYWVmYjQwOGM4OGYyMjNhZTQzNDc1NGJhOTY=
12
+ data.tar.gz: !binary |-
13
+ MDIwOWFmY2E4NGYxMzg5NzA3MjVkMzY4MmE1NmY2NTllYTAyNGMzZDE3Njcw
14
+ ZjdkZGJkMDEyZDc3ZjdlNjA1YmRiZjA1NDNmNTk5NTVhOGQyYzhjNTMxMmM4
15
+ MTJmNzk2MDMyNGI2ODY2OGQyMDM5ZGFlMDMwZDJhODBjZjY1MjE=
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
19
+ log/
20
+ spec/db/schema.rb
@@ -0,0 +1 @@
1
+ 1.9.3-p429
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_index_where.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Yamamoto Kazuhisa
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # PgIndexWhere
2
+
3
+ When create unique index on PostgreSQL, you can speficy WHERE statement as you know.
4
+
5
+ pg_index_where will provide this function with migrate the database.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'pg_index_where'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pg_index_where
20
+
21
+ ## Usage
22
+
23
+ Add this option your migrate files.
24
+
25
+ :where => 'deleted_at is null'
26
+
27
+ Examples.
28
+
29
+ class AddIndex < ActiveRecord::Migration
30
+ def up
31
+ add_index "customers", ["code"], :name => "customers_idx01", :unique => true, :where => 'deleted_at is null'
32
+ end
33
+
34
+ def down
35
+ remove_index "customers", :name => "customers_idx01"
36
+ end
37
+ end
38
+
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ require 'pg_index_where/version'
2
+ require 'pg_index_where/schema_statements'
3
+ require 'pg_index_where/schema_dumper'
@@ -0,0 +1,41 @@
1
+ module ActiveRecord
2
+ class SchemaDumper
3
+ private
4
+ def indexes(table, stream)
5
+ if (indexes = @connection.indexes(table)).any?
6
+ add_index_statements = indexes.map do |index|
7
+ statement_parts = [
8
+ ('add_index ' + remove_prefix_and_suffix(index.table).inspect),
9
+ index.columns.inspect,
10
+ (':name => ' + index.name.inspect),
11
+ ]
12
+ statement_parts << ':unique => true' if index.unique
13
+
14
+ index_lengths = (index.lengths || []).compact
15
+ statement_parts << (':length => ' + Hash[index.columns.zip(index.lengths)].inspect) unless index_lengths.empty?
16
+
17
+ index_orders = (index.orders || {})
18
+ statement_parts << (':order => ' + index.orders.inspect) unless index_orders.empty?
19
+
20
+ index_where = index_with_where(index.name)
21
+ statement_parts << (':where => ' + index_where.inspect) unless index_where.nil?
22
+
23
+ ' ' + statement_parts.join(', ')
24
+ end
25
+
26
+ stream.puts add_index_statements.sort.join("\n")
27
+ stream.puts
28
+ end
29
+ end
30
+
31
+ def index_with_where(name)
32
+ sql = <<-EOS
33
+ select pg_get_expr(i.indpred, i.indrelid) from pg_class c
34
+ inner join pg_index i on c.oid = i.indexrelid
35
+ where c.relname = '#{name}';
36
+ EOS
37
+ query_result = @connection.exec_query(sql).first
38
+ query_result ? query_result['pg_get_expr'] : nil
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ module SchemaStatements
4
+ def add_index(table_name, column_name, options = {})
5
+ where = options[:where] ? "where #{options[:where]}" : ''
6
+ index_name, index_type, index_columns = add_index_options(table_name, column_name, options)
7
+ execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns}) #{where}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module PgIndexWhere
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_index_where/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pg_index_where'
8
+ spec.version = PgIndexWhere::VERSION
9
+ spec.authors = ['Yamamoto Kazuhisa']
10
+ spec.email = ['ak.hisashi@gmail.com']
11
+ spec.description = %q{pg_index_where will provide unique index with WHERE statement on PostgreSQL with migrate the database on Rails.}
12
+ spec.summary = %q{When create unique index on PostgreSQL, you can speficy WHERE statement as you know. pg_index_where will provide this function with migrate the database.}
13
+ spec.homepage = 'https://github.com/kazuhisa/pg_index_where'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.13'
24
+ spec.add_development_dependency 'rails', '>= 3.0.7'
25
+ spec.add_development_dependency 'rspec-rails'
26
+ spec.add_development_dependency 'acts_as_paranoid'
27
+ spec.add_dependency 'pg', '~> 0.15'
28
+ spec.add_dependency 'activerecord', '>= 3.0.7'
29
+ end
@@ -0,0 +1,11 @@
1
+ class CreateCustomers < ActiveRecord::Migration
2
+ def change
3
+ create_table :customers do |t|
4
+ t.string :name
5
+ t.string :code
6
+
7
+ t.datetime :deleted_at
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class AddIndex < ActiveRecord::Migration
2
+ def up
3
+ add_index :customers, :code, :name => 'customers_idx01', :unique => true, :where => 'deleted_at IS NULL'
4
+ end
5
+
6
+ def down
7
+ remove_index :customers, :name => 'customers_idx01'
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'active_record'
3
+ require 'action_controller'
4
+ require 'pg_index_where'
5
+ require 'acts_as_paranoid'
6
+
7
+ # database
8
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'postgresql', :database => 'test_pg_index_where',
9
+ :min_messages => 'WARNING'}}
10
+ ActiveRecord::Base.establish_connection('test')
11
+
12
+ # models
13
+ class Customer < ActiveRecord::Base
14
+ acts_as_paranoid
15
+ attr_accessible :name, :code
16
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'ActiveRecord::ConnectionAdapters::SchemaStatements.add_index' do
4
+ before do
5
+ ActiveRecord::Migrator.migrate(%w(spec/db/migrate), nil)
6
+ end
7
+ context 'migration is performed.' do
8
+ subject do
9
+ sql = "select pg_get_indexdef(relfilenode) from pg_class where relname = 'customers_idx01'"
10
+ ActiveRecord::Base.connection.execute(sql)[0]
11
+ end
12
+ its(['pg_get_indexdef']){should =~ /where.+deleted_at IS NULL/i}
13
+ end
14
+
15
+ describe 'Tests for acts_as_paranoid' do
16
+ context 'Insertion on a table' do
17
+ it do
18
+ expect do
19
+ Customer.create(:code => '001', :name => 'Yukari Tamura')
20
+ Customer.create(:code => '002', :name => 'Nana Mizuki')
21
+ end.to change{Customer.scoped.size}.from(0).to(2)
22
+ end
23
+ end
24
+ context 'When a deleted code is inserted.' do
25
+ before do
26
+ Customer.destroy_all(:code => '001')
27
+ end
28
+ it do
29
+ expect do
30
+ Customer.create(:code => '001', :name => 'Yui Horie')
31
+ end.to change{Customer.scoped.size}.from(1).to(2)
32
+ end
33
+
34
+ context 'When the duplicate code is inserted.' do
35
+ before do
36
+ Customer.create(:code => '001', :name => 'Yui Horie')
37
+ end
38
+ it do
39
+ expect do
40
+ Customer.create!(:code => '001', :name => 'Yukari Tamura')
41
+ end.to raise_error(ActiveRecord::RecordNotUnique)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ describe 'ActiveRecord::SchemaDumper.dump' do
48
+ before do
49
+ filename = 'spec/db/schema.rb'
50
+ File.open(filename, 'w:utf-8') do |file|
51
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
52
+ end
53
+ end
54
+ subject{open('spec/db/schema.rb','r').read}
55
+ it{should =~ /:where => "\(deleted_at IS NULL\)"/}
56
+ end
57
+ end
58
+
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rails'
4
+ require 'rspec'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+ require File.join(File.dirname(__FILE__), 'fake_app')
10
+
11
+ require 'rspec/rails'
12
+
13
+ RSpec.configure do |config|
14
+ config.before :all do
15
+ ActiveRecord::Migration.verbose = false
16
+ ActiveRecord::Migrator.rollback(%w(spec/db/migrate), 2)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_index_where
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yamamoto Kazuhisa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
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: acts_as_paranoid
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: pg
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.15'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.15'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.7
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: 3.0.7
125
+ description: pg_index_where will provide unique index with WHERE statement on PostgreSQL
126
+ with migrate the database on Rails.
127
+ email:
128
+ - ak.hisashi@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .ruby-version
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - lib/pg_index_where.rb
140
+ - lib/pg_index_where/schema_dumper.rb
141
+ - lib/pg_index_where/schema_statements.rb
142
+ - lib/pg_index_where/version.rb
143
+ - pg_index_where.gemspec
144
+ - spec/db/migrate/20130518094839_create_customers.rb
145
+ - spec/db/migrate/20130518101149_add_index.rb
146
+ - spec/fake_app.rb
147
+ - spec/pg_index_where_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: https://github.com/kazuhisa/pg_index_where
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 2.0.3
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: When create unique index on PostgreSQL, you can speficy WHERE statement as
173
+ you know. pg_index_where will provide this function with migrate the database.
174
+ test_files:
175
+ - spec/db/migrate/20130518094839_create_customers.rb
176
+ - spec/db/migrate/20130518101149_add_index.rb
177
+ - spec/fake_app.rb
178
+ - spec/pg_index_where_spec.rb
179
+ - spec/spec_helper.rb