dbd4 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/dbd4 CHANGED
@@ -21,7 +21,7 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #--
23
23
 
24
- DBD4VERSION = '1.0.6'
24
+ DBD4VERSION = '1.0.7'
25
25
 
26
26
  require 'getoptlong'
27
27
  require 'dbd4/dbd4_model_file'
@@ -33,16 +33,48 @@ require 'dbd4/dbd4_model_file'
33
33
  class DBD4Import
34
34
 
35
35
  OPTIONS = [
36
- ['--validate-only', '-n', GetoptLong::NO_ARGUMENT,
37
- "Do a dry run without executing actions, only validate the model."],
38
- ['--help', '-H', GetoptLong::NO_ARGUMENT,
39
- "Display this help message."],
40
- ['--quiet', '-q', GetoptLong::NO_ARGUMENT,
41
- "Do not log messages to standard output."],
42
- ['--usage', '-h', GetoptLong::NO_ARGUMENT,
43
- "Display usage."],
44
- ['--version', '-V', GetoptLong::NO_ARGUMENT,
45
- "Display the program version."],
36
+ [
37
+ '--validate-only',
38
+ '-n',
39
+ GetoptLong::NO_ARGUMENT,
40
+ "Do a dry run without executing actions, only validate the model."
41
+ ],
42
+ [
43
+ '--help',
44
+ '-H',
45
+ GetoptLong::NO_ARGUMENT,
46
+ "Display this help message."
47
+ ],
48
+ [
49
+ '--quiet',
50
+ '-q',
51
+ GetoptLong::NO_ARGUMENT,
52
+ "Do not log messages to standard output."
53
+ ],
54
+ [
55
+ '--usage',
56
+ '-h',
57
+ GetoptLong::NO_ARGUMENT,
58
+ "Display usage."
59
+ ],
60
+ [
61
+ '--version',
62
+ '-V',
63
+ GetoptLong::NO_ARGUMENT,
64
+ "Display the program version."
65
+ ],
66
+ [
67
+ '--force-migrations',
68
+ '-f',
69
+ GetoptLong::NO_ARGUMENT,
70
+ "Force the creation of migration files when they are not present"
71
+ ],
72
+ [
73
+ '--save-migration-data',
74
+ '-s',
75
+ GetoptLong::NO_ARGUMENT,
76
+ "Save migration data in yaml files when migrating downwards, needs migration_data_dumper plugin"
77
+ ],
46
78
  ]
47
79
 
48
80
  # Create a DBModelApp object.
@@ -94,6 +126,10 @@ class DBD4Import
94
126
  when '--version'
95
127
  puts "dbmodel, version #{DBD4VERSION}"
96
128
  exit
129
+ when '--force-migrations'
130
+ $force_migrations = true
131
+ when '--save-migration-data'
132
+ $save_migration_data = true
97
133
  else
98
134
  fail "Unknown option: #{opt}"
99
135
  end
@@ -44,7 +44,7 @@ module DBD4
44
44
  partial_name = "_create_" + Inflector.underscore(Inflector.camelize(@tablename)) + ".rb"
45
45
  fileList = Array.new(Dir.new(File.join('db', 'migrate')).entries).delete_if { |e| e !~ /#{partial_name}/ }
46
46
  if fileList.size == 0
47
- if @virtual == true
47
+ if @virtual == true or $force_migrations == true
48
48
  cmdline = ['migration', "create_" + Inflector.underscore(Inflector.camelize(@tablename)) ]
49
49
  puts "Calling generator: #{cmdline.join(' ')}"
50
50
  Rails::Generator::Scripts::Generate.new.run(cmdline)
@@ -114,15 +114,23 @@ module DBD4
114
114
  def update(table)
115
115
  raise "MigrationFile : migration file name not given" if ! @migrationfile
116
116
  c = Inflector.camelize(@tablename)
117
+
118
+ if $save_migration_data
119
+ extra_up_options = "restore_table_from_fixture(\"#{@tablename}\")"
120
+ extra_down_options = "save_table_to_fixture(\"#{@tablename}\")"
121
+ end
122
+
117
123
  new_migration_file_content = <<"MIGRATIONFILE"
118
124
  class Create#{c} < ActiveRecord::Migration
119
125
  def self.up
120
126
  create_table :#{@tablename} do |t|
121
127
  #{columnsToString(table)}
122
128
  end
129
+ #{extra_up_options}
123
130
  end
124
131
 
125
132
  def self.down
133
+ #{extra_down_options}
126
134
  drop_table :#{@tablename}
127
135
  end
128
136
  end
@@ -139,16 +147,25 @@ MIGRATIONFILE
139
147
  def update_implicit_join_table(relation)
140
148
  raise "MigrationFile : migration file name not given" if ! @migrationfile
141
149
  c = Inflector.camelize(@tablename)
150
+
151
+ if $save_migration_data
152
+ extra_up_options = "restore_table_from_fixture(\"#{@tablename}\")"
153
+ extra_down_options = "save_table_to_fixture(\"#{@tablename}\")"
154
+ end
155
+
142
156
  new_migration_file_content = <<"MIGRATIONFILE"
143
157
  class Create#{c} < ActiveRecord::Migration
144
158
  def self.up
145
159
  create_table :#{@tablename}, :id => false do |t|
146
160
  t.column :#{relation.source_table.modelname}_id, :integer
147
161
  t.column :#{relation.destination_table.modelname}_id, :integer
162
+ t.column :position, :integer, :default => nil
148
163
  end
164
+ #{extra_up_options}
149
165
  end
150
-
166
+
151
167
  def self.down
168
+ #{extra_down_options}
152
169
  drop_table :#{@tablename}
153
170
  end
154
171
  end
@@ -165,19 +182,27 @@ MIGRATIONFILE
165
182
  def update_acts_as_graph_table(relation)
166
183
  raise "MigrationFile : migration file name not given" if ! @migrationfile
167
184
  c = Inflector.camelize(@tablename)
185
+
186
+ if $save_migration_data
187
+ extra_up_options = "restore_table_from_fixture(\"#{@tablename}\")"
188
+ extra_down_options = "save_table_to_fixture(\"#{@tablename}\")"
189
+ end
190
+
168
191
  new_migration_file_content = <<"MIGRATIONFILE"
169
192
  class Create#{c} < ActiveRecord::Migration
170
193
  def self.up
171
194
  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
195
+ t.column :parent_id, :integer, :default => 0, :null => false
196
+ t.column :child_id, :integer, :default => 0, :null => false
174
197
  end
198
+ #{extra_up_options}
175
199
  end
176
-
200
+
177
201
  def self.down
202
+ #{extra_down_options}
178
203
  drop_table :#{@tablename}
179
204
  end
180
- end
205
+ end
181
206
  MIGRATIONFILE
182
207
 
183
208
  old_migration_file_content = IO.readlines(@migrationfile).join("")
@@ -112,14 +112,26 @@ module DBD4
112
112
  else
113
113
  @elements['table_name'] = { :start => i, :name => $~[1] }
114
114
  end
115
- elsif l.match(/^\s*acts_as_graph\s+\{\}\s+$/)
115
+ elsif l.match(/^\s*acts_as_graph\(\{\}\)\s*$/)
116
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
117
+ if @elements['acts_as_graph'][""]
118
+ @warnings << "Warning in file #{@modelfile} at line #{i + 1}:"
119
+ @warnings << " Duplicate acts_as_graph declaration, it is already defined at line #{@elements['acts_as_graph'][table_name][:start] + 1}"
120
+ @lines[i] = "#" + @lines[i]
121
+ i += 1
122
+ while i < @lines.size
123
+ break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/)
124
+ @lines[i] = "#" + @lines[i] if @lines[i] !~ /^\s*#/
125
+ i += 1
126
+ end
127
+ else
128
+ r = @elements['acts_as_graph'][""] = { :start => i, :end => i }
122
129
  i += 1
130
+ while i < @lines.size
131
+ break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end|acts_as)/)
132
+ r[:end] = i
133
+ i += 1
134
+ end
123
135
  end
124
136
  elsif l.match(/^\s*(has_one|has_many|has_and_belongs_to_many|belongs_to)\s+:([a-zA-Z_0-9]+)/)
125
137
  r_type = $~[1]
@@ -140,7 +152,7 @@ module DBD4
140
152
  r[:foreign_key] = $~ if l.match(/:foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/)
141
153
  r[:polymorphic] = $~ if l.match(/:polymorphic\s*=>\s*true/)
142
154
  r[:join_table] = $~ if l.match(/:join_table\s*=>\s*["']([a-zA-Z_0-9]+)/)
143
- r[:as] = $~ if l.match(/:as\s*=>\s*([a-zA-Z_0-9]+)/)
155
+ r[:as] = $~ if l.match(/:as\s*=>\s*:([a-zA-Z_0-9]+)/)
144
156
  r[:association_foreign_key] = $~ if l.match(/:association_foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/)
145
157
 
146
158
  i += 1
@@ -149,7 +161,7 @@ module DBD4
149
161
  break if @lines[i].match(/^\s*(?:has_one|has_many|has_and_belongs_to_many|belongs_to|def|end)/)
150
162
  r[:foreign_key] = $~ if l2.match(/:foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/)
151
163
  r[:polymorphic] = $~ if l2.match(/:polymorphic\s*=>\s*true/)
152
- r[:as] = $~ if l2.match(/:as\s*=>\s*([a-zA-Z_0-9]+)/)
164
+ r[:as] = $~ if l2.match(/:as\s*=>\s*:([a-zA-Z_0-9]+)/)
153
165
  r[:join_table] = $~ if l2.match(/:join_table\s*=>\s*["']([a-zA-Z_0-9]+)/)
154
166
  r[:association_foreign_key] = $~ if l2.match(/:association_foreign_key\s*=>\s*["']([a-zA-Z_0-9]+)/)
155
167
  r[:end] = i
@@ -188,7 +200,9 @@ module DBD4
188
200
 
189
201
  table.relation_starts.each_value do |r|
190
202
  dname = ""
191
- if r.type == :one2one
203
+ if r.type == :acts_as_graph
204
+ dname = ""
205
+ elsif r.type == :one2one
192
206
  dname = r.destination_table.modelname
193
207
  else
194
208
  dname = r.destination_table.name
@@ -207,11 +221,11 @@ module DBD4
207
221
  elsif element[:as]
208
222
  if r.destination_column and r.destination_column.polymorphic
209
223
  for i in (element[:start] .. element[:end])
210
- @lines[i].sub!(/(:as\s*=>\s*["']?)[a-zA-Z_0-9]+/, "\\1#{r.destination_column.polymorphic}")
224
+ @lines[i].sub!(/(:as\s*=>\s*:)[a-zA-Z_0-9]+/, "\\1#{r.destination_column.polymorphic}")
211
225
  end
212
226
  else
213
227
  for i in (element[:start] .. element[:end])
214
- @lines[i].sub!(/,\s*:as\s*=>\s*["']?[a-zA-Z_0-9]+["']?/, "")
228
+ @lines[i].sub!(/,\s*:as\s*=>\s*:[a-zA-Z_0-9]+/, "")
215
229
  end
216
230
  end
217
231
  else
@@ -222,12 +236,12 @@ module DBD4
222
236
  left_over[etype].delete(dname)
223
237
  else
224
238
  if r.type == :acts_as_graph
225
- @lines[@elements['class'][:start]] += " #{etype} {}"
239
+ @lines[@elements['class'][:start]] += " #{etype}({})"
226
240
  else
227
241
  @lines[@elements['class'][:start]] += " #{etype} :#{dname}"
228
242
  end
229
243
  if r.destination_column and r.destination_column.polymorphic
230
- @lines[@elements['class'][:start]] += ", :as => #{r.destination_column.polymorphic}"
244
+ @lines[@elements['class'][:start]] += ", :as => :#{r.destination_column.polymorphic}"
231
245
  end
232
246
  @lines[@elements['class'][:start]] += "\n"
233
247
  end
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.6
7
- date: 2006-08-27 00:00:00 -04:00
6
+ version: 1.0.7
7
+ date: 2006-09-14 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