Skip to content

Commit

Permalink
⚡ don't actually switch schema on shutdown - just revert the stored s…
Browse files Browse the repository at this point in the history
…chema

- i've seen the 'create schema if not exists' line hang on exit
- also query information_schema instead of using `create if not exists`
  • Loading branch information
fluffynuts committed Oct 30, 2023
1 parent 6077a79 commit aa3e176
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
63 changes: 60 additions & 3 deletions source/TempDb/PeanutButter.TempDb.MySql.Base/TempDbMySqlBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,14 @@ private void CreateInitialSchema()
/// </summary>
/// <param name="schema"></param>
public void SwitchToSchema(string schema)
{
SwitchToSchema(schema, true);
}

private void SwitchToSchema(
string schema,
bool createIfMissing
)
{
// last-recorded schema may no longer exist; so go schemaless for this connection
SchemaName = "";
Expand All @@ -888,7 +896,11 @@ public void SwitchToSchema(string schema)
return;
}

CreateSchemaIfNotExists(schema);
if (createIfMissing)
{
CreateSchemaIfNotExists(schema);
}

Log($"Attempting to switch to schema {schema} with connection string: {ConnectionString}");
Execute($"use `{Escape(schema)}`");

Expand All @@ -897,7 +909,12 @@ public void SwitchToSchema(string schema)

public void CreateSchemaIfNotExists(string schema)
{
Execute($"create schema if not exists `{Escape(schema)}`");
var schemaCount = QueryFirst<int>($"select count(*) from information_schema.schemata where schema_name = {Quote(schema)};");
if (schemaCount < 1)
{
Execute($"create schema `{Escape(schema)}`");
}

GrantAllPermissionsFor("root", schema, "localhost");
GrantAllPermissionsFor("root", schema, "%");
}
Expand Down Expand Up @@ -957,6 +974,36 @@ public void Execute(string sql)
command.ExecuteNonQuery();
}

private T QueryFirst<T>(string sql)
{
using var conn = base.OpenConnection();
using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
return (T)(Convert.ChangeType(reader[0], typeof(T)));
}
return default;
}

public IEnumerable<Dictionary<string, object>> ExecuteReader(string sql)
{
using var conn = base.OpenConnection();
using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
var row = new Dictionary<string, object>();
for (var i = 0; i < reader.FieldCount; i++)
{
row[reader.GetName(i)] = reader[i];
}
yield return row;
}
}

private void EnsureIsRemoved(string databasePath)
{
Log($"Ensuring {databasePath} does not already exist");
Expand Down Expand Up @@ -1098,6 +1145,15 @@ private void ForceEndServerProcess()
{
throw new Exception($"MySql process {proc.ProcessId} has not shut down!");
}

if (Platform.IsUnixy)
{
var socketFile = Settings.Socket;
if (File.Exists(socketFile))
{
TryDo(() => File.Delete(socketFile));
}
}
}
catch (Exception ex)
{
Expand All @@ -1117,9 +1173,10 @@ _serverProcess is null
}

Log("Attempting graceful shutdown of mysql server");
TryDo(() => SwitchToSchema("mysql"));
try
{
SwitchToSchema("mysql");
SchemaName = "mysql";
KillAllActiveConnections();
Execute("SHUTDOWN");
var timeout = DateTime.Now.AddSeconds(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ string DumpProcesses()
}

[Test]
// [Explicit("Slow and flaky")]
public void KillService_ShouldKillSingleWithoutArgs()
{
// Arrange
Expand Down Expand Up @@ -495,7 +494,6 @@ private static bool HasMainModule(
// nice for testing this out

[Test]
// [Explicit("Slow and flaky")]
public void KillService_ShouldKillTheCorrectService()
{
// Arrange
Expand Down Expand Up @@ -1123,6 +1121,19 @@ private void EnsureTestServiceIsNotInstalled()
TryDo(() => Run("sc", "stop", serviceName));
TryDo(() => Run("sc", "delete", serviceName));
});

KillAll("spacedservice.exe");
}

void KillAll(string processName)
{
var processes = Process.GetProcesses()
.Where(p => p.MainModule?.FileName.IndexOf(processName, StringComparison.OrdinalIgnoreCase) is > 0)
.ToArray();
foreach (var proc in processes)
{
TryDo(() => proc.Kill());
}
}
}

Expand Down

0 comments on commit aa3e176

Please sign in to comment.