schema_plus 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +29 -0
  4. data/README.md +19 -0
  5. data/gemfiles/rails-3.2/Gemfile.mysql +7 -1
  6. data/gemfiles/rails-3.2/Gemfile.mysql2 +7 -1
  7. data/gemfiles/rails-3.2/Gemfile.postgresql +7 -1
  8. data/gemfiles/rails-3.2/Gemfile.sqlite3 +7 -1
  9. data/gemfiles/rails-4.0/Gemfile.mysql2 +7 -1
  10. data/gemfiles/rails-4.0/Gemfile.postgresql +7 -1
  11. data/gemfiles/rails-4.0/Gemfile.sqlite3 +7 -1
  12. data/gemfiles/rails-edge/Gemfile.mysql2 +7 -1
  13. data/gemfiles/rails-edge/Gemfile.postgresql +7 -1
  14. data/gemfiles/rails-edge/Gemfile.sqlite3 +7 -1
  15. data/lib/schema_plus/active_record/column_options_handler.rb +2 -2
  16. data/lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb +124 -57
  17. data/lib/schema_plus/active_record/connection_adapters/foreign_key_definition.rb +9 -2
  18. data/lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb +30 -10
  19. data/lib/schema_plus/active_record/connection_adapters/postgresql_adapter.rb +55 -30
  20. data/lib/schema_plus/active_record/connection_adapters/schema_statements.rb +1 -0
  21. data/lib/schema_plus/active_record/connection_adapters/sqlite3_adapter.rb +10 -7
  22. data/lib/schema_plus/active_record/connection_adapters/table_definition.rb +7 -16
  23. data/lib/schema_plus/active_record/migration/command_recorder.rb +88 -0
  24. data/lib/schema_plus/active_record/schema_dumper.rb +1 -1
  25. data/lib/schema_plus/version.rb +1 -1
  26. data/lib/schema_plus.rb +10 -1
  27. data/runspecs +24 -5
  28. data/spec/column_definition_spec.rb +69 -9
  29. data/spec/index_definition_spec.rb +25 -0
  30. data/spec/index_spec.rb +1 -1
  31. data/spec/migration_spec.rb +76 -11
  32. data/spec/schema_dumper_spec.rb +19 -5
  33. metadata +3 -2
@@ -150,7 +150,7 @@ describe ActiveRecord::Migration do
150
150
 
151
151
  it "should create an index if specified on column" do
152
152
  recreate_table(@model) do |t|
153
- t.integer :state, :index => true
153
+ t.integer :state, :index => true
154
154
  end
155
155
  @model.should have_index.on(:state)
156
156
  end
@@ -203,7 +203,7 @@ describe ActiveRecord::Migration do
203
203
  end
204
204
  @model.should have_index.on([:state, :city])
205
205
  end
206
-
206
+
207
207
  it "should auto-index foreign keys only" do
208
208
  recreate_table(@model) do |t|
209
209
  t.integer :user_id
@@ -234,7 +234,7 @@ describe ActiveRecord::Migration do
234
234
  end
235
235
 
236
236
  it "should override foreign key auto_index positively" do
237
- with_fk_config(:auto_index => false) do
237
+ with_fk_config(:auto_index => false) do
238
238
  recreate_table @model, :foreign_keys => {:auto_index => true} do |t|
239
239
  t.integer :user_id
240
240
  end
@@ -365,7 +365,7 @@ describe ActiveRecord::Migration do
365
365
 
366
366
  unless SchemaPlusHelpers.mysql?
367
367
  it "should override foreign key auto_index negatively" do
368
- with_fk_config(:auto_index => true) do
368
+ with_fk_config(:auto_index => true) do
369
369
  recreate_table @model, :foreign_keys => {:auto_index => false} do |t|
370
370
  t.integer :user_id
371
371
  end
@@ -386,6 +386,66 @@ describe ActiveRecord::Migration do
386
386
 
387
387
  end
388
388
 
389
+ context "when table is changed" do
390
+ before(:each) do
391
+ @model = Post
392
+ end
393
+ [false, true].each do |bulk|
394
+ suffix = bulk ? ' with :bulk option' : ""
395
+
396
+ it "should create an index if specified on column"+suffix do
397
+ change_table(@model, :bulk => bulk) do |t|
398
+ t.integer :state, :index => true
399
+ end
400
+ @model.should have_index.on(:state)
401
+ end
402
+
403
+ unless SchemaPlusHelpers.sqlite3?
404
+
405
+ it "should create a foreign key constraint"+suffix do
406
+ change_table(@model, :bulk => bulk) do |t|
407
+ t.integer :user_id
408
+ end
409
+ @model.should reference(:users, :id).on(:user_id)
410
+ end
411
+
412
+ context "migrate down" do
413
+ it "should remove a foreign key constraint"+suffix do
414
+ Comment.reset_column_information
415
+ Comment.should reference(:users, :id).on(:user_id)
416
+ migration = Class.new ::ActiveRecord::Migration do
417
+ define_method(:change) {
418
+ change_table("comments", :bulk => bulk) do |t|
419
+ t.integer :user_id
420
+ end
421
+ }
422
+ end
423
+ ActiveRecord::Migration.suppress_messages do
424
+ migration.migrate(:down)
425
+ end
426
+ Comment.reset_column_information
427
+ Comment.should_not reference(:users, :id).on(:user_id)
428
+ end
429
+ end if ActiveRecord::VERSION::MAJOR >= 4
430
+
431
+ it "should create a foreign key constraint using :references"+suffix do
432
+ change_table(@model, :bulk => bulk) do |t|
433
+ t.references :user
434
+ end
435
+ @model.should reference(:users, :id).on(:user_id)
436
+ end
437
+
438
+ it "should create a foreign key constraint using :belongs_to"+suffix do
439
+ change_table(@model, :bulk => bulk) do |t|
440
+ t.belongs_to :user
441
+ end
442
+ @model.should reference(:users, :id).on(:user_id)
443
+ end
444
+ end
445
+ end
446
+ end
447
+
448
+
389
449
  unless SchemaPlusHelpers.sqlite3?
390
450
 
391
451
  context "when column is added" do
@@ -419,8 +479,8 @@ describe ActiveRecord::Migration do
419
479
  end
420
480
 
421
481
  it "should create foreign key to explicitly given table and column name" do
422
- add_column(:author_login, :string, :foreign_key => { :references => [:users, :login]}) do
423
- @model.should reference(:users, :login).on(:author_login)
482
+ add_column(:author_login, :string, :foreign_key => { :references => [:users, :login]}) do
483
+ @model.should reference(:users, :login).on(:author_login)
424
484
  end
425
485
  end
426
486
 
@@ -504,7 +564,7 @@ describe ActiveRecord::Migration do
504
564
  it "should use default on_update action" do
505
565
  SchemaPlus.config.foreign_keys.on_update = :cascade
506
566
  add_column(:post_id, :integer) do
507
- @model.should reference.on(:post_id).on_update(:cascade)
567
+ @model.should reference.on(:post_id).on_update(:cascade)
508
568
  end
509
569
  SchemaPlus.config.foreign_keys.on_update = nil
510
570
  end
@@ -512,7 +572,7 @@ describe ActiveRecord::Migration do
512
572
  it "should use default on_delete action" do
513
573
  SchemaPlus.config.foreign_keys.on_delete = :cascade
514
574
  add_column(:post_id, :integer) do
515
- @model.should reference.on(:post_id).on_delete(:cascade)
575
+ @model.should reference.on(:post_id).on_delete(:cascade)
516
576
  end
517
577
  SchemaPlus.config.foreign_keys.on_delete = nil
518
578
  end
@@ -643,7 +703,7 @@ describe ActiveRecord::Migration do
643
703
  remove_column(:post_id)
644
704
  @model.should_not have_index.on(:post_id)
645
705
  end
646
-
706
+
647
707
  protected
648
708
  def remove_column(column_name)
649
709
  table = @model.table_name
@@ -728,7 +788,7 @@ describe ActiveRecord::Migration do
728
788
  end
729
789
 
730
790
  end
731
-
791
+
732
792
  def recreate_table(model, opts={}, &block)
733
793
  ActiveRecord::Migration.suppress_messages do
734
794
  ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
@@ -736,7 +796,12 @@ describe ActiveRecord::Migration do
736
796
  model.reset_column_information
737
797
  end
738
798
 
739
-
799
+ def change_table(model, opts={}, &block)
800
+ ActiveRecord::Migration.suppress_messages do
801
+ ActiveRecord::Migration.change_table model.table_name, opts, &block
802
+ end
803
+ model.reset_column_information
804
+ end
740
805
 
741
806
  end
742
807
 
@@ -9,7 +9,7 @@ describe "Schema dump" do
9
9
  end
10
10
  ActiveRecord::Migration.suppress_messages do
11
11
  ActiveRecord::Schema.define do
12
- connection.tables.each do |table| drop_table table end
12
+ connection.tables.each do |table| drop_table table, :cascade => true end
13
13
 
14
14
  create_table :users, :force => true do |t|
15
15
  t.string :login
@@ -91,7 +91,7 @@ describe "Schema dump" do
91
91
  if SchemaPlusHelpers.postgresql?
92
92
  it "should dump the default hash expr as now()" do
93
93
  with_additional_column Post, :posted_at, :datetime, :default => :now do
94
- dump_posts.should match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"now\(\)"\s*\}})
94
+ dump_posts.should match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}})
95
95
  end
96
96
  end
97
97
 
@@ -103,7 +103,7 @@ describe "Schema dump" do
103
103
 
104
104
  it "can dump a complex default expression" do
105
105
  with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
106
- dump_posts.should match(%r{t\.string\s+"name",\s*(?:default:|:default =>)\s*{\s*(?:expr:|:expr =>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
106
+ dump_posts.should match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
107
107
  end
108
108
  end
109
109
  end
@@ -164,8 +164,8 @@ describe "Schema dump" do
164
164
  unless SchemaPlusHelpers.mysql?
165
165
 
166
166
  it "should include index order" do
167
- with_index Post, [:user_id, :first_comment_id], :order => { :user_id => :asc, :first_comment_id => :desc } do
168
- dump_posts.should match(%r{t.index \["user_id", "first_comment_id"\],.*:order => {"user_id" => :asc, "first_comment_id" => :desc}})
167
+ with_index Post, [:user_id, :first_comment_id, :short_id], :order => { :user_id => :asc, :first_comment_id => :desc } do
168
+ dump_posts.should match(%r{t.index \["user_id", "first_comment_id", "short_id"\],.*:order => {"user_id" => :asc, "first_comment_id" => :desc, "short_id" => :asc}})
169
169
  end
170
170
  end
171
171
 
@@ -179,6 +179,13 @@ describe "Schema dump" do
179
179
  end
180
180
  end
181
181
 
182
+ it "should define index with type cast" do
183
+ with_index Post, [:integer_col], :name => "index_with_type_cast", :expression => "LOWER(integer_col::text)" do
184
+ dump_posts.should match(to_regexp(%q{t.index :name => "index_with_type_cast", :expression => "lower((integer_col)::text)"}))
185
+ end
186
+ end
187
+
188
+
182
189
  it "should define case insensitive index with mixed ids and strings" do
183
190
  with_index Post, [:user_id, :str_short, :short_id, :body], :case_sensitive => false do
184
191
  dump_posts.should match(to_regexp(%q{t.index ["user_id", "str_short", "short_id", "body"], :name => "index_posts_on_user_id_and_str_short_and_short_id_and_body", :case_sensitive => false}))
@@ -219,6 +226,13 @@ describe "Schema dump" do
219
226
  end
220
227
  end
221
228
 
229
+ it "should not include index order for non-ordered index types" do
230
+ with_index Post, :user_id, :kind => :hash do
231
+ dump_posts.should match(to_regexp(%q{t.index ["user_id"], :name => "index_posts_on_user_id", :kind => "hash"}))
232
+ dump_posts.should_not match(%r{:order})
233
+ end
234
+ end
235
+
222
236
  end
223
237
 
224
238
  unless SchemaPlusHelpers.sqlite3?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-13 00:00:00.000000000 Z
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -141,6 +141,7 @@ files:
141
141
  - lib/schema_plus/active_record/connection_adapters/table_definition.rb
142
142
  - lib/schema_plus/active_record/db_default.rb
143
143
  - lib/schema_plus/active_record/foreign_keys.rb
144
+ - lib/schema_plus/active_record/migration/command_recorder.rb
144
145
  - lib/schema_plus/active_record/schema.rb
145
146
  - lib/schema_plus/active_record/schema_dumper.rb
146
147
  - lib/schema_plus/railtie.rb