Class: DataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/trophy_scraper/data_store.rb

Constant Summary collapse

@@DB_VERSION =
1

Instance Method Summary collapse

Constructor Details

#initialize(connection_string, logger_level, logger_dest) ⇒ DataStore

Returns a new instance of DataStore.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/trophy_scraper/data_store.rb', line 3

def initialize(connection_string, logger_level, logger_dest)
  DataMapper::Logger.new(logger_dest, logger_level)
  DataMapper.setup(:default, connection_string)
  
  begin
    version_meta = Meta.get("version")
  rescue
    #TODO, only eat the table not found error
  end
  if version_meta == nil
    $logger.info("Database is empty, creating schema.")
    DataMapper.auto_migrate!
    version_meta = Meta.new(:name => "version", :value => @@DB_VERSION)
    version_meta.save
    insert_reference_data
  elsif version_meta.value.to_i < @@DB_VERSION
    $logger.info("Database schema version is older than code's, gracefully updating.")
    DataMapper.auto_upgrade!
    version_meta.value = @@DB_VERSION
    version_meta.save
  elsif version_meta.value.to_i > @@DB_VERSION
    #TODO Code is older than db, warn and abort.
    $logger.warn("Database schema version is newer than code's, aborting.")
  else
    $logger.info("Database schema is up to date.")
  end
end