• 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

83.44
/Rdmp.Core/Curation/Data/ExternalDatabaseServer.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.Data.Common;
10
using FAnsi;
11
using FAnsi.Discovery;
12
using FAnsi.Discovery.QuerySyntax;
13
using Rdmp.Core.Curation.Data.Defaults;
14
using Rdmp.Core.Curation.Data.ImportExport;
15
using Rdmp.Core.Curation.Data.Serialization;
16
using Rdmp.Core.Databases;
17
using Rdmp.Core.Logging;
18
using Rdmp.Core.MapsDirectlyToDatabaseTable;
19
using Rdmp.Core.MapsDirectlyToDatabaseTable.Attributes;
20
using Rdmp.Core.MapsDirectlyToDatabaseTable.Versioning;
21
using Rdmp.Core.Repositories;
22
using Rdmp.Core.ReusableLibraryCode.Annotations;
23
using Rdmp.Core.ReusableLibraryCode.Checks;
24
using Rdmp.Core.ReusableLibraryCode.DataAccess;
25

26
namespace Rdmp.Core.Curation.Data;
27

28
/// <inheritdoc cref="IExternalDatabaseServer"/>
29
public class ExternalDatabaseServer : DatabaseEntity, IExternalDatabaseServer, IDataAccessCredentials, INamed,
30
    ICheckable
31
{
32
    #region Database Properties
33

34
    private string _name;
35
    private string _createdByAssembly;
36
    private string _mappedDataPath;
37
    private readonly SelfCertifyingDataAccessPoint _selfCertifyingDataAccessPoint;
38

39
    /// <summary>
40
    /// Human readable name for the server e.g. 'My Favourite Logging Database'
41
    /// </summary>
42
    [Unique]
43
    [NotNull]
44
    public string Name
45
    {
46
        get => _name;
4,942✔
47
        set => SetField(ref _name, value);
7,261✔
48
    }
49

50
    /// <summary>
51
    /// If the database was created by an RDMP schema (or plugin schema) this will contain the name of the dll which holds the schema e.g. DataQualityEngine.Database and was
52
    /// responsible for creating the database.  This determines what roles RDMP lets the database play.
53
    /// </summary>
54
    public string CreatedByAssembly
55
    {
56
        get => _createdByAssembly;
5,646✔
57
        set => SetField(ref _createdByAssembly, value);
7,243✔
58
    }
59

60
    /// <summary>
61
    /// The public network share of the Data path where the physical database files are stored if applicable.  Sharing your database directory on the network is a
62
    /// terrible idea (don't do it).  You can use this to automate detatching and shipping an MDF to your researchers e.g. MsSqlReleaseSource
63
    /// </summary>
64
    public string MappedDataPath
65
    {
66
        get => _mappedDataPath;
4,698✔
67
        set => SetField(ref _mappedDataPath, value);
7,229✔
68
    }
69

70
    /// <inheritdoc/>
71
    public string Server
72
    {
73
        get => _selfCertifyingDataAccessPoint.Server;
7,942✔
74
        set
75
        {
76
            if (Equals(_selfCertifyingDataAccessPoint.Server, value))
2,338✔
77
                return;
1,178✔
78

79
            var old = _selfCertifyingDataAccessPoint.Server;
1,160✔
80
            _selfCertifyingDataAccessPoint.Server = value;
1,160✔
81
            OnPropertyChanged(old, value);
1,160✔
82
        }
1,160✔
83
    }
84

85
    /// <inheritdoc/>
86
    public string Database
87
    {
88
        get => _selfCertifyingDataAccessPoint.Database;
7,176✔
89
        set
90
        {
91
            if (Equals(_selfCertifyingDataAccessPoint.Database, value))
2,330✔
92
                return;
1,178✔
93

94
            var old = _selfCertifyingDataAccessPoint.Database;
1,152✔
95
            _selfCertifyingDataAccessPoint.Database = value;
1,152✔
96
            OnPropertyChanged(old, value);
1,152✔
97
        }
1,152✔
98
    }
99

100
    /// <inheritdoc/>
101
    public string Username
102
    {
103
        get => _selfCertifyingDataAccessPoint.Username;
4,710✔
104
        set
105
        {
106
            if (Equals(_selfCertifyingDataAccessPoint.Username, value))
2,342✔
107
                return;
1,280✔
108

109
            var old = _selfCertifyingDataAccessPoint.Username;
1,062✔
110
            _selfCertifyingDataAccessPoint.Username = value;
1,062✔
111
            OnPropertyChanged(old, value);
1,062✔
112
        }
1,062✔
113
    }
114

115
    /// <inheritdoc/>
116
    public string Password
117
    {
118
        get => _selfCertifyingDataAccessPoint.Password;
4,690✔
119
        set
120
        {
121
            _selfCertifyingDataAccessPoint.Password = value;
2,340✔
122
            OnPropertyChanged(null, value);
2,340✔
123
        }
2,340✔
124
    }
125

126
    /// <inheritdoc/>
127
    public DatabaseType DatabaseType
128
    {
129
        get => _selfCertifyingDataAccessPoint.DatabaseType;
6,486✔
130
        set
131
        {
132
            if (Equals(_selfCertifyingDataAccessPoint.DatabaseType, value))
1,320✔
133
                return;
1,302✔
134

135
            var old = _selfCertifyingDataAccessPoint.DatabaseType;
18✔
136
            _selfCertifyingDataAccessPoint.DatabaseType = value;
18✔
137
            OnPropertyChanged(old, value);
18✔
138
        }
18✔
139
    }
140

141
    #endregion
142

143
    public ExternalDatabaseServer()
2✔
144
    {
145
        _selfCertifyingDataAccessPoint = new SelfCertifyingDataAccessPoint
2✔
146
        {
2✔
147
            DatabaseType = DatabaseType.MicrosoftSQLServer
2✔
148
        };
2✔
149
    }
2✔
150

151
    /// <summary>
152
    /// Creates a new persistent server reference in RDMP platform database that allows it to connect to a (usually database) server.
153
    /// 
154
    /// <para>If you are trying to create a database (e.g. a logging database) you should instead use
155
    /// <see cref="MapsDirectlyToDatabaseTable.Versioning.MasterDatabaseScriptExecutor"/></para>
156
    /// </summary>
157
    /// <param name="repository"></param>
158
    /// <param name="name"></param>
159
    /// <param name="creatorIfAny">If the database referenced was created according to a specific SQL schema, this is the schema provider</param>
160
    public ExternalDatabaseServer(ICatalogueRepository repository, string name, IPatcher creatorIfAny)
1,202✔
161
    {
162
        var parameters = new Dictionary<string, object>
1,202✔
163
        {
1,202✔
164
            { "Name", name },
1,202✔
165
            { "DatabaseType", DatabaseType.MicrosoftSQLServer }
1,202✔
166
        };
1,202✔
167

168
        if (creatorIfAny != null)
1,202✔
169
            parameters.Add("CreatedByAssembly", creatorIfAny.Name);
778✔
170

171
        Repository = repository;
1,202✔
172
        _selfCertifyingDataAccessPoint = new SelfCertifyingDataAccessPoint(repository, DatabaseType.MicrosoftSQLServer);
1,202✔
173
        repository.InsertAndHydrate(this, parameters);
1,202✔
174
    }
1,202✔
175

176

177
    internal ExternalDatabaseServer(ShareManager shareManager, ShareDefinition shareDefinition)
4✔
178
    {
179
        var repo = shareManager.RepositoryLocator.CatalogueRepository;
4✔
180
        Repository = repo;
4✔
181
        _selfCertifyingDataAccessPoint = new SelfCertifyingDataAccessPoint(CatalogueRepository,
4✔
182
            DatabaseType.MicrosoftSQLServer /*will get changed by UpsertAndHydrate*/);
4✔
183

184
        shareManager.UpsertAndHydrate(this, shareDefinition);
4✔
185
    }
4✔
186

187
    internal ExternalDatabaseServer(ICatalogueRepository repository, DbDataReader r) : base(repository, r)
6,051✔
188
    {
189
        Name = r["Name"] as string;
6,051✔
190
        CreatedByAssembly = r["CreatedByAssembly"] as string;
6,051✔
191
        MappedDataPath = r["MappedDataPath"] as string;
6,051✔
192

193
        var databaseType = (DatabaseType)Enum.Parse(typeof(DatabaseType), r["DatabaseType"].ToString());
6,051✔
194

195
        _selfCertifyingDataAccessPoint = new SelfCertifyingDataAccessPoint(repository, databaseType)
6,051✔
196
        {
6,051✔
197
            Database = r["Database"] as string,
6,051✔
198
            Password = r["Password"] as string,
6,051✔
199
            Server = r["Server"] as string,
6,051✔
200
            Username = r["Username"] as string
6,051✔
201
        };
6,051✔
202
    }
6,051✔
203

204
    /// <inheritdoc/>
205
    public override string ToString() => Name;
152✔
206

207
    public void Check(ICheckNotifier notifier)
208
    {
209
        if (string.IsNullOrWhiteSpace(Server))
×
210
            notifier.OnCheckPerformed(new CheckEventArgs("No Server set", CheckResult.Warning));
×
211
        else if (string.IsNullOrWhiteSpace(Database))
×
212
            notifier.OnCheckPerformed(new CheckEventArgs("No Database set", CheckResult.Warning));
×
213
        else
214
            try
215
            {
216
                DataAccessPortal.ExpectServer(this, DataAccessContext.InternalDataProcessing).TestConnection();
×
217
                notifier.OnCheckPerformed(new CheckEventArgs("Successfully connected to server", CheckResult.Success));
×
218
            }
×
219
            catch (Exception exception)
×
220
            {
221
                notifier.OnCheckPerformed(
×
222
                    new CheckEventArgs("Failed to connect to server", CheckResult.Fail, exception));
×
223
                return;
×
224
            }
225

226
        //if it's a logging server run logging checks
227
        if (WasCreatedBy(new LoggingDatabasePatcher()))
×
228
            new LoggingDatabaseChecker(this).Check(notifier);
×
229
    }
×
230

231
    /// <inheritdoc/>
232
    public IQuerySyntaxHelper GetQuerySyntaxHelper() => _selfCertifyingDataAccessPoint.GetQuerySyntaxHelper();
1,260✔
233

234
    /// <inheritdoc/>
235
    public IDataAccessCredentials GetCredentialsIfExists(DataAccessContext context) =>
236
        _selfCertifyingDataAccessPoint.GetCredentialsIfExists(context);
1,522✔
237

238
    /// <inheritdoc/>
239
    public string GetDecryptedPassword() => _selfCertifyingDataAccessPoint.GetDecryptedPassword() ?? "";
10!
240

241
    /// <summary>
242
    /// Sets server,database,username and password properties based on the supplied DiscoveredDatabase (which doesn't have to actually exist).  This method also optionally calls
243
    /// SaveToDatabase which commits the changes to the Catalogue Repository
244
    /// </summary>
245
    /// <param name="discoveredDatabase"></param>
246
    /// <param name="save">true if you want to call SaveToDatabase after setting the properties</param>
247
    public void SetProperties(DiscoveredDatabase discoveredDatabase, bool save = true)
248
    {
249
        Server = discoveredDatabase.Server.Name;
112✔
250
        Database = discoveredDatabase.GetRuntimeName();
112✔
251
        Username = discoveredDatabase.Server.ExplicitUsernameIfAny;
112✔
252
        Password = discoveredDatabase.Server.ExplicitPasswordIfAny;
112✔
253
        DatabaseType = discoveredDatabase.Server.DatabaseType;
112✔
254

255
        if (save)
112✔
256
            SaveToDatabase();
112✔
257
    }
112✔
258

259
    /// <inheritdoc/>
260
    public bool WasCreatedBy(IPatcher patcher) => !string.IsNullOrWhiteSpace(CreatedByAssembly) &&
454✔
261
                                                  (patcher.Name == CreatedByAssembly ||
454✔
262
                                                   patcher.LegacyName == CreatedByAssembly);
454✔
263

264
    /// <inheritdoc/>
265
    public DiscoveredDatabase Discover(DataAccessContext context) => _selfCertifyingDataAccessPoint.Discover(context);
88✔
266

267
    /// <inheritdoc/>
268
    public bool DiscoverExistence(DataAccessContext context, out string reason) =>
269
        _selfCertifyingDataAccessPoint.DiscoverExistence(context, out reason);
4✔
270

271

272
    public override void DeleteInDatabase()
273
    {
274
        
275
         if (WasCreatedBy(new LoggingDatabasePatcher())){
40✔
276
            //If you're trying to delete a logging server, remove all references to it first
277
            var catalogues = Repository.GetAllObjectsWhere<Catalogue>("LiveLoggingServer_ID",base.ID);
2✔
278
            foreach(var catalogue in catalogues){
4!
279
                catalogue.LiveLoggingServer_ID = null;
×
280
                catalogue.SaveToDatabase();
×
281
            }
282
         }
283
         base.DeleteInDatabase();
40✔
284

285
         // normally in database schema deleting an ExternalDatabaseServer will cascade to clear defaults
286
         // but some repositories do not support this implicit removal so lets double check there are no references
287
         foreach (PermissableDefaults d in Enum.GetValues(typeof(PermissableDefaults)))
720✔
288
         {
289
             var existingDefault = CatalogueRepository.GetDefaultFor(d);
320✔
290
             if (Equals(existingDefault)) CatalogueRepository.ClearDefault(d);
320!
291
         }
292
    }
40✔
293

294
    public void SetRepository(ICatalogueRepository repository)
295
    {
296
        _selfCertifyingDataAccessPoint.SetRepository(repository);
4✔
297
    }
4✔
298
}
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