Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
MattEdwardsWaggleBee committed Apr 30, 2024
2 parents 3f48273 + 282b523 commit a16823e
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 56 deletions.
File renamed without changes.
5 changes: 4 additions & 1 deletion NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

## TODO

* There must be something
* Create basic runners that can be used with Powershell tools
* Dynamic provider loading
* Option to specify provider through configuration and/or command line
* Providers should have own startup/registration
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ developer involved. Migrations solve the problem of evolving a database schema (
(for example, the developer's local database, the test database and the production database). Database changes
are described in classes written in C# that can be checked into a version control system.

The framework API is heavily influenced by [Fluent Migrator](https://github.com/schambers/fluentmigrator)
and [Raven Migrations](https://github.com/migrating-ravens/RavenMigrations).
The framework API is heavily influenced by [Fluent Migrator](https://github.com/schambers/fluentmigrator), [Raven Migrations](https://github.com/migrating-ravens/RavenMigrations) and [DbUp](https://github.com/DbUp/DbUp)

## Concepts

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
</ItemGroup>
<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="/" />
<None Include="..\..\LICENSE.md">
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<None Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="/" />
<None Include="..\..\LICENSE.md">
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<None Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
Expand Down
106 changes: 106 additions & 0 deletions src/Hyperbee.Migrations.Providers.MongoDB/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Hyperbee Migrations MongoDB Provider

Welcome to `Hyperbee.Migrations.Providers.MongoDB`, an extension to the foundational `Hyperbee.Migrations` library. This extension is specifically designed to incorporate support for MongoDB.

## Introduction

The `Hyperbee.Migrations.Providers.MongoDB` library is an tool for developers seeking to perform database migrations in a MongoDB environment using the `Hyperbee.Migrations` library. This library furnishes a comprehensive suite of tools and functionalities that integrate effortlessly with `Hyperbee.Migrations`, thereby enabling developers to harness the robust capabilities of MongoDB in their applications.

The `Hyperbee.Migrations.Providers.MongoDB` library is equipped to assist developers in creating, updating, and managing their MongoDB databases. With the aid of this library, developers can concentrate their efforts on the development of their application, while the library handles the intricacies of database management.

We are committed to continuous improvement and feature enhancement. We appreciate your interest and look forward to your valuable feedback.

Please see [Hyperbee Migrations' Read Me](../Hyperbee.Migrations/README.md) for non-database specific usage.

## Configuration

### Add MongoDB Services
```c#
// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Configure MongoDB
services.AddTransient<IMongoClient, MongoClient>( _ => new MongoClient( connectionString ) );

// Add the MigrationRunner
services.AddMongoDBMigrations(...);
}

public void Configure(IApplicationBuilder app, ...)
{
// Run pending migrations.
var migrationService = app.ApplicationServices.GetRequiredService<MigrationRunner>();
migrationService.Run();
}
```
### Preventing simultaneous migrations

By default, Hyperbee Migrations prevents parallel migration runner execution. If you have 2 instances of your
app running, and both try to run migrations, Hyperbee Migrations will prevent the second instance from running
migrations and will log a warning.

Hyperbee Migrations accomplishes this by using a distributed lock at the database layer. The default
implementation is based on the provider and uses a timeout and an auto-renewal interval to prevent orphaned locks.

If you want to change this behavior you can override the default options:

```c#
services.AddMongoDBMigrations( options =>
{
// Locking is on by default. Set to false to allow simultaneous runners - but don't be that guy.
options.LockingEnabled = false;

// You can change locking behavior. Defaults shown.
options.LockMaxLifetime = TimeSpan.FromMinutes( 1 ); // max time-to-live
options.LockName = "ledger"
});
```

### Dependency Injection

Hyperbee Migrations relies on dependency injection to pass services to your migration. For MongoDB you can directly use the `IMongoClient` and interact with MongoDB directly.

```c#
[Migration(1)]
public class MyMigration : Migration
{
private IMongoClient _mongoClient;
private ILogger _logger;

// Injected services registered with the container
public MyMigration( IMongoClient mongoClient, ILogger<MyMigration> logger )
{
_mongoClient = mongoClient;
_logger = logger;
}

public async override Task UpAsync( CancellationToken cancellationToken = default )
{
// do something with mongoClient
}
}
```

The MongoDB provider also provides a `MongoDBResourceRunner<MyMigration>` that adds helpful functionality when using embedded resources.
- `DocumentsFromAsync` inserts documents into database/collections within MongoDB. This is normally use for pre seeding the database.

```c#
[Migration(1)]
public class MyMigration : Migration
{
private readonly MongoDBResourceRunner<MyMigration> _resourceRunner;

public CreateInitialBuckets( MongoDBResourceRunner<MyMigration> resourceRunner )
{
_resourceRunner = resourceRunner;
}

public override async Task UpAsync( CancellationToken cancellationToken = default )
{
// run a `resource` migration to create initial state.
await resourceRunner.DocumentsFromAsync( [
"administration/users/user.json"
], cancellationToken );
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="/" />
<None Include="..\..\LICENSE.md">
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<None Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
Expand Down
84 changes: 84 additions & 0 deletions src/Hyperbee.Migrations.Providers.Postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Hyperbee Migrations Postgres Provider

Welcome to `Hyperbee.Migrations.Providers.Postgres`, an extension to the foundational `Hyperbee.Migrations` library. This extension is specifically designed to incorporate support for Postgres.

## Introduction

The `Hyperbee.Migrations.Providers.Postgres` library is an tool for developers seeking to perform database migrations in a Postgres environment using the `Hyperbee.Migrations` library. This library furnishes a comprehensive suite of tools and functionalities that integrate effortlessly with `Hyperbee.Migrations`, thereby enabling developers to harness the robust capabilities of Postgres in their applications.

The `Hyperbee.Migrations.Providers.Postgres` library is equipped to assist developers in creating, updating, and managing their Postgres databases. With the aid of this library, developers can concentrate their efforts on the development of their application, while the library handles the intricacies of database management.

We are committed to continuous improvement and feature enhancement. We appreciate your interest and look forward to your valuable feedback.

Please see [Hyperbee Migrations' Read Me](../Hyperbee.Migrations/README.md) for non-database specific usage.

## Configuration

### Add Postgres Services
```c#
// In Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Configure Postgres
services.AddNpgsqlDataSource( connectionString );

// Add the MigrationRunner
services.AddPostgresMigrations(...);
}

public void Configure(IApplicationBuilder app, ...)
{
// Run pending migrations.
var migrationService = app.ApplicationServices.GetRequiredService<MigrationRunner>();
migrationService.Run();
}
```
### Preventing simultaneous migrations

By default, Hyperbee Migrations prevents parallel migration runner execution. If you have 2 instances of your
app running, and both try to run migrations, Hyperbee Migrations will prevent the second instance from running
migrations and will log a warning.

Hyperbee Migrations accomplishes this by using a distributed lock at the database layer. The default
implementation is based on the provider and uses a timeout and an auto-renewal interval to prevent orphaned locks.

If you want to change this behavior you can override the default options:

```c#
services.AddPostgresDBMigrations( options =>
{
// Locking is on by default. Set to false to allow simultaneous runners - but don't be that guy.
options.LockingEnabled = false;

// You can change locking behavior. Defaults shown.
options.LockMaxLifetime = TimeSpan.FromMinutes( 1 ); // max time-to-live
options.LockName = "ledger"
});
```

### Dependency Injection

The Postgres provider also provides a `PostgresResourceRunner<MyMigration>` that adds helpful functionality when using embedded resources.
- `SqlFromAsync` runs sql statements and can do any sort of updates supported by Postgres
- `AllSqlFromAsync` runs all the embedded resources with the resource named matching the Migration. See [1000-Initial](../../samples/Hyperbee.Migrations.Postgres.Samples/Migrations/1000-Initial.cs) as an example

```c#
[Migration(1)]
public class MyMigration : Migration
{
private readonly MongoDBResourceRunner<MyMigration> _resourceRunner;

public CreateInitialBuckets( MongoDBResourceRunner<MyMigration> resourceRunner )
{
_resourceRunner = resourceRunner;
}

public override async Task UpAsync( CancellationToken cancellationToken = default )
{
// run a `resource` migration to create initial state.
await resourceRunner.SqlFromAsync( [
"1-MyMigration.sql"
], cancellationToken );
}
}
```
45 changes: 0 additions & 45 deletions src/Hyperbee.Migrations/Hyperbee - Backup.Migrations.csproj

This file was deleted.

4 changes: 2 additions & 2 deletions src/Hyperbee.Migrations/Hyperbee.Migrations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
</ItemGroup>
<ItemGroup>
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="/" />
<None Include="..\..\LICENSE.md">
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<None Include="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
Expand Down
Loading

0 comments on commit a16823e

Please sign in to comment.