Skip to content

Commit 2ce8f7b

Browse files
committed
DbUp creates multiple connections, so we need to pass in a Func instead to use custom connections
1 parent d0801f1 commit 2ce8f7b

2 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/dbup-sqlserver/SqlConnectionManager.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using Microsoft.Data.SqlClient;
34
using DbUp.Engine.Transactions;
@@ -15,22 +16,25 @@ public class SqlConnectionManager : DatabaseConnectionManager
1516
/// </summary>
1617
/// <param name="connectionString"></param>
1718
public SqlConnectionManager(string connectionString)
18-
: this(new SqlConnection(connectionString))
19-
{ }
19+
: this(() => new SqlConnection(connectionString))
20+
{
21+
}
2022

2123
/// <summary>
2224
/// Manages Sql Database Connections using an existing connection.
2325
/// </summary>
24-
/// <param name="connection">The existing SQL connection to use.</param>
25-
public SqlConnectionManager(SqlConnection connection)
26+
/// <param name="connectionFactory">A factory function that creates a new SQL connection.</param>
27+
public SqlConnectionManager(Func<SqlConnection> connectionFactory)
2628
: base(new DelegateConnectionFactory((log, dbManager) =>
2729
{
30+
var conn = connectionFactory();
2831
if (dbManager.IsScriptOutputLogged)
29-
connection.InfoMessage += (sender, e) => log.LogInformation($"{{0}}", e.Message);
32+
conn.InfoMessage += (sender, e) => log.LogInformation("{0}", e.Message);
3033

31-
return connection;
34+
return conn;
3235
}))
33-
{ }
36+
{
37+
}
3438

3539
/// <inheritdoc/>
3640
public override IEnumerable<string> SplitScriptIntoCommands(string scriptContents)
@@ -40,4 +44,4 @@ public override IEnumerable<string> SplitScriptIntoCommands(string scriptContent
4044
return scriptStatements;
4145
}
4246
}
43-
}
47+
}

src/dbup-sqlserver/SqlServerExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported
3535
/// Creates an upgrader for SQL Server databases.
3636
/// </summary>
3737
/// <param name="supported">Fluent helper type.</param>
38-
/// <param name="connection">The sql connection.</param>
38+
/// <param name="connectionFactory">A func that create a new SqlConnection</param>
3939
/// <returns>
4040
/// A builder for a database upgrader designed for SQL Server databases.
4141
/// </returns>
42-
public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported, SqlConnection connection)
42+
public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported, Func<SqlConnection> connectionFactory)
4343
{
44-
return SqlDatabase(supported, connection, null);
44+
return SqlDatabase(supported, connectionFactory, null);
4545
}
4646

4747
/// <summary>
@@ -79,14 +79,14 @@ public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported
7979
/// Creates an upgrader for SQL Server databases.
8080
/// </summary>
8181
/// <param name="supported">Fluent helper type.</param>
82-
/// <param name="connection">The sql connection.</param>
82+
/// <param name="connectionFactory">A func that create a new SqlConnection</param>
8383
/// <param name="schema">The SQL schema name to use. Defaults to 'dbo'.</param>
8484
/// <returns>
8585
/// A builder for a database upgrader designed for SQL Server databases.
8686
/// </returns>
87-
public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported, SqlConnection connection, string schema)
87+
public static UpgradeEngineBuilder SqlDatabase(this SupportedDatabases supported, Func<SqlConnection> connectionFactory, string schema)
8888
{
89-
return SqlDatabase(new SqlConnectionManager(connection), schema);
89+
return SqlDatabase(new SqlConnectionManager(connectionFactory), schema);
9090
}
9191

9292
/// <summary>

0 commit comments

Comments
 (0)