-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDatabase.cs
More file actions
139 lines (123 loc) · 4.38 KB
/
Copy pathDatabase.cs
File metadata and controls
139 lines (123 loc) · 4.38 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
using System.Reflection;
using Npgsql;
// ReSharper disable once CheckNamespace
namespace NpgsqlRestTests;
public static partial class Database
{
private const string Dbname = "npgsql_rest_test";
// Change these when testing against a different PostgreSQL instance
private const string Host = "localhost";
private const string Port = "5432";
private const string Username = "postgres";
private const string Password = "postgres";
private const string InitialConnectionString = $"Host={Host};Port={Port};Database=postgres;Username={Username};Password={Password}";
private static readonly StringBuilder script = new();
private static readonly object _createLock = new();
private static volatile bool _created = false;
static Database()
{
foreach (var method in typeof(Database).GetMethods(BindingFlags.Static | BindingFlags.Public))
{
if (method.GetParameters().Length == 0 && !string.Equals(method.Name, "Create", StringComparison.OrdinalIgnoreCase))
{
method.Invoke(null, []);
}
}
}
public static string GetIinitialConnectionString() => InitialConnectionString;
public static NpgsqlConnection CreateConnection()
{
var builder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname
};
return new NpgsqlConnection(builder.ConnectionString);
}
public static string Create()
{
// Thread-safe singleton pattern - only create database once per test run
if (_created)
{
var builder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname
};
return builder.ConnectionString;
}
lock (_createLock)
{
if (_created)
{
var builder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname
};
return builder.ConnectionString;
}
DropIfExists();
var connBuilder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname
};
using NpgsqlConnection test = new(connBuilder.ConnectionString);
test.Open();
using var command = test.CreateCommand();
command.CommandText = script.ToString();
command.ExecuteNonQuery();
_created = true;
return connBuilder.ConnectionString;
}
}
public static string CreateAdditional(string appName)
{
var builder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname,
ApplicationName = appName
};
return builder.ConnectionString;
}
/// <summary>
/// Creates a connection string for the test_user with minimal privileges (PoLP testing).
/// </summary>
public static string CreatePolpConnection()
{
var builder = new NpgsqlConnectionStringBuilder(InitialConnectionString)
{
Database = Dbname,
Username = "test_user",
Password = "test_pass"
};
return builder.ConnectionString;
}
public static void DropIfExists()
{
using NpgsqlConnection connection = new(InitialConnectionString);
connection.Open();
void Exec(string sql)
{
using var command = connection.CreateCommand();
command.CommandText = sql;
command.ExecuteNonQuery();
}
bool Any(string sql)
{
using var command = connection.CreateCommand();
command.CommandText = sql;
using var reader = command.ExecuteReader();
if (reader.Read())
{
return true;
}
return false;
}
if (Any($"select 1 from pg_database where datname = '{Dbname}'"))
{
Exec($"revoke connect on database {Dbname} from public");
Exec($"select pg_terminate_backend(pid) from pg_stat_activity where datname = '{Dbname}' and pid <> pg_backend_pid()");
Exec($"drop database {Dbname}");
}
Exec($"create database {Dbname}");
Exec($"alter database {Dbname} set timezone to 'UTC'");
}
}