5#include "../SqlConnectInfo.hpp"
6#include "../SqlQuery/MigrationPlan.hpp"
7#include "../SqlRetryPolicy.hpp"
8#include "../SqlSchema.hpp"
28namespace Lightweight::SqlBackup
37enum class CompressionMethod : std::int32_t
68 CompressionMethod
method = CompressionMethod::Deflate;
133LIGHTWEIGHT_API std::size_t GetAvailableSystemMemory() noexcept;
140LIGHTWEIGHT_API
RestoreSettings CalculateRestoreSettings(std::
size_t availableMemory,
unsigned concurrency);
146LIGHTWEIGHT_API
bool IsCompressionMethodSupported(CompressionMethod method) noexcept;
149LIGHTWEIGHT_API std::vector<CompressionMethod> GetSupportedCompressionMethods() noexcept;
152LIGHTWEIGHT_API std::string_view CompressionMethodName(CompressionMethod method) noexcept;
180 std::vector<SqlSchema::IndexDefinition>
indexes;
267 (void) additionalItems;
303 if (progress.
state == Progress::State::Error)
313 size_t _errorCount = 0;
316struct NullProgressManager: ErrorTrackingProgressManager
318 void Update(Progress
const& progress)
override
322 void AllDone()
override {}
337LIGHTWEIGHT_API
void Backup(std::filesystem::path
const& outputFile,
338 SqlConnectionString
const& connectionString,
339 unsigned concurrency,
340 ProgressManager& progress,
341 std::string
const& schema = {},
342 std::string
const& tableFilter =
"*",
343 RetrySettings
const& retrySettings = {},
344 BackupSettings
const& backupSettings = {});
360LIGHTWEIGHT_API
void BackupSchemas(std::filesystem::path
const& outputFile,
361 SqlConnectionString
const& connectionString,
362 unsigned concurrency,
363 ProgressManager& progress,
364 std::vector<std::string>
const& schemas,
365 std::string
const& tableFilter =
"*",
366 RetrySettings
const& retrySettings = {},
367 BackupSettings
const& backupSettings = {});
374[[nodiscard]] LIGHTWEIGHT_API std::expected<std::vector<std::string>, std::string> ReadArchiveSchemas(
375 std::filesystem::path
const& archiveFile);
396[[nodiscard]] LIGHTWEIGHT_API std::expected<ArchiveVerification, std::string> VerifyArchive(
397 std::filesystem::path
const& archiveFile);
407[[nodiscard]] LIGHTWEIGHT_API std::expected<std::vector<std::string>, std::string> ListArchiveAttachments(
408 std::filesystem::path
const& archiveFile);
418[[nodiscard]] LIGHTWEIGHT_API std::expected<std::vector<std::uint8_t>, std::string> ReadArchiveAttachment(
419 std::filesystem::path
const& archiveFile, std::string_view name);
433[[nodiscard]] LIGHTWEIGHT_API std::string DefaultRestoreSchema(std::filesystem::path
const& archiveFile,
434 std::string
const& defaultSchema,
435 bool caseInsensitive);
438inline constexpr std::size_t MaxReportedRowRanges = 100;
440inline constexpr std::size_t MaxReportedReasons = 20;
442inline constexpr std::size_t MaxSampleKeysPerReason = 3;
449[[nodiscard]]
inline std::string CountOf(std::uint64_t count, std::string_view singular, std::string_view plural)
451 return std::format(
"{} {}", count, count == 1 ? singular : plural);
523 auto count = std::uint64_t { 0 };
525 count += column.cellsSetToNull;
533 return std::format(
"{} failed, {} set to NULL",
552 auto count = std::uint64_t { 0 };
553 for (
auto const& table:
tables)
554 count += table.failedRowCount;
561 auto count = std::uint64_t { 0 };
562 for (
auto const& table:
tables)
563 count += table.NulledCellCount();
605LIGHTWEIGHT_API RestoreReport Restore(std::filesystem::path
const& inputFile,
607 unsigned concurrency,
608 ProgressManager& progress,
609 std::string
const& schema = {},
610 std::string
const& tableFilter =
"*",
611 RetrySettings
const& retrySettings = {});
624LIGHTWEIGHT_API RestoreReport Restore(std::filesystem::path
const& inputFile,
625 SqlConnectionString
const& connectionString,
626 unsigned concurrency,
627 ProgressManager& progress,
628 std::string
const& schema,
629 std::string
const& tableFilter,
630 RetrySettings
const& retrySettings,
631 RestoreSettings
const& restoreSettings);
643[[nodiscard]] LIGHTWEIGHT_API std::string RedactConnectionStringSecrets(std::string_view connectionString);
650LIGHTWEIGHT_API std::string CreateMetadata(SqlConnectionString
const& connectionString,
651 SqlSchema::TableList
const& tables,
652 std::string
const& schema = {});
659LIGHTWEIGHT_API std::map<std::string, TableInfo> ParseSchema(std::string_view metadataJson,
660 ProgressManager* progress =
nullptr);
size_t ErrorCount() const noexcept override
Returns the number of errors encountered during the operation.
void Update(Progress const &progress) override
Gets called when the progress of an individual backup/restore operation changes.
What VerifyArchive found.
std::string archiveSha256
SHA-256 of the whole archive file, lowercase hex.
std::size_t verifiedEntries
Entries whose SHA-256 matched checksums.json.
std::vector< std::uint8_t > content
The bytes the archive stores for this attachment.
std::size_t workerArchiveBytes
CompressionMethod method
The compression method to use.
bool forceMssqlConcurrency
std::vector< BackupAttachment > attachments
std::size_t chunkSizeBytes
bool schemaOnly
If true, only export schema metadata without backing up table data.
Cells of one column that were restored as NULL because their value could not be converted.
std::uint64_t cellsSetToNull
Number of cells set to NULL.
std::string column
Column name.
std::string sampleReason
Reason of the first occurrence, cut to at most 200 bytes (at a UTF-8 character boundary).
A contiguous run of rows that failed to restore within one chunk.
std::uint64_t lastRow
Last failed row (1-based, within the chunk, inclusive).
std::string chunkPath
Archive path of the chunk the rows belong to.
std::uint64_t firstRow
First failed row (1-based, within the chunk).
One distinct failure cause of a table, with its occurrence count.
std::string sqlState
SQLSTATE reported by the driver (may be empty).
std::vector< std::string > sampleKeys
A few example row keys (at most MaxSampleKeysPerReason).
std::string message
Message of the first occurrence, cut to at most 200 bytes (at a UTF-8 character boundary).
std::uint64_t occurrences
Number of failures sharing this reason.
virtual void SetTotalItems(size_t totalItems)
ProgressManager()=default
Default constructor.
virtual void SetTotalTables(size_t totalTables)
virtual void Update(Progress const &p)=0
Gets called when the progress of an individual backup/restore operation changes.
virtual void SetMaxTableNameLength(size_t)
virtual size_t ErrorCount() const noexcept
Returns the number of errors encountered during the operation.
ProgressManager & operator=(ProgressManager const &)=default
Default copy assignment operator.
virtual void OnItemsProcessed(size_t count)
ProgressManager(ProgressManager &&)=default
Default move constructor.
ProgressManager & operator=(ProgressManager &&)=default
Default move assignment operator.
virtual void AddTotalItems(size_t additionalItems)
virtual void AllDone()=0
Gets called when all backup/restore operations are finished.
ProgressManager(ProgressManager const &)=default
Default copy constructor.
size_t currentRows
The current number of rows processed.
State state
The state of an individual backup/restore operation.
std::string message
A message associated with the progress update.
std::optional< size_t > totalRows
The total number of rows to be processed, if known.
std::string tableName
The name of the table being backed up / restored.
State
The state of an individual backup/restore operation.
Bounded failure report of a whole restore.
std::vector< std::string > incompleteTables
Tables with chunks left unprocessed by a stopped worker.
std::uint64_t TotalFailedRows() const noexcept
std::uint64_t unattributedChunkErrors
Unreadable archive entries no table could be named for.
std::uint64_t NulledCellCount() const noexcept
std::vector< TableRestoreReport > tables
Only tables with failures, sorted by tableName.
bool HasFailures() const noexcept
bool WorkerStopped() const noexcept
std::size_t memoryLimitBytes
Memory limit in bytes (0 = auto-detect from system).
std::size_t maxRowsPerCommit
bool schemaOnly
If true, only recreate schema without importing data.
Information about a table being backed up.
std::string name
The table's name in the database.
std::vector< SqlColumnDeclaration > columns
The list of columns in the table.
std::vector< SqlSchema::IndexDefinition > indexes
The indexes on the table (excluding primary key index).
std::vector< bool > isBinaryColumn
The list of columns in the table.
std::vector< SqlSchema::ForeignKeyConstraint > foreignKeys
The list of foreign key constraints in the table.
std::string fields
The list of columns in the table in SQL format.
Bounded failure report for one table.
std::vector< FailureReason > reasons
Distinct failure reasons, capped at MaxReportedReasons.
std::string FailureSummary() const
std::string tableName
Name of the table.
std::uint64_t failedRowCount
std::uint64_t occurrencesBeyondReasonCap
Failures not represented in reasons because the cap was reached.
std::uint64_t rowsBeyondRangeCap
Failed rows not represented in failedRowRanges because the cap was reached.
std::vector< FailedRowRange > failedRowRanges
Ranges of failed rows, capped at MaxReportedRowRanges.
std::uint64_t NulledCellCount() const noexcept
std::vector< ColumnValueFailures > valueFailures
Per-column value conversion failures.
bool HasFailures() const noexcept
std::uint64_t unreadableChunks
Number of chunks of this table that could not be read.
std::optional< std::string > tableFailure
Set if the whole table failed; holds the first reason.