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-12-19 11:49:37 +00:00
|
|
|
using System.Linq;
|
2020-11-30 12:14:53 +00:00
|
|
|
using System.Threading;
|
2020-11-27 19:15:49 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence;
|
2020-11-27 19:15:49 +00:00
|
|
|
|
|
|
|
|
// ReSharper disable ConvertToUsingDeclaration
|
2021-02-11 08:30:27 +01:00
|
|
|
namespace Umbraco.Cms.Tests.Integration.Testing
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
|
|
|
|
/// <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
|
|
|
{
|
2020-12-19 11:49:37 +00:00
|
|
|
private readonly TestDatabaseSettings _settings;
|
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
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
public SqlDeveloperTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory, IUmbracoDatabaseFactory databaseFactory, string masterConnectionString)
|
2020-11-27 19:15:49 +00:00
|
|
|
{
|
|
|
|
|
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
|
|
|
|
|
_databaseFactory = databaseFactory ?? throw new ArgumentNullException(nameof(databaseFactory));
|
2020-12-12 11:33:57 +00:00
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
_settings = settings;
|
2020-11-27 19:15:49 +00:00
|
|
|
_masterConnectionString = masterConnectionString;
|
|
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
var counter = 0;
|
|
|
|
|
|
|
|
|
|
var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount)
|
|
|
|
|
.Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", false, masterConnectionString));
|
|
|
|
|
|
|
|
|
|
var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount)
|
|
|
|
|
.Select(x => TestDbMeta.CreateWithMasterConnectionString($"{DatabaseName}-{++counter}", true, masterConnectionString));
|
2020-11-30 12:14:53 +00:00
|
|
|
|
2020-12-19 11:49:37 +00:00
|
|
|
_testDatabases = schema.Concat(empty).ToList();
|
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-19 11:49:37 +00:00
|
|
|
for (int i = 0; i < _settings.PrepareThreadCount; 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
|
|
|
}
|
|
|
|
|
}
|