Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="EFCore.NamingConventions" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="9.0.6" />
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.8" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.8" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="9.0.8" />
<PackageReference Include="Npgsql" Version="9.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SS14.Labeller\SS14.Labeller.csproj" />
</ItemGroup>

</Project>
79 changes: 79 additions & 0 deletions Labeller.SQLiteToPostgres.DataMigrator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Dapper;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SS14.Labeller.Database;
using SS14.Labeller.Database.Entities;

IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var pgConnectionString = configuration.GetConnectionString("Postgres")
?? throw new InvalidOperationException(
"Failed to find 'Default' connection string "
+ "from application configuration for database initialization."
);

var sqliteConnectionString = configuration.GetConnectionString("Sqlite")
?? throw new InvalidOperationException(
"Failed to find 'Default' connection string "
+ "from application configuration for database initialization."
);

var serviceCollection = new ServiceCollection();

serviceCollection.AddPooledDbContextFactory<CustomDbContext>(
optsBuilder => optsBuilder.UseNpgsql(pgConnectionString)
.UseSnakeCaseNamingConvention()
);

serviceCollection.AddSingleton<IContextConfiguration, DiscourseEntitiesContextConfiguration>();

var sp = serviceCollection.BuildServiceProvider();

await using var sqliteConnections = new SqliteConnection(sqliteConnectionString);
sqliteConnections.Open();

var existingRecords = await sqliteConnections.QueryAsync<DiscourseTopicDbModel>(
"""
SELECT
DiscussionId
,RepoOwner
,RepoName
,IssueNumber
,TopicId
FROM Discussions
"""
);

var contextFactory = sp.GetRequiredService<IDbContextFactory<CustomDbContext>>();
using var context = contextFactory.CreateDbContext();
context.Database.Migrate();

var mapped = existingRecords.Select(x => new DiscourseTopicEntity
{
Id = x.DiscussionId,
RepoOwner = x.RepoOwner,
RepoName = x.RepoName,
IssueNumber = x.IssueNumber,
TopicId = x.TopicId,

}).ToArray();
await context.Set<DiscourseTopicEntity>()
.AddRangeAsync(mapped);

await context.SaveChangesAsync();

public record DiscourseTopicDbModel
{
public int DiscussionId {get;set;}
public string RepoOwner {get;set;}
public string RepoName {get;set;}
public int IssueNumber {get;set;}
public int TopicId { get; set; }
}
6 changes: 6 additions & 0 deletions Labeller.SQLiteToPostgres.DataMigrator/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ConnectionStrings": {
"Postgres": "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=labeller;",
"Sqlite": "Data Source=data/Application.db"
}
}
6 changes: 6 additions & 0 deletions SS14.Labeller.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SS14.Labeller.Tests", "SS14.Labeller.Tests\SS14.Labeller.Tests.csproj", "{E6AD98D5-0330-4056-9042-B8DB3F569B38}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Labeller.SQLiteToPostgres.DataMigrator", "Labeller.SQLiteToPostgres.DataMigrator\Labeller.SQLiteToPostgres.DataMigrator.csproj", "{9A4858F0-2F41-44DF-BFA1-1C1131F8F0AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,6 +31,10 @@ Global
{E6AD98D5-0330-4056-9042-B8DB3F569B38}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6AD98D5-0330-4056-9042-B8DB3F569B38}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6AD98D5-0330-4056-9042-B8DB3F569B38}.Release|Any CPU.Build.0 = Release|Any CPU
{9A4858F0-2F41-44DF-BFA1-1C1131F8F0AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A4858F0-2F41-44DF-BFA1-1C1131F8F0AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A4858F0-2F41-44DF-BFA1-1C1131F8F0AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A4858F0-2F41-44DF-BFA1-1C1131F8F0AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down