Fix local db

- correct path to contain application specific folder
- create migration
- apply the migrations at startup
- reactivate commented out code

The code was failing before because the tables were never created.
This commit is contained in:
eelke 2025-10-26 06:27:43 +01:00
parent bee0e0915f
commit 4ff9b78db8
6 changed files with 375 additions and 6 deletions

View file

@ -49,6 +49,6 @@ public partial class App : Application
using var scope = services.CreateScope();
var db = services.GetRequiredService<LocalDb>();
//db.Database.Migrate();
db.Database.Migrate();
}
}

View file

@ -84,9 +84,9 @@ public class EditHistoryManager : IEditHistoryManager
public void SaveToDatabase()
{
// _db.EditHistory.AddRange(_pendingEdits);
// _db.SaveChanges();
// _pendingEdits.Clear();
_db.EditHistory.AddRange(_pendingEdits);
_db.SaveChanges();
_pendingEdits.Clear();
}
public IReadOnlyList<EditHistoryEntry> GetHistory() => _pendingEdits.AsReadOnly();

View file

@ -1,4 +1,5 @@
using System;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
@ -16,8 +17,9 @@ public class LocalDb : DbContext
public LocalDb()
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
path = System.IO.Path.Join(path, "pgLabII");
System.IO.Directory.CreateDirectory(path);
DbPath = System.IO.Path.Join(path, "local.db");
}

View file

@ -0,0 +1,139 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using pgLabII.Infra;
#nullable disable
namespace pgLabII.Migrations
{
[DbContext(typeof(LocalDb))]
[Migration("20251025162617_First")]
partial class First
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
modelBuilder.Entity("pgLabII.Model.Document", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("BaseCopyFilename")
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<DateTime>("LastModified")
.HasColumnType("TEXT");
b.Property<string>("OriginalFilename")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Documents");
});
modelBuilder.Entity("pgLabII.Model.EditHistoryEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("DocumentId")
.HasColumnType("INTEGER");
b.Property<string>("InsertedText")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Offset")
.HasColumnType("INTEGER");
b.Property<string>("RemovedText")
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("DocumentId", "Timestamp");
b.ToTable("EditHistory");
});
modelBuilder.Entity("pgLabII.Model.ServerConfigurationEntity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<int>("ColorArgb")
.HasColumnType("INTEGER");
b.Property<bool>("ColorEnabled")
.HasColumnType("INTEGER");
b.Property<string>("Host")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("InitialDatabase")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.Property<ushort>("Port")
.HasColumnType("INTEGER");
b.Property<int>("SslMode")
.HasColumnType("INTEGER");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ServerConfigurations");
});
modelBuilder.Entity("pgLabII.Model.EditHistoryEntry", b =>
{
b.HasOne("pgLabII.Model.Document", "Document")
.WithMany("EditHistory")
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Document");
});
modelBuilder.Entity("pgLabII.Model.Document", b =>
{
b.Navigation("EditHistory");
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,92 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace pgLabII.Migrations
{
/// <inheritdoc />
public partial class First : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Documents",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
OriginalFilename = table.Column<string>(type: "TEXT", nullable: false),
BaseCopyFilename = table.Column<string>(type: "TEXT", nullable: false),
Created = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModified = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Documents", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ServerConfigurations",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
Host = table.Column<string>(type: "TEXT", nullable: false),
Port = table.Column<ushort>(type: "INTEGER", nullable: false),
InitialDatabase = table.Column<string>(type: "TEXT", nullable: false),
SslMode = table.Column<int>(type: "INTEGER", nullable: false),
ColorEnabled = table.Column<bool>(type: "INTEGER", nullable: false),
ColorArgb = table.Column<int>(type: "INTEGER", nullable: false),
UserName = table.Column<string>(type: "TEXT", nullable: false),
Password = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ServerConfigurations", x => x.Id);
});
migrationBuilder.CreateTable(
name: "EditHistory",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
DocumentId = table.Column<int>(type: "INTEGER", nullable: false),
Timestamp = table.Column<DateTime>(type: "TEXT", nullable: false),
Offset = table.Column<int>(type: "INTEGER", nullable: false),
InsertedText = table.Column<string>(type: "TEXT", nullable: false),
RemovedText = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_EditHistory", x => x.Id);
table.ForeignKey(
name: "FK_EditHistory_Documents_DocumentId",
column: x => x.DocumentId,
principalTable: "Documents",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_EditHistory_DocumentId_Timestamp",
table: "EditHistory",
columns: new[] { "DocumentId", "Timestamp" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "EditHistory");
migrationBuilder.DropTable(
name: "ServerConfigurations");
migrationBuilder.DropTable(
name: "Documents");
}
}
}

View file

@ -0,0 +1,136 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using pgLabII.Infra;
#nullable disable
namespace pgLabII.Migrations
{
[DbContext(typeof(LocalDb))]
partial class LocalDbModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
modelBuilder.Entity("pgLabII.Model.Document", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("BaseCopyFilename")
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<DateTime>("LastModified")
.HasColumnType("TEXT");
b.Property<string>("OriginalFilename")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Documents");
});
modelBuilder.Entity("pgLabII.Model.EditHistoryEntry", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("DocumentId")
.HasColumnType("INTEGER");
b.Property<string>("InsertedText")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("Offset")
.HasColumnType("INTEGER");
b.Property<string>("RemovedText")
.IsRequired()
.HasColumnType("TEXT");
b.Property<DateTime>("Timestamp")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("DocumentId", "Timestamp");
b.ToTable("EditHistory");
});
modelBuilder.Entity("pgLabII.Model.ServerConfigurationEntity", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<int>("ColorArgb")
.HasColumnType("INTEGER");
b.Property<bool>("ColorEnabled")
.HasColumnType("INTEGER");
b.Property<string>("Host")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("InitialDatabase")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT");
b.Property<ushort>("Port")
.HasColumnType("INTEGER");
b.Property<int>("SslMode")
.HasColumnType("INTEGER");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ServerConfigurations");
});
modelBuilder.Entity("pgLabII.Model.EditHistoryEntry", b =>
{
b.HasOne("pgLabII.Model.Document", "Document")
.WithMany("EditHistory")
.HasForeignKey("DocumentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Document");
});
modelBuilder.Entity("pgLabII.Model.Document", b =>
{
b.Navigation("EditHistory");
});
#pragma warning restore 612, 618
}
}
}