schema_plus_indexes 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +8 -12
- data/lib/schema_plus/indexes.rb +1 -0
- data/lib/schema_plus/indexes/active_record/migration/command_recorder.rb +16 -0
- data/lib/schema_plus/indexes/middleware/dumper.rb +2 -2
- data/lib/schema_plus/indexes/middleware/migration.rb +1 -1
- data/lib/schema_plus/indexes/version.rb +1 -1
- data/schema_plus_indexes.gemspec +1 -1
- data/spec/command_recorder_spec.rb +52 -0
- data/spec/index_definition_spec.rb +1 -7
- data/spec/index_spec.rb +3 -9
- data/spec/migration_spec.rb +6 -12
- data/spec/schema_dumper_spec.rb +33 -40
- data/spec/spec_helper.rb +9 -6
- data/spec/support/enableable.rb +34 -0
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df1b409660d57bf87cb28e5b46ff7f4121d9b4db
|
4
|
+
data.tar.gz: afb439a86e897f10b6bab354f5f0643d39f3b7f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cf73bec7659a3e32a047fa092352ca1a9fd3de1d3fb3c78bdeba59459413b62550771aa6e7cad2198fc255ca56d995c85826a1b14f2c471e4e85fa2c3d0dc4c
|
7
|
+
data.tar.gz: e14c80880a1746ce6d03108b7566126682d2d570adc37144dce9b6fdab8f5aa2379d563660f91c41269f3be0b3e977db5b9b0dc2a59285bdbae894cfedd13470
|
data/.travis.yml
CHANGED
@@ -12,7 +12,7 @@ gemfile:
|
|
12
12
|
- gemfiles/activerecord-4.2/Gemfile.sqlite3
|
13
13
|
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
|
14
14
|
addons:
|
15
|
-
postgresql: '9.
|
15
|
+
postgresql: '9.4'
|
16
16
|
before_script: bundle exec rake create_databases
|
17
17
|
after_script: bundle exec rake drop_databases
|
18
18
|
script: bundle exec rake travis
|
data/README.md
CHANGED
@@ -20,14 +20,6 @@ gem "schema_plus_indexes" # in a Gemfile
|
|
20
20
|
gem.add_dependency "schema_plus_indexes" # in a .gemspec
|
21
21
|
```
|
22
22
|
|
23
|
-
To use with a rails app, also include
|
24
|
-
|
25
|
-
```ruby
|
26
|
-
gem "schema_monkey_rails"
|
27
|
-
```
|
28
|
-
|
29
|
-
which creates a Railtie to that will insert SchemaPlus::Indexes appropriately into the rails stack. To use with Padrino, see [schema_monkey_padrino](https://github.com/SchemaPlus/schema_monkey_padrino).
|
30
|
-
|
31
23
|
<!-- SCHEMA_DEV: TEMPLATE INSTALLATION - end -->
|
32
24
|
|
33
25
|
|
@@ -101,11 +93,12 @@ The value gets cached until the next time `Model.reset_column_information` is ca
|
|
101
93
|
|
102
94
|
* When using SQLite3, makes sure that the definitions returned by
|
103
95
|
`connection.indexes` properly include the column orders (`:asc` or `:desc`)
|
104
|
-
|
96
|
+
|
105
97
|
* For the `ActiveRecord::ConnectionAdapters::IndexDefinition` class (the object that's returned by `connection.indexes`), SchemaPlus::Indexes:
|
106
98
|
* Provides an `==` operator to compare if two objects refer to an equivalent index
|
107
99
|
* Allows calling `new` with a signature that matches add_index: `IndexDefinition.new(table_name, column_names, options)`
|
108
100
|
* Fleshes out the `:orders` attribute, listing `:asc` for a column instead of leaving it undefined.
|
101
|
+
* Prevents errors from a down :change migration attempting to remove an index that wasn't previously added (this can arise, e.g. with auto-indexing plugins).
|
109
102
|
|
110
103
|
## Compatibility
|
111
104
|
|
@@ -119,6 +112,10 @@ schema_plus_indexes is tested on
|
|
119
112
|
|
120
113
|
## History
|
121
114
|
|
115
|
+
### v0.2.0
|
116
|
+
|
117
|
+
* Prevent down :change migrations from failing due to trying to remove non-existent indexes
|
118
|
+
|
122
119
|
### v0.1.0
|
123
120
|
|
124
121
|
* Initial release, extracted from schema_plus 1.x
|
@@ -155,8 +152,8 @@ Some things to know about to help you develop and test:
|
|
155
152
|
provides middleware callback stacks to make it easy to extend
|
156
153
|
ActiveRecord's behavior. If that API is missing something you need for
|
157
154
|
your contribution, please head over to
|
158
|
-
[schema_plus_core](https://github/SchemaPlus/schema_plus_core) and open
|
159
|
-
an issue or pull request.
|
155
|
+
[schema_plus_core](https://github.com/SchemaPlus/schema_plus_core) and open
|
156
|
+
an issue or pull request.
|
160
157
|
|
161
158
|
<!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_PLUS_CORE - end -->
|
162
159
|
<!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - begin -->
|
@@ -165,6 +162,5 @@ Some things to know about to help you develop and test:
|
|
165
162
|
[schema_monkey](https://github.com/SchemaPlus/schema_monkey) client,
|
166
163
|
using [schema_monkey](https://github.com/SchemaPlus/schema_monkey)'s
|
167
164
|
convention-based protocols for extending ActiveRecord and using middleware stacks.
|
168
|
-
For more information see [schema_monkey](https://github.com/SchemaPlus/schema_monkey)'s README.
|
169
165
|
|
170
166
|
<!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - end -->
|
data/lib/schema_plus/indexes.rb
CHANGED
@@ -3,6 +3,7 @@ require 'schema_plus/core'
|
|
3
3
|
require_relative 'indexes/active_record/base'
|
4
4
|
require_relative 'indexes/active_record/connection_adapters/abstract_adapter'
|
5
5
|
require_relative 'indexes/active_record/connection_adapters/index_definition'
|
6
|
+
require_relative 'indexes/active_record/migration/command_recorder'
|
6
7
|
require_relative 'indexes/middleware/dumper'
|
7
8
|
require_relative 'indexes/middleware/migration'
|
8
9
|
require_relative 'indexes/middleware/model'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SchemaPlus::Indexes
|
2
|
+
module ActiveRecord
|
3
|
+
module Migration
|
4
|
+
module CommandRecorder
|
5
|
+
|
6
|
+
# inversion of add_index will be remove_index. add if_exists
|
7
|
+
# option for cases where the index doesn't actually exist
|
8
|
+
def invert_add_index(args)
|
9
|
+
super.tap { |(command, (arg, options))|
|
10
|
+
options[:if_exists] = true
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -9,12 +9,12 @@ module SchemaPlus::Indexes
|
|
9
9
|
# list of indexes that AR would dump after the table. Any left
|
10
10
|
# over will still be dumped by AR.
|
11
11
|
env.table.columns.each do |column|
|
12
|
-
|
12
|
+
|
13
13
|
# first check for a single-column index
|
14
14
|
if (index = env.table.indexes.find(&its.columns == [column.name]))
|
15
15
|
column.add_option column_index(env, column, index)
|
16
16
|
env.table.indexes.delete(index)
|
17
|
-
|
17
|
+
|
18
18
|
# then check for the first of a multi-column index
|
19
19
|
elsif (index = env.table.indexes.find(&its.columns.first == column.name))
|
20
20
|
column.add_option column_index(env, column, index)
|
data/schema_plus_indexes.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
|
|
23
23
|
gem.add_development_dependency "bundler", "~> 1.7"
|
24
24
|
gem.add_development_dependency "rake", "~> 10.0"
|
25
25
|
gem.add_development_dependency "rspec", "~> 3.0.0"
|
26
|
-
gem.add_development_dependency "schema_dev", "~> 3.
|
26
|
+
gem.add_development_dependency "schema_dev", "~> 3.3"
|
27
27
|
gem.add_development_dependency "simplecov"
|
28
28
|
gem.add_development_dependency "simplecov-gem-profile"
|
29
29
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AddIndexBefore
|
4
|
+
module Middleware
|
5
|
+
module Migration
|
6
|
+
module Column
|
7
|
+
include Enableable
|
8
|
+
|
9
|
+
def before(env)
|
10
|
+
env.options[:index] = { name: 'whatever' } if enabled_middleware(AddIndexBefore, env)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
SchemaMonkey.register AddIndexBefore
|
18
|
+
|
19
|
+
describe "Command Recorder" do
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
define_schema do
|
23
|
+
create_table "comments" do |t|
|
24
|
+
t.integer :column
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with add-index-before middleware enabled" do
|
30
|
+
let(:middleware) { AddIndexBefore::Middleware::Migration::Column }
|
31
|
+
around(:each) do |example|
|
32
|
+
begin
|
33
|
+
example.run
|
34
|
+
ensure
|
35
|
+
middleware.disable
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not fail when reverting" do
|
40
|
+
migration = Class.new ::ActiveRecord::Migration do
|
41
|
+
define_method(:change) {
|
42
|
+
change_table("comments") do |t|
|
43
|
+
t.integer :column
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
middleware.enable
|
48
|
+
expect { migration.migrate(:down) }.not_to raise_error
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -5,7 +5,7 @@ describe "Index definition" do
|
|
5
5
|
|
6
6
|
let(:migration) { ::ActiveRecord::Migration }
|
7
7
|
|
8
|
-
before(:
|
8
|
+
before(:each) do
|
9
9
|
define_schema(:auto_create => false) do
|
10
10
|
create_table :users, :force => true do |t|
|
11
11
|
t.string :login
|
@@ -23,12 +23,6 @@ describe "Index definition" do
|
|
23
23
|
class Post < ::ActiveRecord::Base ; end
|
24
24
|
end
|
25
25
|
|
26
|
-
around(:each) do |example|
|
27
|
-
migration.suppress_messages do
|
28
|
-
example.run
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
26
|
after(:each) do
|
33
27
|
migration.remove_index :users, :name => 'users_login_index' if migration.index_name_exists? :users, 'users_login_index', true
|
34
28
|
end
|
data/spec/index_spec.rb
CHANGED
@@ -30,9 +30,7 @@ describe "index" do
|
|
30
30
|
|
31
31
|
|
32
32
|
after(:each) do
|
33
|
-
migration.
|
34
|
-
migration.remove_index(:users, :name => @index.name) if (@index ||= nil)
|
35
|
-
end
|
33
|
+
migration.remove_index(:users, :name => @index.name) if (@index ||= nil)
|
36
34
|
end
|
37
35
|
|
38
36
|
it "should create index when called without additional options" do
|
@@ -164,16 +162,12 @@ describe "index" do
|
|
164
162
|
|
165
163
|
protected
|
166
164
|
def add_index(*args)
|
167
|
-
migration.
|
168
|
-
migration.add_index(*args)
|
169
|
-
end
|
165
|
+
migration.add_index(*args)
|
170
166
|
User.reset_column_information
|
171
167
|
end
|
172
168
|
|
173
169
|
def remove_index(*args)
|
174
|
-
migration.
|
175
|
-
migration.remove_index(*args)
|
176
|
-
end
|
170
|
+
migration.remove_index(*args)
|
177
171
|
User.reset_column_information
|
178
172
|
end
|
179
173
|
|
data/spec/migration_spec.rb
CHANGED
@@ -159,28 +159,22 @@ describe ActiveRecord::Migration do
|
|
159
159
|
|
160
160
|
def add_column(column_name, *args)
|
161
161
|
table = @model.table_name
|
162
|
-
ActiveRecord::Migration.
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
ActiveRecord::Migration.remove_column(table, column_name)
|
167
|
-
end
|
162
|
+
ActiveRecord::Migration.add_column(table, column_name, *args)
|
163
|
+
@model.reset_column_information
|
164
|
+
yield if block_given?
|
165
|
+
ActiveRecord::Migration.remove_column(table, column_name)
|
168
166
|
end
|
169
167
|
|
170
168
|
end
|
171
169
|
|
172
170
|
|
173
171
|
def recreate_table(model, opts={}, &block)
|
174
|
-
ActiveRecord::Migration.
|
175
|
-
ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
|
176
|
-
end
|
172
|
+
ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
|
177
173
|
model.reset_column_information
|
178
174
|
end
|
179
175
|
|
180
176
|
def change_table(model, opts={}, &block)
|
181
|
-
ActiveRecord::Migration.
|
182
|
-
ActiveRecord::Migration.change_table model.table_name, opts, &block
|
183
|
-
end
|
177
|
+
ActiveRecord::Migration.change_table model.table_name, opts, &block
|
184
178
|
model.reset_column_information
|
185
179
|
end
|
186
180
|
|
data/spec/schema_dumper_spec.rb
CHANGED
@@ -3,40 +3,37 @@ require 'stringio'
|
|
3
3
|
|
4
4
|
describe "Schema dump" do
|
5
5
|
|
6
|
-
before(:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
t.integer :post_id
|
38
|
-
t.integer :commenter_id
|
39
|
-
end
|
6
|
+
before(:each) do
|
7
|
+
define_schema do
|
8
|
+
|
9
|
+
create_table :users, :force => true do |t|
|
10
|
+
t.string :login
|
11
|
+
t.datetime :deleted_at
|
12
|
+
t.integer :first_post_id
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table :posts, :force => true do |t|
|
16
|
+
t.text :body
|
17
|
+
t.integer :user_id
|
18
|
+
t.integer :first_comment_id
|
19
|
+
t.string :string_no_default
|
20
|
+
t.integer :short_id
|
21
|
+
t.string :str_short
|
22
|
+
t.integer :integer_col
|
23
|
+
t.float :float_col
|
24
|
+
t.decimal :decimal_col
|
25
|
+
t.datetime :datetime_col
|
26
|
+
t.timestamp :timestamp_col
|
27
|
+
t.time :time_col
|
28
|
+
t.date :date_col
|
29
|
+
t.binary :binary_col
|
30
|
+
t.boolean :boolean_col
|
31
|
+
end
|
32
|
+
|
33
|
+
create_table :comments, :force => true do |t|
|
34
|
+
t.text :body
|
35
|
+
t.integer :post_id
|
36
|
+
t.integer :commenter_id
|
40
37
|
end
|
41
38
|
end
|
42
39
|
class ::User < ActiveRecord::Base ; end
|
@@ -83,16 +80,12 @@ describe "Schema dump" do
|
|
83
80
|
def with_index(*args)
|
84
81
|
options = args.extract_options!
|
85
82
|
model, columns = args
|
86
|
-
ActiveRecord::Migration.
|
87
|
-
ActiveRecord::Migration.add_index(model.table_name, columns, options)
|
88
|
-
end
|
83
|
+
ActiveRecord::Migration.add_index(model.table_name, columns, options)
|
89
84
|
model.reset_column_information
|
90
85
|
begin
|
91
86
|
yield
|
92
87
|
ensure
|
93
|
-
ActiveRecord::Migration.
|
94
|
-
ActiveRecord::Migration.remove_index(model.table_name, :name => determine_index_name(model, columns, options))
|
95
|
-
end
|
88
|
+
ActiveRecord::Migration.remove_index(model.table_name, :name => determine_index_name(model, columns, options))
|
96
89
|
end
|
97
90
|
end
|
98
91
|
|
data/spec/spec_helper.rb
CHANGED
@@ -17,16 +17,19 @@ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
|
17
17
|
RSpec.configure do |config|
|
18
18
|
config.include(SchemaPlus::IndexesMatchers)
|
19
19
|
config.warnings = true
|
20
|
+
config.around(:each) do |example|
|
21
|
+
ActiveRecord::Migration.suppress_messages do
|
22
|
+
example.run
|
23
|
+
end
|
24
|
+
end
|
20
25
|
end
|
21
26
|
|
22
27
|
def define_schema(config={}, &block)
|
23
|
-
ActiveRecord::
|
24
|
-
|
25
|
-
|
26
|
-
drop_table table, :force => :cascade
|
27
|
-
end
|
28
|
-
instance_eval &block
|
28
|
+
ActiveRecord::Schema.define do
|
29
|
+
connection.tables.each do |table|
|
30
|
+
drop_table table, :force => :cascade
|
29
31
|
end
|
32
|
+
instance_eval &block
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Enableable
|
2
|
+
|
3
|
+
def enabled_middleware(root, env)
|
4
|
+
middleware = self.singleton_class.ancestors.find(&it.to_s.start_with?("#{root}::Middleware"))
|
5
|
+
return nil unless middleware.enabled?(env)
|
6
|
+
middleware.disable if middleware.once?(env)
|
7
|
+
middleware
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.module_eval do
|
12
|
+
def self.enable(condition = true, once:true)
|
13
|
+
@enabled = condition
|
14
|
+
@once = once
|
15
|
+
end
|
16
|
+
def self.enabled?(env)
|
17
|
+
case @enabled
|
18
|
+
when Proc then @enabled.call(env)
|
19
|
+
else @enabled
|
20
|
+
end
|
21
|
+
end
|
22
|
+
def self.once?(env)
|
23
|
+
@once
|
24
|
+
end
|
25
|
+
def self.disable
|
26
|
+
@enabled = false
|
27
|
+
end
|
28
|
+
|
29
|
+
disable
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_plus_indexes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -86,14 +86,14 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '3.
|
89
|
+
version: '3.3'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '3.
|
96
|
+
version: '3.3'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: simplecov
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -144,6 +144,7 @@ files:
|
|
144
144
|
- lib/schema_plus/indexes/active_record/base.rb
|
145
145
|
- lib/schema_plus/indexes/active_record/connection_adapters/abstract_adapter.rb
|
146
146
|
- lib/schema_plus/indexes/active_record/connection_adapters/index_definition.rb
|
147
|
+
- lib/schema_plus/indexes/active_record/migration/command_recorder.rb
|
147
148
|
- lib/schema_plus/indexes/middleware/dumper.rb
|
148
149
|
- lib/schema_plus/indexes/middleware/migration.rb
|
149
150
|
- lib/schema_plus/indexes/middleware/model.rb
|
@@ -152,12 +153,14 @@ files:
|
|
152
153
|
- lib/schema_plus_indexes.rb
|
153
154
|
- schema_dev.yml
|
154
155
|
- schema_plus_indexes.gemspec
|
156
|
+
- spec/command_recorder_spec.rb
|
155
157
|
- spec/index_definition_spec.rb
|
156
158
|
- spec/index_spec.rb
|
157
159
|
- spec/migration_spec.rb
|
158
160
|
- spec/named_schema_spec.rb
|
159
161
|
- spec/schema_dumper_spec.rb
|
160
162
|
- spec/spec_helper.rb
|
163
|
+
- spec/support/enableable.rb
|
161
164
|
- spec/support/matchers/have_index.rb
|
162
165
|
homepage: https://github.com/SchemaPlus/schema_plus_indexes
|
163
166
|
licenses:
|
@@ -184,10 +187,12 @@ signing_key:
|
|
184
187
|
specification_version: 4
|
185
188
|
summary: Adds shorthands and conveniences to ActiveRecord's handling of indexes
|
186
189
|
test_files:
|
190
|
+
- spec/command_recorder_spec.rb
|
187
191
|
- spec/index_definition_spec.rb
|
188
192
|
- spec/index_spec.rb
|
189
193
|
- spec/migration_spec.rb
|
190
194
|
- spec/named_schema_spec.rb
|
191
195
|
- spec/schema_dumper_spec.rb
|
192
196
|
- spec/spec_helper.rb
|
197
|
+
- spec/support/enableable.rb
|
193
198
|
- spec/support/matchers/have_index.rb
|