Sha256: 96916c06e3b6522ebe1f91fe19ef3dfd9b19e24c5e3a3900c9cc247aa0a30eee

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

# Run and create migrations programatically.
require 'fileutils'

module Volt
  module Sql
    module MigrationGenerator
      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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-sql-0.0.3 app/sql/lib/migration_generator.rb
volt-sql-0.0.2 app/sql/lib/migration_generator.rb
volt-sql-0.0.1 app/sql/lib/migration_generator.rb