class TogglIntegrator::DB

class DB

Public Class Methods

prepare() click to toggle source
# File lib/toggl_integrator/db.rb, line 9
def prepare
  create_database_if_not_exists
  connect_database
  create_table_if_not_exists
end

Private Class Methods

connect_database() click to toggle source
# File lib/toggl_integrator/db.rb, line 17
def connect_database
  spec = {
    adapter: 'sqlite3',
    database: FileUtil.join('toggl_integrator.sqlite3')
  }
  ActiveRecord::Base.establish_connection spec
end
create_database_if_not_exists() click to toggle source
# File lib/toggl_integrator/db.rb, line 48
def create_database_if_not_exists
  FileUtil.new_file_if_not_exists('toggl_integrator.sqlite3')
end
create_table_if_not_exists() click to toggle source
# File lib/toggl_integrator/db.rb, line 25
def create_table_if_not_exists
  connection = ActiveRecord::Base.connection

  return if connection.table_exists? :time_entories

  create_time_entories_table(connection)
  connection.add_index :time_entories, :start
  connection.add_index :time_entories, :stop
  connection.add_index :time_entories, :status
end
create_time_entories_table(connection) click to toggle source
# File lib/toggl_integrator/db.rb, line 36
def create_time_entories_table(connection)
  connection.create_table :time_entories do |t|
    t.column :project_name, :string,    null: true
    t.column :description,  :string,    null: true
    t.column :start,        :timestamp, null: false
    t.column :stop,         :timestamp, null: false
    t.column :at,           :timestamp, null: false
    t.column :status,       :integer,   default: 0, null: true
    t.timestamps
  end
end