diff --git a/src/database/database.cpp b/src/database/database.cpp index 1de600a..29684b0 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #include "database.h" using namespace DB; @@ -23,3 +24,8 @@ using namespace DB; Database::Database() {} +bool Database::try_migration() +{ + std::cerr << "Database::try_migration()" << std::endl; + return false; +} diff --git a/src/database/database.h b/src/database/database.h index 506309d..0f2aac5 100644 --- a/src/database/database.h +++ b/src/database/database.h @@ -27,6 +27,13 @@ namespace DB explicit Database(); virtual ~Database() = default; + /** + * @brief Attempts to migrate the database + * @ + * @return True unless no migration was done + */ + virtual bool try_migration(); + enum class Type { SQLite, diff --git a/src/database/sqlite/sqlite.cpp b/src/database/sqlite/sqlite.cpp index 0d5a429..004de41 100644 --- a/src/database/sqlite/sqlite.cpp +++ b/src/database/sqlite/sqlite.cpp @@ -43,3 +43,65 @@ SQLite::~SQLite() { sqlite3_close(db); } + +// Note: Please refer to SQLite docs on types, many of these are aliases to others, such as BOOLEAN. +// It's still recommended to use them for clarity +bool SQLite::try_migration() +{ + const char* create_tables[] = { + R"sql( +CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY ASC, + email TEXT, + local BOOLEAN, + ap_id TEXT, + ap_follower_addr TEXT, + ap_following_addr TEXT, + password_hash TEXT, + acct TEXT, + nickname TEXT, + bio TEXT, + created_at DATETIME, + updated_at DATETIME, + avatar TEXT, + key TEXT, + banner TEXT, + background TEXT, + statuses_count INTEGER, + follower_count INTEGER, + following_count INTEGER, + last_status TEXT, + is_suggested BOOLEAN DEFAULT FALSE, + is_approved BOOLEAN DEFAULT FALSE, + birthday TEXT, + show_birthday BOOLEAN DEFAULT FALSE, + janny_level INTEGER, + follow_move BOOLEAN DEFAULT FALSE +) +)sql", + + R"sql( +CREATE TABLE IF NOT EXISTS user_activities ( + id INTEGER PRIMARY KEY ASC, + type INTEGER, + user INTEGER, + usee INTEGER, + activity_finish DATETIME, + FOREIGN KEY(user) REFERENCES users(id), + FOREIGN KEY(usee) REFERENCES users(id) +) +)sql", + }; + + char* err; + for (const char* sql: create_tables) + { + if (sqlite3_exec(db, sql, nullptr, nullptr, &err)) + { + Logger::instance().log(err, Logger::Level::ERROR); + throw std::runtime_error(err); + } + } + return false; +} + diff --git a/src/database/sqlite/sqlite.h b/src/database/sqlite/sqlite.h index b774b08..b587bdd 100644 --- a/src/database/sqlite/sqlite.h +++ b/src/database/sqlite/sqlite.h @@ -31,7 +31,7 @@ namespace DB explicit SQLite(std::filesystem::path path); virtual ~SQLite(); - + virtual bool try_migration() override; private: std::filesystem::path path; struct SQLiteDeleter { void operator()(sqlite3* db) { sqlite3_close(db); } }; diff --git a/src/main.cpp b/src/main.cpp index 80528e5..a63be6f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,6 +68,8 @@ int start_wormhole() std::shared_ptr server = std::make_shared(Config::instance().config.http.port, std::make_any(args)); + database->try_migration(); + // TODO Move to another file server->map_routes({ { {HTTP::Request::Type::GET, "/test"}, [](std::any& args, const HTTP::RequestArgs_t& arg){