schema_plus 2.0.0.pre12 → 2.0.0.pre13
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 +5 -3
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +1 -1
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +11 -0
- data/lib/schema_plus/version.rb +1 -1
- data/spec/schema_plus_foreign_keys/migration_spec.rb +65 -47
- data/spec/schema_plus_foreign_keys/schema_dumper_spec.rb +9 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 097dc1df079f835526329efab67628e6b5c80998
|
4
|
+
data.tar.gz: 7953ae4520025b892c725a0821246c734ef685ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19cfecf2e4ad25081836fd1fcb976cb111a4c0163254d65e29b4325e4a366c482423bc7657aa97b3c3d6588e6086cccdf910341dc610ca4fa8961b5456aca664
|
7
|
+
data.tar.gz: c1d32ccfff88705ace650204505aec9887930e0ed0df7e8f623be1e04c2d6c5142f6709b2f1eb5fb9537bed9c89abe32f6d11864b072833dfdcdd768a132ae65
|
data/.travis.yml
CHANGED
@@ -15,7 +15,7 @@ gemfile:
|
|
15
15
|
- gemfiles/activerecord-4.2.1/Gemfile.sqlite3
|
16
16
|
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
|
17
17
|
addons:
|
18
|
-
postgresql: '9.
|
18
|
+
postgresql: '9.4'
|
19
19
|
before_script: bundle exec rake create_databases
|
20
20
|
after_script: bundle exec rake drop_databases
|
21
21
|
script: bundle exec rake travis
|
data/README.md
CHANGED
@@ -35,13 +35,15 @@ See detailed documentation in each gem's README.
|
|
35
35
|
[![Dependency Status](https://gemnasium.com/lomba/schema_plus.svg)](https://gemnasium.com/SchemaPlus/schema_plus)
|
36
36
|
|
37
37
|
|
38
|
-
> ## This is the README for schema_plus 2.0.0
|
39
|
-
> which
|
38
|
+
> ## This is the README for schema_plus 2.0.0.pre13
|
39
|
+
> which supports Rails >= 4.2.0. This prerelease is completely usable. It's still officially a prerelease rather than formal release because some features have yet to be migrated into their own gems.
|
40
|
+
>
|
41
|
+
> For info about the 1.x releases which support Rails 3.1, 4.0, 4.1, and 4.2.0, see the [schema_plus 1.x](https://github.com/SchemaPlus/schema_plus/tree/1.x) branch
|
40
42
|
|
41
43
|
---
|
42
44
|
|
43
45
|
|
44
|
-
The `schema_plus` gem is a wrapper that pulls in a common collection of gems from the SchemaPlus family. But you can feel free to mix and match to get just the gems you want.
|
46
|
+
The `schema_plus` gem is a wrapper that pulls in a common collection of gems from the SchemaPlus family. But you can feel free to ignore this gem and mix and match to get just the gems you want.
|
45
47
|
|
46
48
|
Note: Prior to version 2.0, `schema_plus` was a single monolothic gem that implemented in itself all the features that are now included by the wrapper.
|
47
49
|
|
data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb
CHANGED
@@ -82,7 +82,7 @@ module SchemaPlus::ForeignKeys
|
|
82
82
|
val_or_array = -> val { val.is_a?(Array) ? "[#{val.map(&:inspect).join(', ')}]" : val.inspect }
|
83
83
|
|
84
84
|
dump << ", column: #{val_or_array.call self.column}" unless column
|
85
|
-
dump << ", primary_key: #{val_or_array.call self.
|
85
|
+
dump << ", primary_key: #{val_or_array.call self.primary_key}" if custom_primary_key?
|
86
86
|
dump << ", name: #{name.inspect}" if name
|
87
87
|
dump << ", on_update: #{on_update.inspect}" if on_update
|
88
88
|
dump << ", on_delete: #{on_delete.inspect}" if on_delete
|
@@ -18,6 +18,17 @@ module SchemaPlus::ForeignKeys
|
|
18
18
|
rename_foreign_keys(oldname, newname)
|
19
19
|
end
|
20
20
|
|
21
|
+
def copy_table(*args, &block)
|
22
|
+
fk_override = { :auto_create => false, :auto_index => false }
|
23
|
+
save = Hash[fk_override.keys.collect{|key| [key, SchemaPlus::ForeignKeys.config.send(key)]}]
|
24
|
+
begin
|
25
|
+
SchemaPlus::ForeignKeys.config.update_attributes(fk_override)
|
26
|
+
super
|
27
|
+
ensure
|
28
|
+
SchemaPlus::ForeignKeys.config.update_attributes(save)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
21
32
|
def add_foreign_key(table_name, to_table, options = {})
|
22
33
|
raise NotImplementedError, "Sqlite3 does not support altering a table to add foreign key constraints (table #{table_name.inspect} to #{to_table.inspect})"
|
23
34
|
end
|
data/lib/schema_plus/version.rb
CHANGED
@@ -567,78 +567,96 @@ describe ActiveRecord::Migration do
|
|
567
567
|
end
|
568
568
|
|
569
569
|
|
570
|
-
context "when column is changed"
|
570
|
+
context "when column is changed" do
|
571
571
|
|
572
572
|
before(:each) do
|
573
573
|
@model = Comment
|
574
574
|
end
|
575
575
|
|
576
|
-
|
577
|
-
change_column :user, :string, :foreign_key => { :references => [:users, :login] }
|
578
|
-
expect(@model).to reference(:users, :login).on(:user)
|
579
|
-
end
|
580
|
-
|
581
|
-
context "and initially references to users table" do
|
576
|
+
context "with foreign keys", :sqlite3 => :skip do
|
582
577
|
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
end
|
578
|
+
it "should create foreign key" do
|
579
|
+
change_column :user, :string, :foreign_key => { :references => [:users, :login] }
|
580
|
+
expect(@model).to reference(:users, :login).on(:user)
|
587
581
|
end
|
588
582
|
|
589
|
-
|
590
|
-
expect(@model).to reference(:users)
|
591
|
-
end
|
583
|
+
context "and initially references to users table" do
|
592
584
|
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
585
|
+
before(:each) do
|
586
|
+
recreate_table @model do |t|
|
587
|
+
t.integer :user_id
|
588
|
+
end
|
589
|
+
end
|
597
590
|
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
end
|
591
|
+
it "should have foreign key" do
|
592
|
+
expect(@model).to reference(:users)
|
593
|
+
end
|
602
594
|
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
end
|
595
|
+
it "should drop foreign key if it is no longer valid" do
|
596
|
+
change_column :user_id, :integer, :foreign_key => { :references => :members }
|
597
|
+
expect(@model).not_to reference(:users)
|
598
|
+
end
|
608
599
|
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
600
|
+
it "should drop foreign key if requested to do so" do
|
601
|
+
change_column :user_id, :integer, :foreign_key => { :references => nil }
|
602
|
+
expect(@model).not_to reference(:users)
|
603
|
+
end
|
613
604
|
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
605
|
+
it "should remove auto-created index if foreign key is removed" do
|
606
|
+
expect(@model).to have_index.on(:user_id) # sanity check that index was auto-created
|
607
|
+
change_column :user_id, :integer, :foreign_key => { :references => nil }
|
608
|
+
expect(@model).not_to have_index.on(:user_id)
|
609
|
+
end
|
618
610
|
|
619
|
-
|
620
|
-
|
611
|
+
it "should reference pointed table afterwards if new one is created" do
|
612
|
+
change_column :user_id, :integer, :foreign_key => { :references => :members }
|
613
|
+
expect(@model).to reference(:members)
|
614
|
+
end
|
615
|
+
|
616
|
+
it "should maintain foreign key if it's unaffected by change" do
|
621
617
|
change_column :user_id, :integer, :default => 0
|
622
618
|
expect(@model).to reference(:users)
|
623
619
|
end
|
620
|
+
|
621
|
+
it "should maintain foreign key if it's unaffected by change, even if auto_index is off" do
|
622
|
+
with_fk_config(:auto_create => false) do
|
623
|
+
change_column :user_id, :integer, :default => 0
|
624
|
+
expect(@model).to reference(:users)
|
625
|
+
end
|
626
|
+
end
|
627
|
+
|
624
628
|
end
|
625
629
|
|
626
|
-
|
630
|
+
context "if column defined without foreign key but with index" do
|
631
|
+
before(:each) do
|
632
|
+
recreate_table @model do |t|
|
633
|
+
t.integer :user_id, :foreign_key => false, :index => true
|
634
|
+
end
|
635
|
+
end
|
627
636
|
|
628
|
-
|
629
|
-
|
630
|
-
recreate_table @model do |t|
|
631
|
-
t.integer :user_id, :foreign_key => false, :index => true
|
637
|
+
it "should create the index" do
|
638
|
+
expect(@model).to have_index.on(:user_id)
|
632
639
|
end
|
633
|
-
end
|
634
640
|
|
635
|
-
|
636
|
-
|
641
|
+
it "adding foreign key should not fail due to attempt to auto-create existing index" do
|
642
|
+
expect { change_column :user_id, :integer, :foreign_key => true }.to_not raise_error
|
643
|
+
end
|
637
644
|
end
|
645
|
+
end
|
638
646
|
|
639
|
-
|
640
|
-
|
647
|
+
context "without foreign keys" do
|
648
|
+
|
649
|
+
it "doesn't auto-add foreign keys" do
|
650
|
+
recreate_table @model do |t|
|
651
|
+
t.integer :user_id, :foreign_key => false
|
652
|
+
t.string :other_column
|
653
|
+
end
|
654
|
+
with_fk_auto_create do
|
655
|
+
change_column :other_column, :text
|
656
|
+
end
|
657
|
+
expect(@model).to_not reference(:users)
|
641
658
|
end
|
659
|
+
|
642
660
|
end
|
643
661
|
|
644
662
|
protected
|
@@ -14,7 +14,7 @@ describe "Schema dump" do
|
|
14
14
|
create_table :users, :force => true do |t|
|
15
15
|
t.string :login
|
16
16
|
t.datetime :deleted_at
|
17
|
-
t.integer :first_post_id
|
17
|
+
t.integer :first_post_id, index: { unique: true }
|
18
18
|
end
|
19
19
|
|
20
20
|
create_table :posts, :force => true do |t|
|
@@ -59,6 +59,13 @@ describe "Schema dump" do
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
it "should respect foreign key's primary key" do
|
63
|
+
with_foreign_key Post, :user_id, :users, :first_post_id do
|
64
|
+
expect(dump_posts).to match(%r{t.integer\s+"user_id".*foreign_key.*primary_key: "first_post_id"})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
|
62
69
|
it "should include foreign_key exactly once" do
|
63
70
|
with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do
|
64
71
|
expect(dump_posts.scan(/foreign_key.*yippee"/).length).to eq 1
|
@@ -212,7 +219,7 @@ describe "Schema dump" do
|
|
212
219
|
t.column column.name, column.type
|
213
220
|
end
|
214
221
|
columnsets.each do |columns, referenced_table_name, referenced_columns, options|
|
215
|
-
t.foreign_key columns, referenced_table_name, (options||{}).merge(
|
222
|
+
t.foreign_key columns, referenced_table_name, (options||{}).merge(primary_key: referenced_columns)
|
216
223
|
end
|
217
224
|
end
|
218
225
|
end
|
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: 2.0.0.
|
4
|
+
version: 2.0.0.pre13
|
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: 2015-04-
|
12
|
+
date: 2015-04-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|