Skip to content

Commit

Permalink
Ensure that no error is produced on stderr when executing mysql scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Feb 13, 2024
1 parent 3ceeb4b commit 925f426
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Testcontainers.MySql/MySqlContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Testcontainers.MySql;
public sealed class MySqlContainer : DockerContainer, IDatabaseContainer
{
private readonly MySqlConfiguration _configuration;
private bool _hasConfigFile;

/// <summary>
/// Initializes a new instance of the <see cref="MySqlContainer" /> class.
Expand Down Expand Up @@ -45,7 +46,20 @@ public async Task<ExecResult> ExecScriptAsync(string scriptContent, Cancellation
await CopyAsync(Encoding.Default.GetBytes(scriptContent), scriptFilePath, Unix.FileMode644, ct)
.ConfigureAwait(false);

return await ExecAsync(new[] { "mysql", "--protocol=TCP", $"--port={MySqlBuilder.MySqlPort}", $"--user={_configuration.Username}", $"--password={_configuration.Password}", _configuration.Database, $"--execute=source {scriptFilePath};" }, ct)
if (!_hasConfigFile)
{
var config = new StringWriter { NewLine = "\n" };
config.WriteLine("[client]");
config.WriteLine("protocol=TCP");
config.WriteLine($"user={_configuration.Username}");
config.WriteLine($"password={_configuration.Password}");
await CopyAsync(Encoding.Default.GetBytes(config.ToString()), "/etc/mysql/my.cnf", UnixFileModes.UserRead | UnixFileModes.UserWrite, ct)
.ConfigureAwait(false);

_hasConfigFile = true;
}

return await ExecAsync(new[] { "mysql", _configuration.Database, $"--execute=source {scriptFilePath};" }, ct)
.ConfigureAwait(false);
}
}

0 comments on commit 925f426

Please sign in to comment.