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-10-05 22:52:29 +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);
|
2020-10-10 07:28:11 +00:00
|
|
|
|
await db.OpenAsync().ConfigureAwait(false);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 18:55:26 +00:00
|
|
|
|
public static async Task DoInitialDatabaseSetupAsync()
|
2020-04-02 18:27:55 +00:00
|
|
|
|
{
|
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);
|
2020-04-02 18:27:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|