SQLite migration

FossilOrigin-Name: 70676412a922280b2c445ada1839340176d0a15adb0313a0383e84d5db395231
This commit is contained in:
nekobit 2022-10-03 04:04:10 +00:00
parent 04f9710a6a
commit 6762e27c80
5 changed files with 78 additions and 1 deletions

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <iostream>
#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;
}

View file

@ -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,

View file

@ -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;
}

View file

@ -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); } };

View file

@ -68,6 +68,8 @@ int start_wormhole()
std::shared_ptr<HTTP::Server> server =
std::make_shared<HTTP::MicroHttpdServer>(Config::instance().config.http.port, std::make_any<RouteArgs>(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){