Skip to content

Commit

Permalink
Made FindOrCreateDatabase method of SingleServerDatabaseCollection vi…
Browse files Browse the repository at this point in the history
…rtual to allow overrides.

Used await using where possible
Bumped alpha version
  • Loading branch information
oskardudycz committed Nov 29, 2022
1 parent 5afdffb commit 532798e
Show file tree
Hide file tree
Showing 24 changed files with 59 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>6.0.0-alpha.2</Version>
<Version>6.0.0-alpha.3</Version>
<LangVersion>11.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/CommandBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public async Task<IReadOnlyList<T>> FetchList<T>(TConnection conn, Func<DbDataRe

var list = new List<T>();

using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
await using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
while (await reader.ReadAsync(cancellation).ConfigureAwait(false))
{
list.Add(await transform(reader).ConfigureAwait(false));
Expand Down
4 changes: 2 additions & 2 deletions src/Weasel.Core/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static async Task<IReadOnlyList<T>> FetchList<T>(this DbCommand cmd, Func
{
var list = new List<T>();

using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
await using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
while (await reader.ReadAsync(cancellation).ConfigureAwait(false))
{
list.Add(await transform(reader).ConfigureAwait(false));
Expand Down Expand Up @@ -164,7 +164,7 @@ public static async Task<IReadOnlyList<T>> FetchList<T>(this DbCommand cmd, Func
/// <returns></returns>
public static async Task<T?> FetchOne<T>(this DbCommand cmd, CancellationToken cancellation = default)
{
using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
await using var reader = await cmd.ExecuteReaderAsync(cancellation).ConfigureAwait(false);
if (await reader.ReadAsync(cancellation).ConfigureAwait(false))
{
if (await reader.IsDBNullAsync(0, cancellation).ConfigureAwait(false))
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/ConnectionSourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void RunSql<T>(this IConnectionSource<T> source, string sql) where
/// <typeparam name="T"></typeparam>
public static async Task RunSqlAsync<T>(this IConnectionSource<T> source, string sql) where T : DbConnection
{
using var conn = source.CreateConnection();
await using var conn = source.CreateConnection();
await conn.OpenAsync().ConfigureAwait(false);

try
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/Migrations/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Task<SchemaMigration> CreateMigrationAsync(Type featureType)

public async Task AssertConnectivity()
{
using var conn = CreateConnection();
await using var conn = CreateConnection();
await conn.OpenAsync().ConfigureAwait(false);
await conn.CloseAsync().ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/Migrations/TimedLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<LockReleaser> Lock(TimeSpan timeout)
throw new TimeoutException();
}

public struct LockReleaser : IDisposable
public readonly struct LockReleaser : IDisposable
{
private readonly SemaphoreSlim toRelease;

Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void ReadTemplates()
/// <param name="writeStep"></param>
public async Task WriteTemplatedFile(string filename, Action<Migrator, TextWriter> writeStep)
{
using var stream = new FileStream(filename, FileMode.Create);
await using var stream = new FileStream(filename, FileMode.Create);
var writer = new StreamWriter(stream) { AutoFlush = true };

WriteScript(writer, writeStep);
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Core/SchemaMigration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static async Task<SchemaMigration> Determine(DbConnection conn, params IS
schemaObject.ConfigureQueryCommand(builder);
}

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);

deltas.Add(await schemaObjects[0].CreateDelta(reader).ConfigureAwait(false));

Expand Down
6 changes: 3 additions & 3 deletions src/Weasel.Postgresql.Tests/CommandBuilderIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task use_parameters_to_query_by_anonymous_type()

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down Expand Up @@ -70,7 +70,7 @@ public async Task use_parameters_to_query_by_anonymous_type_by_generic_command_b

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down Expand Up @@ -162,7 +162,7 @@ public async Task add_named_parameter()

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down
24 changes: 12 additions & 12 deletions src/Weasel.Postgresql.Tests/advisory_lock_usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class advisory_lock_usage
[Fact]
public async Task explicitly_release_global_session_locks()
{
using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -46,9 +46,9 @@ public async Task explicitly_release_global_session_locks()
[Fact]
public async Task explicitly_release_global_tx_session_locks()
{
using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -84,9 +84,9 @@ public async Task explicitly_release_global_tx_session_locks()
[Fact] // - too slow
public async Task global_session_locks()
{
using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -117,9 +117,9 @@ public async Task global_session_locks()
[Fact] // -- too slow
public async Task tx_session_locks()
{
using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new NpgsqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new NpgsqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public async Task execute_once()
.With("id", 3).With("tag", "Toodles").With("age", 5);

await command.ExecuteOnce();
using var reader = await theConnection.CreateCommand("select id, tag, age from general.thing")


await using var reader = await theConnection.CreateCommand("select id, tag, age from general.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Postgresql/Functions/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ FROM pg_proc p

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
return await readExisting(reader).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public abstract class SingleServerDatabaseCollection<T> where T : PostgresqlData
private ImHashMap<string, T> _databases = ImHashMap<string, T>.Empty;
private readonly TimedLock _lock = new TimedLock();

public SingleServerDatabaseCollection(string masterConnectionString)
protected SingleServerDatabaseCollection(string masterConnectionString)
{
_masterConnectionString = masterConnectionString;
}

public DatabaseSpecification Specification { get; } = new DatabaseSpecification();
private DatabaseSpecification Specification { get; } = new();

public IReadOnlyList<T> AllDatabases()
{
Expand All @@ -38,7 +38,7 @@ public IReadOnlyList<T> AllDatabases()

protected abstract T buildDatabase(string databaseName, string connectionString);

public async ValueTask<T> FindOrCreateDatabase(string databaseName)
public virtual async ValueTask<T> FindOrCreateDatabase(string databaseName)
{
if (_databases.TryFind(databaseName, out var database))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Postgresql/SchemaObjectsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static async Task<bool> FunctionExists(this NpgsqlConnection conn, DbObje
var sql =
"SELECT specific_schema, routine_name FROM information_schema.routines WHERE type_udt_name != 'trigger' and routine_name like :name and specific_schema = :schema;";

using var reader = await conn.CreateCommand(sql)
await using var reader = await conn.CreateCommand(sql)
.With("name", functionIdentifier.Name)
.With("schema", functionIdentifier.Schema)

Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Postgresql/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<ISchemaObjectDelta> FindDelta(NpgsqlConnection conn)

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);

return await CreateDelta(reader).ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Postgresql/Tables/Table.FetchExisting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ information_schema.columns col

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
return await readExisting(reader).ConfigureAwait(false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.Postgresql/Tables/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public async Task<bool> ExistsInDatabase(NpgsqlConnection conn)
.With("table", Identifier.Name)
.With("schema", Identifier.Schema);

using var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
await using var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false);
var any = await reader.ReadAsync().ConfigureAwait(false);
return any;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Weasel.SqlServer.Tests/CommandBuilderIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task use_parameters_to_query_by_anonymous_type()

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task use_parameters_to_query_by_anonymous_type_generic_db_builder()

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down Expand Up @@ -210,7 +210,7 @@ public async Task add_named_parameter()

await builder.ExecuteNonQueryAsync(theConnection);

using var reader = await theConnection.CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing")
await using var reader = await theConnection.CreateCommand("select id, tag, age, rate, sequence, is_done from integration.thing")
.ExecuteReaderAsync();

await reader.ReadAsync();
Expand Down
24 changes: 12 additions & 12 deletions src/Weasel.SqlServer.Tests/advisory_lock_usage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class advisory_lock_usage
[Fact]
public async Task explicitly_release_global_session_locks()
{
using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -45,9 +45,9 @@ public async Task explicitly_release_global_session_locks()
[Fact]
public async Task explicitly_release_global_tx_session_locks()
{
using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -83,9 +83,9 @@ public async Task explicitly_release_global_tx_session_locks()
[Fact] // - too slow
public async Task global_session_locks()
{
using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down Expand Up @@ -116,9 +116,9 @@ public async Task global_session_locks()
[Fact] // -- too slow
public async Task tx_session_locks()
{
using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn1 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn2 = new SqlConnection(ConnectionSource.ConnectionString))
await using (var conn3 = new SqlConnection(ConnectionSource.ConnectionString))
{
await conn1.OpenAsync();
await conn2.OpenAsync();
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.SqlServer/Procedures/StoredProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public string CanonicizeSql()

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
return await readExisting(reader).ConfigureAwait(false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.SqlServer/SchemaObjectsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static async Task<bool> FunctionExists(this SqlConnection conn, DbObjectN
var sql =
"SELECT specific_schema, routine_name FROM information_schema.routines WHERE type_udt_name != 'trigger' and routine_name like :name and specific_schema = :schema;";

using var reader = await conn.CreateCommand(sql)
await using var reader = await conn.CreateCommand(sql)
.With("name", functionIdentifier.Name)
.With("schema", functionIdentifier.Schema)
.ExecuteReaderAsync().ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.SqlServer/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task<ISchemaObjectDelta> FindDelta(SqlConnection conn)

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);

return await CreateDelta(reader).ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Weasel.SqlServer/Tables/Table.FetchExisting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ order by

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
return await readExisting(reader).ConfigureAwait(false);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Weasel.SqlServer/Tables/TableType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public override bool Equals(object? obj)
return Equals((TableTypeColumn) obj);
}

public override int GetHashCode()
{
return HashCode.Combine(Name, DatabaseType, AllowNulls);
}
}

public async Task<TableType?> FetchExisting(SqlConnection conn)
Expand All @@ -159,7 +163,7 @@ public override bool Equals(object? obj)

ConfigureQueryCommand(builder);

using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
await using var reader = await builder.ExecuteReaderAsync(conn).ConfigureAwait(false);
return await readExisting(reader).ConfigureAwait(false);
}

Expand Down

0 comments on commit 532798e

Please sign in to comment.