5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
|
# File 'lib/nando/parser.rb', line 5
def self.parse_from_dbmate (source_path, destination_path)
source_files = Dir.children(source_path)
source_files.sort!
puts "Found #{source_files.length} source files"
FileUtils.mkdir_p(destination_path)
clear_directory(destination_path)
for filename in source_files do
match = /^(\d+)\_([\w\_]+)\./.match(filename)
migration_version = match[1]
migration_name = match[2]
new_filename = "#{migration_version}_#{migration_name}.rb"
new_file = File.new(File.join(destination_path, new_filename), 'w')
source_file_lines = File.readlines(File.join(source_path, filename))
current_section = nil
up_method, down_method = '', ''
with_transaction = true
for line in source_file_lines do
next if /^--[\s|\\\/_]*$/.match(line)
case current_section
when 'up'
if match = /--\smigrate:down(.*)/.match(line)
with_transaction = false if match[1].include?('transaction:false')
current_section = 'down'
else
up_method += " #{line}".rstrip + "\n"
end
when 'down'
down_method += " #{line}".rstrip + "\n"
else
if match = /--\smigrate:up(.*)/.match(line)
with_transaction = false if match[1].include?('transaction:false')
current_section = 'up'
end
end
end
migration_class_name = migration_name.camelize
migration_type = with_transaction ? Nando::Migration.name.demodulize : Nando::MigrationWithoutTransaction.name.demodulize
migration_up_code = up_method
migration_down_code = down_method
MigrationGenerator.render_to_file(File.join(File.dirname(File.expand_path(__FILE__)), 'parser_templates/migration.rb'), binding, new_file)
end
dest_files = Dir.children(destination_path)
puts "Created #{dest_files.length} migrations in the destination folder"
end
|