• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

HicServices / RDMP / 6245535001

20 Sep 2023 07:44AM UTC coverage: 57.013%. First build
6245535001

push

github

web-flow
8.1.0 Release (#1628)

* Bump Newtonsoft.Json from 13.0.1 to 13.0.2

Bumps [Newtonsoft.Json](https://github.com/JamesNK/Newtonsoft.Json) from 13.0.1 to 13.0.2.
- [Release notes](https://github.com/JamesNK/Newtonsoft.Json/releases)
- [Commits](https://github.com/JamesNK/Newtonsoft.Json/compare/13.0.1...13.0.2)

---
updated-dependencies:
- dependency-name: Newtonsoft.Json
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump NLog from 5.0.5 to 5.1.0

Bumps [NLog](https://github.com/NLog/NLog) from 5.0.5 to 5.1.0.
- [Release notes](https://github.com/NLog/NLog/releases)
- [Changelog](https://github.com/NLog/NLog/blob/dev/CHANGELOG.md)
- [Commits](https://github.com/NLog/NLog/compare/v5.0.5...v5.1.0)

---
updated-dependencies:
- dependency-name: NLog
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump NLog from 5.0.5 to 5.1.0

* Fix -r flag - should have been --results-directory all along

* Bump Newtonsoft.Json from 13.0.1 to 13.0.2

* Bump YamlDotNet from 12.0.2 to 12.1.0

Bumps [YamlDotNet](https://github.com/aaubry/YamlDotNet) from 12.0.2 to 12.1.0.
- [Release notes](https://github.com/aaubry/YamlDotNet/releases)
- [Commits](https://github.com/aaubry/YamlDotNet/compare/v12.0.2...v12.1.0)

---
updated-dependencies:
- dependency-name: YamlDotNet
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* Bump Moq from 4.18.2 to 4.18.3

Bumps [Moq](https://github.com/moq/moq4) from 4.18.2 to 4.18.3.
- [Release notes](https://github.com/moq/moq4/releases)
- [Changelog](https://github.com/moq/moq4/blob/main/CHANGELOG.md)
- [Commits](https://github.com/moq/moq4/compare/v4.18.2...v4.18.3)

---
updated-dependencies:
- dependency-name: Moq
... (continued)

10732 of 20257 branches covered (0.0%)

Branch coverage included in aggregate %.

48141 of 48141 new or added lines in 1086 files covered. (100.0%)

30685 of 52388 relevant lines covered (58.57%)

7387.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.48
/Rdmp.Core/MapsDirectlyToDatabaseTable/Versioning/Patcher.cs
1
// Copyright (c) The University of Dundee 2018-2019
2
// This file is part of the Research Data Management Platform (RDMP).
3
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
6

7
using System;
8
using System.Collections.Generic;
9
using System.IO;
10
using System.Linq;
11
using System.Reflection;
12
using System.Text.RegularExpressions;
13
using FAnsi;
14
using FAnsi.Discovery;
15

16
namespace Rdmp.Core.MapsDirectlyToDatabaseTable.Versioning;
17

18
/// <inheritdoc/>
19
public abstract partial class Patcher : IPatcher
20
{
21
    public const string InitialScriptName = "Initial Create";
22

23
    /// <inheritdoc/>
24
    public bool SqlServerOnly { get; protected init; } = true;
6,044✔
25

26
    /// <inheritdoc/>
27
    public virtual Assembly GetDbAssembly() => GetType().Assembly;
3,856✔
28

29
    /// <inheritdoc/>
30
    public string ResourceSubdirectory { get; private set; }
11,050✔
31

32
    /// <inheritdoc/>
33
    public int Tier { get; }
×
34

35
    public string Name =>
36
        $"{GetDbAssembly().GetName().Name}{(string.IsNullOrEmpty(ResourceSubdirectory) ? "" : $"/{ResourceSubdirectory}")}";
3,634✔
37

38
    public string LegacyName { get; protected set; }
3,760✔
39

40
    protected Patcher(int tier, string resourceSubdirectory)
3,652✔
41
    {
42
        Tier = tier;
3,652✔
43
        ResourceSubdirectory = resourceSubdirectory;
3,652✔
44
    }
3,652✔
45

46
    /// <summary>
47
    /// Generates a properly formatted header for <see cref="Patch"/> creation when you only know the SQL you want to execute
48
    /// </summary>
49
    /// <param name="dbType"></param>
50
    /// <param name="description"></param>
51
    /// <param name="version"></param>
52
    /// <returns></returns>
53
    protected static string GetHeader(DatabaseType dbType, string description, Version version) =>
54
        $"{CommentFor(dbType, Patch.VersionKey + version.ToString())}{Environment.NewLine}{CommentFor(dbType, Patch.DescriptionKey + description)}{Environment.NewLine}";
92✔
55

56
    // some DBMS don't like the -- notation so we need to wrap with C style comments
57
    private static string CommentFor(DatabaseType dbType, string sql) =>
58
        dbType switch
184✔
59
        {
184✔
60
            DatabaseType.MicrosoftSQLServer => sql,
148✔
61
            _ => $"/*{sql}*/"
36✔
62
        };
184✔
63

64
    public virtual Patch GetInitialCreateScriptContents(DiscoveredDatabase db)
65
    {
66
        var assembly = GetDbAssembly();
18✔
67
        var subdirectory = ResourceSubdirectory;
18✔
68

69
        var initialCreationRegex = string.IsNullOrWhiteSpace(subdirectory)
18!
70
            ? AfterCreateRegex()
18✔
71
            : new Regex($@".*\.{Regex.Escape(subdirectory)}\.runAfterCreateDatabase\..*\.sql");
18✔
72

73
        var candidates = assembly.GetManifestResourceNames().Where(r => initialCreationRegex.IsMatch(r)).ToArray();
2,286✔
74

75
        switch (candidates.Length)
18!
76
        {
77
            case 1:
78
                {
79
                    var sr = new StreamReader(assembly.GetManifestResourceStream(candidates[0]));
18✔
80

81
                var sql = sr.ReadToEnd();
18✔
82

83
                    if (!sql.Contains(Patch.VersionKey))
18✔
84
                        sql = GetHeader(db.Server.DatabaseType, InitialScriptName, new Version(1, 0, 0)) + sql;
18✔
85

86

87
                    return new Patch(InitialScriptName, sql);
18✔
88
                }
89
            case 0:
90
                throw new FileNotFoundException(
×
91
                    $"Could not find an initial create database script in dll {assembly.FullName}.  Make sure it is marked as an Embedded Resource and that it is in a folder called 'runAfterCreateDatabase' (and matches regex {initialCreationRegex}). And make sure that it is marked as 'Embedded Resource' in the .csproj build action");
×
92
            default:
93
                throw new Exception(
×
94
                    $"There are too many create scripts in the assembly {assembly.FullName} only 1 create database script is allowed, all other scripts must go into the up folder");
×
95
        }
96
    }
97

98
    /// <inheritdoc/>
99
    public virtual SortedDictionary<string, Patch> GetAllPatchesInAssembly(DiscoveredDatabase db)
100
    {
101
        var assembly = GetDbAssembly();
148✔
102
        var subdirectory = ResourceSubdirectory;
148✔
103

104
        var upgradePatchesRegexPattern = string.IsNullOrWhiteSpace(subdirectory)
148!
105
            ? UpRegex()
148✔
106
            : new Regex($@".*\.{Regex.Escape(subdirectory)}\.up\.(.*\.sql)");
148✔
107

108
        var files = new SortedDictionary<string, Patch>();
148✔
109

110
        //get all resources out of
111
        foreach (var manifestResourceName in assembly.GetManifestResourceNames())
37,592✔
112
        {
113
            var match = upgradePatchesRegexPattern.Match(manifestResourceName);
18,648✔
114
            if (!match.Success) continue;
18,648✔
115
            var fileContents = new StreamReader(assembly.GetManifestResourceStream(manifestResourceName)).ReadToEnd();
1,824✔
116
            files.Add(match.Groups[1].Value, new Patch(match.Groups[1].Value, fileContents));
1,824✔
117
        }
118

119
        return files;
148✔
120
    }
121

122
    [GeneratedRegex(".*\\.up\\.(.*\\.sql)")]
123
    private static partial Regex UpRegex();
124

125
    [GeneratedRegex(".*\\.runAfterCreateDatabase\\..*\\.sql")]
126
    private static partial Regex AfterCreateRegex();
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc