Module: Tasku::Database

Defined in:
lib/tasku/database.rb

Constant Summary collapse

DB_DIR =
File.join(Dir.home, ".tasku")
DB_PATH =
File.join(DB_DIR, "tasks.db")

Class Method Summary collapse

Class Method Details

.connectObject



11
12
13
14
15
16
17
# File 'lib/tasku/database.rb', line 11

def self.connect
  FileUtils.mkdir_p(DB_DIR)
  @db = Sequel.sqlite(DB_PATH)
  Sequel::Model.db = @db
  migrate
  @db
end

.dbObject



19
20
21
# File 'lib/tasku/database.rb', line 19

def self.db
  @db || connect
end

.migrateObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tasku/database.rb', line 23

def self.migrate
  db.create_table? :tasks do
    primary_key :id
    String :name, null: false
    File :description
    String :project
    String :category
    Date :start_day
    Date :due_day
    String :model_name
    String :code
    String :priority, default: "none"
    String :status, default: "todo"
    String :tags
    Float :estimated_hours
    DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
    DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
  end

  if db.table_exists?(:tasks) && !db.schema(:tasks).map(&:first).include?(:code)
    db.alter_table(:tasks) { add_column :code, String }
  end

  db.create_table? :projects do
    String :name, primary_key: true
    String :colour
  end
end