require 'debci'
require 'active_record'
require 'kaminari/core'
require 'kaminari/activerecord'

module Debci
  module DB
    def self.config
      @config ||= ENV['DATABASE_URL'] || Debci.config.database_url
    end

    def self.establish_connection
      ActiveRecord::Base.establish_connection(config)
    end

    def self.migrate(target_version = nil)
      migrations_path = File.join(File.dirname(__FILE__), 'db', 'migrations')
      ActiveRecord::Migration.verbose = !Debci.config.quiet
      migration_context = ActiveRecord::MigrationContext.new(migrations_path)
      migration_context.migrate(target_version)
    end
  end
end

Debci::DB.establish_connection
