forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDatabaseSetup.cs
137 lines (113 loc) · 3.72 KB
/
TestDatabaseSetup.cs
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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using FirebirdSql.Data.FirebirdClient;
using Npgsql;
using NUnit.Framework;
namespace NHibernate.TestDatabaseSetup
{
[TestFixture]
public class DatabaseSetup
{
private static IDictionary<string, Action<Cfg.Configuration>> SetupMethods;
static DatabaseSetup()
{
SetupMethods = new Dictionary<string, Action<Cfg.Configuration>>();
SetupMethods.Add("NHibernate.Driver.SqlClientDriver", SetupSqlServer);
SetupMethods.Add("NHibernate.Driver.FirebirdClientDriver", SetupFirebird);
SetupMethods.Add("NHibernate.Driver.SQLite20Driver", SetupSQLite);
SetupMethods.Add("NHibernate.Driver.NpgsqlDriver", SetupNpgsql);
SetupMethods.Add("NHibernate.Driver.OracleDataClientDriver", SetupOracle);
}
[Test]
public void SetupDatabase()
{
var cfg = new Cfg.Configuration();
var driver = cfg.Properties[Cfg.Environment.ConnectionDriver];
Assert.That(SetupMethods.ContainsKey(driver), "No setup method found for " + driver);
var setupMethod = SetupMethods[driver];
setupMethod(cfg);
}
private static void SetupSqlServer(Cfg.Configuration cfg)
{
var connStr = cfg.Properties[Cfg.Environment.ConnectionString];
using (var conn = new SqlConnection(connStr.Replace("initial catalog=nhibernate", "initial catalog=master")))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "drop database nhibernate";
try
{
cmd.ExecuteNonQuery();
}
catch(Exception e)
{
Console.WriteLine(e);
}
cmd.CommandText = "create database nhibernate";
cmd.ExecuteNonQuery();
}
}
}
private static void SetupFirebird(Cfg.Configuration cfg)
{
try
{
if (File.Exists("NHibernate.fdb"))
File.Delete("NHibernate.fdb");
}
catch (Exception e)
{
Console.WriteLine(e);
}
FbConnection.CreateDatabase("Database=NHibernate.fdb;ServerType=1");
}
private static void SetupNpgsql(Cfg.Configuration cfg)
{
var connStr = cfg.Properties[Cfg.Environment.ConnectionString];
using (var conn = new NpgsqlConnection(connStr.Replace("Database=nhibernate", "Database=postgres")))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "drop database nhibernate";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e);
}
cmd.CommandText = "create database nhibernate";
cmd.ExecuteNonQuery();
}
}
// Install the GUID generator function that uses the most common "random" algorithm.
using (var conn = new NpgsqlConnection(connStr))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText =
@"CREATE OR REPLACE FUNCTION uuid_generate_v4()
RETURNS uuid
AS '$libdir/uuid-ossp', 'uuid_generate_v4'
VOLATILE STRICT LANGUAGE C;";
cmd.ExecuteNonQuery();
}
}
}
private static void SetupSQLite(Cfg.Configuration cfg)
{
if (File.Exists("NHibernate.db"))
File.Delete("NHibernate.db");
}
private static void SetupOracle(Cfg.Configuration cfg)
{
// not done (yet) - current setup is manual
}
}
}