-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBenchmarkHandler.cs
More file actions
155 lines (119 loc) · 4.2 KB
/
Copy pathBenchmarkHandler.cs
File metadata and controls
155 lines (119 loc) · 4.2 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Threading.Tasks;
using Npgsql;
using Venflow.Benchmarks.Models.Configurations;
using Venflow.Shared;
namespace Venflow.Benchmarks
{
public class BenchmarkHandler
{
private static BenchmarkHandler _current = null!;
private static BenchmarkDb _database = null!;
private static readonly object _buildLocker = new object();
private static readonly TaskCompletionSource<bool> _waitHandle = new TaskCompletionSource<bool>();
private BenchmarkHandler()
{
NpgsqlConnection connection;
NpgsqlCommand command;
if (SecretsHandler.IsDevelopmentMachine("Benchmarks"))
{
connection = new NpgsqlConnection(SecretsHandler.GetConnectionString<BenchmarkHandler>("Postgres").TrimEnd(';') + ";Enlist=true;Pooling=false;");
connection.Open();
command = new NpgsqlCommand(@"
DROP DATABASE IF EXISTS venflow_benchmarks;
CREATE DATABASE venflow_benchmarks OWNER venflow_benchmarks;
", connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Dispose();
}
try
{
connection = _database.GetConnection();
connection.Open();
command = new NpgsqlCommand(_createTablesCommand, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.ReloadTypes();
connection.Close();
}
catch
{
// We are running on a different Framework version.
}
}
public static void Init(BenchmarkDb database)
{
lock (_buildLocker)
{
if (_current != null)
return;
_database = database;
_current = new BenchmarkHandler();
_waitHandle.SetResult(true);
}
}
public static void Wait()
{
_waitHandle.Task.GetAwaiter().GetResult();
}
~BenchmarkHandler()
{
_database.GetConnection().Close();
}
private const string _createTablesCommand =
@"ALTER SCHEMA public OWNER TO venflow_benchmarks;
COMMENT ON SCHEMA public IS 'standard public schema';
CREATE EXTENSION ""uuid-ossp"";
CREATE TABLE public.""EmailContents"" (
""Id"" integer NOT NULL,
""Content"" text NOT NULL,
""EmailId"" integer NOT NULL
);
ALTER TABLE public.""EmailContents"" OWNER TO venflow_benchmarks;
ALTER TABLE public.""EmailContents"" ALTER COLUMN ""Id"" ADD GENERATED ALWAYS AS IDENTITY(
SEQUENCE NAME public.""EmailContents_Id_seq""
START WITH 0
INCREMENT BY 1
MINVALUE 0
NO MAXVALUE
CACHE 1
);
CREATE TABLE public.""Emails"" (
""Id"" integer NOT NULL,
""Address"" text NOT NULL,
""PersonId"" integer NOT NULL
);
ALTER TABLE public.""Emails"" OWNER TO venflow_benchmarks;
ALTER TABLE public.""Emails"" ALTER COLUMN ""Id"" ADD GENERATED ALWAYS AS IDENTITY(
SEQUENCE NAME public.""Emails_Id_seq""
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
CREATE TABLE public.""People"" (
""Id"" integer NOT NULL,
""Name"" text
);
ALTER TABLE public.""People"" OWNER TO venflow_benchmarks;
ALTER TABLE public.""People"" ALTER COLUMN ""Id"" ADD GENERATED ALWAYS AS IDENTITY(
SEQUENCE NAME public.""People_Id_seq""
START WITH 0
INCREMENT BY 1
MINVALUE 0
NO MAXVALUE
CACHE 1
);
ALTER TABLE ONLY public.""EmailContents""
ADD CONSTRAINT ""EmailContents_pkey"" PRIMARY KEY(""Id"");
ALTER TABLE ONLY public.""Emails""
ADD CONSTRAINT ""Emails_pkey"" PRIMARY KEY(""Id"");
ALTER TABLE ONLY public.""People""
ADD CONSTRAINT ""People_pkey"" PRIMARY KEY(""Id"");
ALTER TABLE ONLY public.""EmailContents""
ADD CONSTRAINT ""FK_Emails_EmailContents"" FOREIGN KEY(""EmailId"") REFERENCES public.""Emails""(""Id"") ON DELETE CASCADE NOT VALID;
ALTER TABLE ONLY public.""Emails""
ADD CONSTRAINT emails_people_id_fk FOREIGN KEY(""PersonId"") REFERENCES public.""People""(""Id"") ON DELETE CASCADE;";
}
}