From d04fc2a4e7ddabf3a2a5a65b40eba5c6a7c33384 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 22 Jan 2018 09:23:45 -0600 Subject: [PATCH] remove unused method, move seed extensions --- src/FilterLists.Api/Startup.cs | 1 + .../Extensions/SeedFilterListsDbContext.cs} | 20 +++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-) rename src/FilterLists.Data/{FilterListsDbContextExtensions.cs => Seed/Extensions/SeedFilterListsDbContext.cs} (85%) diff --git a/src/FilterLists.Api/Startup.cs b/src/FilterLists.Api/Startup.cs index 36adb79e5..fa2e5165e 100644 --- a/src/FilterLists.Api/Startup.cs +++ b/src/FilterLists.Api/Startup.cs @@ -1,5 +1,6 @@ using FilterLists.Api.DependencyInjection.Extensions; using FilterLists.Data; +using FilterLists.Data.Seed.Extensions; using FilterLists.Services.DependencyInjection.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HttpOverrides; diff --git a/src/FilterLists.Data/FilterListsDbContextExtensions.cs b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs similarity index 85% rename from src/FilterLists.Data/FilterListsDbContextExtensions.cs rename to src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs index 8b6bbc06e..1ffd67b49 100644 --- a/src/FilterLists.Data/FilterListsDbContextExtensions.cs +++ b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs @@ -4,23 +4,13 @@ using System.Linq; using FilterLists.Data.Entities; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; using Newtonsoft.Json; -namespace FilterLists.Data +namespace FilterLists.Data.Seed.Extensions { - public static class FilterListsDbContextExtensions + public static class SeedFilterListsDbContext { - public static bool AllMigrationsApplied(this FilterListsDbContext dbContext) - { - var appliedMigrationIds = dbContext.GetService().GetAppliedMigrations() - .Select(m => m.MigrationId); - var allMigrationKeys = dbContext.GetService().Migrations.Select(m => m.Key); - return !allMigrationKeys.Except(appliedMigrationIds).Any(); - } - //TODO: consider handling deleted entities on seed //TODO: read entities from model and iterate over public static void SeedOrUpdate(this FilterListsDbContext dbContext, string dataPath) @@ -46,7 +36,7 @@ private static void InsertOnDuplicateKeyUpdate(this DbContext dbCon var properties = GetPropertiesLessValueGeneratedTimestamps(entityType); var values = CreateValues(properties, dataPath); if (values == "") return; - var columns = string.Join(", ", properties.Select(x => x.Name)); + var columns = string.Join(", ", Enumerable.Select(properties, x => x.Name)); var updates = CreateUpdates(properties); var rawSqlString = "INSERT INTO " + entityType.Relational().TableName + " (" + columns + ") VALUES " + values + " ON DUPLICATE KEY UPDATE " + updates; @@ -62,7 +52,7 @@ private static List GetPropertiesLessValueGeneratedTimestamps(IEntity private static string CreateValues(IReadOnlyCollection properties, string dataPath) { - return GetSeedRows(dataPath).Select(row => CreateRowValues(properties, row)).Aggregate("", + return Enumerable.Select(GetSeedRows(dataPath), row => CreateRowValues(properties, row)).Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues); } @@ -84,7 +74,7 @@ private static string CreateRowValues(IEnumerable proper { return (from property in properties let value = row.GetType().GetProperty(property.Name).GetValue(row) - select FormatDataForMySql(property, value)).Aggregate("", + select FormatDataForMySql(property, value)).Aggregate("", (rowValues, value) => rowValues == "" ? "(" + value : rowValues + ", " + value) + ")"; }