|
| 1 | +// Copyright (C) 2025 Jérôme "SirLynix" Leclercq (lynix680@gmail.com) |
| 2 | +// This file is part of the "This Space Of Mine" project |
| 3 | +// For conditions of distribution and use, see copyright notice in LICENSE |
| 4 | + |
| 5 | +#include <ServerLib/Database/ServerDatabase.hpp> |
| 6 | +#include <Nazara/Core/ApplicationBase.hpp> |
| 7 | +#include <Nazara/Core/FilesystemAppComponent.hpp> |
| 8 | +#include <fmt/color.h> |
| 9 | +#include <tsl/hopscotch_set.h> |
| 10 | +#include <SQLiteCpp/Transaction.h> |
| 11 | + |
| 12 | +namespace tsom |
| 13 | +{ |
| 14 | + ServerDatabase::ServerDatabase(Nz::ApplicationBase& app, const std::string& filename) : |
| 15 | + m_database(filename, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE), |
| 16 | + m_app(app) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + std::vector<Database::Planet> ServerDatabase::GetAllPlanets() const |
| 21 | + { |
| 22 | + std::vector<Database::Planet> planets; |
| 23 | + |
| 24 | + SQLite::Statement query(m_database, "SELECT id, generator, seed, chunk_count_x, chunk_count_y, chunk_count_z, corner_radius, gravity FROM planets"); |
| 25 | + while (query.executeStep()) |
| 26 | + { |
| 27 | + auto& planet = planets.emplace_back(); |
| 28 | + planet.id = query.getColumn(0); |
| 29 | + planet.generatorName = query.getColumn(1).getString(); |
| 30 | + planet.seed = query.getColumn(2); |
| 31 | + planet.chunkCount.x = query.getColumn(3); |
| 32 | + planet.chunkCount.y = query.getColumn(4); |
| 33 | + planet.chunkCount.z = query.getColumn(5); |
| 34 | + planet.cornerRadius = Nz::SafeCaster(query.getColumn(6).getDouble()); |
| 35 | + planet.gravity = Nz::SafeCaster(query.getColumn(7).getDouble()); |
| 36 | + } |
| 37 | + |
| 38 | + return planets; |
| 39 | + } |
| 40 | + |
| 41 | + void ServerDatabase::Migrate() |
| 42 | + { |
| 43 | + tsl::hopscotch_set<std::string, std::hash<std::string_view>, std::equal_to<>> migrated; |
| 44 | + if (m_database.tableExists("migration")) |
| 45 | + { |
| 46 | + SQLite::Statement query(m_database, "SELECT name FROM migration"); |
| 47 | + while (query.executeStep()) |
| 48 | + migrated.insert(query.getColumn(0)); |
| 49 | + } |
| 50 | + |
| 51 | + std::vector<std::string> remainingMigrationScripts; |
| 52 | + |
| 53 | + auto& fs = m_app.GetComponent<Nz::FilesystemAppComponent>(); |
| 54 | + fs.IterateOnDirectory("database/server", [&](std::string_view entryName, Nz::VirtualDirectory::Entry entry) |
| 55 | + { |
| 56 | + if (!std::holds_alternative<Nz::VirtualDirectory::FileEntry>(entry)) |
| 57 | + return; //< not a file |
| 58 | + |
| 59 | + if (!Nz::EndsWith(entryName, ".sql")) |
| 60 | + return; //< not a migration script |
| 61 | + |
| 62 | + if (!migrated.contains(entryName)) |
| 63 | + remainingMigrationScripts.emplace_back(entryName); |
| 64 | + }); |
| 65 | + |
| 66 | + if (remainingMigrationScripts.empty()) |
| 67 | + return; |
| 68 | + |
| 69 | + std::sort(remainingMigrationScripts.begin(), remainingMigrationScripts.end()); |
| 70 | + |
| 71 | + SQLite::Transaction transaction(m_database); |
| 72 | + |
| 73 | + for (const std::string& migrationScript : remainingMigrationScripts) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + bool success = fs.GetFileContent(fmt::format("database/server/{0}", migrationScript), [&](const void* data, Nz::UInt64 size) |
| 78 | + { |
| 79 | + std::string scriptContent(static_cast<const char*>(data), Nz::SafeCast<std::size_t>(size)); |
| 80 | + m_database.exec(scriptContent); |
| 81 | + }); |
| 82 | + |
| 83 | + // We have to create the statement after the first migration script has been executed |
| 84 | + SQLite::Statement insertTransaction(m_database, "INSERT INTO migration(name) VALUES(?)"); |
| 85 | + insertTransaction.bindNoCopy(1, migrationScript.c_str()); |
| 86 | + insertTransaction.exec(); |
| 87 | + |
| 88 | + if (!success) |
| 89 | + throw std::runtime_error("failed to read"); |
| 90 | + } |
| 91 | + catch (const std::exception& e) |
| 92 | + { |
| 93 | + fmt::print(fg(fmt::color::red), "[Database Migration] failed to execute migration script {0}: {1}", migrationScript, e.what()); |
| 94 | + transaction.rollback(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + transaction.commit(); |
| 99 | + } |
| 100 | +} |
0 commit comments