Skip to content

Commit

Permalink
run db migrations only if enabled loic-sharma#96 loic-sharma#101
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzo committed Oct 1, 2018
1 parent 7bc737f commit ba9c3cb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/BaGet.Core/Configuration/DatabaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public class DatabaseOptions
{
public bool RunMigrations { get; set; }
public DatabaseType Type { get; set; }
public string ConnectionString { get; set; }
}
Expand Down
10 changes: 8 additions & 2 deletions src/BaGet/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using BaGet.Configurations;
using BaGet.Core.Configuration;
using BaGet.Core.Entities;
using BaGet.Extensions;
using BaGet.Web.Extensions;
Expand All @@ -8,6 +9,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace BaGet
{
Expand All @@ -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<IOptions<BaGetOptions>>()
.Value
.Database;
if(databaseOptions.RunMigrations) {
var scopeFactory = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>();

using (var scope = scopeFactory.CreateScope())
{
scope.ServiceProvider
Expand Down
1 change: 1 addition & 0 deletions src/BaGet/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"ApiKeyHash": "",

"Database": {
"RunMigrations": true,
"Type": "Sqlite",
"ConnectionString": "Data Source=baget.db"
},
Expand Down

0 comments on commit ba9c3cb

Please sign in to comment.