Sha256: 8cd58ad390abce866640a76fa26910a996662b812b72470c3b9877e4fa8bf494

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

require 'apartment/adapters/abstract_adapter'

module Apartment
  module Database
    def self.sqlite3_adapter(config)
      Adapters::Sqlite3Adapter.new(config)
    end
  end

  module Adapters
    class Sqlite3Adapter < AbstractAdapter
      def initialize(config)
        @default_dir = File.expand_path(File.dirname(config[:database]))

        super
      end

      def drop(tenant)
        raise DatabaseNotFound,
          "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))

        File.delete(database_file(tenant))
      end

      def current_tenant
        File.basename(Apartment.connection.instance_variable_get(:@config)[:database], '.sqlite3')
      end

    protected

      def connect_to_new(tenant)
        raise DatabaseNotFound,
          "The tenant #{environmentify(tenant)} cannot be found." unless File.exists?(database_file(tenant))

        super database_file(tenant)
      end

      def create_tenant(tenant)
        raise DatabaseExists,
          "The tenant #{environmentify(tenant)} already exists." if File.exists?(database_file(tenant))

        f = File.new(database_file(tenant), File::CREAT)
        f.close
      end

    private

      def database_file(tenant)
        "#{@default_dir}/#{tenant}.sqlite3"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apartment-0.24.3 lib/apartment/adapters/sqlite3_adapter.rb
apartment-0.24.2 lib/apartment/adapters/sqlite3_adapter.rb
apartment-0.24.1 lib/apartment/adapters/sqlite3_adapter.rb
apartment-0.24.0 lib/apartment/adapters/sqlite3_adapter.rb