Skip to content

Commit

Permalink
Fix migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Mar 1, 2024
1 parent 0c03330 commit d67895e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"version": 1,
"isRoot": true,
"tools": {
Expand All @@ -7,4 +8,4 @@
"commands": ["dotnet-ef"]
}
}
}
}
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ SPDX-License-Identifier: MIT
<!-- all -->
<PackageVersion Include="GitVersion.MsBuild" Version="5.12.0" />
<!-- SqliteTimestamp -->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<!-- unit tests -->
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageVersion Include="MSTest.TestFramework" Version="3.2.2" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="coverlet.collector" Version="6.0.1" />
<!-- Example -->
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ SPDX-License-Identifier: MIT
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 0 additions & 5 deletions SqliteTimestamp/SqliteTimestamp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ SPDX-License-Identifier: MIT
<AssemblyName>Dorssel.EntityFrameworkCore.Sqlite.Timestamp</AssemblyName>

<IsPackable>true</IsPackable>
<GenerateRuntimeConfigurationFiles>false</GenerateRuntimeConfigurationFiles>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>Dorssel.EntityFrameworkCore.Sqlite.Timestamp</PackageId>
<SignAssembly>true</SignAssembly>
Expand All @@ -23,10 +22,6 @@ SPDX-License-Identifier: MIT
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" />
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
</ItemGroup>
Expand Down
21 changes: 12 additions & 9 deletions SqliteTimestamp/SqliteTimestampMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ public override IReadOnlyList<MigrationCommand> Generate(IReadOnlyList<Migration
{
switch (operation)
{
case TableOperation tableOperation:
if (operation is RenameTableOperation renameTableOperation)
{
tablesAffected.Add(new(renameTableOperation.NewName ?? renameTableOperation.Name, renameTableOperation.NewSchema));
}
tablesAffected.Add(new(tableOperation.Name, tableOperation.Schema));
case CreateTableOperation createTableOperation:
tablesAffected.Add(new(createTableOperation.Name, createTableOperation.Schema));
break;

case ColumnOperation columnOperation:
tablesAffected.Add(new(columnOperation.Table, columnOperation.Schema));
case AlterTableOperation alterTableOperation:
tablesAffected.Add(new(alterTableOperation.Name, alterTableOperation.Schema));
tablesAffected.Add(new(alterTableOperation.OldTable.Name, alterTableOperation.OldTable.Schema));
break;
case RenameTableOperation renameTableOperation:
tablesAffected.Add(new(renameTableOperation.NewName ?? renameTableOperation.Name, renameTableOperation.NewSchema));
tablesAffected.Add(new(renameTableOperation.Name, renameTableOperation.Schema));
break;
case DropTableOperation dropTableOperation:
tablesAffected.Add(new(dropTableOperation.Name, dropTableOperation.Schema));
break;
}
}
Expand Down
90 changes: 88 additions & 2 deletions UnitTests/SqliteTimestampMigrationsSqlGenerator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,102 @@
//
// SPDX-License-Identifier: MIT

using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;

namespace UnitTests;

[TestClass]
sealed class SqliteTimestampMigrationsSqlGenerator_Tests
{
[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_Create()
public void SqliteTimestampMigrationsSqlGenerator_CreateTable()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();
var designTimeModel = db.GetService<IDesignTimeModel>();
var operations = new List<MigrationOperation>
{
new CreateTableOperation() { Name = MemoryDbContext.TestTableName }
};

var result = generator.Generate(operations, designTimeModel.Model);

Assert.IsTrue(result.Any(c => c.CommandText.Contains("TRIGGER")));
}

[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_RenameTable()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();
var designTimeModel = db.GetService<IDesignTimeModel>();
var operations = new List<MigrationOperation>
{
new RenameTableOperation() { Name = MemoryDbContext.TestTableName, NewName = "Renamed" }
};

var result = generator.Generate(operations, designTimeModel.Model);

Assert.IsTrue(result.Any(c => c.CommandText.Contains("TRIGGER")));
}

[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_RenameSchema()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();
var designTimeModel = db.GetService<IDesignTimeModel>();
var operations = new List<MigrationOperation>
{
new RenameTableOperation() { Name = MemoryDbContext.TestTableName, NewSchema = "Renamed" }
};

var result = generator.Generate(operations, designTimeModel.Model);

Assert.IsTrue(result.Any(c => c.CommandText.Contains("TRIGGER")));
}

[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_AlterTable()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();
var designTimeModel = db.GetService<IDesignTimeModel>();
var operations = new List<MigrationOperation>
{
new AlterTableOperation() { Name = MemoryDbContext.TestTableName }
};

var result = generator.Generate(operations, designTimeModel.Model);

Assert.IsTrue(result.Any(c => c.CommandText.Contains("TRIGGER")));
}

[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_DropTable()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();
var designTimeModel = db.GetService<IDesignTimeModel>();
var operations = new List<MigrationOperation>
{
new DropTableOperation() { Name = MemoryDbContext.TestTableName }
};

var result = generator.Generate(operations, designTimeModel.Model);

Assert.IsTrue(result.Any(c => c.CommandText.Contains("TRIGGER")));
}

[TestMethod]
public void SqliteTimestampMigrationsSqlGenerator_NullModelNoThrow()
{
using var db = new MemoryDbContext();
var generator = db.GetService<IMigrationsSqlGenerator>();

db.Database.EnsureCreated();
_ = generator.Generate([]);
}
}
1 change: 0 additions & 1 deletion UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ SPDX-License-Identifier: MIT
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="coverlet.collector" />
Expand Down
13 changes: 13 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
#
# SPDX-License-Identifier: MIT

---
coverage:
status:
patch:
default:
informational: true
project:
default:
informational: true
1 change: 1 addition & 0 deletions dotnet-ef-sqlite-timestamp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitattributes = .gitattributes
.gitignore = .gitignore
.mega-linter.yml = .mega-linter.yml
codecov.yml = codecov.yml
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
GitVersion.yml = GitVersion.yml
Expand Down

0 comments on commit d67895e

Please sign in to comment.