legacy_data 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/generators/models_from_tables/templates/model.rb +8 -5
- data/legacy_data.gemspec +2 -2
- data/lib/legacy_data/schema.rb +20 -15
- data/spec/functional/expected/comment.rb +0 -1
- data/spec/functional/expected/post.rb +0 -1
- data/spec/legacy_data/schema_spec.rb +48 -15
- data/spec/models_from_tables_generator_spec.rb +3 -2
- data/spec/spec_helper.rb +6 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.7
|
@@ -21,11 +21,14 @@ class <%= class_name -%> < ActiveRecord::Base
|
|
21
21
|
-%> #validates_uniqueness_of_multiple_column_constraint :<%= cols.inspect %>
|
22
22
|
<%- end -%>
|
23
23
|
<%= "validates_presence_of #{constraints[:presence_of].map {|cols| cols.downcase.to_sym.inspect}.join(', ')}" unless constraints[:presence_of].blank? %>
|
24
|
-
<%- constraints[:
|
25
|
-
-%>
|
26
|
-
|
24
|
+
<%- constraints[:inclusion_of].each do |col, possible_values|
|
25
|
+
-%> def self.possible_values_for_<%= col %>
|
26
|
+
[ <%= possible_values %> ]
|
27
|
+
end
|
28
|
+
validates_inclusion_of <%= col.to_sym.inspect %>, :in => possible_values_for_<%= col %>, :message => "is not one of (#{possible_values_for_<%= col %>.join(', ')})"
|
29
|
+
<%- end if constraints[:inclusion_of] -%>
|
27
30
|
<%- [:allow_nil, :do_not_allow_nil].each do |nullable|
|
28
|
-
unless constraints[:numericality_of][nullable].blank?
|
31
|
+
unless constraints[:numericality_of][nullable].blank?
|
29
32
|
-%> <%= "validates_numericality_of #{constraints[:numericality_of][nullable].map {|cols| cols.downcase.to_sym.inspect}.join(', ')}" %><%= ", {:allow_nil=>true}" if nullable == :allow_nil %>
|
30
33
|
<%- end
|
31
34
|
end unless constraints[:numericality_of].blank? -%>
|
@@ -37,6 +40,6 @@ class <%= class_name -%> < ActiveRecord::Base
|
|
37
40
|
<%= sql_rule %>
|
38
41
|
SQL
|
39
42
|
end
|
40
|
-
<%- end -%>
|
43
|
+
<%- end if constraints[:custom] -%>
|
41
44
|
end
|
42
45
|
|
data/legacy_data.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{legacy_data}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Rothenberg"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-14}
|
13
13
|
s.description = %q{Create ActiveRecord models from an existing database}
|
14
14
|
s.email = %q{alex@alexrothenberg.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/legacy_data/schema.rb
CHANGED
@@ -82,12 +82,14 @@ module LegacyData
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def primary_key
|
85
|
-
if
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
if @pk.nil?
|
86
|
+
if connection.respond_to?(:pk_and_sequence_for)
|
87
|
+
@pk, seq = connection.pk_and_sequence_for(table_name)
|
88
|
+
elsif connection.respond_to?(:primary_key)
|
89
|
+
@pk = connection.primary_key(table_name)
|
90
|
+
end
|
89
91
|
end
|
90
|
-
pk
|
92
|
+
@pk
|
91
93
|
end
|
92
94
|
|
93
95
|
def relations
|
@@ -121,17 +123,15 @@ module LegacyData
|
|
121
123
|
@constraints = {}
|
122
124
|
|
123
125
|
@constraints[:unique], @constraints[:multi_column_unique] = uniqueness_constraints
|
124
|
-
|
126
|
+
boolean_presence_columns, @constraints[:presence_of] = presence_constraints
|
125
127
|
@constraints[:numericality_of] = numericality_constraints
|
126
|
-
@constraints[:custom]
|
128
|
+
@constraints[:custom], @constraints[:inclusion_of] = custom_constraints
|
129
|
+
|
130
|
+
boolean_presence_columns.each {|col| @constraints[:inclusion_of][col] = "true, false" }
|
127
131
|
end
|
128
132
|
@constraints
|
129
133
|
##### TO DO
|
130
134
|
# presence_of schoolparentid => school_parent - FOREIGN KEY
|
131
|
-
# # booleans presence_of => inclusion_of
|
132
|
-
# integer numericality_of - INTEGER COLUMNS (except foreign keys)
|
133
|
-
# custom /(.*) IN \(.*\)/i => inclusion_of $1, :in=> %w($2) -- TRY TO PARSE CUSTOM SQL RULES
|
134
|
-
#
|
135
135
|
end
|
136
136
|
|
137
137
|
def numericality_constraints
|
@@ -159,7 +159,7 @@ module LegacyData
|
|
159
159
|
end
|
160
160
|
|
161
161
|
def integer_columns
|
162
|
-
columns.select {|column| column.type == :integer }
|
162
|
+
columns.select {|column| column.type == :integer }.reject {|column| column.name == primary_key}
|
163
163
|
end
|
164
164
|
|
165
165
|
def columns
|
@@ -179,11 +179,16 @@ module LegacyData
|
|
179
179
|
|
180
180
|
def custom_constraints
|
181
181
|
return [] unless connection.respond_to? :constraints
|
182
|
-
|
182
|
+
custom_constraints, inclusion_constraints = {}, {}
|
183
183
|
connection.constraints(table_name).each do |constraint|
|
184
|
-
|
184
|
+
constraint_sql = constraint.second
|
185
|
+
if constraint_sql =~ /\s*\"*(\w*)\"*\s*IN\s*\((.*)\)/i
|
186
|
+
inclusion_constraints[$1.downcase.to_sym] = $2
|
187
|
+
else
|
188
|
+
custom_constraints[constraint.first.underscore.to_sym] = constraint_sql
|
189
|
+
end
|
185
190
|
end
|
186
|
-
|
191
|
+
[custom_constraints, inclusion_constraints]
|
187
192
|
end
|
188
193
|
|
189
194
|
def log msg
|
@@ -112,17 +112,21 @@ describe LegacyData::Schema do
|
|
112
112
|
|
113
113
|
describe 'constraints' do
|
114
114
|
it 'should have the different types of constraints' do
|
115
|
-
@schema.stub!(:uniqueness_constraints ).and_return([unique
|
116
|
-
@schema.stub!(:presence_constraints ).and_return([boolean_presence=
|
117
|
-
@schema.stub!(:numericality_constraints).and_return(numericality={}
|
118
|
-
@schema.stub!(:custom_constraints ).and_return(custom
|
119
|
-
|
120
|
-
@schema.constraints
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
115
|
+
@schema.stub!(:uniqueness_constraints ).and_return([unique =[mock], multi_column_unique=[mock]])
|
116
|
+
@schema.stub!(:presence_constraints ).and_return([[boolean_presence=mock], presence_of =[mock]])
|
117
|
+
@schema.stub!(:numericality_constraints).and_return(numericality ={} )
|
118
|
+
@schema.stub!(:custom_constraints ).and_return([custom =[mock], custom_inclusion ={} ])
|
119
|
+
|
120
|
+
constraints = @schema.constraints
|
121
|
+
|
122
|
+
constraints[:unique ].should == unique
|
123
|
+
constraints[:multi_column_unique].should == multi_column_unique
|
124
|
+
constraints[:presence_of ].should == presence_of
|
125
|
+
constraints[:numericality_of ].should == numericality
|
126
|
+
constraints[:custom ].should == custom
|
127
|
+
constraints[:inclusion_of ].should == custom_inclusion.merge(boolean_presence=> "true, false")
|
128
|
+
|
129
|
+
|
126
130
|
end
|
127
131
|
|
128
132
|
it 'should have the uniqueness constraints (booleans are done differently)' do
|
@@ -165,11 +169,40 @@ describe LegacyData::Schema do
|
|
165
169
|
@schema.custom_constraints.should == []
|
166
170
|
end
|
167
171
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
+
describe 'custom constraints' do
|
173
|
+
before :each do
|
174
|
+
@connection.should_receive(:respond_to?).with(:constraints).and_return(true)
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should get all custom constraints on a table (this may only apply to oracle?)' do
|
178
|
+
@connection.should_receive(:constraints).and_return([['SomeConstraint', %{custom sql 1} ],
|
179
|
+
['in_constraint', %{SOMEColumn IN ('Y', 'N')}],
|
180
|
+
['anotherconstraint', %{"aColumn" IN ('Yes', 'No')}],
|
181
|
+
['anotherconstraint', %{more custom sql}],
|
182
|
+
])
|
183
|
+
custom_constraints = @schema.custom_constraints
|
184
|
+
|
185
|
+
custom_constraints.first.should == {:some_constraint => 'custom sql 1', :anotherconstraint => 'more custom sql'}
|
186
|
+
custom_constraints.second.should == {:acolumn => "'Yes', 'No'", :somecolumn => "'Y', 'N'"}
|
187
|
+
end
|
188
|
+
|
189
|
+
it 'should parse inclusion constraints out of custom constraints' do
|
190
|
+
@connection.should_receive(:constraints).and_return([['in_constraint', %{SOMEColumn IN ('Y', 'N')} ],
|
191
|
+
['with_white_space', %{ SOMEColumn2 IN ('Y', 'N') }],
|
192
|
+
['with_quotes', %{"aColumn" IN ('Yes', 'No')} ],
|
193
|
+
['with_quotes_and_whitespace', %{ "aColumn2" IN ('Yes', 'No') }]
|
194
|
+
])
|
195
|
+
custom_constraints = @schema.custom_constraints
|
196
|
+
|
197
|
+
custom_constraints.first.should == {}
|
198
|
+
custom_constraints.second.should == {:somecolumn => "'Y', 'N'" ,
|
199
|
+
:somecolumn2 => "'Y', 'N'" ,
|
200
|
+
:acolumn => "'Yes', 'No'",
|
201
|
+
:acolumn2 => "'Yes', 'No'"
|
202
|
+
}
|
203
|
+
end
|
172
204
|
end
|
205
|
+
|
173
206
|
end
|
174
207
|
end
|
175
208
|
|
@@ -24,7 +24,7 @@ describe 'Models From Tables generator' do
|
|
24
24
|
},
|
25
25
|
:constraints => { :unique =>['title'],
|
26
26
|
:multi_column_unique=>[],
|
27
|
-
:
|
27
|
+
:inclusion_of =>[],
|
28
28
|
:presence_of =>['body'],
|
29
29
|
:numericality_of =>[],
|
30
30
|
:custom =>[]
|
@@ -37,7 +37,8 @@ describe 'Models From Tables generator' do
|
|
37
37
|
LegacyData::TableClassNameMapper.stub!(:class_name_for).with('posts' ).and_return('Post')
|
38
38
|
LegacyData::TableClassNameMapper.stub!(:class_name_for).with('comments').and_return('Comment')
|
39
39
|
|
40
|
-
|
40
|
+
cmd = command_for_generator('models_from_tables', ["--table-name", "posts"], :create)
|
41
|
+
cmd.invoke!
|
41
42
|
end
|
42
43
|
|
43
44
|
it 'should generate a posts model' do
|
data/spec/spec_helper.rb
CHANGED
@@ -46,10 +46,15 @@ def load_generator(generator_name="models_from_tables", args=[])
|
|
46
46
|
{:generator=>generator_name, :command=>:create, :destination=>RAILS_ROOT})
|
47
47
|
end
|
48
48
|
|
49
|
-
def
|
49
|
+
def command_for_generator(generator_name, args, the_command= :create)
|
50
50
|
generator = load_generator(generator_name, args)
|
51
51
|
LegacyData::TableClassNameMapper.stub!(:log)
|
52
52
|
cmd = generator.command(the_command)
|
53
53
|
cmd.stub!(:logger).and_return(stub('stub').as_null_object)
|
54
|
+
cmd
|
55
|
+
end
|
56
|
+
|
57
|
+
def invoke_generator(generator_name, args, the_command= :create)
|
58
|
+
cmd = command_for_generator(generator_name, args, the_command)
|
54
59
|
cmd.invoke!
|
55
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: legacy_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Rothenberg
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-14 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|