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 +1 -1
- data/Gemfile +1 -1
- data/lib/lol_dba.rb +13 -16
- data/lib/lol_dba/version.rb +1 -1
- data/spec/associations_index_spec.rb +25 -39
- data/spec/common_function_spec.rb +4 -3
- data/spec/fixtures/app/models/freelancer.rb +0 -5
- data/spec/fixtures/app/models/worker.rb +5 -0
- metadata +4 -2
data/.gitignore
CHANGED
data/Gemfile
CHANGED
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
|
-
|
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
|
-
|
81
|
-
if
|
82
|
-
model_classes <<
|
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
|
-
|
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
|
-
|
128
|
-
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 =
|
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
|
data/lib/lol_dba/version.rb
CHANGED
@@ -2,83 +2,69 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe "Collect indexes based on associations:" do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
10
|
+
relationship_indexes.should_not be_empty
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
62
|
-
|
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
|
-
|
67
|
-
|
68
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
81
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
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
|
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.
|
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-
|
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
|