Sha256: 81a4e276f2494cf558b97218bbdad4c9b1ce96e00f4a932ea0e8f1938614219c

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'earth'
require 'rake'
require 'active_record/connection_adapters/abstract/connection_specification'

module Earth
  class Tasks
    include Rake::DSL

    def initialize
      init_earth_tasks
      init_bare unless rails?

      namespace :db do
        unless rails?
          task :create => 'earth:db:create'
          task :drop => 'earth:db:drop'
        end
        task :migrate => 'earth:db:migrate'
        task :seed => 'earth:db:seed'
      end
    end

    def rails?
      @rails ||= Object.const_defined?('Rails')
    end

    def init_bare
      Object.const_set 'Rails', Earth

      require 'active_record'
      load 'active_record/railties/databases.rake'

      Rake::Task['db:load_config'].clear
      Rake::Task['db:create'].clear
      Rake::Task['db:drop'].clear
      Rake::Task['db:migrate'].clear
      Rake::Task['db:seed'].clear
    end

    def config
      spec = ENV['DATABASE_URL']
      resolver = ActiveRecord::Base::ConnectionSpecification::Resolver.new spec, {}
      resolver.spec.config.stringify_keys
    end
        
    def init_earth_tasks
      namespace :earth do
        namespace :db do
          task :create do
            create_database(config)
          end
          task :drop do
            drop_database_and_rescue(config)
          end
          task :load_config do
            if rails?
              Rake::Task['db:load_config'].invoke
            else
              Earth.connect
            end
          end
          task :migrate => :load_config do
            Earth.reset_schemas!
          end
          task :seed => :load_config do
            Earth.run_data_miner!
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
earth-1.0.1 lib/earth/tasks.rb