Skip to content

Commit

Permalink
Added NpgsqlDataSource to Postgres SingleServerDatabaseCollection and…
Browse files Browse the repository at this point in the history
… Migrator
  • Loading branch information
oskardudycz committed Dec 7, 2023
1 parent d1be272 commit 036af5c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 21 deletions.
26 changes: 11 additions & 15 deletions src/Weasel.Core/Migrations/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,33 @@ public DatabaseBase(
Migrator migrator,
string identifier,
string connectionString
)
): this(logger, autoCreate, migrator, identifier, () => CreateConnection(connectionString))
{
_logger = logger;
_connectionSource = () =>
{
var conn = new TConnection();
conn.ConnectionString = connectionString;

return conn;
};

AutoCreate = autoCreate;
Migrator = migrator;
Identifier = identifier;
}

public DatabaseBase(
IMigrationLogger logger,
AutoCreate autoCreate,
Migrator migrator,
string identifier,
Func<TConnection> connectionSource
Func<TConnection>? connectionSource
)
{
_logger = logger;
_connectionSource = connectionSource;
_connectionSource = connectionSource ?? CreateConnection;
AutoCreate = autoCreate;
Migrator = migrator;
Identifier = identifier;
}

private static TConnection CreateConnection(string connectionString)
{
var conn = new TConnection();
conn.ConnectionString = connectionString;

return conn;
}

public abstract IFeatureSchema[] BuildFeatureSchemas();

public AutoCreate AutoCreate { get; set; }
Expand Down
10 changes: 5 additions & 5 deletions src/Weasel.Core/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task WriteTemplatedFile(string filename, Action<Migrator, TextWrite
/// <param name="migration"></param>
/// <param name="autoCreate"></param>
/// <param name="logger"></param>
public async Task ApplyAllAsync(
public Task ApplyAllAsync(
DbConnection conn,
SchemaMigration migration,
AutoCreate autoCreate,
Expand All @@ -118,23 +118,23 @@ public async Task ApplyAllAsync(
{
if (autoCreate == AutoCreate.None)
{
return;
return Task.CompletedTask;
}

if (migration.Difference == SchemaPatchDifference.None)
{
return;
return Task.CompletedTask;
}

if (!migration.Deltas.Any())
{
return;
return Task.CompletedTask;
}

migration.AssertPatchingIsValid(autoCreate);

logger ??= new DefaultMigrationLogger();
await executeDelta(migration, conn, autoCreate, logger, ct).ConfigureAwait(false);
return executeDelta(migration, conn, autoCreate, logger, ct);
}

protected abstract Task executeDelta(
Expand Down
13 changes: 13 additions & 0 deletions src/Weasel.Postgresql.Tests/Migrations/migration_scenario_tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ protected override IEnumerable<ISchemaObject> schemaObjects()

public class DatabaseWithTables: PostgresqlDatabase
{
public static DatabaseWithTables ForDataSource(NpgsqlDataSource dataSource)
{
var builder = new NpgsqlConnectionStringBuilder(dataSource.ConnectionString);
var identifier = builder.Database;

return new DatabaseWithTables(identifier, dataSource);
}

public static DatabaseWithTables ForConnectionString(string connectionString)
{
var builder = new NpgsqlConnectionStringBuilder(connectionString);
Expand All @@ -169,6 +177,11 @@ public DatabaseWithTables(string identifier, string connectionString)
{
}

public DatabaseWithTables(string identifier, NpgsqlDataSource dataSource)
: base(new DefaultMigrationLogger(), AutoCreate.All, new PostgresqlMigrator(), identifier, dataSource)
{
}

public LightweightCache<string, NamedTableFeature> Features { get; } =
new LightweightCache<string, NamedTableFeature>(name =>
new NamedTableFeature(name, new PostgresqlMigrator()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ namespace Weasel.Postgresql.Migrations;
/// <typeparam name="T"></typeparam>
public abstract class SingleServerDatabaseCollection<T> where T : PostgresqlDatabase
{
private readonly NpgsqlDataSource? npgsqlDataSource;
private readonly TimedLock _lock = new();
private readonly string _masterConnectionString;
private ImHashMap<string, T> _databases = ImHashMap<string, T>.Empty;

protected SingleServerDatabaseCollection(NpgsqlDataSource npgsqlDataSource)
{
this.npgsqlDataSource = npgsqlDataSource;
_masterConnectionString = npgsqlDataSource.ConnectionString;
}

protected SingleServerDatabaseCollection(string masterConnectionString)
{
_masterConnectionString = masterConnectionString;
Expand Down Expand Up @@ -48,7 +55,9 @@ public virtual async ValueTask<T> FindOrCreateDatabase(string databaseName, Canc
return database;
}

await using var conn = new NpgsqlConnection(_masterConnectionString);
await using var conn =
npgsqlDataSource?.CreateConnection()
?? new NpgsqlConnection(_masterConnectionString);
await conn.OpenAsync(ct).ConfigureAwait(false);

if (DropAndRecreate)
Expand Down
10 changes: 10 additions & 0 deletions src/Weasel.Postgresql/PostgresqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ string connectionString
{
}

protected PostgresqlDatabase(
IMigrationLogger logger,
AutoCreate autoCreate,
Migrator migrator,
string identifier,
NpgsqlDataSource dataSource
): base(logger, autoCreate, migrator, identifier, dataSource.CreateConnection)
{
}

protected PostgresqlDatabase(
IMigrationLogger logger,
AutoCreate autoCreate,
Expand Down

0 comments on commit 036af5c

Please sign in to comment.