Skip to content

Commit

Permalink
Merge branch 'main' into feature/pgversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jas88 committed Jul 16, 2024
2 parents 22f3ccc + 94e3ade commit 10e1bdd
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Listing databases on Postgres now respects connection string database and timeout

## [3.2.5] - 2024-06-07

- Bugfix for resource lifetime in ListDatabasesAsync, add unit tests
Expand Down
3 changes: 1 addition & 2 deletions FAnsiSql/Discovery/DiscoveredColumn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using FAnsi.Discovery.QuerySyntax;
using FAnsi.Discovery.QuerySyntax;
using FAnsi.Naming;
using TypeGuesser;

Expand Down
1 change: 0 additions & 1 deletion FAnsiSql/Discovery/DiscoveredServerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down
13 changes: 6 additions & 7 deletions FAnsiSql/Implementations/PostgreSql/PostgreSqlServerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@ public override DbConnectionStringBuilder EnableAsync(DbConnectionStringBuilder
public override void CreateDatabase(DbConnectionStringBuilder builder, IHasRuntimeName newDatabaseName)
{
var b = (NpgsqlConnectionStringBuilder)GetConnectionStringBuilder(builder.ConnectionString);
b.Database = null;

using var con = new NpgsqlConnection(b.ConnectionString);
con.Open();
using var cmd = GetCommand($"CREATE DATABASE \"{newDatabaseName.GetRuntimeName()}\"",con);
using var cmd = GetCommand($"CREATE DATABASE \"{newDatabaseName.GetRuntimeName()}\"", con);
cmd.CommandTimeout = CreateDatabaseTimeoutInSeconds;
cmd.ExecuteNonQuery();
}

public override Dictionary<string, string> DescribeServer(DbConnectionStringBuilder builder) => throw new NotImplementedException();

public override string? GetExplicitUsernameIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder) builder).Username;
public override string? GetExplicitUsernameIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder)builder).Username;

public override string? GetExplicitPasswordIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder) builder).Password;
public override string? GetExplicitPasswordIfAny(DbConnectionStringBuilder builder) => ((NpgsqlConnectionStringBuilder)builder).Password;

public override Version GetVersion(DiscoveredServer server)
{
Expand All @@ -59,10 +58,10 @@ public override Version GetVersion(DiscoveredServer server)
public override IEnumerable<string> ListDatabases(DbConnectionStringBuilder builder)
{
//create a copy so as not to corrupt the original

var b = new NpgsqlConnectionStringBuilder(builder.ConnectionString)
{
Database = null,
Timeout = 5
Timeout = builder.TryGetValue("Timeout", out var timeout) ? (int)timeout : 5
};

using var con = new NpgsqlConnection(b.ConnectionString);
Expand All @@ -83,7 +82,7 @@ public override IEnumerable<string> ListDatabases(DbConnection con)

public override DbDataAdapter GetDataAdapter(DbCommand cmd) => new NpgsqlDataAdapter((NpgsqlCommand)cmd);

public override DbCommandBuilder GetCommandBuilder(DbCommand cmd) => new NpgsqlCommandBuilder(new NpgsqlDataAdapter((NpgsqlCommand) cmd));
public override DbCommandBuilder GetCommandBuilder(DbCommand cmd) => new NpgsqlCommandBuilder(new NpgsqlDataAdapter((NpgsqlCommand)cmd));

public override DbParameter GetParameter(string parameterName) => new NpgsqlParameter { ParameterName = parameterName };

Expand Down
12 changes: 7 additions & 5 deletions Tests/FAnsiTests/CrossPlatformTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,13 @@ public void HorribleDatabaseAndTableNames(DatabaseType type,string horribleDatab
var database = GetTestDatabase(type);

SqlConnection.ClearAllPools();
if (type == DatabaseType.PostgreSql)
database.Server.CreateDatabase(horribleDatabaseName);

database = database.Server.ExpectDatabase(horribleDatabaseName);
database.Create(true);

if(type != DatabaseType.PostgreSql)
database.Create(true);

SqlConnection.ClearAllPools();

try
Expand Down Expand Up @@ -918,10 +921,9 @@ public void HorribleColumnNames(DatabaseType type,string horribleDatabaseName,st
AssertCanCreateDatabases();

var database = GetTestDatabase(type);

database.Server.CreateDatabase(horribleDatabaseName);
database = database.Server.ExpectDatabase(horribleDatabaseName);
database.Create(true);
Assert.That(database.GetRuntimeName(),Is.EqualTo(horribleDatabaseName).IgnoreCase);
Assert.That(database.GetRuntimeName(), Is.EqualTo(horribleDatabaseName).IgnoreCase);

try
{
Expand Down
29 changes: 18 additions & 11 deletions Tests/FAnsiTests/DatabaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace FAnsiTests;
[NonParallelizable]
public class DatabaseTests
{
protected readonly Dictionary<DatabaseType,string> TestConnectionStrings = [];
protected readonly Dictionary<DatabaseType, string> TestConnectionStrings = [];

private bool _allowDatabaseCreation;
private string _testScratchDatabase;
Expand All @@ -39,11 +39,11 @@ public void CheckFiles()

var file = Path.Combine(TestContext.CurrentContext.TestDirectory, TestFilename);

Assert.That(File.Exists(file),"Could not find " + TestFilename);
Assert.That(File.Exists(file), "Could not find " + TestFilename);

var doc = XDocument.Load(file);

var root = doc.Element("TestDatabases")??throw new Exception($"Missing element 'TestDatabases' in {TestFilename}");
var root = doc.Element("TestDatabases") ?? throw new Exception($"Missing element 'TestDatabases' in {TestFilename}");

var settings = root.Element("Settings") ??
throw new Exception($"Missing element 'Settings' in {TestFilename}");
Expand All @@ -62,13 +62,20 @@ public void CheckFiles()
{
var type = element.Element("DatabaseType")?.Value;

if(!Enum.TryParse(type, out DatabaseType databaseType))
if (!Enum.TryParse(type, out DatabaseType databaseType))
throw new Exception($"Could not parse DatabaseType {type}");


var constr = element.Element("ConnectionString")?.Value;

TestConnectionStrings.Add(databaseType,constr ?? throw new InvalidOperationException());
TestConnectionStrings.Add(databaseType, constr);

// Make sure our scratch db exists for PostgreSQL
if (databaseType == DatabaseType.PostgreSql)
{
var server = GetTestServer(DatabaseType.PostgreSql);
if (server.DiscoverDatabases().All(db => db.GetWrappedName()?.Contains(_testScratchDatabase) != true)) server.CreateDatabase(_testScratchDatabase);
}
}
}
catch (Exception exception)
Expand All @@ -85,19 +92,19 @@ protected IEnumerable<DiscoveredServer> TestServer()
}
protected DiscoveredServer GetTestServer(DatabaseType type)
{
if(!TestConnectionStrings.ContainsKey(type))
if (!TestConnectionStrings.ContainsKey(type))
Assert.Inconclusive("No connection string configured for that server");

return new DiscoveredServer(TestConnectionStrings[type], type);
}

protected DiscoveredDatabase GetTestDatabase(DatabaseType type, bool cleanDatabase=true)
protected DiscoveredDatabase GetTestDatabase(DatabaseType type, bool cleanDatabase = true)
{
var server = GetTestServer(type);
var db = server.ExpectDatabase(_testScratchDatabase);

if(!db.Exists())
if(_allowDatabaseCreation)
if (!db.Exists())
if (_allowDatabaseCreation)
db.Create();
else
Assert.Inconclusive(
Expand Down Expand Up @@ -131,7 +138,7 @@ protected DiscoveredDatabase GetTestDatabase(DatabaseType type, bool cleanDataba

protected void AssertCanCreateDatabases()
{
if(!_allowDatabaseCreation)
if (!_allowDatabaseCreation)
Assert.Inconclusive("Test cannot run when AllowDatabaseCreation is false");
}

Expand Down Expand Up @@ -165,7 +172,7 @@ protected static void AssertAreEqual(DataTable dt1, DataTable dt2)

foreach (DataRow row1 in dt1.Rows)
{
var match = dt2.Rows.Cast<DataRow>().Any(row2=> dt1.Columns.Cast<DataColumn>().All(c => AreBasicallyEquals(row1[c.ColumnName], row2[c.ColumnName])));
var match = dt2.Rows.Cast<DataRow>().Any(row2 => dt1.Columns.Cast<DataColumn>().All(c => AreBasicallyEquals(row1[c.ColumnName], row2[c.ColumnName])));
Assert.That(match, $"Couldn't find match for row:{string.Join(",", row1.ItemArray)}");
}

Expand Down
5 changes: 1 addition & 4 deletions Tests/FAnsiTests/Table/CreateIndexTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data;
using FAnsi;
using FAnsi.Discovery;
using FAnsi.Exceptions;
Expand Down
12 changes: 7 additions & 5 deletions Tests/FAnsiTests/Table/LongNamesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace FAnsiTests.Table;

internal sealed class LongNamesTests : DatabaseTests
{
[TestCaseSource(typeof(All),nameof(All.DatabaseTypes))]
[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void Test_LongTableName_CreateAndReadBack(DatabaseType dbType)
{
var db = GetTestDatabase(dbType);

var tableName = new StringBuilder(db.Server.GetQuerySyntaxHelper().MaximumTableLength).Append('a', db.Server.GetQuerySyntaxHelper().MaximumTableLength).ToString();
var columnName = new StringBuilder(db.Server.GetQuerySyntaxHelper().MaximumColumnLength).Append('b', db.Server.GetQuerySyntaxHelper().MaximumColumnLength).ToString();

var tbl = db.CreateTable(tableName,[new DatabaseColumnRequest(columnName,new DatabaseTypeRequest(typeof(string),100))]);
var tbl = db.CreateTable(tableName, [new DatabaseColumnRequest(columnName, new DatabaseTypeRequest(typeof(string), 100))]);

Assert.Multiple(() =>
{
Expand All @@ -29,7 +29,7 @@ public void Test_LongTableName_CreateAndReadBack(DatabaseType dbType)
Assert.That(col.GetRuntimeName(), Is.EqualTo(columnName).IgnoreCase);
}

[TestCaseSource(typeof(All),nameof(All.DatabaseTypes))]
[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void Test_LongDatabaseNames_CreateAndReadBack(DatabaseType dbType)
{
AssertCanCreateDatabases();
Expand All @@ -40,9 +40,11 @@ public void Test_LongDatabaseNames_CreateAndReadBack(DatabaseType dbType)

for (var i = 0; i < db.Server.GetQuerySyntaxHelper().MaximumDatabaseLength; i++)
sb.Append('a');

if (dbType == DatabaseType.PostgreSql)
db.Server.CreateDatabase(sb.ToString());
var db2 = db.Server.ExpectDatabase(sb.ToString());
db2.Create(true);
if (dbType != DatabaseType.PostgreSql)
db2.Create(true);

Assert.Multiple(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/FAnsiTests/TestDatabases-github.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
</TestDatabase>
<TestDatabase>
<DatabaseType>PostgreSql</DatabaseType>
<ConnectionString>User ID=postgres;Password=pgpass4291;Host=127.0.0.1;Port=5432</ConnectionString>
<ConnectionString>User ID=postgres;Password=pgpass4291;Host=127.0.0.1;Port=5432;Database=postgres</ConnectionString>
</TestDatabase>
</TestDatabases>

0 comments on commit 10e1bdd

Please sign in to comment.