Lightweight 0.20260303.0
Loading...
Searching...
No Matches
SqlEmitter.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../Api.hpp"
6#include "Folder.hpp"
7
8#include <filesystem>
9
10namespace Lightweight
11{
12class SqlQueryFormatter;
13}
14
15namespace Lightweight::MigrationFold
16{
17
18/// @brief Configuration for `EmitSqlBaseline`.
20{
21 /// Output `.sql` file.
22 std::filesystem::path outputPath;
23 /// Required dialect — the formatter that drives all `ToSql(...)` rendering.
24 /// `EmitSqlBaseline` itself never opens a connection; the dialect determines
25 /// the emitted SQL flavour.
26 SqlQueryFormatter const* formatter = nullptr;
27 /// Human-readable dialect label included in the file's header comment so the
28 /// artifact is self-describing.
29 std::string_view dialectLabel;
30};
31
32/// @brief Emits a flat `.sql` baseline that reproduces the post-fold schema and data.
33///
34/// The emitted file:
35/// 1. Header comment naming the dialect (`-- Dialect: PostgreSQL`).
36/// 2. For each table in fold creation order: a `CREATE TABLE` rendered via
37/// `ToSql(formatter, ...)`.
38/// 3. For each surviving index: a `CREATE INDEX` rendered via `ToSql(...)`.
39/// 4. Every data step rendered via `ToSql(...)` (INSERT / UPDATE / DELETE / RawSql).
40/// 5. A trailing `INSERT INTO schema_migrations (...) VALUES ...` for every
41/// fold-input timestamp so the post-fold DB looks identical to a real apply-all.
42///
43/// The emitted SQL is dialect-specific to the chosen `formatter`. There is no
44/// runtime guard against applying it to a non-empty `schema_migrations` — a flat
45/// SQL script is the operator's tool, and the trailing inserts will fail loudly
46/// with a primary-key conflict in that situation.
47LIGHTWEIGHT_API void EmitSqlBaseline(FoldResult const& fold, SqlEmitOptions const& options);
48
49} // namespace Lightweight::MigrationFold
API to format SQL queries for different SQL dialects.
Configuration for EmitSqlBaseline.
std::filesystem::path outputPath
Output .sql file.
Snapshot of the schema the registered migrations intend to produce.