dbd4 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/dbd4 +1 -1
- data/lib/dbd4/dbd4_model_file.rb +20 -7
- data/lib/dbd4/rails_migration_file.rb +30 -2
- data/lib/dbd4/rails_model_file.rb +23 -4
- data/tests/unit/test_dbd4.rb +4 -1
- metadata +2 -2
data/bin/dbd4
CHANGED
data/lib/dbd4/dbd4_model_file.rb
CHANGED
@@ -176,8 +176,10 @@ module DBD4
|
|
176
176
|
end
|
177
177
|
|
178
178
|
def generateModelFile
|
179
|
-
|
180
|
-
|
179
|
+
if @nm_table == '0'
|
180
|
+
m = RailsModelFile.new({:modelname => @modelname})
|
181
|
+
m.update(self)
|
182
|
+
end
|
181
183
|
m = RailsMigrationFile.new({:tablename => @name})
|
182
184
|
m.update(self)
|
183
185
|
|
@@ -185,6 +187,9 @@ module DBD4
|
|
185
187
|
if rs.type == :many2many and rs.join_table == nil
|
186
188
|
m = RailsMigrationFile.new({:virtual => true, :tablename => Table.createJoinTableName(self, rs.destination_table)})
|
187
189
|
m.update_implicit_join_table(rs)
|
190
|
+
elsif rs.type == :acts_as_graph
|
191
|
+
m = RailsMigrationFile.new({:virtual => true, :tablename => "#{name}_edges"})
|
192
|
+
m.update_acts_as_graph_table(rs)
|
188
193
|
end
|
189
194
|
end
|
190
195
|
end
|
@@ -301,6 +306,10 @@ module DBD4
|
|
301
306
|
@source_column = @source_table.columns.primary_keys[@fk_fields[0].source_field]
|
302
307
|
@destination_column = @destination_table.columns.foreign_keys[@fk_fields[0].destination_field]
|
303
308
|
end
|
309
|
+
|
310
|
+
if @type == :many2many and @name =~ /acts_as_graph/
|
311
|
+
@type = :acts_as_graph
|
312
|
+
end
|
304
313
|
end
|
305
314
|
|
306
315
|
def validate(messages)
|
@@ -312,11 +321,15 @@ module DBD4
|
|
312
321
|
messages[:warnings] << "Warning : between table #{@destination_table.name} and table #{@source_table.name}, relation #{name} targets more than 1 foreign_key"
|
313
322
|
end
|
314
323
|
|
315
|
-
if !( @type != nil and @type == :many2many and @join_table == nil )
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
@
|
324
|
+
if !( @type != nil and (@type == :many2many or @type == :acts_as_graph) and @join_table == nil )
|
325
|
+
if (@source_table.name == @destination_table.name)
|
326
|
+
messages[:warnings] << "Warning : relation #{name} has same source and target table name : \"#{@source_table.name}\", dbd4 does not support this yet"
|
327
|
+
else
|
328
|
+
expected_fkey_name = Inflector.singularize(@source_table.name) + "_id"
|
329
|
+
if @destination_column.name != expected_fkey_name and @destination_column.polymorphic == nil then
|
330
|
+
messages[:warnings] << "Warning : table #{@destination_column.table.name}, foreign key #{@destination_column.name} does not match expected name #{expected_fkey_name}"
|
331
|
+
@destination_column.non_standard_name = true
|
332
|
+
end
|
320
333
|
end
|
321
334
|
end
|
322
335
|
end
|
@@ -27,6 +27,9 @@ module DBD4
|
|
27
27
|
class RailsMigrationFile
|
28
28
|
attr_writer :tablename
|
29
29
|
attr_reader :warnings, :tablename
|
30
|
+
|
31
|
+
class RailsModelFileError < Exception
|
32
|
+
end
|
30
33
|
|
31
34
|
def initialize(options = {})
|
32
35
|
@warnings = Array.new
|
@@ -122,7 +125,7 @@ class Create#{c} < ActiveRecord::Migration
|
|
122
125
|
def self.down
|
123
126
|
drop_table :#{@tablename}
|
124
127
|
end
|
125
|
-
end
|
128
|
+
end
|
126
129
|
MIGRATIONFILE
|
127
130
|
|
128
131
|
old_migration_file_content = IO.readlines(@migrationfile).join("")
|
@@ -148,7 +151,7 @@ class Create#{c} < ActiveRecord::Migration
|
|
148
151
|
def self.down
|
149
152
|
drop_table :#{@tablename}
|
150
153
|
end
|
151
|
-
end
|
154
|
+
end
|
152
155
|
MIGRATIONFILE
|
153
156
|
|
154
157
|
old_migration_file_content = IO.readlines(@migrationfile).join("")
|
@@ -159,6 +162,31 @@ MIGRATIONFILE
|
|
159
162
|
end
|
160
163
|
end
|
161
164
|
|
165
|
+
def update_acts_as_graph_table(relation)
|
166
|
+
raise "MigrationFile : migration file name not given" if ! @migrationfile
|
167
|
+
c = Inflector.camelize(@tablename)
|
168
|
+
new_migration_file_content = <<"MIGRATIONFILE"
|
169
|
+
class Create#{c} < ActiveRecord::Migration
|
170
|
+
def self.up
|
171
|
+
create_table :#{@tablename}, :id => false, :force => true do |t|
|
172
|
+
t.column :#{relation.source_table.modelname}_id, :integer, :default => 0, :null => false
|
173
|
+
t.column :#{relation.destination_table.modelname}_id, :integer, :default => 0, :null => false
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def self.down
|
178
|
+
drop_table :#{@tablename}
|
179
|
+
end
|
180
|
+
end
|
181
|
+
MIGRATIONFILE
|
182
|
+
|
183
|
+
old_migration_file_content = IO.readlines(@migrationfile).join("")
|
184
|
+
if (old_migration_file_content != new_migration_file_content)
|
185
|
+
puts "Updating migration file for table #{@tablename} (file : #{@migrationfile})..."
|
186
|
+
newFile = File.open(@migrationfile, "w")
|
187
|
+
newFile.puts new_migration_file_content
|
188
|
+
end
|
189
|
+
end
|
162
190
|
def to_str
|
163
191
|
@lines.join("")
|
164
192
|
end
|
@@ -43,7 +43,8 @@ module DBD4
|
|
43
43
|
'class' => {},
|
44
44
|
'primary_key' => {},
|
45
45
|
'table_name' => {},
|
46
|
-
'as' => {}
|
46
|
+
'as' => {},
|
47
|
+
'acts_as_graph' => {}
|
47
48
|
}
|
48
49
|
end
|
49
50
|
|
@@ -111,6 +112,15 @@ module DBD4
|
|
111
112
|
else
|
112
113
|
@elements['table_name'] = { :start => i, :name => $~[1] }
|
113
114
|
end
|
115
|
+
elsif l.match(/^\s*acts_as_graph\s+\{\}\s+$/)
|
116
|
+
table_name = Inflector.pluralize(@modelname)
|
117
|
+
r = @elements['acts_as_graph'][table_name] = { :start => i, :end => i }
|
118
|
+
i += 1
|
119
|
+
while i < @lines.size
|
120
|
+
break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/)
|
121
|
+
r[:end] = i
|
122
|
+
i += 1
|
123
|
+
end
|
114
124
|
elsif l.match(/^\s*(has_one|has_many|has_and_belongs_to_many|belongs_to)\s+:([a-zA-Z_0-9]+)/)
|
115
125
|
r_type = $~[1]
|
116
126
|
t_name = $~[2]
|
@@ -121,7 +131,7 @@ module DBD4
|
|
121
131
|
@lines[i] = "#" + @lines[i]
|
122
132
|
i += 1
|
123
133
|
while i < @lines.size
|
124
|
-
break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end)/)
|
134
|
+
break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/)
|
125
135
|
@lines[i] = "#" + @lines[i] if @lines[i] !~ /^\s*#/
|
126
136
|
i += 1
|
127
137
|
end
|
@@ -187,11 +197,14 @@ module DBD4
|
|
187
197
|
etype = 'has_one' if r.type == :one2one
|
188
198
|
etype = 'has_many' if r.type == :one2many
|
189
199
|
etype = 'has_and_belongs_to_many' if r.type == :many2many
|
200
|
+
etype = 'acts_as_graph' if r.type == :acts_as_graph
|
190
201
|
element = @elements[etype][dname]
|
191
202
|
|
192
203
|
#If the line exists
|
193
204
|
if element
|
194
|
-
if
|
205
|
+
if r.type == :acts_as_graph
|
206
|
+
#do nothing, acts as graph can only be used with default parameters({})
|
207
|
+
elsif element[:as]
|
195
208
|
if r.destination_column and r.destination_column.polymorphic
|
196
209
|
for i in (element[:start] .. element[:end])
|
197
210
|
@lines[i].sub!(/(:as\s*=>\s*["']?)[a-zA-Z_0-9]+/, "\\1#{r.destination_column.polymorphic}")
|
@@ -208,7 +221,11 @@ module DBD4
|
|
208
221
|
end
|
209
222
|
left_over[etype].delete(dname)
|
210
223
|
else
|
211
|
-
|
224
|
+
if r.type == :acts_as_graph
|
225
|
+
@lines[@elements['class'][:start]] += " #{etype} {}"
|
226
|
+
else
|
227
|
+
@lines[@elements['class'][:start]] += " #{etype} :#{dname}"
|
228
|
+
end
|
212
229
|
if r.destination_column and r.destination_column.polymorphic
|
213
230
|
@lines[@elements['class'][:start]] += ", :as => #{r.destination_column.polymorphic}"
|
214
231
|
end
|
@@ -220,6 +237,8 @@ module DBD4
|
|
220
237
|
dname = ""
|
221
238
|
if r.type == :many2many
|
222
239
|
dname = r.source_table.name
|
240
|
+
elsif r.type == :acts_as_graph
|
241
|
+
next
|
223
242
|
else
|
224
243
|
if r.destination_column.polymorphic
|
225
244
|
dname = r.destination_column.polymorphic
|
data/tests/unit/test_dbd4.rb
CHANGED
@@ -40,6 +40,7 @@ $files = {
|
|
40
40
|
'db/migrate/013_create_kids.rb' => "",
|
41
41
|
'db/migrate/014_create_houses.rb' => "",
|
42
42
|
'db/migrate/015_create_cars.rb' => "",
|
43
|
+
'db/migrate/103_create_cars_edges.rb' => "",
|
43
44
|
'db/migrate/104_create_cars_people.rb' => ""
|
44
45
|
}
|
45
46
|
|
@@ -230,6 +231,7 @@ INPUT
|
|
230
231
|
'db/migrate/012_create_dogs.rb' => "",
|
231
232
|
'db/migrate/013_create_kids.rb' => "",
|
232
233
|
'db/migrate/014_create_houses.rb' => "",
|
234
|
+
'db/migrate/103_create_cars_edges.rb' => "",
|
233
235
|
'db/migrate/104_create_cars_people.rb' => "",
|
234
236
|
'db/migrate/015_create_cars.rb' => ""
|
235
237
|
}
|
@@ -245,7 +247,6 @@ INPUT
|
|
245
247
|
assert_equal(1, run_info['model person'])
|
246
248
|
assert_equal(1, run_info['model security_number'])
|
247
249
|
assert_equal(1, run_info['model user'])
|
248
|
-
assert_equal(1, run_info['model jobs_person'])
|
249
250
|
assert_equal(1, run_info['model kid'])
|
250
251
|
assert_equal(1, run_info['model generic_animal'])
|
251
252
|
assert_equal(1, run_info['model house'])
|
@@ -253,6 +254,7 @@ INPUT
|
|
253
254
|
assert_equal(1, run_info['model job'])
|
254
255
|
assert_equal(1, run_info['migration create_cars_people'])
|
255
256
|
assert_equal(1, run_info['model car'])
|
257
|
+
assert_equal(1, run_info['migration create_cars_edges'])
|
256
258
|
end
|
257
259
|
|
258
260
|
def _test_comment
|
@@ -287,6 +289,7 @@ INPUT
|
|
287
289
|
'db/migrate/012_create_dogs.rb' => "",
|
288
290
|
'db/migrate/013_create_kids.rb' => "",
|
289
291
|
'db/migrate/014_create_houses.rb' => "",
|
292
|
+
'db/migrate/103_create_cars_edges.rb' => "",
|
290
293
|
'db/migrate/104_create_cars_people.rb' => "",
|
291
294
|
'db/migrate/015_create_cars.rb' => ""
|
292
295
|
}
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: dbd4
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2006-08-
|
6
|
+
version: 1.0.6
|
7
|
+
date: 2006-08-27 00:00:00 -04:00
|
8
8
|
summary: A package for importing DB Designer 4 xml models in rails.
|
9
9
|
require_paths:
|
10
10
|
- lib
|