|
1 | 1 | using System.Diagnostics.CodeAnalysis; |
2 | 2 | using System.Runtime.CompilerServices; |
3 | 3 | using Refresh.Common.Time; |
| 4 | +using Refresh.Database.Configuration; |
4 | 5 | using Refresh.Database.Models.Authentication; |
5 | 6 | using Refresh.Database.Models.Activity; |
6 | 7 | using Refresh.Database.Models.Assets; |
@@ -35,6 +36,7 @@ public partial class GameDatabaseContext : |
35 | 36 | private static readonly object IdLock = new(); |
36 | 37 |
|
37 | 38 | private readonly IDateTimeProvider _time; |
| 39 | + private readonly IDatabaseConfig _dbConfig; |
38 | 40 |
|
39 | 41 | #if !POSTGRES |
40 | 42 | private RealmDbSet<GameUser> GameUsers => new(this._realm); |
@@ -119,28 +121,29 @@ public partial class GameDatabaseContext : |
119 | 121 | internal DbSet<GameSkillReward> GameSkillRewards { get; set; } |
120 | 122 | #endif |
121 | 123 |
|
122 | | - internal GameDatabaseContext(IDateTimeProvider time) |
| 124 | + internal GameDatabaseContext(IDateTimeProvider time, IDatabaseConfig dbConfig) |
123 | 125 | { |
124 | 126 | this._time = time; |
| 127 | + this._dbConfig = dbConfig; |
125 | 128 | } |
126 | 129 |
|
127 | 130 | #if POSTGRES |
128 | 131 | [Obsolete("For use by the `dotnet ef` tool only.", true)] |
129 | | - public GameDatabaseContext() : this(new SystemDateTimeProvider()) |
| 132 | + public GameDatabaseContext() : this(new SystemDateTimeProvider(), new EmptyDatabaseConfig()) |
130 | 133 | {} |
131 | 134 |
|
132 | 135 | protected override void OnConfiguring(DbContextOptionsBuilder options) |
133 | 136 | { |
134 | 137 | base.OnConfiguring(options); |
135 | | - NpgsqlConnectionStringBuilder builder = new() |
| 138 | + string connectionString = this._dbConfig.ConnectionString; |
| 139 | + if (this._dbConfig.PreferConnectionStringEnvironmentVariable) |
136 | 140 | { |
137 | | - Database = "refresh", |
138 | | - Username = "refresh", |
139 | | - Password = "refresh", |
140 | | - Host = "localhost", |
141 | | - Port = 5432, |
142 | | - }; |
143 | | - options.UseNpgsql(builder.ToString()); |
| 141 | + string? envVarString = Environment.GetEnvironmentVariable("POSTGRES_CONNECTION_STRING"); |
| 142 | + if (envVarString != null) |
| 143 | + connectionString = envVarString; |
| 144 | + } |
| 145 | + |
| 146 | + options.UseNpgsql(connectionString); |
144 | 147 | } |
145 | 148 |
|
146 | 149 | protected override void ConfigureConventions(ModelConfigurationBuilder config) |
|
0 commit comments