2020-12-23 11:35:49 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
using System;
|
2020-11-30 12:14:53 +00:00
|
|
|
using System.Collections.Concurrent;
|
2020-11-27 19:15:49 +00:00
|
|
|
using System.Data.SqlClient;
|
2020-11-30 12:14:53 +00:00
|
|
|
using System.Threading;
|
2020-11-27 19:15:49 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
|
|
|
|
// ReSharper disable ConvertToUsingDeclaration
|
|
|
|
|
namespace Umbraco.Tests.Integration.Testing
|
|
|
|
|
{
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// It's not meant to be pretty, rushed port of LocalDb.cs + LocalDbTestDatabase.cs
|
|
|
|
|
/// </remarks>
|
2020-12-12 11:33:57 +00:00
|
|
|
public class SqlDeveloperTestDatabase : BaseTestDatabase, ITestDatabase
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
|
|
|
|
private readonly string _masterConnectionString;
|
2020-12-12 11:33:57 +00:00
|
|
|
public const string DatabaseName = "UmbracoTests";
|
2020-11-30 12:14:53 +00:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
public static SqlDeveloperTestDatabase Instance { get; private set; }
|
2020-11-27 19:15:49 +00:00
|
|
|
|
|
|
|
|
public SqlDeveloperTestDatabase(ILoggerFactory loggerFactory, IUmbracoDatabaseFactory databaseFactory, string masterConnectionString)
|
|
|
|
|
{
|
|
|
|
|
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
|
|
|
|
|
_databaseFactory = databaseFactory ?? throw new ArgumentNullException(nameof(databaseFactory));
|
2020-12-12 11:33:57 +00:00
|
|
|
|
2020-11-27 19:15:49 +00:00
|
|
|
_masterConnectionString = masterConnectionString;
|
|
|
|
|
|
2020-11-30 12:14:53 +00:00
|
|
|
_testDatabases = new[]
|
|
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
// With Schema
|
|
|
|
|
TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-1", false, masterConnectionString),
|
|
|
|
|
TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-2", false, masterConnectionString),
|
2020-11-30 12:14:53 +00:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
// Empty (for migration testing etc)
|
|
|
|
|
TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-3", true, masterConnectionString),
|
|
|
|
|
TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-4", true, masterConnectionString),
|
|
|
|
|
};
|
2020-11-30 12:14:53 +00:00
|
|
|
|
|
|
|
|
Instance = this; // For GlobalSetupTeardown.cs
|
|
|
|
|
}
|
2020-11-27 19:15:49 +00:00
|
|
|
|
2020-12-12 11:33:57 +00:00
|
|
|
protected override void Initialize()
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
_prepareQueue = new BlockingCollection<TestDbMeta>();
|
|
|
|
|
_readySchemaQueue = new BlockingCollection<TestDbMeta>();
|
|
|
|
|
_readyEmptyQueue = new BlockingCollection<TestDbMeta>();
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (TestDbMeta meta in _testDatabases)
|
2020-11-30 12:14:53 +00:00
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
CreateDatabase(meta);
|
|
|
|
|
_prepareQueue.Add(meta);
|
2020-11-30 12:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
for (int i = 0; i < ThreadCount; i++)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-12-12 11:33:57 +00:00
|
|
|
var thread = new Thread(PrepareDatabase);
|
|
|
|
|
thread.Start();
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 12:14:53 +00:00
|
|
|
private void CreateDatabase(TestDbMeta meta)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
|
|
|
|
using (var connection = new SqlConnection(_masterConnectionString))
|
|
|
|
|
{
|
|
|
|
|
connection.Open();
|
2020-12-23 11:35:49 +01:00
|
|
|
using (SqlCommand command = connection.CreateCommand())
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
2020-11-30 12:14:53 +00:00
|
|
|
SetCommand(command, $@"CREATE DATABASE {LocalDb.QuotedName(meta.Name)}");
|
|
|
|
|
command.ExecuteNonQuery();
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 12:14:53 +00:00
|
|
|
private void Drop(TestDbMeta meta)
|
|
|
|
|
{
|
|
|
|
|
using (var connection = new SqlConnection(_masterConnectionString))
|
|
|
|
|
{
|
|
|
|
|
connection.Open();
|
2020-12-23 11:35:49 +01:00
|
|
|
using (SqlCommand command = connection.CreateCommand())
|
2020-11-30 12:14:53 +00:00
|
|
|
{
|
2020-12-23 11:35:49 +01:00
|
|
|
string sql = $@"
|
2020-11-30 12:14:53 +00:00
|
|
|
ALTER DATABASE{LocalDb.QuotedName(meta.Name)}
|
|
|
|
|
SET SINGLE_USER
|
2020-12-23 11:35:49 +01:00
|
|
|
WITH ROLLBACK IMMEDIATE";
|
|
|
|
|
SetCommand(command, sql);
|
2020-11-30 12:14:53 +00:00
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
|
|
|
|
|
SetCommand(command, $@"DROP DATABASE {LocalDb.QuotedName(meta.Name)}");
|
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Finish()
|
|
|
|
|
{
|
|
|
|
|
if (_prepareQueue == null)
|
2020-12-23 11:35:49 +01:00
|
|
|
{
|
2020-11-30 12:14:53 +00:00
|
|
|
return;
|
2020-12-23 11:35:49 +01:00
|
|
|
}
|
2020-11-30 12:14:53 +00:00
|
|
|
|
|
|
|
|
_prepareQueue.CompleteAdding();
|
2020-12-23 11:35:49 +01:00
|
|
|
while (_prepareQueue.TryTake(out _))
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-30 12:14:53 +00:00
|
|
|
|
|
|
|
|
_readyEmptyQueue.CompleteAdding();
|
2020-12-23 11:35:49 +01:00
|
|
|
while (_readyEmptyQueue.TryTake(out _))
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-30 12:14:53 +00:00
|
|
|
|
|
|
|
|
_readySchemaQueue.CompleteAdding();
|
2020-12-23 11:35:49 +01:00
|
|
|
while (_readySchemaQueue.TryTake(out _))
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-11-30 12:14:53 +00:00
|
|
|
|
2020-12-23 11:35:49 +01:00
|
|
|
foreach (TestDbMeta testDatabase in _testDatabases)
|
2020-11-30 12:14:53 +00:00
|
|
|
{
|
|
|
|
|
Drop(testDatabase);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-27 19:15:49 +00:00
|
|
|
}
|
|
|
|
|
}
|