Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix database migration for selfhosted instances #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ services:
- MARIADB_DATABASE=librum
- MARIADB_ROOT_PASSWORD=mariadb
restart: unless-stopped
ports:
- 3306:3306
healthcheck: # Ensures the DB is up before the server.
test: ["CMD", "mariadb-admin", "ping", "-u", "librum", "-p'mariadb'", "-h", "localhost"]
interval: 20s
Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure/Persistence/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Domain.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Infrastructure.Persistence;

Expand Down Expand Up @@ -33,6 +34,13 @@ protected override void OnModelCreating(ModelBuilder builder)
.WithOne(pf => pf.Product)
.HasForeignKey(pf => pf.ProductId)
.IsRequired();

builder
.Entity<Folder>()
.Property(f => f.Description)
.HasColumnType(this.Database.ProviderName == "Pomelo.EntityFrameworkCore.MySql"
? "longtext"
: "nvarchar(max)");

base.OnModelCreating(builder);
}
Expand Down
34 changes: 33 additions & 1 deletion src/Presentation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Application.Common.Middleware;
using Application.Interfaces.Repositories;
using Stripe;
using Azure.Identity;
using Domain.Entities;
using Infrastructure.Persistence;
using Microsoft.AspNetCore.Identity;
using Presentation;
using Product = Domain.Entities.Product;


var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -65,6 +67,11 @@
await roleManager.CreateAsync(new IdentityRole(role));
}

if (builder.Configuration["LIBRUM_SELFHOSTED"] == "true")
{
await SeedWithSelfHostedProduct(services);
}

await SeedWithAdminUser(services);
}

Expand Down Expand Up @@ -93,6 +100,8 @@ async Task SeedWithAdminUser(IServiceProvider services)
string email = config["AdminEmail"];
string password = config["AdminPassword"];

var isSelfHosted = config["LIBRUM_SELFHOSTED"] == "true";

var userManager = services.GetRequiredService<UserManager<User>>();
if (await userManager.FindByEmailAsync(email) == null)
{
Expand All @@ -101,11 +110,34 @@ async Task SeedWithAdminUser(IServiceProvider services)
Name = name,
Email = email,
UserName = email,
AccountCreation = DateTime.UtcNow
AccountCreation = DateTime.UtcNow,
ProductId = isSelfHosted ? "1" : null,
};

await userManager.CreateAsync(user, password);
var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
await userManager.ConfirmEmailAsync(user, token);
}
}

async Task SeedWithSelfHostedProduct(IServiceProvider services)
{
var product = new Product
{
ProductId = "1",
Name = "SelfHosted",
Description = "Dummy self hosted product",
Price = 0,
PriceId = "0",
BookStorageLimit = long.MaxValue,
AiRequestLimit = int.MaxValue,
LiveMode = true
};

var productRepo = services.GetRequiredService<IProductRepository>();
var existingProduct = await productRepo.GetByIdAsync(product.ProductId);
if (existingProduct == default)
{
productRepo.CreateProduct(product);
}
}