immigrant 0.3.2 → 0.3.3

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: 7dc2d24a68bb12d527576e6b877f143f2c42bf63
4
- data.tar.gz: 02333cc92bc40d48c3f7e043b842432ee5998e00
3
+ metadata.gz: bdd0fcce996e63a9bbd79d5846a03508a5eb6f37
4
+ data.tar.gz: 8223a67a2b04354b3d76a3914954e63f6497f69a
5
5
  SHA512:
6
- metadata.gz: 165a88a133f812b38899ea19b2d0fc079956d5fae1c4c2814c53569d8da37502c3c37d6052d7069c171c43a671426eecfaac49710cec5854c561d9c45e3abd03
7
- data.tar.gz: be53c1539e0c349c76afd32136fdec1df908f15ca25357e5321f77d76d1b7768d414516dae27814339c0fc040fa387a4196944927d298364d66e18868e976819
6
+ metadata.gz: bde334922bc6b593d7746e2f1a722cb936ad46b5f6de00e5a8c616163a96e12664a79527ba18163f11b102910385ba96ad0bd4f2d697cb4bf71837d58477a566
7
+ data.tar.gz: 76e6525a673b96efaa26506b4408f5db93010fac02d520ea03b0ce9606afd3f585f28bccbaa736b5494df5a531ac4d6013e4817bc25fa04bc9e150e0c193253e
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Jon Jensen
1
+ Copyright (c) 2012-2015 Jon Jensen
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -35,6 +35,22 @@ task you can add to your CI setup. Just run `rake immigrant:check_keys`,
35
35
  and if anything is missing it will tell you about it and exit with a
36
36
  non-zero status.
37
37
 
38
+ ### Skipping associations
39
+
40
+ `Immigrant.ignore_keys` allows you to specify a list of keys that should
41
+ be ignored (both in the migration generator and the rake task). This is
42
+ useful if you have associations spanning databases.
43
+
44
+ Just create an config/initializers/immigrant.rb file with something like
45
+ the following:
46
+
47
+ ```ruby
48
+ Immigrant.ignore_keys = [
49
+ { from_table: "users", column: "account_id" },
50
+ # etc
51
+ ]
52
+ ```
53
+
38
54
  ## Considerations
39
55
 
40
56
  If the data in your tables is bad, then the migration will fail to run
@@ -46,10 +62,10 @@ add foreign keys.
46
62
  Immigrant currently only looks for foreign keys in `ActiveRecord::Base`'s
47
63
  database. So if a model is using a different database connection and it has
48
64
  foreign keys, Immigrant will incorrectly include them again in the generated
49
- migration.
65
+ migration. `Immigrant.ignore_keys` can be used to work around this.
50
66
 
51
67
  ## [Changelog](CHANGELOG.md)
52
68
 
53
69
  ## License
54
70
 
55
- Copyright (c) 2012-2014 Jon Jensen, released under the MIT license
71
+ Copyright (c) 2012-2015 Jon Jensen, released under the MIT license
data/lib/immigrant.rb CHANGED
@@ -1,13 +1,28 @@
1
1
  require 'active_support/all'
2
2
 
3
3
  module Immigrant
4
+ class << self
5
+ # expected format:
6
+ # [{from_table: "the_table", column: "the_column"}, ...]"
7
+ attr_writer :ignore_keys
8
+
9
+ def ignore_keys
10
+ @ignore_keys ||= []
11
+ end
12
+ end
13
+
4
14
  class KeyFinder
5
15
  def infer_keys(db_keys = current_foreign_keys, classes = model_classes)
6
- database_keys = db_keys.inject({}) { |hash, foreign_key|
7
- hash[foreign_key.hash_key] = foreign_key
8
- hash
9
- }
16
+ database_keys = Hash[db_keys.map { |foreign_key|
17
+ [foreign_key.hash_key, foreign_key]
18
+ }]
19
+
20
+ ignore_keys = Hash[Immigrant.ignore_keys.map { |key|
21
+ [[key[:from_table], key[:column]], true]
22
+ }]
23
+
10
24
  model_keys, warnings = model_keys(classes)
25
+
11
26
  new_keys = []
12
27
  model_keys.keys.each do |hash_key|
13
28
  foreign_key = model_keys[hash_key]
@@ -18,7 +33,7 @@ module Immigrant
18
33
  if current_key.to_table != foreign_key.to_table || current_key.options[:primary_key] != foreign_key.options[:primary_key]
19
34
  warnings[hash_key] = "Skipping #{foreign_key.from_table}.#{foreign_key.options[:column]}: its association references a different key/table than its current foreign key"
20
35
  end
21
- else
36
+ elsif !ignore_keys[hash_key]
22
37
  new_keys << foreign_key
23
38
  end
24
39
  end
@@ -455,6 +455,30 @@ class ImmigrantTest < ActiveSupport::TestCase
455
455
  end
456
456
  end
457
457
 
458
+ test 'ignore_keys should be respected' do
459
+ given <<-CODE
460
+ class User < ActiveRecord::Base; end
461
+ class Category < ActiveRecord::Base; end
462
+ class Widget < ActiveRecord::Base
463
+ belongs_to :user
464
+ belongs_to :category
465
+ end
466
+ CODE
467
+
468
+ Immigrant.ignore_keys = [{:from_table => "widgets", :column => "category_id"}]
469
+ begin
470
+ assert_equal(
471
+ [foreign_key_definition(
472
+ 'widgets', 'users',
473
+ :column => 'user_id', :primary_key => 'id'
474
+ )],
475
+ infer_keys
476
+ )
477
+ ensure
478
+ Immigrant.ignore_keys = []
479
+ end
480
+ end
481
+
458
482
  test 'ForeignKeyDefinition#to_ruby should correctly dump the key' do
459
483
  if ActiveRecord::VERSION::STRING < '4.2.'
460
484
  definition = foreign_key_definition('foos', 'bars', dependent: 'delete')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immigrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-13 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord