30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/db_structure.rb', line 30
def self.db_structure
config = ActiveRecord::Base.configurations
['production', 'test', 'development'].each do |target|
begin
ENV['RAILS_ENV'] = target
db_helper(config[target]['adapter']).split(/\s*;\s*/).each do |sql|
begin
puts "Recreating database #{target}"
ActiveRecord::Base.connection.execute("DROP DATABASE #{target}")
rescue
puts "Database #{target} doesn't exist yet"
ensure
ActiveRecord::Base.connection.execute("CREATE DATABASE #{target}")
end
Dir['db/*.erbsql'].each do |filename|
File.open(filename, "r") do |file|
file.each_line do |line|
if line =~ /(create|CREATE).+?(table|TABLE).+?(\w+)/
begin
ActiveRecord::Base.connection.execute("DROP TABLE #{$3}")
rescue
puts "Table #{$3} does not exist"
end
end
end
end
end
puts "Creating tables for #{target}..."
ActiveRecord::Base.connection.execute(sql)
end
puts "done."
rescue => e
puts "failed: " + e.inspect
end
end
end
|