Add button to recreate db
This commit is contained in:
parent
87ee56de65
commit
35cbf57f41
@ -714,6 +714,9 @@
|
||||
<input type="checkbox" id="eraseModeCacheCheckbox" style="margin-left: 10px" />
|
||||
<label for="eraseModeCacheCheckbox" style="margin-left: 5px">Erase global cached fingerprint files</label>
|
||||
</div>
|
||||
<div>
|
||||
<button is="emby-button" class="button-submit emby-button" id="btnRebuildDatabase">Rebuild database</button>
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
<br />
|
||||
@ -758,6 +761,7 @@
|
||||
var btnEraseRecapTimestamps = document.querySelector("button#btnEraseRecapTimestamps");
|
||||
var btnEraseCreditTimestamps = document.querySelector("button#btnEraseCreditTimestamps");
|
||||
var btnErasePreviewTimestamps = document.querySelector("button#btnErasePreviewTimestamps");
|
||||
var btnRebuildDatabase = document.querySelector("button#btnRebuildDatabase");
|
||||
|
||||
// all plugin configuration fields that can be get or set with .value (i.e. strings or numbers).
|
||||
var configurationFields = [
|
||||
@ -1504,6 +1508,9 @@
|
||||
eraseTimestamps("Preview");
|
||||
e.preventDefault();
|
||||
});
|
||||
btnRebuildDatabase.addEventListener("click", () => {
|
||||
fetchWithAuth("Intros/RebuildDatabase", "POST", null);
|
||||
});
|
||||
btnSeasonEraseTimestamps.addEventListener("click", () => {
|
||||
Dashboard.confirm("Are you sure you want to erase all timestamps for this season?", "Confirm timestamp erasure", (result) => {
|
||||
if (!result) {
|
||||
|
@ -251,6 +251,20 @@ public class SkipIntroController(MediaSegmentUpdateManager mediaSegmentUpdateMan
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rebuilds the database.
|
||||
/// </summary>
|
||||
/// <response code="204">Database rebuilt.</response>
|
||||
/// <returns>No content.</returns>
|
||||
[Authorize(Policy = Policies.RequiresElevation)]
|
||||
[HttpPost("Intros/RebuildDatabase")]
|
||||
public ActionResult RebuildDatabase()
|
||||
{
|
||||
using var db = new IntroSkipperDbContext(Plugin.Instance!.DbPath);
|
||||
db.RebuildDatabase();
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the user interface configuration.
|
||||
/// </summary>
|
||||
|
@ -109,22 +109,52 @@ public class IntroSkipperDbContext : DbContext
|
||||
/// </summary>
|
||||
public void ApplyMigrations()
|
||||
{
|
||||
// If migrations table exists, just apply pending migrations normally
|
||||
if (Database.GetAppliedMigrations().Any() || !Database.CanConnect())
|
||||
// If database doesn't exist or can't connect, create it with migrations
|
||||
if (!Database.CanConnect())
|
||||
{
|
||||
Database.Migrate();
|
||||
return;
|
||||
}
|
||||
|
||||
// If migrations table exists, apply pending migrations normally
|
||||
if (Database.GetAppliedMigrations().Any())
|
||||
{
|
||||
Database.Migrate();
|
||||
return;
|
||||
}
|
||||
|
||||
// For databases without migration history
|
||||
try
|
||||
RebuildDatabase();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rebuilds the database while preserving valid segments and season information.
|
||||
/// </summary>
|
||||
public void RebuildDatabase()
|
||||
{
|
||||
// Backup existing data
|
||||
List<DbSegment> segments;
|
||||
List<DbSegment> segments = [];
|
||||
List<DbSeasonInfo> seasonInfos = [];
|
||||
using (var db = new IntroSkipperDbContext(_dbPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
segments = [.. db.DbSegment.AsEnumerable().Where(s => s.ToSegment().Valid)];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to read DbSegment data", ex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
seasonInfos = [.. db.DbSeasonInfo];
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to read DbSeasonInfo data", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete old database
|
||||
Database.EnsureDeleted();
|
||||
@ -134,14 +164,18 @@ public class IntroSkipperDbContext : DbContext
|
||||
|
||||
// Restore the data
|
||||
using (var db = new IntroSkipperDbContext(_dbPath))
|
||||
{
|
||||
if (segments.Count > 0)
|
||||
{
|
||||
db.DbSegment.AddRange(segments);
|
||||
}
|
||||
|
||||
if (seasonInfos.Count > 0)
|
||||
{
|
||||
db.DbSeasonInfo.AddRange(seasonInfos);
|
||||
}
|
||||
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to apply migrations", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user