-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdatabase_d_sqlite.v
More file actions
97 lines (84 loc) · 2.35 KB
/
Copy pathdatabase_d_sqlite.v
File metadata and controls
97 lines (84 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module main
import config
import db.sqlite
import os
import orm
type GitlyDb = sqlite.DB
fn connect_db(conf config.Config) !GitlyDb {
path := first_env(['GITLY_SQLITE_PATH', 'GITLY_DB_PATH'], conf.sqlite.path)
mut db := sqlite.connect(path)!
if db.busy_timeout(10000) != 0 {
return error('failed to configure sqlite busy timeout')
}
db.exec('pragma journal_mode = WAL;') or { eprintln('cannot enable sqlite WAL mode: ${err}') }
return GitlyDb(db)
}
fn db_backend_name() string {
return 'sqlite'
}
fn db_exec_values(mut db GitlyDb, query string) ![][]string {
rows := db.exec(query)!
mut values := [][]string{cap: rows.len}
for row in rows {
values << row.vals.clone()
}
return values
}
fn db_exec_param_values(mut db GitlyDb, query string, params []string) ![][]string {
rows := db.exec_param_many(query, params)!
mut values := [][]string{cap: rows.len}
for row in rows {
values << row.vals.clone()
}
return values
}
fn db_parameter_marker(_index int) string {
return '?'
}
fn db_bool_value(value bool) string {
return if value { '1' } else { '0' }
}
fn db_begin_transaction(mut db GitlyDb) !orm.Tx {
return orm.begin(mut db)
}
// db_acquire_migration_lock takes SQLite's single-writer reservation before
// schema inspection begins. The lock row is intentionally updated even when it
// already exists so concurrent Gitly processes cannot both pass a
// check-then-ALTER migration.
fn db_acquire_migration_lock(mut db GitlyDb) !orm.Tx {
mut tx := db_begin_transaction(mut db)!
tx.execute('create table if not exists "__gitly_migration_lock" ("id" integer primary key)') or {
tx.rollback() or {}
return err
}
tx.execute('insert or ignore into "__gitly_migration_lock" ("id") values (1)') or {
tx.rollback() or {}
return err
}
tx.execute('update "__gitly_migration_lock" set "id" = "id" where "id" = 1') or {
tx.rollback() or {}
return err
}
return tx
}
fn db_column_exists(mut db GitlyDb, table_name string, column_name string) !bool {
rows := db_exec_values(mut db, 'pragma table_info(${sql_table(table_name)})')!
for row in rows {
if row.len > 1 && row[1] == column_name {
return true
}
}
return false
}
fn db_bool_column_type() string {
return 'INTEGER NOT NULL DEFAULT 0'
}
fn first_env(keys []string, fallback string) string {
for key in keys {
value := os.getenv(key)
if value != '' {
return value
}
}
return fallback
}