• 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

93.62
/Rdmp.Core/DataLoad/Triggers/Implementations/MySqlTriggerImplementer.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.Linq;
9
using System.Text.RegularExpressions;
10
using FAnsi.Discovery;
11
using FAnsi.Discovery.QuerySyntax;
12
using Rdmp.Core.ReusableLibraryCode.Checks;
13
using Rdmp.Core.ReusableLibraryCode.Exceptions;
14
using Rdmp.Core.ReusableLibraryCode.Settings;
15

16
namespace Rdmp.Core.DataLoad.Triggers.Implementations;
17

18
/// <inheritdoc/>
19
internal class MySqlTriggerImplementer : TriggerImplementer
20
{
21
    /// <inheritdoc cref="TriggerImplementer(DiscoveredTable,bool)"/>
22
    public MySqlTriggerImplementer(DiscoveredTable table, bool createDataLoadRunIDAlso = true) : base(table,
92✔
23
        createDataLoadRunIDAlso)
92✔
24
    {
25
    }
92✔
26

27
    public override void DropTrigger(out string problemsDroppingTrigger, out string thingsThatWorkedDroppingTrigger)
28
    {
29
        problemsDroppingTrigger = "";
28✔
30
        thingsThatWorkedDroppingTrigger = "";
28✔
31

32
        try
33
        {
34
            using var con = _server.GetConnection();
28✔
35
            con.Open();
28✔
36

37
            using (var cmd = _server.GetCommand($"DROP TRIGGER {GetTriggerName()}", con))
28✔
38
            {
39
                cmd.CommandTimeout = UserSettings.ArchiveTriggerTimeout;
28✔
40
                cmd.ExecuteNonQuery();
28✔
41
            }
8✔
42

43
            thingsThatWorkedDroppingTrigger = $"Dropped trigger {GetTriggerName()}";
8✔
44
        }
8✔
45
        catch (Exception exception)
20✔
46
        {
47
            //this is not a problem really since it is likely that DLE chose to recreate the trigger because it was FUBARed or missing, this is just belt and braces try and drop anything that is lingering, whether or not it is there
48
            problemsDroppingTrigger += $"Failed to drop Trigger:{exception.Message}{Environment.NewLine}";
20✔
49
        }
20✔
50
    }
28✔
51

52
    public override string CreateTrigger(ICheckNotifier notifier)
53
    {
54
        var creationSql = base.CreateTrigger(notifier);
42✔
55

56
        var sql = $@"CREATE TRIGGER {GetTriggerName()} BEFORE UPDATE ON {_table.GetFullyQualifiedName()} FOR EACH ROW
38✔
57
{CreateTriggerBody()};";
38✔
58

59
        using var con = _server.GetConnection();
38✔
60
        con.Open();
38✔
61

62
        using var cmd = _server.GetCommand(sql, con);
38✔
63
        cmd.CommandTimeout = UserSettings.ArchiveTriggerTimeout;
38✔
64
        cmd.ExecuteNonQuery();
38✔
65

66
        return creationSql;
38✔
67
    }
38✔
68

69
    protected override void AddValidFrom(DiscoveredTable table, IQuerySyntaxHelper syntaxHelper)
70
    {
71
        // MySql changed how they do default date fields between 5.5 and 5.6
72
        //https://dba.stackexchange.com/a/132954
73

74
        table.AddColumn(SpecialFieldNames.ValidFrom,
32!
75
            UseOldDateTimeDefaultMethod(table)
32✔
76
                ? "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
32✔
77
                : "DATETIME DEFAULT CURRENT_TIMESTAMP", true, UserSettings.ArchiveTriggerTimeout);
32✔
78
    }
32✔
79

80
    public static bool UseOldDateTimeDefaultMethod(DiscoveredTable table)
81
    {
82
        using var con = table.Database.Server.GetConnection();
32✔
83
        con.Open();
32✔
84
        return UseOldDateTimeDefaultMethod(table.GetCommand("SELECT VERSION()", con).ExecuteScalar()?.ToString());
32!
85
    }
32✔
86

87
    public static bool UseOldDateTimeDefaultMethod(string version)
88
    {
89
        if (string.IsNullOrWhiteSpace(version))
42!
90
            return false;
×
91

92
        var match = Regex.Match(version, @"(\d+)\.(\d+)");
42✔
93

94
        //If the version string doesn't start with numbers we have bigger problems than creating a default constraint
95
        if (!match.Success)
42!
96
            return false;
×
97

98
        var major = int.Parse(match.Groups[1].Value);
42✔
99
        var minor = int.Parse(match.Groups[2].Value);
42✔
100

101
        return major < 5 || (major == 5 && minor <= 5);
42✔
102
    }
103

104
    protected virtual string CreateTriggerBody()
105
    {
106
        var syntax = _server.GetQuerySyntaxHelper();
84✔
107

108
        return $@"BEGIN
84✔
109
    INSERT INTO {_archiveTable.GetFullyQualifiedName()} SET {string.Join(",", _columns.Select(c =>
84✔
110
        $"{syntax.EnsureWrapped(c.GetRuntimeName())}=OLD.{syntax.EnsureWrapped(c.GetRuntimeName())}"))},hic_validTo=now(),hic_userID=CURRENT_USER(),hic_status='U';
406✔
111

84✔
112
        SET NEW.{syntax.EnsureWrapped(SpecialFieldNames.ValidFrom)} = now();
84✔
113
  END";
84✔
114
    }
115

116
    public override TriggerStatus GetTriggerStatus() =>
117
        string.IsNullOrWhiteSpace(GetTriggerBody()) ? TriggerStatus.Missing : TriggerStatus.Enabled;
82✔
118

119
    protected virtual string GetTriggerBody()
120
    {
121
        using var con = _server.GetConnection();
128✔
122
        con.Open();
128✔
123

124
        using var cmd = _server.GetCommand($"show triggers like '{_table.GetRuntimeName()}'", con);
128✔
125
        using var r = cmd.ExecuteReader();
128✔
126
        while (r.Read())
128✔
127
            if (r["Trigger"].Equals(GetTriggerName()))
104✔
128
                return (string)r["Statement"];
104✔
129

130
        return null;
24✔
131
    }
128✔
132

133
    protected virtual object GetTriggerName() =>
134
        $"{QuerySyntaxHelper.MakeHeaderNameSensible(_table.GetRuntimeName())}_onupdate";
178✔
135

136
    public override bool CheckUpdateTriggerIsEnabledAndHasExpectedBody()
137
    {
138
        if (!base.CheckUpdateTriggerIsEnabledAndHasExpectedBody())
66✔
139
            return false;
18✔
140

141
        var sqlThen = GetTriggerBody();
46✔
142
        var sqlNow = CreateTriggerBody();
46✔
143

144
        AssertTriggerBodiesAreEqual(sqlThen, sqlNow);
46✔
145

146
        return true;
40✔
147
    }
148

149
    protected virtual void AssertTriggerBodiesAreEqual(string sqlThen, string sqlNow)
150
    {
151
        if (!sqlNow.Equals(sqlThen))
46✔
152
            throw new ExpectedIdenticalStringsException("Sql body for trigger doesn't match expected sql", sqlNow,
6✔
153
                sqlThen);
6✔
154
    }
40✔
155
}
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

© 2026 Coveralls, Inc