YodasMyDad / ZauberCMS

The Blazor CMS, built by .Net Developers for .Net Developers
MIT License
53 stars 5 forks source link

Migrations Help #6

Closed YodasMyDad closed 3 months ago

YodasMyDad commented 3 months ago

Currently setup with SQLite migrations and these are auto applied at start up.

Investigate how to give the user an option to install SQLite or MS SQL. Do we create two lots of migrations? How would the app know which one and when to install those specific migrations?

ahwm commented 3 months ago

Definitely keeping an eye on this project. Looks very cool!

Maybe just need some more context, but my basic understanding of the codebase looks like this is already handled between SQLite and MS SQL?

I have always just done something like this and it's worked just fine (also with SQLite):

            using var serviceScope = app.Services.GetRequiredService<IServiceScopeFactory>().CreateScope();
            using var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>()!;
            context.Database.Migrate();

Are there specific scenarios I could help with testing?

YodasMyDad commented 3 months ago

Yes that's similar to how they are setup now. The issue is you have to generate the migrations based on the connection string/db. So as it's using SQLite it will generate SQLite specific migrations. I can change the connection string and tell it to use MS SQL like it shows in the docs, and generate those in a different folder

https://aptitude.gitbook.io/zaubercms/getting-started/entity-framework

However, the issue is, you will then end up with two sets of migrations. And calling .Migrate() will try and run both. Need a way to only run the SQLite migrations if SQLite is the db. Or only run the MS SQL ones if MS SQL is set in the appSettings.

ahwm commented 3 months ago

That makes sense. I'm guessing that how Umbraco gets around this is by using the third party ORM (NPoco). I'll see if I can mess with it a bit.

YodasMyDad commented 3 months ago

I think I have an idea for a solution, but it's going to take a bit of reworking and thinking about, thinking something like this

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        var dbProvider = GetProviderMethod();

        if (dbProvider == "Sqlite")
        {
            modelBuilder.HasDefaultSchema("sqlite");
            modelBuilder.ApplyConfigurationsFromAssembly(???);
        }
        else if (dbProvider == "SqlServer")
        {
            modelBuilder.HasDefaultSchema("mssql");
            modelBuilder.ApplyConfigurationsFromAssembly(???);
        }

        // Apply other configurations
        modelBuilder.Entity<User>().ToTable("ZauberUsers");
       .. etc...
    }