Skip to content

Instantly share code, notes, and snippets.

@marzdgzmn
Created January 2, 2018 07:23
Show Gist options
  • Save marzdgzmn/9bf6ccf1ab85c2a5ae652e8390a9d40b to your computer and use it in GitHub Desktop.
Save marzdgzmn/9bf6ccf1ab85c2a5ae652e8390a9d40b to your computer and use it in GitHub Desktop.
class MirrorDatabase
include Singleton
include MirrorFileUtils
SYNC_STATUS_SYNCING = 'Syncing'.freeze
SYNC_STATUS_SUCCESSFUL = 'Successful'.freeze
SYNC_STATUS_FAILED = 'Failed'.freeze
SYNC_STATUS_STOP = 'Stop'.freeze
#DATABASE_FILE = "#{MirrorFileUtils::DATA_DIR}/mirror.db".freeze
:db_file = "#{MirrorFileUtils::DATA_DIR}/mirror.db".freeze
UPSERT = 'INSERT OR REPLACE INTO mirror '.freeze
#attr_accessor :db_file
#public_class_method :new
def initialize()
#@db_file = params.fetch(:db_file, DATABASE_FILE)
#@mutex = Mutex.new
unless File.exist?(:db_file)
MirrorFileUtils.create_file(:db_file)
initialize_db
end
end
def initialize_db
SQLite3::Database.new(:db_file) do |db|
db.execute "CREATE TABLE IF NOT EXISTS mirror(
mirror TEXT PRIMARY KEY,
last_successful_sync TEXT,
start_sync TEXT,
end_sync TEXT,
status INTEGER);"
db.execute "CREATE TABLE IF NOT EXISTS stats(
mirror TEXT,
start_sync TEXT,
number_of_files INTEGER,
number_of_files_transferred INTEGER,
total_file_size INTEGER,
total_transferred_file_size INTEGER,
download_speed INTEGER,
FOREIGN KEY(mirror) REFERENCES mirror(mirror));"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment