Skip to content

Commit

Permalink
migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed Jun 30, 2020
1 parent fee773c commit 397f892
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.0.12 (2020/06/30)
## 0.0.14 (2020/06/30)
* Safer DB migrations

## 0.0.11 (2020/06/25)
Expand Down
6 changes: 3 additions & 3 deletions Common.targets
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project>
<PropertyGroup>
<Version>0.0.11</Version>
<AssemblyVersion>0.0.11</AssemblyVersion>
<FileVersion>0.0.11</FileVersion>
<Version>0.0.14</Version>
<AssemblyVersion>0.0.14</AssemblyVersion>
<FileVersion>0.0.14</FileVersion>
</PropertyGroup>
<PropertyGroup>
<!-- Χρόνος -->
Expand Down
3 changes: 3 additions & 0 deletions src/K4os.Shared/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public static T NotLessThan<T>(this T subject, T limit) =>
public static T NotMoreThan<T>(this T subject, T limit) =>
Compare(subject, limit) > 0 ? limit : subject;

public static string NotBlank(this string text, string defaultValue = null) =>
string.IsNullOrWhiteSpace(text) ? defaultValue : text;

public static R PipeTo<T, R>(this T subject, Func<T, R> func) =>
func(subject);

Expand Down
17 changes: 11 additions & 6 deletions src/K4os.Xpovoc.Core/Sql/AnySqlStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ protected virtual async Task<ILease<TConnection>> Connect()
if (connection.State == ConnectionState.Closed)
await OpenConnection(connection);

if (!IsDatabaseReady)
if (!DatabaseReady)
await TryCreateDatabase(connection);

return lease;
}
catch
Expand All @@ -59,26 +59,31 @@ protected virtual async Task<ILease<TConnection>> Connect()

protected async Task TryCreateDatabase(TConnection connection)
{
if (IsDatabaseReady)
if (DatabaseReady)
return;

await _migrationLock.WaitAsync();
try
{
// check again
if (IsDatabaseReady)
if (DatabaseReady)
return;

await CreateDatabase(connection);

DatabaseReady = true;
}
finally
{
_migrationLock.Release();
}
}

private bool IsDatabaseReady =>
Interlocked.CompareExchange(ref _databaseReady, 0, 0) != 0;
public bool DatabaseReady
{
get => Interlocked.CompareExchange(ref _databaseReady, 0, 0) != 0;
set => Interlocked.Exchange(ref _databaseReady, value ? 1 : 0);
}

protected virtual async Task<T> Exec<T>(
TConnection connection, Func<TConnection, Task<T>> action,
Expand Down
3 changes: 2 additions & 1 deletion src/K4os.Xpovoc.MsSql/MsSqlMigrator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using Dapper;
Expand All @@ -19,7 +20,7 @@ public MsSqlMigrator(
base("Xpovoc", connection, migrations)
{
var hasSchema = !string.IsNullOrWhiteSpace(schema);
_schemaName = schema ?? string.Empty;
_schemaName = schema ?? "dbo";
_tableName = "Migrations";

_schema = hasSchema ? Quoted(schema) : string.Empty;
Expand Down

0 comments on commit 397f892

Please sign in to comment.