Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move navigation renamers to tables #738

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/GUI/EFCorePowerTools/ViewModels/ObjectTreeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ public IEnumerable<Schema> GetRenamedObjects()
var originalReplacers = _allSchemas.Where(s => s.SchemaName == schema.Name)
.SelectMany(a => a.Tables.Where(t => t.Columns != null && t.Name == obj.Name))
.ToList();
var ignoredReplacers = originalReplacers.SelectMany(o => o.Columns.Where(c => c.Name.Equals(c.NewName))).ToList();

if (objectIsRenamed || renamedColumns.Any() || ignoredReplacers.Any())

var ignoredReplacers = originalReplacers
.SelectMany(o => o.Columns.Where(c => c.Name.Equals(c.NewName)))
.ToList();

var originalNavigationReplacers = originalReplacers
.Where(o => o.Navigations != null)
.SelectMany(o => o.Navigations).ToList();

if (objectIsRenamed || renamedColumns.Any() || ignoredReplacers.Any() || originalNavigationReplacers.Any())
{
var columnRenamers = renamedColumns
.Select(c => new ColumnNamer { Name = c.Name, NewName = c.NewName })
Expand All @@ -94,7 +101,8 @@ public IEnumerable<Schema> GetRenamedObjects()
Name = obj.Name,
NewName = obj.NewName,
Columns = columnRenamers.ToList(),
});
Navigations = originalNavigationReplacers,
});
}
}

Expand Down
64 changes: 55 additions & 9 deletions src/GUI/RevEng.Core/ReplacingCandidateNamingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using RevEng.Shared;
Expand All @@ -13,15 +14,15 @@ namespace RevEng.Core
public class ReplacingCandidateNamingService : CandidateNamingService
{
private readonly List<Schema> _customNameOptions;
private readonly Dictionary<string, string> _navigationReplacers;
private readonly List<TableRenamer> _navigationReplacers;

public ReplacingCandidateNamingService(List<Schema> customNameOptions)
{
_customNameOptions = customNameOptions;
_navigationReplacers = customNameOptions
.Where(o => o.Navigations != null)
.SelectMany(o => o.Navigations)
.ToDictionary(n => n.Name, n => n.NewName);
.Where(o => o.Tables != null)
.SelectMany(t => t.Tables)
.ToList();
}

public override string GenerateCandidateIdentifier(DatabaseTable originalTable)
Expand Down Expand Up @@ -122,10 +123,32 @@ public override string GenerateCandidateIdentifier(DatabaseColumn originalColumn
public override string GetPrincipalEndCandidateNavigationPropertyName(IForeignKey foreignKey, string dependentEndNavigationPropertyName)
{
var baseName = base.GetPrincipalEndCandidateNavigationPropertyName(foreignKey, dependentEndNavigationPropertyName);
var tableName = foreignKey.PrincipalEntityType.GetTableName();
var schemaName = foreignKey.PrincipalEntityType.GetSchema();

var schema = _customNameOptions
.Where(o => o.SchemaName == schemaName
&& o.Tables != null && o.Tables.Any())
.SingleOrDefault();

if (_navigationReplacers.TryGetValue(baseName, out string name))
if (schema != null)
{
return name;
var table = schema.Tables
.Where(t => t.Name == tableName
&& t.Navigations != null)
.SingleOrDefault();

if (table != null)
{
var navigationRenamer = table.Navigations
.Where(n => n.Name == baseName)
.SingleOrDefault();

if (navigationRenamer != null && navigationRenamer.NewName != null)
{
return navigationRenamer.NewName;
}
}
}

return baseName;
Expand All @@ -135,9 +158,32 @@ public override string GetDependentEndCandidateNavigationPropertyName(IForeignKe
{
var baseName = base.GetDependentEndCandidateNavigationPropertyName(foreignKey);

if (_navigationReplacers.TryGetValue(baseName, out string name))
var tableName = foreignKey.DeclaringEntityType.GetTableName();
var schemaName = foreignKey.DeclaringEntityType.GetSchema();

var schema = _customNameOptions
.Where(o => o.SchemaName == schemaName
&& o.Tables != null && o.Tables.Any())
.SingleOrDefault();

if (schema != null)
{
return name;
var table = schema.Tables
.Where(t => t.Name == tableName
&& t.Navigations != null)
.SingleOrDefault();

if (table != null)
{
var navigationRenamer = table.Navigations
.Where(n => n.Name == baseName)
.SingleOrDefault();

if (navigationRenamer != null && navigationRenamer.NewName != null)
{
return navigationRenamer.NewName;
}
}
}

return baseName;
Expand Down
3 changes: 0 additions & 3 deletions src/GUI/RevEng.Shared/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public Schema()
[DataMember(EmitDefaultValue = false, IsRequired = false)]
public List<TableRenamer> Tables { get; set; }

[DataMember(EmitDefaultValue = false, IsRequired = false)]
public List<NavigationRenamer> Navigations { get; set; }

[DataMember(EmitDefaultValue = false, IsRequired = false)]
public string TableRegexPattern { get; set; }

Expand Down
3 changes: 3 additions & 0 deletions src/GUI/RevEng.Shared/TableRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public class TableRenamer

[DataMember(EmitDefaultValue = false, IsRequired = false)]
public List<ColumnNamer> Columns { get; set; }

[DataMember(EmitDefaultValue = false, IsRequired = false)]
public List<NavigationRenamer> Navigations { get; set; }
}
}
35 changes: 28 additions & 7 deletions src/GUI/UnitTests/ViewModels/ObjectTreeViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ public void AddObjects_Replacers_Issue679()
// Arrange
var vm = new ObjectTreeViewModel(CreateSchemaInformationViewModelMockObject, CreateTableInformationViewModelMockObject, CreateColumnInformationViewModelMockObject);

var objects = new TableModel[2];
var objects = new TableModel[3];
objects[0] = new TableModel("departmentdetail", null, DatabaseType.Mysql, ObjectType.Table, new List<ColumnModel> { new ColumnModel("DEPTCode", false) }.ToArray());
objects[1] = new TableModel("employeedetail", null, DatabaseType.Mysql, ObjectType.Table, new List<ColumnModel> { new ColumnModel("EMPCode", false) }.ToArray());
objects[2] = new TableModel("same", null, DatabaseType.Mysql, ObjectType.Table, new List<ColumnModel> { new ColumnModel("same", false) }.ToArray());

var replacers = new Schema[1];
replacers[0] = new Schema()
Expand All @@ -138,7 +139,7 @@ public void AddObjects_Replacers_Issue679()
Columns = new List<ColumnNamer>
{
new ColumnNamer { Name = "DEPTCode", NewName = "DEPTCode" },
}
},
},
new TableRenamer
{
Expand All @@ -147,19 +148,39 @@ public void AddObjects_Replacers_Issue679()
Columns = new List<ColumnNamer>
{
new ColumnNamer { Name = "EMPCode", NewName = "EMPCode" },
}
}

},
Navigations = new List<NavigationRenamer>
{
new NavigationRenamer { Name = "First", NewName = "Second" },
},
},
new TableRenamer
{
Name = "same",
NewName = "same",
Columns = new List<ColumnNamer>(),
Navigations = new List<NavigationRenamer>
{
new NavigationRenamer { Name = "SameFirst", NewName = "SameSecond" },
},
},
},
};

// Act
vm.AddObjects(objects, replacers);
vm.SetSelectionState(true);
var renamers = vm.GetRenamedObjects();
var renamers = vm.GetRenamedObjects().ToList();

// Assert
Assert.AreEqual(1, renamers.First().Tables[0].Columns.Count);
Assert.AreEqual(1, renamers.First().Tables[1].Columns.Count);
Assert.AreEqual(3, renamers[0].Tables.Count);
Assert.AreEqual(1, renamers[0].Tables[0].Columns.Count);
Assert.AreEqual(0, renamers[0].Tables[0].Navigations.Count);
Assert.AreEqual(1, renamers[0].Tables[1].Columns.Count);
Assert.AreEqual(1, renamers[0].Tables[1].Navigations.Count);
Assert.AreEqual(0, renamers[0].Tables[2].Columns.Count);
Assert.AreEqual(1, renamers[0].Tables[1].Navigations.Count);
}

[Test]
Expand Down
17 changes: 11 additions & 6 deletions src/GUI/efreveng/TestFiles/fulltest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
"ContextNamespace": null,
"CustomReplacers": [
{
"Navigations": [
{
"Name": "ShipVia",
"NewName": "ShipBy"
}
],
"SchemaName": "dbo",
"Tables": [
{
Expand All @@ -21,6 +15,17 @@
],
"Name": "Categories",
"NewName": "Categories"
},
{
"Columns": [ ],
"Navigations": [
{
"Name": "ShipVia",
"NewName": "ShipBy"
}
],
"Name": "Orders",
"NewName": "Orders"
}
],
"UseSchemaName": false
Expand Down
Binary file modified src/GUI/lib/efreveng.exe.zip
Binary file not shown.
Binary file modified src/GUI/lib/efreveng50.exe.zip
Binary file not shown.
18 changes: 12 additions & 6 deletions test/ScaffoldingTester/ScaffoldingTester/efpt.renaming.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
[
{
"Navigations": [
{
"Name": "ShipVia",
"NewName": "ShipBy"
}
],
"SchemaName": "dbo",
"Tables": [
{
Expand All @@ -16,7 +10,19 @@
}
],
"Name": "Categories",
"Navigations": [ ],
"NewName": "Categories"
},
{
"Columns": [ ],
"Name": "Orders",
"Navigations": [
{
"Name": "ShipVia",
"NewName": "ShipBy"
}
],
"NewName": "Orders"
}
],
"UseSchemaName": false
Expand Down