Class: TmsTaskManager::Configs::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/tms_task_manager/configs/database.rb

Constant Summary collapse

DB_PATH =
File.expand_path('../remind.db', __dir__)

Class Method Summary collapse

Class Method Details

.connectionObject



11
12
13
14
15
# File 'lib/tms_task_manager/configs/database.rb', line 11

def connection
  @connection ||= SQLite3::Database.new(DB_PATH).tap do |db|
    db.results_as_hash = true
  end
end

.setupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tms_task_manager/configs/database.rb', line 17

def setup
  connection.execute_batch <<-SQL
  CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR(255),
    description TEXT,
    start_date DATETIME,
    end_date DATETIME,
    status VARCHAR(20) CHECK( status IN ('pending', 'in_progress', 'completed') )
  );

  CREATE TABLE IF NOT EXISTS categories (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR(255),
    description TEXT,
    slug VARCHAR(255) UNIQUE
  );

  CREATE TABLE IF NOT EXISTS task_category (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    task_id INTEGER,
    category_id INTEGER,
    FOREIGN KEY (task_id) REFERENCES tasks(id),
    FOREIGN KEY (category_id) REFERENCES categories(id)
  );
  SQL
end