licensed, a bit better file handling, a bit better migration handling

This commit is contained in:
Blue 2023-12-10 20:23:15 -03:00
parent 319895db64
commit 03d7614673
Signed by: blue
GPG key ID: 9B203B252A63EE38
28 changed files with 934 additions and 22 deletions

View file

@ -1,6 +1,10 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "mysql.h"
#include <fstream>
#include <iostream>
#include "mysqld_error.h"
@ -8,6 +12,8 @@
constexpr const char* updateQuery = "UPDATE system SET `value` = ? WHERE `key` = 'version'";
static const std::filesystem::path buildSQLPath = "database";
struct ResDeleter {
void operator () (MYSQL_RES* res) {
mysql_free_result(res);
@ -98,8 +104,15 @@ void MySQL::disconnect() {
mysql_init(con); //this is ridiculous!
}
void MySQL::executeFile(const std::string& path) {
void MySQL::executeFile(const std::filesystem::path& relativePath) {
MYSQL* con = &connection;
std::filesystem::path path = sharedPath() / relativePath;
if (!std::filesystem::exists(path))
throw std::runtime_error("Error executing file "
+ std::filesystem::absolute(path).string()
+ ": file doesn't exist");
std::cout << "Executing file " << path << std::endl;
std::ifstream inputFile(path);
std::string query;
while (std::getline(inputFile, query, ';')) {
@ -109,7 +122,7 @@ void MySQL::executeFile(const std::string& path) {
if (errcode == ER_EMPTY_QUERY)
continue;
throw std::runtime_error("Error executing file " + path + ": " + mysql_error(con));
throw std::runtime_error("Error executing file " + path.string() + ": " + mysql_error(con));
}
}
@ -144,3 +157,20 @@ void MySQL::setVersion(uint8_t version) {
statement.bind(strVersion.data(), MYSQL_TYPE_VAR_STRING);
statement.execute();
}
void MySQL::migrate(uint8_t targetVersion) {
uint8_t currentVersion = getVersion();
while (currentVersion < targetVersion) {
std::string fileName = "migrations/m" + std::to_string(currentVersion) + ".sql";
std::cout << "Performing migration "
<< std::to_string(currentVersion)
<< " -> "
<< std::to_string(++currentVersion)
<< std::endl;
executeFile(fileName);
setVersion(currentVersion);
}
std::cout << "Database is now on actual version " << std::to_string(targetVersion) << std::endl;
}

View file

@ -1,10 +1,15 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <stdexcept>
#include <filesystem>
#include <mysql.h>
#include "database/dbinterface.h"
#include "utils/helpers.h"
class MySQL : public DBInterface {
class Statement;
@ -17,10 +22,13 @@ public:
void setCredentials(const std::string& login, const std::string& password) override;
void setDatabase(const std::string& database) override;
void executeFile(const std::string& path) override;
void migrate(uint8_t targetVersion) override;
uint8_t getVersion() override;
void setVersion(uint8_t version) override;
private:
void executeFile(const std::filesystem::path& relativePath);
protected:
MYSQL connection;
std::string login;

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "statement.h"
#include <cstring>

View file

@ -1,3 +1,6 @@
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <vector>