Skip to content

Commit

Permalink
feat: Add convenience methods to perform Postgres initialization scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Feb 15, 2024
1 parent c2e763a commit 1dd120e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Testcontainers.PostgreSql/PostgreSqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,48 @@ public PostgreSqlBuilder WithPassword(string password)
.WithEnvironment("POSTGRES_PASSWORD", password);
}

/// <summary>
/// Copies scripts to be run during the container initialization.
/// </summary>
/// <param name="scriptUrls">The URLs of the scripts to be run during the container initialization.</param>
/// <returns>A configured instance of <see cref="PostgreSqlBuilder" />.</returns>
/// <remarks>
/// If there are multiple initialization scripts, they will be executed in sorted file name order.
/// See the <a href="https://github.com/docker-library/docs/blob/master/postgres/README.md#initialization-scripts">PostgreSql docker documentation</a> for more information.
/// </remarks>
public PostgreSqlBuilder WithInitializationScripts(params Uri[] scriptUrls)
{
return scriptUrls.Aggregate(this, (builder, scriptUrl) => builder.WithResourceMapping(scriptUrl, $"/docker-entrypoint-initdb.d/{scriptUrl.Segments.Last()}"));
}

/// <summary>
/// Copies scripts to be run during the container initialization.
/// </summary>
/// <param name="scriptFiles">The script files on the test host to be run during the container initialization.</param>
/// <returns>A configured instance of <see cref="PostgreSqlBuilder" />.</returns>
/// <remarks>
/// If there are multiple initialization scripts, they will be executed in sorted file name order.
/// See the <a href="https://github.com/docker-library/docs/blob/master/postgres/README.md#initialization-scripts">PostgreSql docker documentation</a> for more information.
/// </remarks>
public PostgreSqlBuilder WithInitializationScripts(params FileInfo[] scriptFiles)
{
return scriptFiles.Aggregate(this, (builder, scriptFile) => builder.WithResourceMapping(scriptFile, $"/docker-entrypoint-initdb.d/{scriptFile.Name}"));
}

/// <summary>
/// Copies scripts to be run during the container initialization.
/// </summary>
/// <param name="scriptsDirectory">A directory on the test host containing scripts to be run during the container initialization.</param>
/// <returns>A configured instance of <see cref="PostgreSqlBuilder" />.</returns>
/// <remarks>
/// If there are multiple initialization scripts, they will be executed in sorted file name order.
/// See the <a href="https://github.com/docker-library/docs/blob/master/postgres/README.md#initialization-scripts">PostgreSql docker documentation</a> for more information.
/// </remarks>
public PostgreSqlBuilder WithInitializationScripts(DirectoryInfo scriptsDirectory)
{
return WithResourceMapping(scriptsDirectory, "/docker-entrypoint-initdb.d/");
}

/// <inheritdoc />
public override PostgreSqlContainer Build()
{
Expand Down

0 comments on commit 1dd120e

Please sign in to comment.