Sha256: 4ce61b00ee20956766c944ff334f88fdeeec62a46b931c798f19cb661c9ee1cf

Contents?: true

Size: 1.49 KB

Versions: 16

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'apartment/adapters/abstract_adapter'

module Apartment
  module Tenant
    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)
        unless File.exist?(database_file(tenant))
          raise TenantNotFound,
                "The tenant #{environmentify(tenant)} cannot be found."
        end

        File.delete(database_file(tenant))
      end

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

      protected

      def connect_to_new(tenant)
        return reset if tenant.nil?

        unless File.exist?(database_file(tenant))
          raise TenantNotFound,
                "The tenant #{environmentify(tenant)} cannot be found."
        end

        super database_file(tenant)
      end

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

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

      private

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

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
ros-apartment-3.2.0 lib/apartment/adapters/sqlite3_adapter.rb
synerma-apartment-3.1.0 lib/apartment/adapters/sqlite3_adapter.rb
puzzle-apartment-2.13.0 lib/apartment/adapters/sqlite3_adapter.rb
puzzle-apartment-2.12.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.11.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.10.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.9.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.8.1 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.8.1.rc2 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.8.1.rc1 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.8.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.7.2 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.7.1 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.7.0 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.6.1 lib/apartment/adapters/sqlite3_adapter.rb
ros-apartment-2.6.0 lib/apartment/adapters/sqlite3_adapter.rb