Skip to content

Commit

Permalink
correct down sql server migrations + tests (#106)
Browse files Browse the repository at this point in the history
* correct down sql server migrations + tests

* add rollback migrations for async context dispose
  • Loading branch information
win7user10 authored Oct 1, 2024
1 parent ccca2f9 commit ba8f1f5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public interface ISqlGenerator
/// <param name="entity"></param>
/// <returns></returns>
string GetTableSql(Type entity);

/// <summary>
/// Get schema prefix SQL, e.g. "dbo".
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
string? GetSchemaPrefixSql(Type entity);

/// <summary>
/// Get the function name with the entities schema.
Expand Down
17 changes: 12 additions & 5 deletions src/Laraue.EfCoreTriggers.Common/SqlGeneration/SqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ public string GetColumnSql(Type type, MemberInfo memberInfo, ArgumentType argume
/// <inheritdoc />
public string GetTableSql(Type entity)
{
var schemaName = _adapter.GetTableSchemaName(entity);
var schemaPrefix = GetSchemaPrefixSql(entity);
var tableSql = WrapWithDelimiters(_adapter.GetTableName(entity));

return string.IsNullOrWhiteSpace(schemaName)
? tableSql
: $"{WrapWithDelimiters(schemaName)}.{tableSql}";
return $"{schemaPrefix}{tableSql}";
}

/// <inheritdoc />
public string? GetSchemaPrefixSql(Type entity)
{
var schemaName = _adapter.GetTableSchemaName(entity);
return string.IsNullOrEmpty(schemaName)
? null
: $"{WrapWithDelimiters(schemaName)}.";
}

/// <inheritdoc />
Expand All @@ -115,7 +122,7 @@ public string GetFunctionNameSql(Type entity, string name)
{
return GetFunctionNameSql(_adapter.GetTableSchemaName(entity), name);
}

private string GetFunctionNameSql(string? schemaName, string triggerName)
{
var functionName = WrapWithDelimiters(triggerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ private static string GetDeclareCursorSql(string cursorName)

public override string GenerateDeleteTriggerSql(string triggerName, IEntityType entityType)
{
return $"DROP TRIGGER {triggerName};";
var tableSchemaPrefix = _sqlGenerator.GetSchemaPrefixSql(entityType.ClrType);

return $"DROP TRIGGER {tableSchemaPrefix}{triggerName};";
}

private string GetSelectFromCursorSql(Type triggerEntityType, ArgumentType argumentType, MemberInfo[] members)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Laraue.EfCoreTriggers.Tests.Infrastructure
{
Expand Down Expand Up @@ -30,14 +34,45 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

public override async ValueTask DisposeAsync()
{
foreach (var command in GetMigrationDownCommands())
{
await Database.ExecuteSqlRawAsync(command.CommandText);
}

await Database.EnsureDeletedAsync();
await base.DisposeAsync();
}

public override void Dispose()
{
foreach (var command in GetMigrationDownCommands())
{
Database.ExecuteSqlRaw(command.CommandText);
}

Database.EnsureDeleted();
base.Dispose();
}

private IReadOnlyList<MigrationCommand> GetMigrationDownCommands()
{
var relationalModel = GetRelationalModel();

var migrationsModelDiffer = Database.GetService<IMigrationsModelDiffer>();
var downMigrationDifferences = migrationsModelDiffer
.GetDifferences(relationalModel, null);

var migrationsSqlGenerator = Database.GetService<IMigrationsSqlGenerator>();
return migrationsSqlGenerator.Generate(downMigrationDifferences);
}

private IRelationalModel GetRelationalModel()
{
#if NET5_0
return Model.GetRelationalModel();
#else
return Database.GetService<IDesignTimeModel>().Model.GetRelationalModel();
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static DynamicDbContext GetDbContext(IContextOptionsFactory<DynamicDbCont

var context = new DynamicDbContext(contextOptions, setupDbContext);

context.Database.EnsureDeleted();
context.Database.EnsureCreated();

return context;
Expand Down

0 comments on commit ba8f1f5

Please sign in to comment.