Skip to content

Commit

Permalink
feat(auto migrations): Add Migrations & IConfig (#16)
Browse files Browse the repository at this point in the history
* adding migration project

* add connection string

* edited environment variables

* fix env

* Fix ENV!

---------

Co-authored-by: sadq <[email protected]>
  • Loading branch information
msmahdinejad and SwimmingRieux authored Aug 19, 2024
1 parent b3dc3fe commit 2a0e1dc
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions RelationAnalysis.Migrations/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONNECTION_STRING=""
38 changes: 38 additions & 0 deletions RelationAnalysis.Migrations/ConsoleStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using DotNetEnv;
using DotNetEnv.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using RelationshipAnalysis.Context;
using Microsoft.Extensions.Configuration;

namespace RelationAnalysis.Migrations
{
public class ConsoleStartup
{
public IConfiguration Configuration { get; } = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
public ConsoleStartup()
{

//.. for test
Console.WriteLine(Configuration["CONNECTION_STRING"]);
}

public void ConfigureServices(IServiceCollection services)
{

services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseNpgsql(Configuration["CONNECTION_STRING"]).UseLazyLoadingProxies();
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

}
}
}
25 changes: 25 additions & 0 deletions RelationAnalysis.Migrations/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using DotNetEnv;
using RelationshipAnalysis.Context;

namespace RelationAnalysis.Migrations
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Applying migrations");
var webHost = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<ConsoleStartup>()
.Build();

using (var context = (ApplicationDbContext) webHost.Services.GetService(typeof(ApplicationDbContext)))
{
context.Database.Migrate();
}
Console.WriteLine("Done");
}
}
}
28 changes: 28 additions & 0 deletions RelationAnalysis.Migrations/RelationAnalysis.Migrations.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dotenv" Version="0.0.1.1" />
<PackageReference Include="DotNetEnv" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Hosting">
<HintPath>..\..\..\..\..\..\usr\share\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.AspNetCore.Hosting.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Extensions.Configuration">
<HintPath>..\..\..\..\..\..\usr\share\dotnet\shared\Microsoft.AspNetCore.App\8.0.8\Microsoft.Extensions.Configuration.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RelationshipAnalysis\RelationshipAnalysis.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions RelationshipAnalysis.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RelationshipAnalysis.Test",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RelationshipAnalysis.Integration.Test", "RelationshipAnalysis.Integration.Test\RelationshipAnalysis.Integration.Test.csproj", "{50D5723D-08CE-405C-B989-219F2992B181}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RelationAnalysis.Migrations", "RelationAnalysis.Migrations\RelationAnalysis.Migrations.csproj", "{E1B31A8D-C63B-4C1A-9C6A-5E909545B40D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,5 +26,9 @@ Global
{50D5723D-08CE-405C-B989-219F2992B181}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50D5723D-08CE-405C-B989-219F2992B181}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50D5723D-08CE-405C-B989-219F2992B181}.Release|Any CPU.Build.0 = Release|Any CPU
{E1B31A8D-C63B-4C1A-9C6A-5E909545B40D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E1B31A8D-C63B-4C1A-9C6A-5E909545B40D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E1B31A8D-C63B-4C1A-9C6A-5E909545B40D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E1B31A8D-C63B-4C1A-9C6A-5E909545B40D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace RelationshipAnalysis.Migrations
{
/// <inheritdoc />
public partial class GraphmodelsUpdate : Migration
public partial class initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
Expand Down

0 comments on commit 2a0e1dc

Please sign in to comment.