-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM005_AddTimestampAndReworkElapsed.cs
More file actions
79 lines (62 loc) · 2.25 KB
/
M005_AddTimestampAndReworkElapsed.cs
File metadata and controls
79 lines (62 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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,
}
}