Lightweight 0.20260303.0
Loading...
Searching...
No Matches
CppEmitter.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../Api.hpp"
6#include "Folder.hpp"
7
8#include <cstddef>
9#include <filesystem>
10#include <string>
11#include <string_view>
12
13namespace Lightweight::MigrationFold
14{
15
16/// @brief Configuration for `EmitCppBaseline`.
18{
19 /// Output `.cpp` file. May be a `<stem>.cpp` — the writer will split into
20 /// `<stem>_part01.cpp`, `<stem>_part02.cpp`, ... when needed.
21 std::filesystem::path outputPath;
22 /// Threshold for splitting the body across multiple `.cpp` files. Zero
23 /// disables splitting and emits a single file.
24 std::size_t maxLinesPerFile = 5000;
25 /// Optional plugin name. When non-empty (and `--emit-cmake` is requested by
26 /// the caller) `EmitPluginCmake` is invoked alongside the source emission.
27 std::string pluginName;
28 /// When true, emit `CMakeLists.txt` + `Plugin.cpp` next to the generated
29 /// sources so the directory becomes a drop-in plugin.
30 bool emitCmake = false;
31};
32
33/// @brief Emits a self-contained baseline migration as one `LIGHTWEIGHT_SQL_MIGRATION`
34/// body that reproduces the post-fold schema and data state.
35///
36/// The emitted code includes:
37/// 1. A runtime guard inside `Up()` that throws if `schema_migrations` already has
38/// rows (the operator must run `dbtool hard-reset` or `mark-applied` first).
39/// 2. One `plan.CreateTable(...)` call per surviving table, with all columns and
40/// composite FKs reproduced from the fold.
41/// 3. One `plan.CreateIndex(...)` call per surviving index.
42/// 4. Every data step rendered as the equivalent DSL builder call (Insert / Update /
43/// Delete / RawSql), grouped by source migration via header comments.
44/// 5. `LIGHTWEIGHT_SQL_RELEASE(...)` markers for releases inside the fold range.
45///
46/// When `options.maxLinesPerFile > 0` and the body would exceed that budget, the body
47/// is split into `<stem>_partNN.cpp` companion files using the shared `SplitFileWriter`.
48LIGHTWEIGHT_API void EmitCppBaseline(FoldResult const& fold, CppEmitOptions const& options);
49
50} // namespace Lightweight::MigrationFold
Configuration for EmitCppBaseline.
Snapshot of the schema the registered migrations intend to produce.