Skip to content

Commit

Permalink
Allow to exclude a table from migrations
Browse files Browse the repository at this point in the history
Store the entity types mapped to views in the snapshot

Fixes #2725
  • Loading branch information
AndriySvyryd committed Jun 24, 2020
1 parent 2213f95 commit d2b6759
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Newtonsoft.Json.Linq;

namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
{
Expand Down
87 changes: 62 additions & 25 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public virtual void Generate(string builderName, IModel model, IndentedStringBui
GenerateSequence(builderName, sequence, stringBuilder);
}

GenerateEntityTypes(builderName, Sort(model.GetEntityTypes().Where(et => !et.IsIgnoredByMigrations()).ToList()), stringBuilder);
GenerateEntityTypes(builderName, Sort(model.GetEntityTypes()), stringBuilder);
}

private static IReadOnlyList<IEntityType> Sort(IReadOnlyList<IEntityType> entityTypes)
private static IReadOnlyList<IEntityType> Sort(IEnumerable<IEntityType> entityTypes)
{
var entityTypeGraph = new Multigraph<IEntityType, int>();
entityTypeGraph.AddVertices(entityTypes);
Expand Down Expand Up @@ -774,38 +774,75 @@ protected virtual void GenerateEntityTypeAnnotations(
.ToDictionary(a => a.Name, a => a);

var tableNameAnnotation = annotations.Find(RelationalAnnotationNames.TableName);
var schemaAnnotation = annotations.Find(RelationalAnnotationNames.Schema);

var nonDefaultName = false;
if (tableNameAnnotation?.Value != null
|| entityType.BaseType == null)
{
stringBuilder
.AppendLine()
.Append(builderName)
.Append(".")
.Append(nameof(RelationalEntityTypeBuilderExtensions.ToTable))
.Append("(")
.Append(Code.Literal((string)tableNameAnnotation?.Value ?? entityType.GetTableName()));
if (tableNameAnnotation != null)
var tableName = (string)tableNameAnnotation?.Value ?? entityType.GetTableName();
if (tableName != null)
{
annotations.Remove(tableNameAnnotation.Name);
stringBuilder
.AppendLine()
.Append(builderName)
.Append(".ToTable(")
.Append(Code.Literal(tableName));
if (tableNameAnnotation != null)
{
annotations.Remove(tableNameAnnotation.Name);
}

var schemaAnnotation = annotations.Find(RelationalAnnotationNames.Schema);
if (schemaAnnotation?.Value != null)
{
stringBuilder
.Append(", ")
.Append(Code.Literal((string)schemaAnnotation.Value));
annotations.Remove(schemaAnnotation.Name);
}

var isExcludedAnnotation = annotations.Find(RelationalAnnotationNames.IsTableExcludedFromMigrations);
if (isExcludedAnnotation != null)
{
if (((bool?)isExcludedAnnotation.Value) == true)
{
stringBuilder
.Append(", ")
.Append(Code.Literal(true));
}
annotations.Remove(isExcludedAnnotation.Name);
}

stringBuilder.AppendLine(");");
}
nonDefaultName = true;
}

if (schemaAnnotation?.Value != null)
var viewNameAnnotation = annotations.Find(RelationalAnnotationNames.ViewName);
if (viewNameAnnotation?.Value != null
|| entityType.BaseType == null)
{
stringBuilder
.Append(",")
.Append(Code.Literal((string)schemaAnnotation.Value));
annotations.Remove(schemaAnnotation.Name);
nonDefaultName = true;
}
var viewName = (string)viewNameAnnotation?.Value ?? entityType.GetViewName();
if (viewName != null)
{
stringBuilder
.AppendLine()
.Append(builderName)
.Append(".ToView(")
.Append(Code.Literal(viewName));
if (viewNameAnnotation != null)
{
annotations.Remove(viewNameAnnotation.Name);
}

if (nonDefaultName)
{
stringBuilder.AppendLine(");");
var viewSchemaAnnotation = annotations.Find(RelationalAnnotationNames.ViewSchema);
if (viewSchemaAnnotation?.Value != null)
{
stringBuilder
.Append(", ")
.Append(Code.Literal((string)viewSchemaAnnotation.Value));
annotations.Remove(viewSchemaAnnotation.Name);
}

stringBuilder.AppendLine(");");
}
}

if ((discriminatorPropertyAnnotation?.Value
Expand Down
Loading

0 comments on commit d2b6759

Please sign in to comment.