activerecord-concurrent-index 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-concurrent-index.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # ActiveRecord Concurrent Index
2
+
3
+ Extension to allow indexes to be added concurrently on supported
4
+ databases (e.g., postgres)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's `Gemfile`:
9
+
10
+ ```ruby
11
+ gem 'activerecord-concurrent-index'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ```bash
23
+ $ gem install activerecord-concurrent-index
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ Inside your database migration:
29
+
30
+ ```ruby
31
+ def self.up
32
+ add_index_concurrently :table_name, :field_to_index
33
+ end
34
+ ```
35
+
36
+ ## Why?
37
+
38
+ Postgres allows you to add an [index to a table concurrently](http://www.postgresql.org/docs/8.3/static/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY)
39
+ which means it will not block regular database operations, such as inserts,
40
+ while the index is being applied. This is especially useful on production tables
41
+ with a lot of data where the time to create an index can be measured in seconds or
42
+ minutes rather than being near-instant. During this period, inserts to the
43
+ table will be blocked and users may ultimately experience an outage.
44
+
45
+ The downside to running indexes concurrently is that they will take longer to
46
+ be applied.
47
+
48
+ ## Status
49
+
50
+ Production ready
51
+
52
+ ## Bugs, Feature Requests, etc.
53
+
54
+ If you think you've found a bug or have a feature you'd like to contribute here are the ways to let me know, in order of likelihood for a speedy resolution:
55
+
56
+ Fork the repo, add a test, implement the feature, send me a pull request
57
+ Fork the repo, add a failing test, send me a pull request
58
+ Create an issue on Github.
59
+
60
+ ## License
61
+
62
+ ActiveRecord Concurrent Index is released under the MIT license.
63
+
64
+ Copyright (c) 2012 Glenn Gillen
65
+
66
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
67
+
68
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
69
+
70
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/activerecord-concurrent-index/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Glenn Gillen"]
6
+ gem.email = ["me@glenngillen.com"]
7
+ gem.description = %q{Concurrently adds database indexes}
8
+ gem.summary = %q{Extension to allow indexes to be added concurrently on supported databases (e.g., postgres)}
9
+ gem.homepage = "https://github.com/glenngillen/activerecord-concurrent-index"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-concurrent-index"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Activerecord::Concurrent::Index::VERSION
17
+ gem.add_dependency 'activerecord'
18
+ gem.add_development_dependency 'rake'
19
+ end
@@ -0,0 +1,50 @@
1
+ require "activerecord-concurrent-index/version"
2
+
3
+ module ActiveRecord
4
+ module ConnectionAdapters # :nodoc:
5
+ module SchemaStatements
6
+ def without_transaction
7
+ original_value = ActiveRecord::Base.connection.supports_ddl_transactions?
8
+ commit_db_transaction unless outside_transaction?
9
+ ActiveRecord::Base.connection.class_eval do
10
+ def supports_ddl_transactions?
11
+ false
12
+ end
13
+ end
14
+ yield
15
+ ensure
16
+ if original_value
17
+ ActiveRecord::Base.connection.class_eval do
18
+ def supports_ddl_transactions?
19
+ true
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def add_index_concurrently(table_name, column_name, options = {})
26
+ column_names = Array.wrap(column_name)
27
+ index_name = index_name(table_name, :column => column_names)
28
+
29
+ if Hash === options # legacy support, since this param was a string
30
+ index_type = options[:unique] ? "UNIQUE" : ""
31
+ index_name = options[:name].to_s if options.key?(:name)
32
+ else
33
+ index_type = options
34
+ end
35
+
36
+ if index_name.length > index_name_length
37
+ raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
38
+ end
39
+ if index_name_exists?(table_name, index_name, false)
40
+ raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
41
+ end
42
+ quoted_column_names = quoted_columns_for_index(column_names, options).join(", ")
43
+
44
+ without_transaction do
45
+ execute "CREATE #{index_type} INDEX CONCURRENTLY #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{quoted_column_names})"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,7 @@
1
+ module Activerecord
2
+ module Concurrent
3
+ module Index
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-concurrent-index
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Glenn Gillen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70225014254500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70225014254500
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70225014253720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70225014253720
36
+ description: Concurrently adds database indexes
37
+ email:
38
+ - me@glenngillen.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - activerecord-concurrent-index.gemspec
48
+ - lib/activerecord-concurrent-index.rb
49
+ - lib/activerecord-concurrent-index/version.rb
50
+ homepage: https://github.com/glenngillen/activerecord-concurrent-index
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ segments:
63
+ - 0
64
+ hash: -4507500718456437394
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ segments:
72
+ - 0
73
+ hash: -4507500718456437394
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.15
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Extension to allow indexes to be added concurrently on supported databases
80
+ (e.g., postgres)
81
+ test_files: []