aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2023-04-06 15:45:39 -0400
committerCara Salter <cara@devcara.com>2023-04-06 17:11:52 -0400
commit7629de5b888bd3d1cdb94dce3ba51cb1e1c2e625 (patch)
tree0dd37a701fb242fa953c23f2b0a94252fa60762b /src
parentb66bf922cc2d8eb3a623d1f776ab6a144832f587 (diff)
download142bot-7629de5b888bd3d1cdb94dce3ba51cb1e1c2e625.tar.gz
142bot-7629de5b888bd3d1cdb94dce3ba51cb1e1c2e625.zip
pg: Add postgres support
Supports migrations Change-Id: Ifca8358d4f57a4b417f1d972e400e98767e01bb5
Diffstat (limited to 'src')
-rw-r--r--src/bot.cpp51
-rw-r--r--src/db.cpp76
-rw-r--r--src/main.cpp2
3 files changed, 128 insertions, 1 deletions
diff --git a/src/bot.cpp b/src/bot.cpp
index 03bea02..ad818a0 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -1,21 +1,72 @@
#include <dpp/snowflake.h>
#include <stdlib.h>
+#include <string>
#include <142bot/bot.hpp>
#include <dpp/dpp.h>
#include <142bot/modules.hpp>
#include <142bot/util.hpp>
#include <fmt/format.h>
#include <fmt/format-inl.h>
+#include <pqxx/pqxx>
+#include <142bot/db.hpp>
+#include <filesystem>
+
+namespace fs = std::filesystem;
Bot::Bot(bool devel, dpp::cluster* cluster) {
dev = devel;
this->core = cluster;
+
+ std::ifstream f("config.json");
+ json cfg = json::parse(f);
+ std::string token = cfg.value("token", "bad-token");
+
+ this->conn = db::connect(cfg["postgres"]["host"], cfg["postgres"]["user"], cfg["postgres"]["pass"], cfg["postgres"]["database"], cfg["postgres"]["port"]);
+
+ run_database_migrations();
+
this->loader = new ModuleLoader(this);
this->loader->load_all();
// this->loader->LoadAll();
}
+/**
+ * Attempts to run migrations stored in a directory local to CWD.
+ *
+ * Returns false on any error
+*/
+bool Bot::run_database_migrations() {
+
+ // Start a transaction
+ this->core->log(dpp::ll_info, "Attempting database migrations...");
+ pqxx::work w(this->conn);
+ // Get all files in ./migrations
+ std::string path = "./migrations";
+ for (const auto &entry : fs::directory_iterator(path)) {
+ std::ifstream f(entry.path());
+ std::ostringstream sstr;
+ sstr << f.rdbuf();
+ std::string stmt = sstr.str();
+
+ this->core->log(dpp::ll_debug, fmt::format("Attempting to migrate database with statement {}", stmt));
+
+ try {
+ w.exec0(stmt);
+ } catch (std::exception &e) {
+ w.abort();
+ this->core->log(dpp::ll_error, e.what());
+ return false;
+ }
+
+ }
+
+ w.commit();
+ this->core->log(dpp::ll_info, "Done.");
+
+ return true;
+}
+
bool Bot::isDevMode() {
return dev;
}
diff --git a/src/db.cpp b/src/db.cpp
new file mode 100644
index 0000000..92e3d1d
--- /dev/null
+++ b/src/db.cpp
@@ -0,0 +1,76 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: db.cpp
+ *
+ * Description: Connection to a postgres database
+ *
+ * Version: 1.0
+ * Created: 04/06/2023 11:40:39 AM
+ * Revision: none
+ * Compiler: gcc
+ *
+ * Author: Cara Salter (muirrum), cara@devcara.com
+ * Organization: Worcester Polytechnic Institute
+ *
+ * =====================================================================================
+ */
+#include <fmt/core.h>
+#include <mutex>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+#include <142bot/db.hpp>
+#include <pqxx/pqxx>
+#include <fmt/format.h>
+#include <cstdarg>
+
+using namespace std;
+
+namespace db {
+ std::mutex db_mutex;
+ std::string _error;
+
+ /**
+ * Connects to a postgres database, returns false if error
+ **/
+ pqxx::connection connect(const std::string &host, const std::string &user, const std::string &pass, const std::string &db, int port) {
+ std::lock_guard<std::mutex> db_lock(db_mutex);
+
+ std::string cn_s = "postgresql://";
+
+ if (!user.empty()) {
+ cn_s = cn_s + user;
+ }
+ if (!pass.empty() && !user.empty()) {
+ cn_s = cn_s + ":" + pass;
+ }
+
+ if ((!user.empty() || !pass.empty())) {
+ cn_s = cn_s + "@";
+ }
+
+ if (!host.empty()) {
+ cn_s = cn_s + "localhost";
+ }
+ if (port != 0 && !host.empty()) {
+ cn_s = cn_s + ":" + std::to_string(port);
+ }
+ if (!db.empty()) {
+ cn_s = cn_s + "/" + db;
+ }
+
+ try {
+ pqxx::connection c{cn_s};
+ return c;
+ } catch (std::exception const &e) {
+ _error = e.what();
+ throw e;
+ }
+ }
+
+ const std::string& error() {
+ return _error;
+ }
+
+}
diff --git a/src/main.cpp b/src/main.cpp
index 8f3ad9a..f54e418 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,7 @@
#include <dpp/dpp.h>
#include <dpp/json.h>
#include <142bot/bot.hpp>
+#include <142bot/db.hpp>
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
@@ -17,7 +18,6 @@ int main(int argc, char const *argv[]) {
std::ifstream f("config.json");
json cfg = json::parse(f);
string token = cfg.value("token", "bad-token");
-
dpp::cluster bot(token, dpp::intents::i_all_intents);
std::shared_ptr<spdlog::logger> log;