BirthdayBot/Data/Database.cs

33 lines
1.1 KiB
C#
Raw Normal View History

using Npgsql;
2020-07-16 18:55:26 +00:00
using System;
using System.Threading.Tasks;
namespace BirthdayBot.Data
{
/// <summary>
2020-07-16 18:55:26 +00:00
/// Database access and some abstractions.
/// </summary>
2020-07-16 18:55:26 +00:00
internal static class Database
{
2020-10-05 22:52:29 +00:00
public static string DBConnectionString { get; set; }
2020-07-16 18:55:26 +00:00
public static async Task<NpgsqlConnection> OpenConnectionAsync()
{
2020-07-16 18:55:26 +00:00
if (DBConnectionString == null) throw new Exception("Database connection string not set");
var db = new NpgsqlConnection(DBConnectionString);
2020-10-10 07:28:11 +00:00
await db.OpenAsync().ConfigureAwait(false);
return db;
}
2020-07-16 18:55:26 +00:00
public static async Task DoInitialDatabaseSetupAsync()
{
2020-10-10 07:28:11 +00:00
using var db = await OpenConnectionAsync().ConfigureAwait(false);
2020-07-16 18:55:26 +00:00
// Refer to the methods being called for information on how the database is set up.
2020-10-10 07:28:11 +00:00
// Note: The order these are called is important. (Foreign reference constraints.)
await GuildConfiguration.DatabaseSetupAsync(db).ConfigureAwait(false);
await GuildUserConfiguration.DatabaseSetupAsync(db).ConfigureAwait(false);
}
}
}