lol_dba 1.4.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
1
  .bundle
2
2
  .rvmrc
3
3
  .DS_Store
4
- coverage/
4
+ .gem
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source :rubygems
2
2
 
3
- gem 'activerecord', :require => 'active_record'
3
+ gem 'activerecord'
4
4
  gem 'actionpack'
5
5
  gem 'railties'
6
6
  gem 'rspec-rails'
data/lib/lol_dba.rb CHANGED
@@ -6,7 +6,7 @@ module LolDba
6
6
 
7
7
  def self.form_migration_content(migration_name, index_array)
8
8
  migration = <<EOM
9
- ## run `rails g migration AddMissingIndexes` and add the following content
9
+ * run `rails g migration AddMissingIndexes` and add the following content:
10
10
 
11
11
 
12
12
  class #{migration_name} < ActiveRecord::Migration
@@ -77,22 +77,17 @@ EOM
77
77
  Dir.glob(Rails.root + "app/models/**/*.rb").each {|file| require file }
78
78
 
79
79
  model_classes = []
80
- ActiveRecord::Base.subclasses.each do |klass|
81
- if !klass.abstract_class? && klass != ActiveRecord::SessionStore::Session
82
- model_classes << klass
80
+ ObjectSpace.each_object(Module) do |obj|
81
+ if Class == obj.class && obj != ActiveRecord::Base && obj.ancestors.include?(ActiveRecord::Base) && obj != ActiveRecord::SessionStore::Session
82
+ model_classes << obj
83
83
  end
84
84
  end
85
85
 
86
86
  @index_migrations = Hash.new([])
87
87
 
88
88
  model_classes.each do |class_name|
89
-
90
- # check if this is an STI child instance
91
- #if class_name.base_class.name != class_name.name && (class_name.column_names.include?(class_name.base_class.inheritance_column) || class_name.column_names.include?(class_name.inheritance_column))
92
- unless class_name < ActiveRecord::Base
93
- # add the inharitance column on the parent table
94
- # index migration for STI should require both the primary key and the inheritance_column in a composite index.
95
- @index_migrations[class_name.base_class.table_name] += [[class_name.inheritance_column, class_name.base_class.primary_key].sort] unless @index_migrations[class_name.base_class.table_name].include?([class_name.base_class.inheritance_column].sort)
89
+ unless class_name.descends_from_active_record?
90
+ @index_migrations[class_name.base_class.table_name] += [[class_name.inheritance_column, class_name.base_class.primary_key].sort] unless @index_migrations[class_name.base_class.table_name].include?([class_name.inheritance_column, class_name.base_class.primary_key].sort)
96
91
  end
97
92
  class_name.reflections.each_pair do |reflection_name, reflection_options|
98
93
  begin
@@ -111,7 +106,6 @@ EOM
111
106
  end
112
107
  when :has_and_belongs_to_many
113
108
  table_name = reflection_options.options[:join_table] ||= [class_name.table_name, reflection_name.to_s].sort.join('_')
114
-
115
109
 
116
110
  association_foreign_key = reflection_options.options[:association_foreign_key] ||= "#{reflection_name.to_s.singularize}_id"
117
111
 
@@ -124,13 +118,13 @@ EOM
124
118
  # has_many tables are threaten by the other side of the relation
125
119
  next unless reflection_options.options[:through]
126
120
 
127
- association_clazz = class_name.reflections[reflection_options.options[:through]].klass
128
- table_name = association_clazz.table_name
121
+ through_class = class_name.reflections[reflection_options.options[:through]].klass
122
+ table_name = through_class.table_name
129
123
 
130
124
  foreign_key = get_through_foreign_key(class_name, reflection_options)
131
125
 
132
126
  if reflection_options.options[:source]
133
- association_class = association_clazz.reflections[reflection_options.options[:source]].klass
127
+ association_class = through_class.reflections[reflection_options.options[:source]].klass
134
128
  association_foreign_key = get_through_foreign_key(association_class, reflection_options)
135
129
  else
136
130
  # go to joining model through has_many and find belongs_to
@@ -152,7 +146,7 @@ EOM
152
146
 
153
147
  #FIXME currently we don't support :through => :another_regular_has_many_and_non_through_relation
154
148
  next if association_foreign_key.nil?
155
- composite_keys = [association_foreign_key.to_s, foreign_key.to_s]
149
+ composite_keys = [association_foreign_key.to_s, foreign_key.to_s].sort
156
150
  @index_migrations[table_name] += [composite_keys] unless @index_migrations[table_name].include?(composite_keys)
157
151
  end
158
152
  rescue Exception => e
@@ -184,6 +178,9 @@ EOM
184
178
  if indexes.keys.empty?
185
179
  puts "Yey, no missing indexes found!"
186
180
  else
181
+ tip = "* TIP: if you have a problem with the index name('index name too long') you can solve with the :name option. "
182
+ tip += "Something like :name => 'my_index'."
183
+ puts tip
187
184
  add = form_data_for_migration(indexes)
188
185
  puts form_migration_content(migration_name, add)
189
186
  end
@@ -1,3 +1,3 @@
1
1
  module LolDba
2
- VERSION = "1.4.0" unless defined? LolDba::VERSION
2
+ VERSION = "1.4.1" unless defined? LolDba::VERSION
3
3
  end
@@ -2,83 +2,69 @@ require 'spec_helper'
2
2
 
3
3
  describe "Collect indexes based on associations:" do
4
4
 
5
- before(:all) do
6
- @relationship_indexes, @warning_messages = LolDba.check_for_indexes
7
- end
5
+ let!(:lol_dba){ LolDba.check_for_indexes }
6
+ let(:relationship_indexes){ lol_dba[0] }
7
+ let(:warning_messages){ lol_dba[1] }
8
8
 
9
9
  it "find relationship indexes" do
10
- @relationship_indexes.should_not be_empty
10
+ relationship_indexes.should_not be_empty
11
11
 
12
- @relationship_indexes.should have_key("companies")
13
- @relationship_indexes.should have_key("companies_freelancers")
14
- @relationship_indexes.should have_key("addresses")
15
- @relationship_indexes.should have_key("purchases")
12
+ relationship_indexes.should have_key("companies")
13
+ relationship_indexes.should have_key("companies_freelancers")
14
+ relationship_indexes.should have_key("addresses")
15
+ relationship_indexes.should have_key("purchases")
16
16
  end
17
17
 
18
18
  it "find indexes for belongs_to" do
19
- @relationship_indexes["addresses"].should include("country_id")
19
+ relationship_indexes["addresses"].should include("country_id")
20
20
  end
21
21
 
22
22
  it "find indexes for belongs_to with custom foreign key" do
23
- @relationship_indexes["companies"].should include("owner_id")
23
+ relationship_indexes["companies"].should include("owner_id")
24
24
  end
25
25
 
26
26
  it "find indexes for has_and_belongs_to_many" do
27
- @relationship_indexes["companies_freelancers"].should include(["freelancer_id", "company_id"])
27
+ relationship_indexes["companies_freelancers"].should include(["freelancer_id", "company_id"])
28
28
  end
29
29
 
30
30
  it "find indexes for has_and_belongs_to_many with custom join_table, primary and foreign keys" do
31
- @relationship_indexes["purchases"].should include(["present_id", "buyer_id"])
32
- end
33
-
34
- it "find two combinations for the combined index for has_and_belongs_to_many" do
35
- @relationship_indexes["companies_freelancers"].should include(["freelancer_id", "company_id"])
36
- @relationship_indexes["companies_freelancers"].should include(["company_id", "freelancer_id"])
37
- @relationship_indexes["purchases"].should include(["present_id", "buyer_id"])
38
- @relationship_indexes["purchases"].should include(["buyer_id", "present_id"])
31
+ relationship_indexes["purchases"].should include(["present_id", "buyer_id"])
39
32
  end
40
33
 
41
34
  it "do not add an already existing index" do
42
- @relationship_indexes["companies"].should_not include("country_id")
35
+ relationship_indexes["companies"].should_not include("country_id")
43
36
  end
44
37
 
45
38
  it "find indexes for has_many :through" do
46
- @relationship_indexes["billable_weeks"].should include(["remote_worker_id", "timesheet_id"])
39
+ relationship_indexes["billable_weeks"].should include(["remote_worker_id", "timesheet_id"])
47
40
  end
48
41
 
49
42
  it "find indexes for has_many :through with source and foreign key" do
50
- @relationship_indexes["complex_billable_week"].should include(["freelancer_id", "id_complex_timesheet"])
51
- end
52
-
53
- it "find two combinations for the combined index for has_many :through" do
54
- @relationship_indexes["billable_weeks"].should include(["remote_worker_id", "timesheet_id"])
55
- @relationship_indexes["billable_weeks"].should include(["timesheet_id", "remote_worker_id"])
56
- @relationship_indexes["complex_billable_week"].should include(["freelancer_id", "id_complex_timesheet"])
57
- @relationship_indexes["complex_billable_week"].should include(["id_complex_timesheet", "freelancer_id"])
43
+ relationship_indexes["complex_billable_week"].should include(["freelancer_id", "id_complex_timesheet"])
58
44
  end
59
45
 
60
46
  it "do not include wrong class" do
61
- @relationship_indexes["wrongs"].should be_nil
62
- @relationship_indexes["addresses_wrongs"].should be_nil
47
+ relationship_indexes["wrongs"].should be_nil
48
+ relationship_indexes["addresses_wrongs"].should be_nil
63
49
  end
64
50
 
65
51
  it "have warnings(non-existent table) on test data" do
66
- @warning_messages.should_not be_empty
67
- @warning_messages.should =~ /\'wrongs\'/
68
- @warning_messages.should =~ /\'addresses_wrongs\'/
52
+ warning_messages.should_not be_empty
53
+ warning_messages.should =~ /\'wrongs\'/
54
+ warning_messages.should =~ /\'addresses_wrongs\'/
69
55
  end
70
56
 
71
57
  it "find indexes for STI" do
72
- @relationship_indexes["users"].should include(["id", "type"])
58
+ relationship_indexes["users"].should include(["id", "type"])
73
59
  end
74
60
 
75
61
  it "find indexes for STI with custom inheritance column" do
76
- @relationship_indexes["freelancers"].should include(["id", "worker_type"])
62
+ relationship_indexes["freelancers"].should include(["id", "worker_type"])
77
63
  end
78
64
 
79
65
  it "find indexes, than use custom class name option in association" do
80
- @relationship_indexes["employers_freelancers"].should be_nil
81
- @relationship_indexes["companies_freelancers"].should include(["freelancer_id", "company_id"])
66
+ relationship_indexes["employers_freelancers"].should be_nil
67
+ relationship_indexes["companies_freelancers"].should include(["freelancer_id", "company_id"])
82
68
  end
83
69
 
84
- end
70
+ end
@@ -75,9 +75,10 @@ describe "Function puts_migration_content:" do
75
75
  end
76
76
 
77
77
  it "print migration code" do
78
- $stdout.should_receive(:puts).with("")
79
- $stdout.should_receive(:puts).with(/TestMigration/i)
80
- LolDba.puts_migration_content("TestMigration", @relationship_indexes, "")
78
+ $stdout.should_receive(:puts).with("")
79
+ $stdout.should_receive(:puts).with(/TIP/)
80
+ $stdout.should_receive(:puts).with(/TestMigration/i)
81
+ LolDba.puts_migration_content("TestMigration", @relationship_indexes, "")
81
82
  end
82
83
 
83
84
  it "print warning messages if they exist" do
@@ -12,8 +12,3 @@ class Freelancer < ActiveRecord::Base
12
12
  has_many :complex_timesheets, :through => :complex_billable_weeks
13
13
 
14
14
  end
15
-
16
- # Check custom STI
17
- class Worker
18
-
19
- end
@@ -0,0 +1,5 @@
1
+
2
+ # Check custom STI
3
+ class Worker < Freelancer
4
+
5
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lol_dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2012-12-21 00:00:00.000000000 Z
16
+ date: 2012-12-22 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activerecord
@@ -103,6 +103,7 @@ files:
103
103
  - spec/fixtures/app/models/god.rb
104
104
  - spec/fixtures/app/models/timesheet.rb
105
105
  - spec/fixtures/app/models/user.rb
106
+ - spec/fixtures/app/models/worker.rb
106
107
  - spec/fixtures/app/models/worker_user.rb
107
108
  - spec/fixtures/app/models/wrong.rb
108
109
  - spec/fixtures/app/sweepers/user_sweeper.rb
@@ -150,6 +151,7 @@ test_files:
150
151
  - spec/fixtures/app/models/god.rb
151
152
  - spec/fixtures/app/models/timesheet.rb
152
153
  - spec/fixtures/app/models/user.rb
154
+ - spec/fixtures/app/models/worker.rb
153
155
  - spec/fixtures/app/models/worker_user.rb
154
156
  - spec/fixtures/app/models/wrong.rb
155
157
  - spec/fixtures/app/sweepers/user_sweeper.rb