Sha256: e36a862fccb5a5b26ba3dc887733f945d0c99a7b2d069b1a41c4a2968b4981b1

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

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(database)
        raise DatabaseNotFound,
          "The database #{environmentify(database)} cannot be found." unless File.exists?(database_file(database))

        File.delete(database_file(database))
      end

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

      protected

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

        super database_file(database)
      end

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

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

      private

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
apartment-0.22.1 lib/apartment/adapters/sqlite3_adapter.rb
apartment-0.22.0 lib/apartment/adapters/sqlite3_adapter.rb
apartment-0.21.1 lib/apartment/adapters/sqlite3_adapter.rb