2020-04-02 18:27:55 +00:00
|
|
|
|
using Npgsql;
|
2020-07-16 18:55:26 +00:00
|
|
|
|
using System;
|
2020-04-02 18:27:55 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BirthdayBot.Data
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-07-16 18:55:26 +00:00
|
|
|
|
/// Database access and some abstractions.
|
2020-04-02 18:27:55 +00:00
|
|
|
|
/// </summary>
|
2020-07-16 18:55:26 +00:00
|
|
|
|
internal static class Database
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
2020-07-16 18:55:26 +00:00
|
|
|
|
public static string DBConnectionString { get; set; }
|
2020-04-02 18:27:55 +00:00
|
|
|
|
|
2020-07-16 18:55:26 +00:00
|
|
|
|
public static async Task<NpgsqlConnection> OpenConnectionAsync()
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
2020-07-16 18:55:26 +00:00
|
|
|
|
if (DBConnectionString == null) throw new Exception("Database connection string not set");
|
2020-04-02 18:27:55 +00:00
|
|
|
|
var db = new NpgsqlConnection(DBConnectionString);
|
|
|
|
|
await db.OpenAsync();
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 18:55:26 +00:00
|
|
|
|
public static async Task DoInitialDatabaseSetupAsync()
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
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);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|