Module: Volt::Sql::MigrationGenerator

Defined in:
app/sql/lib/migration_generator.rb

Class Method Summary collapse

Class Method Details

.create_migration(name, up_content, down_content) ⇒ Object



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
# File 'app/sql/lib/migration_generator.rb', line 7

def self.create_migration(name, up_content, down_content)
  timestamp = Time.now.to_i
  file_name = "#{timestamp}_#{name.underscore}"
  class_name = name.camelize
  output_file = "#{Dir.pwd}/config/db/migrations/#{file_name}.rb"

  FileUtils.mkdir_p(File.dirname(output_file))

  content = <<-END.gsub(/^ {8}/, '')
  class #{class_name} < Volt::Migration
    def up
      #{indent_string(up_content, 4)}
    end

    def down
      #{indent_string(down_content, 4)}
    end
  end
  END

  File.open(output_file, 'w') {|f| f.write(content) }

  # return the path to the file
  output_file
end

.indent_string(string, count) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/sql/lib/migration_generator.rb', line 33

def self.indent_string(string, count)
  string.split("\n").map.with_index do |line,index|
    if index == 0
      line
    else
      (' ' * count) + line
    end
  end.join("\n")
end