Lightweight 0.20251202.0
Loading...
Searching...
No Matches
Backup.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5#include "../SqlConnection.hpp"
6#include "../SqlSchema.hpp"
7#include "../ThreadSafeQueue.hpp"
8#include "SqlBackup.hpp"
9
10#include <map>
11#include <mutex>
12#include <string>
13
14#if defined(__clang__)
15 #pragma clang diagnostic push
16 #pragma clang diagnostic ignored "-Wnullability-extension"
17#endif
18#include <zip.h>
19#if defined(__clang__)
20 #pragma clang diagnostic pop
21#endif
22
23namespace Lightweight::SqlBackup::detail
24{
25
26/// Context for backup operations, shared between backup workers.
27struct BackupContext
28{
29 zip_t* zip;
30 std::mutex& zipMutex;
31 ProgressManager& progress;
32 SqlConnectionString const& connectionString;
33 std::string const& schema;
34 std::map<std::string, std::string>* checksums; // entryName -> SHA-256 hash
35 std::mutex* checksumMutex;
36 RetrySettings const& retrySettings;
37 BackupSettings const& backupSettings;
38};
39
40/// Builds a SELECT query with ORDER BY for deterministic results.
41///
42/// This is critical for:
43/// 1. MS SQL Server which requires ORDER BY when using OFFSET
44/// 2. Deterministic results for resumption after transient errors
45///
46/// For MS SQL Server, DECIMAL columns are wrapped in CONVERT(VARCHAR, ...) to preserve
47/// full precision, as the ODBC driver loses precision when reading DECIMAL as SQL_C_CHAR.
48///
49/// @param formatter The SQL query formatter for the database.
50/// @param serverType The type of database server.
51/// @param schema The schema name.
52/// @param tableName The table name.
53/// @param columns The table columns.
54/// @param primaryKeys The primary key columns.
55/// @param offset The row offset for pagination.
56/// @return The SQL SELECT query string.
57std::string BuildSelectQueryWithOffset(SqlQueryFormatter const& formatter,
58 SqlServerType serverType,
59 std::string_view schema,
60 std::string const& tableName,
61 std::vector<SqlSchema::Column> const& columns,
62 std::vector<std::string> const& primaryKeys,
63 size_t offset);
64
65/// Processes a single table backup.
66///
67/// Reads all rows from the table and writes them to msgpack chunks in the ZIP archive.
68/// Handles retry logic for transient errors with offset-based resumption.
69///
70/// @param ctx The backup context.
71/// @param conn The database connection.
72/// @param table The table schema information.
73void ProcessTableBackup(BackupContext& ctx, SqlConnection& conn, SqlSchema::Table const& table);
74
75/// Backup worker that processes tables from a thread-safe queue.
76///
77/// Workers block on the queue until a table is available or the queue is finished.
78/// This allows workers to start immediately and begin processing tables as soon
79/// as the schema producer pushes them.
80///
81/// Each worker receives a pre-created connection to avoid data races in the ODBC driver
82/// during concurrent connection establishment.
83///
84/// @param tableQueue The thread-safe queue of tables to process.
85/// @param ctx The backup context.
86/// @param conn The database connection for this worker.
87void BackupWorker(ThreadSafeQueue<SqlSchema::Table>& tableQueue, BackupContext ctx, SqlConnection& conn);
88
89} // namespace Lightweight::SqlBackup::detail