Skip to content

Commit 2fef339

Browse files
committed
Replace ElapsedTime column with EndTime + Timestamp
1 parent cd1e494 commit 2fef339

File tree

6 files changed

+139
-16
lines changed

6 files changed

+139
-16
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using SQLite;
2+
3+
namespace TimeTracker.Database.Migrations;
4+
5+
public class M005_AddTimestampAndReworkElapsed : IDbMigration
6+
{
7+
public async Task Do(SQLiteAsyncConnection db)
8+
{
9+
var timestamp = DateTime.Now;
10+
11+
// await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Timestamp datetime");
12+
// await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD EndTime datetime");
13+
//
14+
// var all = await db.Table<TrackedTimeDb_M005>().ToListAsync();
15+
// foreach (var trackedTimeDb in all)
16+
// {
17+
// trackedTimeDb.Timestamp = timestamp;
18+
// #pragma warning disable CS0618
19+
// trackedTimeDb.EndTime = trackedTimeDb.StartTime + trackedTimeDb.ElapsedTime;
20+
// #pragma warning restore CS0618
21+
// await db.UpdateAsync(trackedTimeDb);
22+
// }
23+
24+
// NOTE: sqlite does not support DROP COLUMN
25+
// await db.ExecuteAsync("ALTER TABLE TrackedTimeDb DROP COLUMN ElapsedTime");
26+
27+
const string TemporaryTable = "t1_backup";
28+
await db.ExecuteAsync(
29+
$"CREATE TABLE {TemporaryTable} AS SELECT Id, Uuid, Name, Status, StartTime, EndTime, Timestamp FROM TrackedTimeDb");
30+
await db.ExecuteAsync("DROP TABLE TrackedTimeDb");
31+
await db.ExecuteAsync($"ALTER TABLE {TemporaryTable} RENAME TO TrackedTimeDb");
32+
}
33+
34+
public Task UnDo(SQLiteAsyncConnection db)
35+
{
36+
throw new NotImplementedException();
37+
}
38+
39+
public string Serialize()
40+
{
41+
throw new NotImplementedException();
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Data model at the time of migration <see cref="M005_AddTimestampAndReworkElapsed"/>
47+
/// </summary>
48+
[Table(nameof(TrackedTimeDb))]
49+
public class TrackedTimeDb_M005 : ITable
50+
{
51+
[PrimaryKey, AutoIncrement]
52+
public int Id { get; set; }
53+
54+
public Guid Uuid { get; set; }
55+
56+
public string Name { get; set; }
57+
58+
public DateTime StartTime { get; set; }
59+
60+
public TimeSpan ElapsedTime { get; init; }
61+
public DateTime EndTime { get; set; }
62+
63+
public int Status { get; set; }
64+
65+
/// <summary>
66+
/// Last modification time - considering Start and Elapsed may ba changed later
67+
/// </summary>
68+
public DateTime Timestamp { get; set; }
69+
70+
public enum TrackingStatus
71+
{
72+
Completed = 0,
73+
74+
/// <summary>
75+
/// Currently running
76+
/// </summary>
77+
Running = 1,
78+
}
79+
}

TimeTracker.Core/Database/Migrations/M4_AddUuid.cs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ namespace TimeTracker.Database.Migrations;
44

55
public class M4_AddUuid : IDbMigration
66
{
7-
public Task Do(SQLiteAsyncConnection db)
7+
public async Task Do(SQLiteAsyncConnection db)
88
{
9-
// iterate over TrackedTimeDb and add Guid.NewGuid() as parameter
10-
return db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Uuid text")
11-
.ContinueWith(async _ =>
12-
{
13-
var all = await db.Table<TrackedTimeDb>().ToListAsync();
14-
foreach (var trackedTimeDb in all)
15-
{
16-
trackedTimeDb.Uuid = Guid.NewGuid();
17-
await db.UpdateAsync(trackedTimeDb);
18-
}
19-
});
9+
await db.ExecuteAsync("ALTER TABLE TrackedTimeDb ADD Uuid text");
10+
11+
var all = await db.Table<TrackedTimeDb_M004>().ToListAsync();
12+
foreach (var trackedTimeDb in all)
13+
{
14+
trackedTimeDb.Uuid = Guid.NewGuid();
15+
await db.UpdateAsync(trackedTimeDb);
16+
}
2017
}
2118

2219
public Task UnDo(SQLiteAsyncConnection db)
@@ -29,3 +26,33 @@ public string Serialize()
2926
throw new NotImplementedException();
3027
}
3128
}
29+
30+
/// <summary>
31+
/// Data model at the time of migration <see cref="M4_AddUuid"/>
32+
/// </summary>
33+
[Table(nameof(TrackedTimeDb))]
34+
public class TrackedTimeDb_M004 : ITable
35+
{
36+
[PrimaryKey, AutoIncrement]
37+
public int Id { get; set; }
38+
39+
public Guid Uuid { get; set; }
40+
41+
public string Name { get; set; }
42+
43+
public DateTime StartTime { get; set; }
44+
45+
public TimeSpan ElapsedTime { get; init; }
46+
47+
public int Status { get; set; }
48+
49+
public enum TrackingStatus
50+
{
51+
Completed = 0,
52+
53+
/// <summary>
54+
/// Currently running
55+
/// </summary>
56+
Running = 1,
57+
}
58+
}

TimeTracker.Core/Database/Migrator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class Migrator
1818
new M2_AddCategories(),
1919
new M3_AddStatuses(),
2020
new M4_AddUuid(),
21+
new M005_AddTimestampAndReworkElapsed(),
2122
};
2223

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

40-
await migration.Do(db);
41+
try
42+
{
43+
await migration.Do(db);
44+
}
45+
catch (Exception e)
46+
{
47+
Debug.WriteLine($"Migration {migration.GetType().Name} failed: {e}");
48+
throw;
49+
}
4150
await db.UpdateAsync(new ControlDb(ControlDb.ParamId.Version, i + 1));
4251
}
4352
}

TimeTracker.Core/Database/Tables/TrackedTimeDb.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ public class TrackedTimeDb : ITable
1616

1717
public DateTime StartTime { get; set; }
1818

19-
public TimeSpan ElapsedTime { get; init; }
19+
public DateTime EndTime { get; set; }
20+
21+
[Ignore] public TimeSpan ElapsedTime => EndTime - StartTime;
2022

2123
public int Status { get; set; }
2224

25+
/// <summary>
26+
/// Last modification time - considering Start and Elapsed may ba changed later
27+
/// </summary>
28+
public DateTime Timestamp { get; set; }
29+
2330
[Ignore]
2431
public TrackingStatus StatusEnum
2532
{

TimeTracker.Core/Models/TimeTracker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public TimeTracker(TrackedTimeDb tracker)
3030
StartTime = tracker.StartTime;
3131
IsRunning = tracker.StatusEnum == TrackedTimeDb.TrackingStatus.Running;
3232
if (IsRunning == false)
33-
EndTime = tracker.StartTime + tracker.ElapsedTime;
33+
EndTime = tracker.EndTime;
3434
}
3535

3636
public TimeTracker Start()
@@ -56,7 +56,7 @@ public TrackedTimeDb ToDb()
5656
Uuid = new Guid(),
5757
Name = Name,
5858
StartTime = StartTime,
59-
ElapsedTime = ElapsedTime,
59+
EndTime = EndTime,
6060
StatusEnum = IsRunning
6161
? TrackedTimeDb.TrackingStatus.Running
6262
: TrackedTimeDb.TrackingStatus.Completed,

TimeTracker.Core/TimeTracker.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net7.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<RootNamespace>TimeTracker</RootNamespace>
78
</PropertyGroup>
89

910
<ItemGroup>

0 commit comments

Comments
 (0)