From ba9c3cb9f4187da656c89ec5c4778be24b72b329 Mon Sep 17 00:00:00 2001 From: Tomasz Setkowski Date: Mon, 1 Oct 2018 11:57:01 +0000 Subject: [PATCH] run db migrations only if enabled #96 #101 --- Dockerfile | 3 ++- src/BaGet.Core/Configuration/DatabaseOptions.cs | 1 + src/BaGet/Startup.cs | 10 ++++++++-- src/BaGet/appsettings.json | 1 + 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index c6ea7ff95..ec4fa4f11 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,10 +20,11 @@ RUN mkdir -p /home/baget /home/baget/.nuget/NuGet &&\ useradd -d /home/baget -s /bin/bash -u 1000 -g baget baget &&\ chown -R baget:baget /home/baget /var/baget/ -ENV ASPNETCORE_ENVIRONMENT=Development \ +ENV ASPNETCORE_ENVIRONMENT=Production \ ApiKeyHash=658489D79E218D2474D049E8729198D86DB0A4AF43981686A31C7DCB02DC0900 \ Storage__Type=FileSystem \ Storage__Path=/var/baget/packages \ + Database__RunMigrations=true \ Database__Type=Sqlite \ Database__ConnectionString="Data Source=/var/baget/db/sqlite.db" \ Search__Type=Database diff --git a/src/BaGet.Core/Configuration/DatabaseOptions.cs b/src/BaGet.Core/Configuration/DatabaseOptions.cs index 570426c21..cc8be9111 100644 --- a/src/BaGet.Core/Configuration/DatabaseOptions.cs +++ b/src/BaGet.Core/Configuration/DatabaseOptions.cs @@ -2,6 +2,7 @@ { public class DatabaseOptions { + public bool RunMigrations { get; set; } public DatabaseType Type { get; set; } public string ConnectionString { get; set; } } diff --git a/src/BaGet/Startup.cs b/src/BaGet/Startup.cs index 047501717..24eb143e8 100644 --- a/src/BaGet/Startup.cs +++ b/src/BaGet/Startup.cs @@ -1,5 +1,6 @@ using System; using BaGet.Configurations; +using BaGet.Core.Configuration; using BaGet.Core.Entities; using BaGet.Extensions; using BaGet.Web.Extensions; @@ -8,6 +9,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; namespace BaGet { @@ -32,11 +34,15 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseDeveloperExceptionPage(); app.UseStatusCodePages(); + } - // Run migrations automatically in development mode. + // Run migrations if enabled + var databaseOptions = app.ApplicationServices.GetRequiredService>() + .Value + .Database; + if(databaseOptions.RunMigrations) { var scopeFactory = app.ApplicationServices .GetRequiredService(); - using (var scope = scopeFactory.CreateScope()) { scope.ServiceProvider diff --git a/src/BaGet/appsettings.json b/src/BaGet/appsettings.json index 8378b5000..396ac1341 100644 --- a/src/BaGet/appsettings.json +++ b/src/BaGet/appsettings.json @@ -2,6 +2,7 @@ "ApiKeyHash": "", "Database": { + "RunMigrations": true, "Type": "Sqlite", "ConnectionString": "Data Source=baget.db" },