Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Apr 20, 2024
1 parent 7f18737 commit 1727694
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public class PostgreSqlGrateTestContext(
public override SqlStatements Sql => new()
{
SelectVersion = "SELECT version()",
SleepTwoSeconds = "SELECT pg_sleep(2);"
SleepTwoSeconds = "SELECT pg_sleep(2);",
CreateUser = (_, user, password) => $"CREATE USER {user} WITH PASSWORD '{password}';",
GrantAccess = (db, user) =>
$"""
GRANT CONNECT ON DATABASE "{db}" TO {user};
-- GRANT pg_read_all_data ON DATABASE "{db}" TO {user};
-- GRANT pg_write_all_data ON DATABASE "{db}" TO {user};
"""
};

public override string ExpectedVersionPrefix => "PostgreSQL 1";
Expand Down
45 changes: 33 additions & 12 deletions unittests/TestCommon/Generic/GenericDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public virtual async Task Is_up_and_running_with_appropriate_database_version()
[Fact]
public async Task Is_created_if_confed_and_it_does_not_exist()
{
var db = "NEWDATABASE";
//var db = "NEWDATABASE";
var db = TestConfig.RandomDatabase();

await using (var migrator = GetMigrator(GetConfiguration(db, true)))
{
Expand Down Expand Up @@ -83,9 +84,11 @@ public virtual async Task Is_created_with_custom_script_if_custom_create_databas
[Fact]
public async Task Is_not_created_if_not_confed()
{
var db = "SOMEOTHERDATABASE";
//var db = "SOMEOTHERDATABASE";
var db = TestConfig.RandomDatabase();

IEnumerable<string> databasesBeforeMigration = await GetDatabases();

databasesBeforeMigration.Should().NotContain(db);

await using (var migrator = GetMigrator(GetConfiguration(db, false)))
Expand All @@ -105,7 +108,7 @@ public async Task Is_not_created_if_not_confed()
[Fact]
public async Task Does_not_error_if_configured_to_create_but_already_exists()
{
var db = "DAATAA";
var db = TestConfig.RandomDatabase();

// Create the database manually before running the migration
await CreateDatabaseFromConnectionString(db, Context.UserConnectionString(db));
Expand All @@ -128,7 +131,8 @@ public async Task Does_not_error_if_configured_to_create_but_already_exists()
[InlineData(null)]
public async Task Does_not_need_admin_connection_if_database_already_exists(string? adminConnectionString)
{
var db = "DATADATBADATABASE";
//var db = "DATADATBADATABASE";
var db = TestConfig.RandomDatabase();

// Create the database manually before running the migration
await CreateDatabaseFromConnectionString(db, Context.UserConnectionString(db));
Expand Down Expand Up @@ -162,25 +166,37 @@ protected virtual async Task CreateDatabaseFromConnectionString(string db, strin
{
using var conn = Context.CreateAdminDbConnection();

string? commandText = null;
try
{
var commandText = Context.Syntax.CreateDatabase(db, pwd);
commandText = Context.Syntax.CreateDatabase(db, pwd);
await conn.ExecuteAsync(commandText);
}
catch (DbException)
catch (DbException dbe)
{
TestOutput.WriteLine("Got error when creating database: " + dbe.Message);
TestOutput.WriteLine("database: " + db);
TestOutput.WriteLine("admin connection string: " + conn.ConnectionString);
TestOutput.WriteLine("user connection string: " + connectionString);
TestOutput.WriteLine("commandText: " + commandText);
}


string? createUserSql = null;
try
{
var createUserSql = Context.Sql.CreateUser(db, uid, pwd);
createUserSql = Context.Sql.CreateUser(db, uid, pwd);
if (createUserSql is not null)
{
await conn.ExecuteAsync(createUserSql);
}
}
catch (DbException)
catch (DbException dbe)
{
TestOutput.WriteLine("Got error when creating user: " + dbe.Message);
TestOutput.WriteLine("Error creating user: " + uid + " for database: " + db);
TestOutput.WriteLine("admin connection string: " + conn.ConnectionString);
TestOutput.WriteLine("user connection string: " + connectionString);
TestOutput.WriteLine("createUserSql: " + createUserSql);
}

var grantAccessSql = Context.Sql.GrantAccess(db, uid);
Expand All @@ -191,8 +207,9 @@ protected virtual async Task CreateDatabaseFromConnectionString(string db, strin

break;
}
catch (DbException)
catch (DbException dbe)
{
TestOutput.WriteLine($"Got error in loop, iteration: {i}: {dbe.Message}");
}

}
Expand All @@ -208,13 +225,17 @@ protected virtual async Task<IEnumerable<string>> GetDatabases()
{
for (var i = 0; i < 5; i++)
{
using var conn = Context.CreateAdminDbConnection();
try
{
using var conn = Context.CreateAdminDbConnection();
databases = (await conn.QueryAsync<string>(sql)).ToArray();
break;
}
catch (DbException) { }
catch (DbException dbe)
{
TestOutput.WriteLine("Got error when listing databases: " + dbe.Message);
TestOutput.WriteLine("admin connection string: " + conn.ConnectionString);
}
}
}
return databases.ToArray();
Expand Down

0 comments on commit 1727694

Please sign in to comment.