Module: FunCi::Persistence::Database

Defined in:
lib/fun_ci/persistence/database.rb

Class Method Summary collapse

Class Method Details

.connection(db_path) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/fun_ci/persistence/database.rb', line 8

def self.connection(db_path)
  db = SQLite3::Database.new(db_path)
  db.busy_timeout = 5000
  db.execute("PRAGMA journal_mode=WAL")
  db.execute("PRAGMA foreign_keys=ON")
  db
end

.migrate!(db) ⇒ Object



16
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
# File 'lib/fun_ci/persistence/database.rb', line 16

def self.migrate!(db)
  db.execute(<<~SQL)
    CREATE TABLE IF NOT EXISTS pipeline_runs (
      id INTEGER PRIMARY KEY,
      commit_hash TEXT,
      branch TEXT,
      status TEXT DEFAULT 'scheduled',
      pid INTEGER,
      created_at TEXT,
      updated_at TEXT
    )
  SQL

  add_column_if_missing(db, "pipeline_runs", "pid", "INTEGER")
  add_column_if_missing(db, "pipeline_runs", "project_path", "TEXT")

  db.execute(<<~SQL)
    CREATE TABLE IF NOT EXISTS stage_jobs (
      id INTEGER PRIMARY KEY,
      pipeline_run_id INTEGER REFERENCES pipeline_runs(id),
      stage TEXT,
      status TEXT DEFAULT 'scheduled',
      started_at TEXT,
      completed_at TEXT
    )
  SQL
end