Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit a07d4eb

Browse files
committed
Create drafts database if necessary.
1 parent fecdccf commit a07d4eb

File tree

1 file changed

+77
-26
lines changed

1 file changed

+77
-26
lines changed

src/GitHub.App/Services/SqliteMessageDraftStore.cs

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,39 @@ namespace GitHub.Services
2121
public class SqliteMessageDraftStore : IMessageDraftStore
2222
{
2323
static readonly ILogger log = LogManager.ForContext<SqliteMessageDraftStore>();
24-
readonly SQLiteAsyncConnection connection;
24+
readonly IOperatingSystem os;
25+
SQLiteAsyncConnection connection;
26+
bool initialized;
2527

2628
[ImportingConstructor]
2729
public SqliteMessageDraftStore(IOperatingSystem os)
2830
{
29-
var path = Path.Combine(
30-
os.Environment.GetApplicationDataPath(),
31-
"drafts.db");
32-
connection = new SQLiteAsyncConnection(path);
31+
this.os = os;
3332
}
3433

3534
public async Task<T> GetDraft<T>(string key, string secondaryKey) where T : class
3635
{
3736
Guard.ArgumentNotEmptyString(key, nameof(key));
3837
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
3938

40-
try
39+
if (await Initialize().ConfigureAwait(false))
4140
{
42-
var result = await connection.Table<Draft>().Where(
43-
x => x.Key == key && x.SecondaryKey == secondaryKey)
44-
.FirstOrDefaultAsync()
45-
.ConfigureAwait(false);
46-
47-
if (result != null)
41+
try
4842
{
49-
return JsonConvert.DeserializeObject<T>(result.Data);
43+
var result = await connection.Table<Draft>().Where(
44+
x => x.Key == key && x.SecondaryKey == secondaryKey)
45+
.FirstOrDefaultAsync()
46+
.ConfigureAwait(false);
47+
48+
if (result != null)
49+
{
50+
return JsonConvert.DeserializeObject<T>(result.Data);
51+
}
52+
}
53+
catch (Exception ex)
54+
{
55+
log.Error(ex, "Failed to load message draft into {Type}", typeof(T));
5056
}
51-
}
52-
catch (Exception ex)
53-
{
54-
log.Error(ex, "Failed to load message draft into {Type}", typeof(T));
5557
}
5658

5759
return null;
@@ -61,17 +63,20 @@ public async Task<T> GetDraft<T>(string key, string secondaryKey) where T : clas
6163
{
6264
Guard.ArgumentNotEmptyString(key, nameof(key));
6365

64-
try
66+
if (await Initialize().ConfigureAwait(false))
6567
{
66-
var result = await connection.Table<Draft>().Where(x => x.Key == key)
67-
.ToListAsync()
68-
.ConfigureAwait(false);
68+
try
69+
{
70+
var result = await connection.Table<Draft>().Where(x => x.Key == key)
71+
.ToListAsync()
72+
.ConfigureAwait(false);
6973

70-
return result.Select(x => (x.SecondaryKey, JsonConvert.DeserializeObject<T>(x.Data)));
71-
}
72-
catch (Exception ex)
73-
{
74-
log.Error(ex, "Failed to load message drafts into {Type}", typeof(T));
74+
return result.Select(x => (x.SecondaryKey, JsonConvert.DeserializeObject<T>(x.Data)));
75+
}
76+
catch (Exception ex)
77+
{
78+
log.Error(ex, "Failed to load message drafts into {Type}", typeof(T));
79+
}
7580
}
7681

7782
return null;
@@ -82,6 +87,11 @@ public async Task UpdateDraft<T>(string key, string secondaryKey, T data) where
8287
Guard.ArgumentNotEmptyString(key, nameof(key));
8388
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
8489

90+
if (!await Initialize().ConfigureAwait(false))
91+
{
92+
return;
93+
}
94+
8595
try
8696
{
8797
var row = new Draft
@@ -104,6 +114,11 @@ public async Task DeleteDraft(string key, string secondaryKey)
104114
Guard.ArgumentNotEmptyString(key, nameof(key));
105115
Guard.ArgumentNotNull(secondaryKey, nameof(secondaryKey));
106116

117+
if (!await Initialize().ConfigureAwait(false))
118+
{
119+
return;
120+
}
121+
107122
try
108123
{
109124
await connection.ExecuteAsync(
@@ -117,6 +132,42 @@ await connection.ExecuteAsync(
117132
}
118133
}
119134

135+
async Task<bool> Initialize()
136+
{
137+
if (!initialized)
138+
{
139+
var path = Path.Combine(os.Environment.GetApplicationDataPath(), "drafts.db");
140+
141+
try
142+
{
143+
connection = new SQLiteAsyncConnection(path);
144+
145+
var draftsTable = await connection.GetTableInfoAsync("Drafts").ConfigureAwait(false);
146+
147+
if (draftsTable.Count == 0)
148+
{
149+
await connection.ExecuteAsync(@"
150+
CREATE TABLE `Drafts` (
151+
`Key` TEXT,
152+
`SecondaryKey` TEXT,
153+
`Data` TEXT,
154+
UNIQUE(`Key`,`SecondaryKey`)
155+
);").ConfigureAwait(false);
156+
}
157+
}
158+
catch (Exception ex)
159+
{
160+
log.Error(ex, "Error opening drafts from {Path}.", path);
161+
}
162+
finally
163+
{
164+
initialized = true;
165+
}
166+
}
167+
168+
return connection != null;
169+
}
170+
120171
[Table("Drafts")]
121172
private class Draft
122173
{

0 commit comments

Comments
 (0)