schema_generator 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/schema_generator.rb +76 -66
- metadata +2 -2
data/schema_generator.rb
CHANGED
@@ -69,6 +69,10 @@ module ActiveRecord
|
|
69
69
|
|
70
70
|
def dbmigrate_save
|
71
71
|
end
|
72
|
+
|
73
|
+
def define_read_method(symbol, attr_name, column)
|
74
|
+
#nothing
|
75
|
+
end
|
72
76
|
|
73
77
|
class << self
|
74
78
|
alias_method :old_find, :find
|
@@ -81,7 +85,7 @@ module ActiveRecord
|
|
81
85
|
def dbmigrate_create(attributes = nil)
|
82
86
|
DBMigrator::Database.new_data(self,attributes)
|
83
87
|
end
|
84
|
-
|
88
|
+
|
85
89
|
end
|
86
90
|
end
|
87
91
|
|
@@ -127,6 +131,7 @@ module ActiveRecord
|
|
127
131
|
def self.remove_index(tablename,name)
|
128
132
|
DBMigrator::Database.indexes.delete_if {|i| i.table == tablename and i.name == name}
|
129
133
|
end
|
134
|
+
|
130
135
|
end
|
131
136
|
end
|
132
137
|
|
@@ -208,95 +213,100 @@ module DBMigrator
|
|
208
213
|
end
|
209
214
|
|
210
215
|
def self.dump(databasename)
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
class << @@dbdriver
|
224
|
-
def execute(sql,name = nil)
|
225
|
-
DBMigrator::Database.execute(sql,name)
|
216
|
+
begin
|
217
|
+
@@commandstring=''
|
218
|
+
case databasename
|
219
|
+
when 'postgresql'
|
220
|
+
@@dbdriver = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(nil)
|
221
|
+
when 'mysql'
|
222
|
+
@@dbdriver = ActiveRecord::ConnectionAdapters::MysqlAdapter.new(nil,nil)
|
223
|
+
when 'sqlite'
|
224
|
+
@@dbdriver = ActiveRecord::ConnectionAdapters::SQLiteAdapter.new(nil)
|
225
|
+
else
|
226
|
+
raise 'Unknown driver'
|
226
227
|
end
|
227
228
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
229
|
+
class << @@dbdriver
|
230
|
+
def execute(sql,name = nil)
|
231
|
+
DBMigrator::Database.execute(sql,name)
|
232
|
+
end
|
232
233
|
|
233
|
-
|
234
|
-
|
235
|
-
|
234
|
+
def columns(tablename,name = nil)
|
235
|
+
table = DBMigrator::Database.tables.find {|t| t.name.to_s == tablename}
|
236
|
+
table.fields.collect {|f| ActiveRecord::ConnectionAdapters::Column.new(f.name.to_s, nil, f.type)}
|
237
|
+
end
|
236
238
|
|
237
|
-
|
238
|
-
|
239
|
-
|
239
|
+
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
|
240
|
+
execute(sql, name = nil)
|
241
|
+
end
|
242
|
+
|
243
|
+
def rollback_db_transaction; end
|
244
|
+
def begin_db_transaction; end
|
245
|
+
def commit_db_transaction; end
|
240
246
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
247
|
+
# This is for the schema code, so we don't have security to worry about
|
248
|
+
# This implementation might be attackable, but it won't generally fail
|
249
|
+
# on reasonable strings.
|
250
|
+
def quote_string(string)
|
251
|
+
string.gsub(/\'/,"''")
|
252
|
+
end
|
246
253
|
end
|
247
|
-
end
|
248
254
|
|
249
|
-
|
255
|
+
@@commandstring += "-- tables \n\n"
|
250
256
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
257
|
+
tables.sort_by {|t| t.name.to_s}.each do |table|
|
258
|
+
dbdriver.create_table table.name, table.options do |t|
|
259
|
+
table.fields.each do |f|
|
260
|
+
t.column f.name, f.type, f.options
|
261
|
+
end
|
255
262
|
end
|
256
263
|
end
|
257
|
-
end
|
258
264
|
|
259
|
-
|
265
|
+
@@commandstring += "\n-- indexes \n\n" if indexes.size > 0
|
260
266
|
|
261
|
-
|
262
|
-
|
263
|
-
|
267
|
+
indexes.sort_by {|t| t.table.to_s}.each do |index|
|
268
|
+
dbdriver.add_index index.table, index.name.to_s
|
269
|
+
end
|
264
270
|
|
265
|
-
|
266
|
-
|
267
|
-
|
271
|
+
class << ActiveRecord::Base
|
272
|
+
def retrieve_connection
|
273
|
+
DBMigrator::Database.dbdriver
|
274
|
+
end
|
268
275
|
end
|
269
|
-
end
|
270
276
|
|
271
|
-
|
277
|
+
enable_db
|
272
278
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
279
|
+
ActiveRecord::Base.class_eval do
|
280
|
+
def attributes_from_column_definition
|
281
|
+
tables = DBMigrator::Database.tables
|
282
|
+
table = tables.find {|t| t.name.to_s == self.class.table_name}
|
283
|
+
table.fields.inject({}) do |a,f|
|
284
|
+
a[f.name.to_s]=nil
|
285
|
+
a
|
286
|
+
end
|
280
287
|
end
|
281
288
|
end
|
282
|
-
end
|
283
289
|
|
284
|
-
|
290
|
+
@@commandstring += "\n-- data \n\n" if @@data.size > 0
|
285
291
|
|
286
|
-
|
287
|
-
|
288
|
-
|
292
|
+
@@data.each do |data|
|
293
|
+
data.table.create(data.data)
|
294
|
+
end
|
289
295
|
|
290
|
-
|
296
|
+
@@commandstring += "\n-- schema version meta-info \n\n"
|
291
297
|
|
292
|
-
|
293
|
-
|
294
|
-
|
298
|
+
dbdriver.create_table :schema_info, :id => false do |t|
|
299
|
+
t.column :version, :integer
|
300
|
+
end
|
295
301
|
|
296
|
-
|
302
|
+
dbdriver.execute "insert into schema_info (version) values (#{version})"
|
297
303
|
|
298
|
-
|
299
|
-
|
304
|
+
disable_db
|
305
|
+
@@commandstring
|
306
|
+
rescue => err
|
307
|
+
STDERR.puts "Error: #{err}"
|
308
|
+
STDERR.puts " #{err.backtrace.join("\n")}"
|
309
|
+
end
|
300
310
|
end
|
301
311
|
|
302
312
|
def self.reformat_create_table(sql)
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: schema_generator
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2005-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2005-10-24 00:00:00 -07:00
|
8
8
|
summary: "The rails schema generator generates complete SQL schemas from a collection of
|
9
9
|
rails migrations. It currently produces one schema file each for MySQL,
|
10
10
|
PostgreSQL, and SQLite."
|