Skip to content

Commit

Permalink
Replace ElapsedTime column with EndTime + Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
deitry committed Jul 8, 2023
1 parent cd1e494 commit 8eb9696
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using SQLite;

namespace TimeTracker.Database.Migrations;

public class M005_AddTimestampAndReworkElapsed : IDbMigration
{
public async Task Do(SQLiteAsyncConnection db)
{
var timestamp = DateTime.Now;

await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Timestamp datetime");
await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD EndTime datetime");

var all = await db.Table<TrackedTimeDb_M005>().ToListAsync();
foreach (var trackedTimeDb in all)
{
trackedTimeDb.Timestamp = timestamp;
#pragma warning disable CS0618
trackedTimeDb.EndTime = trackedTimeDb.StartTime + trackedTimeDb.ElapsedTime;
#pragma warning restore CS0618
await db.UpdateAsync(trackedTimeDb);
}

// NOTE: sqlite does not support DROP COLUMN
// await db.ExecuteAsync("ALTER TABLE TrackedTimeDb DROP COLUMN ElapsedTime");

const string TemporaryTable = "t1_backup";
await db.ExecuteAsync(
$"CREATE TABLE {TemporaryTable} AS SELECT Id, Uuid, Name, Status, StartTime, EndTime, Timestamp FROM TrackedTimeDb");
await db.ExecuteAsync("DROP TABLE TrackedTimeDb");
await db.ExecuteAsync($"ALTER TABLE {TemporaryTable} RENAME TO TrackedTimeDb");
}

public Task UnDo(SQLiteAsyncConnection db)
{
throw new NotImplementedException();
}

public string Serialize()
{
throw new NotImplementedException();
}
}

/// <summary>
/// Data model at the time of migration <see cref="M005_AddTimestampAndReworkElapsed"/>
/// </summary>
[Table(nameof(TrackedTimeDb))]
public class TrackedTimeDb_M005 : ITable
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

public Guid Uuid { get; set; }

public string Name { get; set; }

public DateTime StartTime { get; set; }

public TimeSpan ElapsedTime { get; init; }
public DateTime EndTime { get; set; }

public int Status { get; set; }

/// <summary>
/// Last modification time - considering Start and Elapsed may ba changed later
/// </summary>
public DateTime Timestamp { get; set; }

public enum TrackingStatus
{
Completed = 0,

/// <summary>
/// Currently running
/// </summary>
Running = 1,
}
}
51 changes: 39 additions & 12 deletions TimeTracker.Core/Database/Migrations/M4_AddUuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ namespace TimeTracker.Database.Migrations;

public class M4_AddUuid : IDbMigration
{
public Task Do(SQLiteAsyncConnection db)
public async Task Do(SQLiteAsyncConnection db)
{
// iterate over TrackedTimeDb and add Guid.NewGuid() as parameter
return db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Uuid text")
.ContinueWith(async _ =>
{
var all = await db.Table<TrackedTimeDb>().ToListAsync();
foreach (var trackedTimeDb in all)
{
trackedTimeDb.Uuid = Guid.NewGuid();
await db.UpdateAsync(trackedTimeDb);
}
});
await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Uuid text");

var all = await db.Table<TrackedTimeDb_M004>().ToListAsync();
foreach (var trackedTimeDb in all)
{
trackedTimeDb.Uuid = Guid.NewGuid();
await db.UpdateAsync(trackedTimeDb);
}
}

public Task UnDo(SQLiteAsyncConnection db)
Expand All @@ -29,3 +26,33 @@ public string Serialize()
throw new NotImplementedException();
}
}

/// <summary>
/// Data model at the time of migration <see cref="M4_AddUuid"/>
/// </summary>
[Table(nameof(TrackedTimeDb))]
public class TrackedTimeDb_M004 : ITable
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }

public Guid Uuid { get; set; }

public string Name { get; set; }

public DateTime StartTime { get; set; }

public TimeSpan ElapsedTime { get; init; }

public int Status { get; set; }

public enum TrackingStatus
{
Completed = 0,

/// <summary>
/// Currently running
/// </summary>
Running = 1,
}
}
11 changes: 10 additions & 1 deletion TimeTracker.Core/Database/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Migrator
new M2_AddCategories(),
new M3_AddStatuses(),
new M4_AddUuid(),
new M005_AddTimestampAndReworkElapsed(),
};

public static async Task Migrate(SQLiteAsyncConnection db)
Expand All @@ -37,7 +38,15 @@ public static async Task Migrate(SQLiteAsyncConnection db)
{
var migration = Migrations[i];

await migration.Do(db);
try
{
await migration.Do(db);
}
catch (Exception e)
{
Debug.WriteLine($"Migration {migration.GetType().Name} failed: {e}");
throw;
}
await db.UpdateAsync(new ControlDb(ControlDb.ParamId.Version, i + 1));
}
}
Expand Down
9 changes: 8 additions & 1 deletion TimeTracker.Core/Database/Tables/TrackedTimeDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public class TrackedTimeDb : ITable

public DateTime StartTime { get; set; }

public TimeSpan ElapsedTime { get; init; }
public DateTime EndTime { get; set; }

[Ignore] public TimeSpan ElapsedTime => EndTime - StartTime;

public int Status { get; set; }

/// <summary>
/// Last modification time - considering Start and Elapsed may ba changed later
/// </summary>
public DateTime Timestamp { get; set; }

[Ignore]
public TrackingStatus StatusEnum
{
Expand Down
4 changes: 2 additions & 2 deletions TimeTracker.Core/Models/TimeTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public TimeTracker(TrackedTimeDb tracker)
StartTime = tracker.StartTime;
IsRunning = tracker.StatusEnum == TrackedTimeDb.TrackingStatus.Running;
if (IsRunning == false)
EndTime = tracker.StartTime + tracker.ElapsedTime;
EndTime = tracker.EndTime;
}

public TimeTracker Start()
Expand All @@ -56,7 +56,7 @@ public TrackedTimeDb ToDb()
Uuid = new Guid(),
Name = Name,
StartTime = StartTime,
ElapsedTime = ElapsedTime,
EndTime = EndTime,
StatusEnum = IsRunning
? TrackedTimeDb.TrackingStatus.Running
: TrackedTimeDb.TrackingStatus.Completed,
Expand Down
1 change: 1 addition & 0 deletions TimeTracker.Core/TimeTracker.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>TimeTracker</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 8eb9696

Please sign in to comment.