Skip to content

Commit db03ff1

Browse files
feature/NV-9899
2 parents 60a4ff5 + a80aea9 commit db03ff1

13 files changed

+458
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
src/.idea/
12
src/.vs/
23
temp/
34
src2/
@@ -13,3 +14,4 @@ bin
1314
obj
1415

1516
src/Ormico.DbPatchManager.CLI/deb/
17+
src/Ormico.DbPatchManager.CLI/Properties/launchSettings.json

src/Ormico.DbPatchManager.CLI/Ormico.DbPatchManager.CLI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

src/Ormico.DbPatchManager.CLI/Program.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using CommandLine;
3+
using Newtonsoft.Json;
34
using Ormico.DbPatchManager.Logic;
45
using Ormico.DbPatchManager.CLI.CommandLineOptions;
56

@@ -12,14 +13,24 @@ static int Main(string[] args)
1213
int rc = 0;
1314
try
1415
{
15-
rc = CommandLine.Parser.Default.ParseArguments(args, typeof(InitCmdLineOptions), typeof(AddPatchCmdLineOptions), typeof(BuildCmdLineOptions))
16+
rc = CommandLine.Parser.Default.ParseArguments(args, typeof(InitCmdLineOptions),
17+
typeof(AddPatchCmdLineOptions), typeof(BuildCmdLineOptions))
1618
.MapResult(
1719
(InitCmdLineOptions o) => InitBuildSettings(o),
1820
(AddPatchCmdLineOptions o) => AddPatch(o),
1921
(BuildCmdLineOptions o) => Build(o),
2022
err => 1
2123
);
2224
}
25+
catch (JsonException jsonException)
26+
{
27+
Console.WriteLine("{0}", jsonException.Message);
28+
Console.WriteLine("{0}", jsonException.StackTrace);
29+
if (jsonException.InnerException != null)
30+
{
31+
Console.WriteLine("{0}", jsonException.InnerException.Message);
32+
}
33+
}
2334
catch (Exception ex)
2435
{
2536
Console.WriteLine($"{ex.Message}");
@@ -54,19 +65,29 @@ static int InitBuildSettings(InitCmdLineOptions options)
5465
static int AddPatch(AddPatchCmdLineOptions options)
5566
{
5667
int rc = 0;
68+
var startTime = DateTimeOffset.Now;
69+
Console.WriteLine("{0:O} - Database Add Patch Started", startTime);
5770
PatchManager manager = new PatchManager(_patchFileName, _patchLocalFileName);
5871
//todo: pass all settings
5972
manager.AddPatch(options.Name, new PatchOptions()
6073
{
6174
});
75+
var endTime = DateTimeOffset.Now;
76+
Console.WriteLine("{0:O} - Database Add Patch Completed", endTime);
77+
Console.WriteLine("{0:g} - Add Patch Time", endTime.Subtract(startTime));
6278
return rc;
6379
}
6480

6581
static int Build(BuildCmdLineOptions options)
6682
{
6783
int rc = 0;
84+
var startTime = DateTimeOffset.Now;
85+
Console.WriteLine("{0:O} - Database Build Started", startTime);
6886
PatchManager manager = new PatchManager(_patchFileName, _patchLocalFileName);
6987
manager.Build();
88+
var endTime = DateTimeOffset.Now;
89+
Console.WriteLine("{0:O} - Database Build Completed", endTime);
90+
Console.WriteLine("{0:g} - Build Time", endTime.Subtract(startTime));
7091
return rc;
7192
}
7293
}

src/Ormico.DbPatchManager.Common/Ormico.DbPatchManager.Common.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
54
<RepositoryUrl>https://github.com/ormico/dbpatchmanager</RepositoryUrl>
65
<PackageLicenseFile>LICENSE</PackageLicenseFile>
76
<Authors>Zack Moore</Authors>
@@ -14,6 +13,7 @@
1413
<Copyright>Copyright (c) 2020 Zack Moore</Copyright>
1514
<PackageProjectUrl>https://dbpatch.dev/</PackageProjectUrl>
1615
<Version>2.1.2</Version>
16+
<TargetFrameworks>net8.0;netstandard2.1</TargetFrameworks>
1717
</PropertyGroup>
1818

1919
<ItemGroup>
@@ -27,4 +27,8 @@
2727
</None>
2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
32+
</ItemGroup>
33+
3034
</Project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System.Collections.Generic;
2+
using System.Globalization;
3+
using System.Linq;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Converters;
6+
7+
namespace Ormico.DbPatchManager.Common
8+
{
9+
public partial class PatchFile
10+
{
11+
[JsonProperty("DatabaseType")] public string DatabaseType { get; set; }
12+
13+
[JsonProperty("ConnectionString")] public string ConnectionString { get; set; }
14+
15+
[JsonProperty("CodeFolder")] public string CodeFolder { get; set; }
16+
17+
[JsonProperty("CodeFiles")] public List<string> CodeFiles { get; set; }
18+
19+
[JsonProperty("PatchFolder")] public string PatchFolder { get; set; }
20+
21+
[JsonProperty("Options")] public Dictionary<string, string> Options { get; set; }
22+
23+
[JsonProperty("patches")] public List<PatchFromFile> Patches { get; set; }
24+
25+
public PatchFile()
26+
{
27+
Patches = new List<PatchFromFile>();
28+
Options = new Dictionary<string, string>();
29+
CodeFiles = new List<string>();
30+
}
31+
}
32+
33+
public partial class PatchFromFile
34+
{
35+
[JsonProperty("id")] public string Id { get; set; }
36+
37+
[JsonProperty("dependsOn")] public List<string> DependsOn { get; set; }
38+
39+
public PatchFromFile()
40+
{
41+
DependsOn = new List<string>();
42+
}
43+
}
44+
45+
public partial class PatchFile
46+
{
47+
public static PatchFile FromJson(string json) =>
48+
JsonConvert.DeserializeObject<PatchFile>(json, PatchFileConverter.Settings);
49+
}
50+
51+
public static class PatchFileSerializer
52+
{
53+
public static string ToJson(this PatchFile self) =>
54+
JsonConvert.SerializeObject(self, PatchFileConverter.Settings);
55+
}
56+
57+
internal static class PatchFileConverter
58+
{
59+
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
60+
{
61+
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
62+
DateParseHandling = DateParseHandling.None,
63+
Converters =
64+
{
65+
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
66+
},
67+
Formatting = Formatting.Indented
68+
};
69+
}
70+
}

src/Ormico.DbPatchManager.Logic.Tests/Ormico.DbPatchManager.Logic.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
7+
8+
<LangVersion>10</LangVersion>
79
</PropertyGroup>
810

911
<ItemGroup>

src/Ormico.DbPatchManager.Logic/BuildConfigurationWriter.cs

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
namespace Ormico.DbPatchManager.Logic
1111
{
12-
/// <summary>
13-
/// Readn and Write DatabaseBuildConfiguration to storage.
14-
/// </summary>
15-
public class BuildConfigurationWriter
12+
public class BuildConfigurationWriter : IBuildConfigurationWriter
1613
{
1714
public BuildConfigurationWriter(string filePath, string localFilePath)
1815
{
@@ -46,14 +43,17 @@ public BuildConfigurationWriter(string filePath, string localFilePath)
4643
public DatabaseBuildConfiguration Read()
4744
{
4845
DatabaseBuildConfiguration rc = null;
49-
if(_io.File.Exists(_filePath))
46+
if (_io.File.Exists(_filePath))
5047
{
5148
//rc = JsonConvert.DeserializeObject<DatabaseBuildConfiguration>(_io.File.ReadAllText(_filePath), _jsonSettings);
52-
var o = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_filePath));
49+
var o = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
50+
_io.File.ReadAllText(_filePath));
5351

5452
if (_io.File.Exists(_localFilePath))
5553
{
56-
var localO = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_localFilePath));
54+
var localO =
55+
(Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
56+
_io.File.ReadAllText(_localFilePath));
5757
o.Merge(localO, new JsonMergeSettings()
5858
{
5959
MergeArrayHandling = MergeArrayHandling.Union
@@ -76,37 +76,39 @@ public DatabaseBuildConfiguration Read()
7676
rc.patches = new List<Patch>();
7777
}
7878
else
79-
{
79+
{
8080
var patches = (from p in o["patches"]
81-
select new Patch()
82-
{
83-
Id = (string)p["id"]
84-
}).ToList();
81+
select new Patch()
82+
{
83+
Id = (string)p["id"]
84+
}).ToList();
8585
// populate DependsOn
86-
foreach(var p in patches)
86+
foreach (var p in patches)
8787
{
8888
var cur = from x in o["patches"]
89-
from d in x["dependsOn"]
90-
from a in patches
91-
where (string)x["id"] == p.Id &&
92-
a.Id == (string)d
93-
select a;
89+
from d in x["dependsOn"]
90+
from a in patches
91+
where (string)x["id"] == p.Id &&
92+
a.Id == (string)d
93+
select a;
9494
p.DependsOn = cur.Distinct(new PatchComparer()).ToList();
9595
//todo: double check this query
9696
var children = from x in o["patches"]
97-
from d in x["dependsOn"]
98-
from a in patches
99-
where (string)d == p.Id && (string)x["id"] == a.Id
100-
select a;
97+
from d in x["dependsOn"]
98+
from a in patches
99+
where (string)d == p.Id && (string)x["id"] == a.Id
100+
select a;
101101
p.Children = children.Distinct(new PatchComparer()).ToList();
102102
}
103+
103104
rc.patches = patches.ToList();
104105
}
105106
}
106107
else
107108
{
108109
throw new ApplicationException("Configuration file does not exist. Call init first.");
109110
}
111+
110112
return rc;
111113
}
112114

@@ -117,7 +119,9 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
117119
//todo: if local file exists, don't write values to patches.json if value exists in patches.local.json
118120
if (_io.File.Exists(_localFilePath))
119121
{
120-
var localO = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_localFilePath));
122+
var localO =
123+
(Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
124+
_io.File.ReadAllText(_localFilePath));
121125
data = new JObject();
122126

123127
//todo: find a way to do this that isn't manual. can you loop over all values in buildConfiguration?
@@ -136,8 +140,9 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
136140
data["CodeFolder"] = buildConfiguration.CodeFolder;
137141
}
138142

139-
if (localO["CodeFiles"] == null && buildConfiguration.CodeFiles != null)
143+
if (localO["CodeFiles"] == null || localO["CodeFiles"].HasValues == false)
140144
{
145+
buildConfiguration.CodeFiles = buildConfiguration.CodeFiles ?? new List<string>();
141146
data["CodeFiles"] = JArray.FromObject(buildConfiguration.CodeFiles);
142147
}
143148

@@ -146,20 +151,31 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
146151
data["PatchFolder"] = buildConfiguration.PatchFolder;
147152
}
148153

149-
if (localO["Options"] == null && buildConfiguration.Options != null)
154+
try
155+
{
156+
if (localO["Options"] == null || localO["Options"].HasValues == false)
157+
{
158+
buildConfiguration.Options = buildConfiguration.Options ?? new Dictionary<string, string>();
159+
data["Options"] = JObject.FromObject(buildConfiguration.Options);
160+
}
161+
}
162+
catch (JsonException e)
150163
{
151-
data["Options"] = JArray.FromObject(buildConfiguration.Options);
164+
Console.WriteLine(e.Message);
165+
data["Options"] = JObject.FromObject(new Dictionary<string, string>());
152166
}
153167

154168
if (localO["patches"] == null && buildConfiguration.patches != null)
155169
{
156170
data["patches"] = JArray.FromObject(from p in buildConfiguration.patches
157-
select new
158-
{
159-
id = p.Id,
160-
dependsOn = p.DependsOn != null ? (from d in p.DependsOn
161-
select d.Id) : null
162-
});
171+
select new
172+
{
173+
id = p.Id,
174+
dependsOn = p.DependsOn != null
175+
? (from d in p.DependsOn
176+
select d.Id)
177+
: null
178+
});
163179
}
164180
}
165181
else
@@ -174,16 +190,18 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
174190
PatchFolder = buildConfiguration.PatchFolder,
175191
Options = buildConfiguration.Options,
176192
patches = from p in buildConfiguration.patches
177-
select new
178-
{
179-
id = p.Id,
180-
dependsOn = p.DependsOn != null?(from d in p.DependsOn.Distinct(new PatchComparer())
181-
select d.Id):null
182-
}
193+
select new
194+
{
195+
id = p.Id,
196+
dependsOn = p.DependsOn != null
197+
? (from d in p.DependsOn.Distinct(new PatchComparer())
198+
select d.Id)
199+
: null
200+
}
183201
});
184202
}
185203

186204
_io.File.WriteAllText(_filePath, data.ToString());
187205
}
188206
}
189-
}
207+
}

src/Ormico.DbPatchManager.Logic/DatabaseBuildConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public DatabaseBuildConfiguration()
1212
{
1313
//defaults
1414
PatchFolder = "Patches";
15+
Options = new Dictionary<string, string>();
1516
CodeFolder = "Code";
1617
patches = new List<Patch>();
1718
CodeFiles = new List<string>()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Ormico.DbPatchManager.Logic
2+
{
3+
public interface IBuildConfigurationWriter
4+
{
5+
/// <summary>
6+
/// Read DatabaseBuildConfiguration data from file path passed to constructor.
7+
/// </summary>
8+
/// <returns></returns>
9+
DatabaseBuildConfiguration Read();
10+
11+
void Write(DatabaseBuildConfiguration buildConfiguration);
12+
}
13+
}

0 commit comments

Comments
 (0)