schema_plus 2.0.0.pre16 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c99278211ca16613b477d638db63506511855b55
4
- data.tar.gz: 70bc7a4a0085cc04d381b2e013002d9516159174
3
+ metadata.gz: 6baf672aae665ce18e90211330b7a8a85413e19c
4
+ data.tar.gz: fe37aa4396dc65ce843ef0cb3b43c33abcc3dd21
5
5
  SHA512:
6
- metadata.gz: abdc2561b776542283c32e85fb3ec6e37ae4a412114d45b268262a787777edfed250002f28906829294ad87f2ccf3104f783bb875539c78dada889bf3341856c
7
- data.tar.gz: 9ff399f35e5a5efb6c2163912352355ab833769a6337b8105cebdcae05ca53849ae67e4c2a78fc533a9e123dd607d9d266f8b0772889454fb9f2d9be93496a23
6
+ metadata.gz: 98d96007fe6a44b042762015eb90767c2378357fe22521df84884d74792fc026425e99fb5f0e745f54a4b3bf9ee8aaaff505dd22ee76f4bc3f9d1e208630f62f
7
+ data.tar.gz: 8071c349aebb2df8c6f91ddbad29c813c74d4c7e6723f9c3559d9d59c2ccb15644d5698b35e1abeb06594912149b9e77e0dfc29b864be7a30911b871eaf70ebc
data/README.md CHANGED
@@ -3,24 +3,33 @@
3
3
 
4
4
  # SchemaPlus family
5
5
 
6
- The SchemaPlus family of gems provide various extensions and enhancements to ActiveRecord.
7
-
8
- Listed alphabetically:
9
-
10
- Gem | Description | Included In `schema_plus` gem?
11
- ----| ----------- |:------------------------------:
12
- [schema_associations](https://github.com/SchemaPlus/schema_associations) | Automatically defines model associations based on foreign key relations |
13
- <p style="color:grey">schema_auto_foreign_keys</p> | Automatically creates foreign keys on referencing columns | Y
14
- [schema_plus_columns](https://github.com/SchemaPlus/schema_plus_columns) | Column attributes including `column.indexes` and `column.unique?` | Y
15
- [schema_plus_db_default](https://github.com/SchemaPlus/schema_plus_db_default) | Use `ActiveRecord::DB_DEFAULT` to set an attribute to the database default | Y
16
- [schema_plus_default_expr](https://github.com/SchemaPlus/schema_plus_default_expr) | Use SQL expressions for database default values | Y
17
- [schema_plus_enums](https://github.com/SchemaPlus/schema_plus_enums) | Define enum types in migrations | Y
18
- [schema_plus_foreign_keys](https://github.com/SchemaPlus/schema_plus_foreign_keys) | Extended support for foreign keys, including creation as column options, `:deferrable`, and SQLite3 support | Y
19
- [schema_plus_indexes](https://github.com/SchemaPlus/schema_plus_indexes) | Convenience and consistency in using indexes | Y
20
- [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) |PostgreSQL index features: `case_insenstive`, `expression` and `operator_class` | Y
21
- [schema_plus_tables](https://github.com/SchemaPlus/schema_plus_tables) | Convenience and consistency in using tables | Y
22
- [schema_plus_views](https://github.com/SchemaPlus/schema_plus_views) | Create and drop views in migrations | Y
23
- [schema_validations](https://github.com/SchemaPlus/schema_validations) | Automatically defines ActiveRecord validations based on database constraints |
6
+ The SchemaPlus family of gems provide various extensions and enhancements to ActiveRecord >= 4.2. There are two varieties:
7
+
8
+ * **Gems that provide new default automatic behavior**
9
+
10
+ These gems run with the spirit of DRY and convention over configuration -- and automatically take care of things for you. Just include any of these gems and they'll get to work.
11
+
12
+ Gem | Description
13
+ ----| -----------
14
+ [schema_associations](https://github.com/SchemaPlus/schema_associations) | DRY up your models! Why manually define associations (and their inverses) in the models, when you've already defined those relations in the database?
15
+ [schema_auto_foreign_keys](https://github.com/SchemaPlus/schema_auto_foreign_keys) | DRY up your migrations! It goes without saying that a foreign key relationship should have a foreign key constraint -- it should also go without typing.
16
+ [schema_validations](https://github.com/SchemaPlus/schema_validations) | DRY up your validations! The database has constraints in it, your models should automatically validate based on those constraints.
17
+
18
+ * **Gems that extend ActiveRecord's feature set**
19
+
20
+ These gems provide new features and capabilities to ActiveRecord that you may wish to take advantage of. None of these have default automagic; once you include the gem the new features are available, but you need to invoke them to use them.
21
+
22
+ Gem | Description
23
+ ----| -----------
24
+ [schema_plus_columns](https://github.com/SchemaPlus/schema_plus_columns) | Column attributes including `column.indexes` and `column.unique?`
25
+ [schema_plus_db_default](https://github.com/SchemaPlus/schema_plus_db_default) | Use `ActiveRecord::DB_DEFAULT` to set an attribute to the database default
26
+ [schema_plus_default_expr](https://github.com/SchemaPlus/schema_plus_default_expr) | Use SQL expressions for database default values
27
+ [schema_plus_enums](https://github.com/SchemaPlus/schema_plus_enums) | Define enum types in migrations
28
+ [schema_plus_foreign_keys](https://github.com/SchemaPlus/schema_plus_foreign_keys) | Extended support for foreign keys, including creation as column options, `:deferrable`, and SQLite3 support
29
+ [schema_plus_indexes](https://github.com/SchemaPlus/schema_plus_indexes) | Convenience and consistency in using indexes
30
+ [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes) |PostgreSQL index features: `case_insenstive`, `expression` and `operator_class`
31
+ [schema_plus_tables](https://github.com/SchemaPlus/schema_plus_tables) | Convenience and consistency in using tables
32
+ [schema_plus_views](https://github.com/SchemaPlus/schema_plus_views) | Create and drop views in migrations
24
33
 
25
34
  See detailed documentation in each gem's README.
26
35
 
@@ -34,21 +43,22 @@ See detailed documentation in each gem's README.
34
43
  [![Coverage Status](https://img.shields.io/coveralls/SchemaPlus/schema_plus.svg)](https://coveralls.io/r/SchemaPlus/schema_plus)
35
44
  [![Dependency Status](https://gemnasium.com/lomba/schema_plus.svg)](https://gemnasium.com/SchemaPlus/schema_plus)
36
45
 
46
+ The `schema_plus` gem (v2.0) is a wrapper that pulls in a collection of gems from the SchemaPlus family. For the most part we recommend skipping this gem and directly including the specific feature gems you're interested in using.
37
47
 
38
- > ## This is the README for schema_plus 2.0.0.pre16
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
42
-
43
- ---
44
-
45
-
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.
47
-
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.
48
+ This wrapper is mostly provided for easy upgrade for those who were using schema_plus v1.8, which was a single monolothic gem. This wrapper pulls in the gems that provide the same set of features and automatic behavior as the previous version:
49
49
 
50
+ * [schema_auto_foreign_keys](https://github.com/SchemaPlus/schema_auto_foreign_keys)
51
+ * [schema_plus_columns](https://github.com/SchemaPlus/schema_plus_columns)
52
+ * [schema_plus_db_default](https://github.com/SchemaPlus/schema_plus_db_default)
53
+ * [schema_plus_default_expr](https://github.com/SchemaPlus/schema_plus_default_expr)
54
+ * [schema_plus_enums](https://github.com/SchemaPlus/schema_plus_enums)
55
+ * [schema_plus_foreign_keys](https://github.com/SchemaPlus/schema_plus_foreign_keys)
56
+ * [schema_plus_indexes](https://github.com/SchemaPlus/schema_plus_indexes)
57
+ * [schema_plus_pg_indexes](https://github.com/SchemaPlus/schema_plus_pg_indexes)
58
+ * [schema_plus_tables](https://github.com/SchemaPlus/schema_plus_tables)
59
+ * [schema_plus_views](https://github.com/SchemaPlus/schema_plus_views)
50
60
 
51
- > **IN PROGRESS:** In the prerelease versions of SchemaPlus 2.0, some features have yet to be migrated out to their own gems, and their code is still in the body of schema_plus. Those gems are greyed out in the list above. The documentation for their features is at the end of this README.
61
+ Note that the earlier version (1.x) supports earlier versions of ActiveRecord: 3.1, 4.0, 4.1, and 4.2.0. For more info about that version, see the [schema_plus 1.x](https://github.com/SchemaPlus/schema_plus/tree/1.x) branch README.
52
62
 
53
63
 
54
64
  ### Upgrading from `schema_plus` 1.8.x
@@ -114,31 +124,4 @@ or in a Gemfile
114
124
 
115
125
  * And [lots of contributors](https://github.com/SchemaPlus/schema_plus/graphs/contributors) since then.
116
126
 
117
- ---
118
- ---
119
-
120
- # Prerelease: Documentation of features still be moved into separate feature gems
121
-
122
-
123
- ### Auto Foreign Key Constraints
124
-
125
- SchemaPlus adds support for the common convention that you name a column
126
- with suffix `_id` to indicate that it's a foreign key, SchemaPlus
127
- automatically defines the appropriate constraint.
128
-
129
- SchemaPlus also creates foreign key constraints for rails' `t.references` or
130
- `t.belongs_to`, which take the singular of the referenced table name and
131
- implicitly create the column suffixed with `_id`.
132
-
133
- You can explicitly specify whether or not to generate a foreign key
134
- constraint, and specify or override automatic options, using the
135
- `:foreign_key` keyword
136
-
137
- Here are some examples:
138
-
139
- t.integer :author_id # automatically references table 'authors', key id
140
- t.integer :parent_id # special name parent_id automatically references its own table (for tree nodes)
141
- t.integer :author_id, foreign_key: true # same as default automatic behavior
142
- t.integer :author, foreign_key: true # non-conventional column name needs to force creation, table name is assumed to be 'authors'
143
- t.integer :author_id, foreign_key: false # don't create a constraint
144
-
127
+ * In 2015, the monolithic schema_plus gem was split into the SchemaPlus family of specific feature gems.
@@ -7,7 +7,7 @@ require 'schema_plus/indexes'
7
7
  require 'schema_plus/tables'
8
8
  require 'schema_plus/views'
9
9
  require 'schema_plus_pg_indexes'
10
- require 'schema_plus/auto_foreign_keys'
10
+ require 'schema_auto_foreign_keys'
11
11
 
12
12
  require_relative 'schema_plus/version'
13
13
 
@@ -1,3 +1,3 @@
1
1
  module SchemaPlus
2
- VERSION = "2.0.0.pre16"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -10,8 +10,8 @@ Gem::Specification.new do |gem|
10
10
  gem.authors = ["Ronen Barzel", "Michal Lomnicki"]
11
11
  gem.email = ["ronen@barzel.org", "michal.lomnicki@gmail.com"]
12
12
  gem.homepage = "https://github.com/SchemaPlus/schema_plus"
13
- gem.summary = "Enhances ActiveRecord schema mechanism, including more DRY index creation and support for foreign key constraints and views."
14
- gem.description = "SchemaPlus is an ActiveRecord extension that provides enhanced capabilities for schema definition and querying, including: enhanced and more DRY index capabilities, support and automation for foreign key constraints, and support for views."
13
+ gem.summary = "Wrapper that pulls in many gems from the SchemaPlus family of ActiveRecord extensions"
14
+ gem.description = "SchemaPlus is a gem that simply pulls in a collection of other gems from the SchemaPlus family of ActiveRecord extensions"
15
15
  gem.license = 'MIT'
16
16
 
17
17
  gem.rubyforge_project = "schema_plus"
@@ -22,7 +22,7 @@ Gem::Specification.new do |gem|
22
22
  gem.require_paths = ["lib"]
23
23
 
24
24
  gem.add_dependency "activerecord", "~> 4.2"
25
- gem.add_dependency "valuable"
25
+ gem.add_dependency "schema_auto_foreign_keys", "~> 0.1"
26
26
  gem.add_dependency "schema_plus_core", "~> 0.4"
27
27
  gem.add_dependency "schema_monkey", "~> 2.1"
28
28
  gem.add_dependency "schema_plus_columns", "~> 0.1"
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe "Sanity Check" do
5
+
6
+ it "database is connected" do
7
+ expect(ActiveRecord::Base).to be_connected
8
+ end
9
+
10
+ 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.pre16
4
+ version: 2.0.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: 2015-05-17 00:00:00.000000000 Z
12
+ date: 2015-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -26,19 +26,19 @@ dependencies:
26
26
  - !ruby/object:Gem::Version
27
27
  version: '4.2'
28
28
  - !ruby/object:Gem::Dependency
29
- name: valuable
29
+ name: schema_auto_foreign_keys
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
34
+ version: '0.1'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '0.1'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: schema_plus_core
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -289,9 +289,8 @@ dependencies:
289
289
  - - ">="
290
290
  - !ruby/object:Gem::Version
291
291
  version: '0'
292
- description: 'SchemaPlus is an ActiveRecord extension that provides enhanced capabilities
293
- for schema definition and querying, including: enhanced and more DRY index capabilities,
294
- support and automation for foreign key constraints, and support for views.'
292
+ description: SchemaPlus is a gem that simply pulls in a collection of other gems from
293
+ the SchemaPlus family of ActiveRecord extensions
295
294
  email:
296
295
  - ronen@barzel.org
297
296
  - michal.lomnicki@gmail.com
@@ -318,20 +317,12 @@ files:
318
317
  - gemfiles/activerecord-4.2.1/Gemfile.postgresql
319
318
  - gemfiles/activerecord-4.2.1/Gemfile.sqlite3
320
319
  - lib/schema_plus.rb
321
- - lib/schema_plus/auto_foreign_keys.rb
322
- - lib/schema_plus/auto_foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb
323
- - lib/schema_plus/auto_foreign_keys/active_record/migration/command_recorder.rb
324
- - lib/schema_plus/auto_foreign_keys/middleware/migration.rb
325
- - lib/schema_plus/auto_foreign_keys/middleware/schema.rb
326
320
  - lib/schema_plus/version.rb
327
321
  - schema_dev.yml
328
322
  - schema_plus.gemspec
323
+ - spec/sanity_spec.rb
329
324
  - spec/schema/auto_schema.rb
330
325
  - spec/schema/core_schema.rb
331
- - spec/schema_auto_foreign_keys/foreign_key_spec.rb
332
- - spec/schema_auto_foreign_keys/migration_spec.rb
333
- - spec/schema_auto_foreign_keys/schema_dumper_spec.rb
334
- - spec/schema_auto_foreign_keys/schema_spec.rb
335
326
  - spec/spec_helper.rb
336
327
  - spec/support/helpers.rb
337
328
  - spec/support/matchers/automatic_foreign_key_matchers.rb
@@ -352,23 +343,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
352
343
  version: 1.9.2
353
344
  required_rubygems_version: !ruby/object:Gem::Requirement
354
345
  requirements:
355
- - - ">"
346
+ - - ">="
356
347
  - !ruby/object:Gem::Version
357
- version: 1.3.1
348
+ version: '0'
358
349
  requirements: []
359
350
  rubyforge_project: schema_plus
360
351
  rubygems_version: 2.2.2
361
352
  signing_key:
362
353
  specification_version: 4
363
- summary: Enhances ActiveRecord schema mechanism, including more DRY index creation
364
- and support for foreign key constraints and views.
354
+ summary: Wrapper that pulls in many gems from the SchemaPlus family of ActiveRecord
355
+ extensions
365
356
  test_files:
357
+ - spec/sanity_spec.rb
366
358
  - spec/schema/auto_schema.rb
367
359
  - spec/schema/core_schema.rb
368
- - spec/schema_auto_foreign_keys/foreign_key_spec.rb
369
- - spec/schema_auto_foreign_keys/migration_spec.rb
370
- - spec/schema_auto_foreign_keys/schema_dumper_spec.rb
371
- - spec/schema_auto_foreign_keys/schema_spec.rb
372
360
  - spec/spec_helper.rb
373
361
  - spec/support/helpers.rb
374
362
  - spec/support/matchers/automatic_foreign_key_matchers.rb
@@ -1,31 +0,0 @@
1
- require 'schema_plus/foreign_keys'
2
-
3
- require_relative 'auto_foreign_keys/active_record/migration/command_recorder'
4
- require_relative 'auto_foreign_keys/middleware/migration'
5
- require_relative 'auto_foreign_keys/middleware/schema'
6
-
7
- module SchemaAutoForeignKeys
8
- module ActiveRecord
9
- module ConnectionAdapters
10
- autoload :Sqlite3Adapter, 'schema_plus/auto_foreign_keys/active_record/connection_adapters/sqlite3_adapter'
11
- end
12
- end
13
- end
14
-
15
- class SchemaPlus::ForeignKeys::Config
16
- ##
17
- # :attr_accessor: auto_create
18
- #
19
- # Whether to automatically create foreign key constraints for columns
20
- # suffixed with +_id+. Boolean, default is +true+.
21
- has_value :auto_create, :klass => :boolean, :default => true
22
-
23
- ##
24
- # :attr_accessor: auto_index
25
- #
26
- # Whether to automatically create indexes when creating foreign key constraints for columns.
27
- # Boolean, default is +true+.
28
- has_value :auto_index, :klass => :boolean, :default => true
29
- end
30
-
31
- SchemaMonkey.register SchemaAutoForeignKeys
@@ -1,22 +0,0 @@
1
- module SchemaAutoForeignKeys
2
- module ActiveRecord
3
- module ConnectionAdapters
4
-
5
- # SchemaPlus::ForeignKeys includes an Sqlite3 implementation of the AbstractAdapter
6
- # extensions.
7
- module Sqlite3Adapter
8
-
9
- def copy_table(*args, &block)
10
- fk_override = { :auto_create => false, :auto_index => false }
11
- save = Hash[fk_override.keys.collect{|key| [key, SchemaPlus::ForeignKeys.config.send(key)]}]
12
- begin
13
- SchemaPlus::ForeignKeys.config.update_attributes(fk_override)
14
- super
15
- ensure
16
- SchemaPlus::ForeignKeys.config.update_attributes(save)
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,14 +0,0 @@
1
- module SchemaAutoForeignKeys
2
- module ActiveRecord
3
- module Migration
4
- module CommandRecorder
5
-
6
- # overrides to add if_exists option
7
- def invert_add_index(args)
8
- table, columns, options = *args
9
- [:remove_index, [table, (options||{}).merge(column: columns, if_exists: true)]]
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,66 +0,0 @@
1
- module SchemaAutoForeignKeys
2
- module Middleware
3
- module Migration
4
-
5
- module Column
6
-
7
- def before(env)
8
- config = (env.caller.try(:schema_plus_foreign_keys_config) || SchemaPlus::ForeignKeys.config)
9
- set_foreign_key(env) if auto_fk?(env, config)
10
- set_auto_index(env) if auto_index?(env, config)
11
- end
12
-
13
- def after(env)
14
- remove_auto_index(env) if env.operation == :change and remove_auto_index?(env)
15
- end
16
-
17
- private
18
-
19
- def auto_fk?(env, config)
20
- return false if env.options.include? :foreign_key
21
- return false unless config.auto_create?
22
- return true if env.type == :reference
23
- return false if env.implements_reference
24
- return true if env.column_name.to_s =~ /_id$/ # later on add a config option for this
25
- end
26
-
27
- def auto_index?(env, config)
28
- return false if env.options.include? :index
29
- return false unless env.options[:foreign_key]
30
- return true if config.auto_index?
31
- end
32
-
33
- def remove_auto_index?(env)
34
- env.options.include? :foreign_key and not env.options[:foreign_key]
35
- end
36
-
37
- def set_foreign_key(env)
38
- env.options[:foreign_key] = true
39
- end
40
-
41
- def set_auto_index(env)
42
- env.options[:index] = { name: auto_index_name(env) }
43
- end
44
-
45
- def remove_auto_index(env)
46
- env.caller.remove_index(env.table_name, :name => auto_index_name(env), :column => env.column_name, :if_exists => true)
47
- end
48
-
49
- def auto_index_name(env)
50
- SchemaAutoForeignKeys.auto_index_name(env.table_name, env.column_name)
51
- end
52
-
53
- end
54
- end
55
- end
56
-
57
- def self.auto_index_name(from_table, column_name)
58
- "fk__#{fixup_schema_name(from_table)}_#{Array.wrap(column_name).join('_and_')}"
59
- # this should enforce a maximum length
60
- end
61
-
62
- def self.fixup_schema_name(table_name)
63
- # replace . with _
64
- table_name.to_s.gsub(/[.]/, '_')
65
- end
66
- end
@@ -1,18 +0,0 @@
1
- module SchemaAutoForeignKeys
2
- module Middleware
3
- module Schema
4
- module Define
5
- def around(env)
6
- fk_override = { :auto_create => false, :auto_index => false }
7
- save = Hash[fk_override.keys.collect{|key| [key, SchemaPlus::ForeignKeys.config.send(key)]}]
8
- begin
9
- SchemaPlus::ForeignKeys.config.update_attributes(fk_override)
10
- yield env
11
- ensure
12
- SchemaPlus::ForeignKeys.config.update_attributes(save)
13
- end
14
- end
15
- end
16
- end
17
- end
18
- end
@@ -1,206 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe "Foreign Key" do
4
-
5
- let(:migration) { ::ActiveRecord::Migration }
6
-
7
- context "created with table" do
8
- before(:all) do
9
- define_schema(:auto_create => true) do
10
- create_table :users, :force => true do |t|
11
- t.string :login
12
- end
13
- create_table :comments, :force => true do |t|
14
- t.integer :user_id
15
- t.foreign_key :user_id, :users
16
- end
17
- end
18
- class User < ::ActiveRecord::Base ; end
19
- class Comment < ::ActiveRecord::Base ; end
20
- end
21
-
22
- it "should report foreign key constraints" do
23
- expect(Comment.foreign_keys.collect(&:column).flatten).to eq([ "user_id" ])
24
- end
25
-
26
- it "should report reverse foreign key constraints" do
27
- expect(User.reverse_foreign_keys.collect(&:column).flatten).to eq([ "user_id" ])
28
- end
29
-
30
- end
31
-
32
- context "modification" do
33
-
34
- before(:each) do
35
- define_schema(:auto_create => false) do
36
- create_table :users, :force => true do |t|
37
- t.string :login
38
- t.datetime :deleted_at
39
- end
40
-
41
- create_table :posts, :force => true do |t|
42
- t.text :body
43
- t.integer :user_id
44
- t.integer :author_id
45
- end
46
-
47
- create_table :comments, :force => true do |t|
48
- t.text :body
49
- t.integer :post_id
50
- t.foreign_key :post_id, :posts
51
- end
52
- end
53
- class User < ::ActiveRecord::Base ; end
54
- class Post < ::ActiveRecord::Base ; end
55
- class Comment < ::ActiveRecord::Base ; end
56
- Comment.reset_column_information
57
- end
58
-
59
-
60
- context "works", :sqlite3 => :skip do
61
-
62
- context "when is added", "posts(author_id)" do
63
-
64
- before(:each) do
65
- add_foreign_key(:posts, :users, :column => :author_id, :on_update => :cascade, :on_delete => :restrict)
66
- end
67
-
68
- it "references users(id)" do
69
- expect(Post).to reference(:users, :id).on(:author_id)
70
- end
71
-
72
- it "cascades on update" do
73
- expect(Post).to reference(:users).on_update(:cascade)
74
- end
75
-
76
- it "restricts on delete" do
77
- expect(Post).to reference(:users).on_delete(:restrict)
78
- end
79
-
80
- it "is available in Post.foreign_keys" do
81
- expect(Post.foreign_keys.collect(&:column)).to include('author_id')
82
- end
83
-
84
- it "is available in User.reverse_foreign_keys" do
85
- expect(User.reverse_foreign_keys.collect(&:column)).to include('author_id')
86
- end
87
-
88
- end
89
-
90
- context "when is dropped", "comments(post_id)" do
91
-
92
- let(:foreign_key_name) { fk = Comment.foreign_keys.detect(&its.column == 'post_id') and fk.name }
93
-
94
- before(:each) do
95
- remove_foreign_key(:comments, name: foreign_key_name)
96
- end
97
-
98
- it "doesn't reference posts(id)" do
99
- expect(Comment).not_to reference(:posts).on(:post_id)
100
- end
101
-
102
- it "is no longer available in Post.foreign_keys" do
103
- expect(Comment.foreign_keys.collect(&:column)).not_to include('post_id')
104
- end
105
-
106
- it "is no longer available in User.reverse_foreign_keys" do
107
- expect(Post.reverse_foreign_keys.collect(&:column)).not_to include('post_id')
108
- end
109
-
110
- end
111
-
112
- context "when drop using hash", "comments(post_id)" do
113
-
114
- let(:foreign_key_name) { fk = Comment.foreign_keys.detect(&its.column == 'post_id') and fk.name }
115
-
116
- it "finds by name" do
117
- remove_foreign_key(:comments, name: foreign_key_name)
118
- expect(Comment).not_to reference(:posts).on(:post_id)
119
- end
120
-
121
- it "finds by column_names" do
122
- remove_foreign_key(:comments, column: "post_id", to_table: "posts")
123
- expect(Comment).not_to reference(:posts).on(:post_id)
124
- end
125
- end
126
-
127
- context "when attempt to drop nonexistent foreign key" do
128
- it "raises error" do
129
- expect{remove_foreign_key(:comments, "posts", column: "nonesuch")}.to raise_error(/no foreign key/i)
130
- end
131
-
132
- it "does not error with :if_exists" do
133
- expect{remove_foreign_key(:comments, "posts", column: "nonesuch", :if_exists => true)}.to_not raise_error
134
- end
135
- end
136
-
137
- context "when referencing column and column is removed" do
138
-
139
- let(:foreign_key_name) { Comment.foreign_keys.detect { |definition| definition.column == 'post_id' }.name }
140
-
141
- it "should remove foreign keys" do
142
- remove_foreign_key(:comments, name: foreign_key_name)
143
- expect(Post.reverse_foreign_keys.collect { |fk| fk.column == 'post_id' && fk.from_table == "comments" }).to be_empty
144
- end
145
-
146
- end
147
-
148
- context "when table name is a reserved word" do
149
- before(:each) do
150
- migration.suppress_messages do
151
- migration.create_table :references, :force => true do |t|
152
- t.integer :post_id, :foreign_key => false
153
- end
154
- end
155
- end
156
-
157
- it "can add, detect, and remove a foreign key without error" do
158
- migration.suppress_messages do
159
- expect {
160
- migration.add_foreign_key(:references, :posts)
161
- foreign_key = migration.foreign_keys(:references).detect{|definition| definition.column == "post_id"}
162
- migration.remove_foreign_key(:references, name: foreign_key.name)
163
- }.to_not raise_error
164
- end
165
- end
166
- end
167
-
168
- end
169
-
170
- context "raises an exception", :sqlite3 => :only do
171
-
172
- it "when attempting to add" do
173
- expect {
174
- add_foreign_key(:posts, :users, :column => :author_id, :on_update => :cascade, :on_delete => :restrict)
175
- }.to raise_error(NotImplementedError)
176
- end
177
-
178
- it "when attempting to remove" do
179
- expect {
180
- remove_foreign_key(:posts, name: "dummy")
181
- }.to raise_error(NotImplementedError)
182
- end
183
-
184
- end
185
- end
186
-
187
- protected
188
- def add_foreign_key(*args)
189
- migration.suppress_messages do
190
- migration.add_foreign_key(*args)
191
- end
192
- User.reset_column_information
193
- Post.reset_column_information
194
- Comment.reset_column_information
195
- end
196
-
197
- def remove_foreign_key(*args)
198
- migration.suppress_messages do
199
- migration.remove_foreign_key(*args)
200
- end
201
- User.reset_column_information
202
- Post.reset_column_information
203
- Comment.reset_column_information
204
- end
205
-
206
- end