unless defined?(Rails)

namespace :db do
  task :env do
    require 'active_record'
    include ActiveRecord::Tasks
    DatabaseTasks.database_configuration = YAML.load_file('config/database.yml')
    DatabaseTasks.db_dir = 'db'
    ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
    @env_name = ENV['NOTIFIABLE_ENV'] || 'test'
    @migration_paths = [File.join(File.dirname(__FILE__), '..', '..', 'db', 'migrate')]
  end

  # used to override migration paths
  # bit of a hack
  task :before_migrate do

  end

  desc 'Create the database'
  task create: :env do
    DatabaseTasks.create_current(@env_name)
  end

  desc 'Drop the database'
  task drop: :env do
    DatabaseTasks.drop_current(@env_name)
  end

  desc 'Migrate the database'
  task migrate: [:env, :before_migrate] do
    ActiveRecord::Base.establish_connection(YAML.load_file('config/database.yml')[@env_name])
    ActiveRecord::Migration.verbose = true
    ActiveRecord::MigrationContext.new(@migration_paths).migrate
  end
end

end