Sha256: e342c57080b92b6bced2bb8b4b2862d9ac140e3bfa88cd7ad3a2dd369464e369

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 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
            line
          else
            (' ' * count) + line
          end
        end.join("\n")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
volt-sql-0.0.7 app/sql/lib/migration_generator.rb
volt-sql-0.0.6 app/sql/lib/migration_generator.rb
volt-sql-0.0.5 app/sql/lib/migration_generator.rb
volt-sql-0.0.4 app/sql/lib/migration_generator.rb