BirthdayBot/Data/Database.cs

32 lines
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-07-16 18:55:26 +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);
await db.OpenAsync();
return db;
}
2020-07-16 18:55:26 +00:00
public static async Task DoInitialDatabaseSetupAsync()
{
2020-07-16 18:55:26 +00:00
using var db = await OpenConnectionAsync();
// Refer to the methods being called for information on how the database is set up.
await GuildConfiguration.DatabaseSetupAsync(db); // Note: Call this first. (Foreign reference constraints.)
await GuildUserConfiguration.DatabaseSetupAsync(db);
}
}
}