18#ifdef ORBIT_ENABLE_POSTGRES
20#if defined(__APPLE__) || defined(__FreeBSD__)
28#error "orm::MigrationRunner needs PostgreSQL support, which this build of Orbit leaves out (ORBIT_ENABLE_POSTGRES=OFF)"
61 if (
applied.empty())
return "Database is up to date";
62 return "Successfully applied " + std::to_string(
applied.size()) +
" migrations.";
66template <
typename DbClient>
84 auto lock =
co_await lock_query;
86 result.
error =
"Could not take the migration lock: " + lock.error();
92 "CREATE TABLE IF NOT EXISTS orbit_migrations ("
93 "id SERIAL PRIMARY KEY, "
94 "version VARCHAR(255) UNIQUE NOT NULL, "
95 "applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);");
96 auto created =
co_await create_query;
97 if (!created.ok()) { result.
error =
"Could not create orbit_migrations: " + created.error();
break; }
99 auto applied_query =
database::query_async(db,
"SELECT version FROM orbit_migrations ORDER BY version ASC;");
101 if (!applied_res.
ok()) { result.
error =
"Could not read orbit_migrations: " + applied_res.
error();
break; }
103 std::vector<std::string> applied_versions;
104 for (
size_t i = 0; i < applied_res.
size(); ++i) {
105 applied_versions.push_back(applied_res[i].get(0).value_or(
""));
108 if (!std::filesystem::exists(migrations_dir)) {
109 LOG_INFO(
"[Migrations] Directory '" << migrations_dir <<
"' not found. Skipping migrations.");
114 std::vector<std::string> pending_files;
115 for (
const auto& entry : std::filesystem::directory_iterator(migrations_dir)) {
116 if (entry.path().extension() ==
".sql") {
117 pending_files.push_back(entry.path().string());
120 std::sort(pending_files.begin(), pending_files.end());
122 for (
const auto& filepath : pending_files) {
123 std::string filename = std::filesystem::path(filepath).filename().string();
124 if (std::find(applied_versions.begin(), applied_versions.end(), filename) != applied_versions.end()) {
128 std::ifstream ifs(filepath);
129 if (!ifs.is_open()) { result.
error =
"Failed to open " + filepath;
break; }
130 std::string sql((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
132 LOG_INFO(
"[Migrations] Applying " << filename <<
"...");
134 auto begin =
co_await begin_query;
135 if (!begin.ok()) { result.
error =
"BEGIN failed: " + begin.error();
break; }
138 auto ran =
co_await run_query;
141 std::vector<std::optional<std::string>> tracking_params;
142 tracking_params.emplace_back(filename);
143 auto track_query =
database::query_async(db,
"INSERT INTO orbit_migrations (version) VALUES ($1);", tracking_params);
144 track =
co_await track_query;
146 if (!ran.ok() || !track.
ok()) {
148 co_await rollback_query;
149 result.
error = filename +
" failed and was rolled back: " + (ran.ok() ? track.
error() : ran.error());
154 auto commit =
co_await commit_query;
155 if (!commit.ok()) { result.
error =
"COMMIT of " + filename +
" failed: " + commit.error();
break; }
156 result.
applied.push_back(filename);
161 co_await unlock_query;
171 auto running =
run(std::move(db), std::move(migrations_dir));
190 std::chrono::milliseconds timeout = std::chrono::minutes(5)) {
191#if defined(__APPLE__) || defined(__FreeBSD__)
192 network::KqueueProactor proactor;
194 network::IocpProactor proactor;
198 auto db = std::make_shared<database::PostgresClient>(&proactor, conninfo);
199 auto done = std::make_shared<std::optional<MigrationResult>>();
200 [](std::shared_ptr<database::PostgresClient> client, std::string dir,
203 if (!
co_await connecting) {
205 failed.
error =
"Could not connect to the database";
206 *out = std::move(failed);
210 *out =
co_await running;
211 }(db, migrations_dir, done);
213 const auto deadline = std::chrono::steady_clock::now() + timeout;
214 while (!*done && std::chrono::steady_clock::now() < deadline) proactor.run_once(50);
215 if (*done)
return std::move(**done);
219 timed_out.
error =
"Migrations did not finish within the timeout";
#define LOG_ERROR(msg)
Definition Logger.hpp:72
#define LOG_INFO(msg)
Definition Logger.hpp:70
A lazy coroutine that produces a T for whoever co_awaits it.
Definition Awaitable.hpp:77
Represents a generic, unified result set from a database query.
Definition ResultSet.hpp:197
const std::string & error() const
Definition ResultSet.hpp:213
bool ok() const
False if the query failed; error() then says why.
Definition ResultSet.hpp:212
size_t size() const
Definition ResultSet.hpp:216
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:107
Definition EpollProactor.hpp:11
Definition MigrationRunner.hpp:67
static concurrency::Awaitable< MigrationResult > run(std::shared_ptr< DbClient > db, std::string migrations_dir)
Applies the pending migrations in migrations_dir over a connected client, with no HTTP involved: for ...
Definition MigrationRunner.hpp:79
static constexpr long long kAdvisoryLockKey
Definition MigrationRunner.hpp:69
static concurrency::Task run_migrations(std::shared_ptr< DbClient > db, std::string migrations_dir, std::shared_ptr< http::ResponseWriter > res)
run(), answered over HTTP: 200 with the summary, or a 500 whose details go to the log only.
Definition MigrationRunner.hpp:170
ConnectAwaiter connect_async(std::shared_ptr< PostgresClient > client)
Creates an awaiter for connecting a PostgresClient.
Definition PostgresCoro.hpp:35
QueryAwaiter query_async(std::shared_ptr< PostgresClient > client, const std::string &sql)
Creates an awaiter for executing a query on a PostgresClient.
Definition PostgresCoro.hpp:75
Definition legacy_namespaces.hpp:23
MigrationResult migrate_sync(const std::string &conninfo, const std::string &migrations_dir, std::chrono::milliseconds timeout=std::chrono::minutes(5))
Connects to PostgreSQL and applies the pending migrations in migrations_dir, blocking until done: for...
Definition MigrationRunner.hpp:189
Fire-and-forget coroutine type, used as the return type of coroutine route handlers.
Definition Task.hpp:36
Database migration runner (PostgreSQL)
Definition MigrationRunner.hpp:49
bool ok() const
Definition MigrationRunner.hpp:54
std::string summary() const
Definition MigrationRunner.hpp:58
std::vector< std::string > applied
Files applied by this run, in order.
Definition MigrationRunner.hpp:50
bool directory_missing
The migrations directory does not exist (not an error).
Definition MigrationRunner.hpp:52
std::string error
Why the run stopped; empty on success.
Definition MigrationRunner.hpp:51