#include "store/migrations.hpp" #include // Generated at build time from store/migrations/*.sql by embed_migrations.cmake. #include "migrations_embedded.hpp" namespace velox::daemon::store { std::span embedded_migrations() { return {kEmbeddedMigrations.data(), kEmbeddedMigrations.size()}; } DbResult migrate_to_head(Db& db) { const auto all = embedded_migrations(); MigrationOutcome out; out.from_version = db.user_version(); out.to_version = out.from_version; if (out.from_version < 0) { return std::unexpected(DbError{0, "could not read PRAGMA user_version"}); } for (const auto& m : all) { if (m.version <= out.from_version) continue; // Each migration is one transaction: a failure half-way leaves user_version and // the schema exactly where they were. auto r = db.transaction([&]() -> DbResult { if (auto e = db.exec(m.sql); !e) return e; return db.set_user_version(m.version); }); if (!r) { return std::unexpected(DbError{r.error().code, "migration " + std::string(m.name) + " failed: " + r.error().message}); } out.to_version = m.version; ++out.applied; } return out; } } // namespace velox::daemon::store