Du kannst nicht mehr als 25 Themen auswählen
Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
|
- using System.Data;
- using System.Threading;
- using Dapper;
- using Microsoft.EntityFrameworkCore;
-
- namespace DotNetCore.CAP.MySql.Test
- {
- public abstract class DatabaseTestHost : TestHost
- {
- private static bool _sqlObjectInstalled;
- public static object _lock = new object();
-
- protected override void PostBuildServices()
- {
- base.PostBuildServices();
- lock (_lock)
- {
- if (!_sqlObjectInstalled)
- {
- InitializeDatabase();
- }
- }
- }
-
- public override void Dispose()
- {
- DeleteAllData();
- base.Dispose();
- }
-
- private void InitializeDatabase()
- {
- using (CreateScope())
- {
- var storage = GetService<MySqlStorage>();
- var token = new CancellationTokenSource().Token;
- CreateDatabase();
- storage.InitializeAsync(token).GetAwaiter().GetResult();
- _sqlObjectInstalled = true;
- }
- }
-
- private void CreateDatabase()
- {
- var masterConn = ConnectionUtil.GetMasterConnectionString();
- var databaseName = ConnectionUtil.GetDatabaseName();
- using (var connection = ConnectionUtil.CreateConnection(masterConn))
- {
- connection.Execute($@"
- DROP DATABASE IF EXISTS `{databaseName}`;
- CREATE DATABASE `{databaseName}`;");
- }
- }
-
- private void DeleteAllData()
- {
- var conn = ConnectionUtil.GetConnectionString();
-
- using (var connection = ConnectionUtil.CreateConnection(conn))
- {
- connection.Execute($@"
- TRUNCATE TABLE `cap.published`;
- TRUNCATE TABLE `cap.received`;
- TRUNCATE TABLE `cap.queue`;");
- }
- }
- }
- }
|