Module: Keenser::DB

Defined in:
lib/keenser/db.rb

Class Method Summary collapse

Class Method Details

.connect!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/keenser/db.rb', line 11

def self.connect!
  sync = ENV['SYNC'] || raise(Error.new "Please specify SYNC directory")

  unless sync
    raise SyncError.new "You must specify a SYNC directory"
  end

  unless Dir.exists? sync
    raise SyncError.new "Could not find sync directory `#{sync}`"
  end

  Sequel.connect "sqlite://#{URI.escape sync}/keenser.sqlite"
end

.connectionObject



3
4
5
6
7
8
9
# File 'lib/keenser/db.rb', line 3

def self.connection
  @connection ||= begin
    connect!
  rescue SyncError
    nil
  end
end

.setup!Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/keenser/db.rb', line 25

def self.setup!
  Model.without_tables.each do |m|
    connection.create_table m.table_name do
      primary_key :id
      instance_exec &m.schema
    end

    m.db = connect!
  end
end

.statusObject



36
37
38
39
40
41
42
43
44
# File 'lib/keenser/db.rb', line 36

def self.status
  connect!

  ms = Model.without_tables.map { |m| m.table_name }
  raise Error.new "Expected tables not found: #{ms.join ', '}" unless ms.empty?
  raise Error.new "Local computer is not configured" unless Computer.local

  "Ready to go"
end