Class: Dataset::Database::Sqlite3

Inherits:
Base
  • Object
show all
Defined in:
lib/dataset/database/sqlite3.rb

Overview

The interface to a sqlite3 database, this will capture by copying the db file and restore by replacing and reconnecting to one of the same.

Instance Method Summary collapse

Methods inherited from Base

#clear, #record_heirarchy, #record_meta

Constructor Details

#initialize(database_spec, storage_path) ⇒ Sqlite3

Returns a new instance of Sqlite3.



8
9
10
11
# File 'lib/dataset/database/sqlite3.rb', line 8

def initialize(database_spec, storage_path)
  @database_path, @storage_path = database_spec[:database], storage_path
  FileUtils.mkdir_p(@storage_path)
end

Instance Method Details

#capture(datasets) ⇒ Object



13
14
15
16
# File 'lib/dataset/database/sqlite3.rb', line 13

def capture(datasets)
  return if datasets.nil? || datasets.empty?
  cp @database_path, storage_path(datasets)
end

#restore(datasets) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/dataset/database/sqlite3.rb', line 18

def restore(datasets)
  store = storage_path(datasets)
  if File.file?(store)
    mv store, @database_path
    ActiveRecord::Base.establish_connection 'test'
    true
  end
end

#storage_path(datasets) ⇒ Object



27
28
29
# File 'lib/dataset/database/sqlite3.rb', line 27

def storage_path(datasets)
  "#{@storage_path}/#{datasets.collect {|c| c.__id__}.join('_')}.sqlite3.db"
end