-
Notifications
You must be signed in to change notification settings - Fork 6
/
initdb.go
129 lines (114 loc) · 3.87 KB
/
initdb.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package initdb
import (
"context"
"database/sql"
"go.uber.org/multierr"
"github.com/kakao/varlog/internal/benchmark/model/executiontrigger"
"github.com/kakao/varlog/internal/benchmark/model/macro/metric"
)
const (
createTableExecutionTrigger = `
create table if not exists execution_trigger (
id serial primary key,
name varchar unique not null
)
`
dropTableExecutionTrigger = "drop table if exists execution_trigger"
createTableExecution = `
create table if not exists execution (
id serial primary key,
commit_hash varchar(40) unique not null,
trigger_id integer not null references execution_trigger (id)
)
`
dropTableExecution = "drop table if exists execution cascade"
createTableMacrobenchmarkWorkload = `
create table if not exists macrobenchmark_workload (
id serial primary key,
name varchar(128) unique not null,
description text not null default ''
)
`
dropTableMacrobenchmarkWorkload = "drop table if exists macrobenchmark_workload"
insertMacrobenchmarkWorkload = `
INSERT INTO macrobenchmark_workload (name)
VALUES ('one_logstream'),
('all_logstream')
`
createTableMacrobenchmark = `
create table if not exists macrobenchmark (
id serial primary key,
execution_id integer not null references execution (id) on delete cascade,
workload_id integer not null references macrobenchmark_workload (id),
start_time timestamptz not null,
finish_time timestamptz not null,
unique (execution_id, workload_id)
)
`
dropTableMacrobenchmark = "drop table if exists macrobenchmark cascade"
createTableMacrobenchmarkTarget = `
create table if not exists macrobenchmark_target (
id serial primary key,
name varchar(32) unique not null
)
`
dropTableMacrobenchmarkTarget = "drop table if exists macrobenchmark_target"
createTableMacrobenchmarkMetric = `
create table if not exists macrobenchmark_metric (
id serial primary key,
name varchar(64) unique not null,
description text not null default ''
)
`
dropTableMacrobenchmarkMetric = "drop table if exists macrobenchmark_metric"
createTableMacrobenchmarkResult = `
create table if not exists macrobenchmark_result (
macrobenchmark_id integer not null references macrobenchmark (id) on delete cascade,
target_id integer not null references macrobenchmark_target (id),
metric_id integer not null references macrobenchmark_metric (id),
value float not null,
primary key (macrobenchmark_id, target_id, metric_id)
)
`
dropTableMacrobenchmarkResult = "drop table if exists macrobenchmark_result"
)
func CreateTables(ctx context.Context, db *sql.DB) error {
return executeStatement(ctx, db,
createTableExecutionTrigger,
createTableExecution,
createTableMacrobenchmarkWorkload,
createTableMacrobenchmark,
createTableMacrobenchmarkTarget,
createTableMacrobenchmarkMetric,
createTableMacrobenchmarkResult,
)
}
func DropTables(ctx context.Context, db *sql.DB) error {
return executeStatement(ctx, db,
dropTableMacrobenchmarkResult,
dropTableMacrobenchmark,
dropTableExecution,
dropTableMacrobenchmarkWorkload,
dropTableExecutionTrigger,
dropTableMacrobenchmarkTarget,
dropTableMacrobenchmarkMetric,
)
}
func InitTables(ctx context.Context, db *sql.DB) error {
return multierr.Combine(
executiontrigger.InitTable(ctx, db),
metric.InitTable(ctx, db),
)
}
func executeStatement(ctx context.Context, db *sql.DB, statements ...string) error {
tx, err := db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
for _, stmt := range statements {
if _, err = tx.ExecContext(ctx, stmt); err != nil {
return multierr.Append(err, tx.Rollback())
}
}
return tx.Commit()
}