mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-03-03 14:52:21 +02:00
Add infoArchiveIdHistoryMatch() to the InfoArchive object.
Match a PostgreSQL system identifier and version to a pgBackRest archive id. Contributed by Cynthia Shang.
This commit is contained in:
parent
f0417ee524
commit
2f15a90d18
@ -112,7 +112,7 @@
|
||||
<release-item-contributor id="cynthia.shang"/>
|
||||
</release-item-contributor-list>
|
||||
|
||||
<p>Info module improvements. Rename constants in <code>Info</code> module for consistency. Remove <code>#define</code> statements in the <code>InfoPg</code> module to conform with newly-adopted coding standards. Use cast to make for loop more readable in <code>InfoPg</code> module.</p>
|
||||
<p>Info module improvements. Rename constants in <code>Info</code> module for consistency. Remove <code>#define</code> statements in the <code>InfoPg</code> module to conform with newly-adopted coding standards. Use cast to make for loop more readable in <code>InfoPg</code> module. Add <code>infoArchiveIdHistoryMatch()</code> to the <code>InfoArchive</code> object.</p>
|
||||
</release-item>
|
||||
|
||||
<release-item>
|
||||
|
@ -120,6 +120,65 @@ infoArchiveCheckPg(const InfoArchive *this, unsigned int pgVersion, uint64_t pgS
|
||||
FUNCTION_DEBUG_RESULT_VOID();
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Given a backrest history id and postgres systemId and version, return the archiveId of the best match
|
||||
***********************************************************************************************************************************/
|
||||
const String *
|
||||
infoArchiveIdHistoryMatch(
|
||||
const InfoArchive *this, const unsigned int historyId, const unsigned int pgVersion, const uint64_t pgSystemId)
|
||||
{
|
||||
FUNCTION_DEBUG_BEGIN(logLevelTrace);
|
||||
FUNCTION_DEBUG_PARAM(INFO_ARCHIVE, this);
|
||||
FUNCTION_DEBUG_PARAM(UINT, historyId);
|
||||
FUNCTION_DEBUG_PARAM(UINT, pgVersion);
|
||||
FUNCTION_DEBUG_PARAM(UINT64, pgSystemId);
|
||||
|
||||
FUNCTION_DEBUG_ASSERT(this != NULL);
|
||||
FUNCTION_DEBUG_END();
|
||||
|
||||
String *archiveId = NULL;
|
||||
InfoPg *infoPg = infoArchivePg(this);
|
||||
|
||||
// Search the history list, from newest to oldest
|
||||
for (unsigned int pgIdx = 0; pgIdx < infoPgDataTotal(infoPg); pgIdx++)
|
||||
{
|
||||
InfoPgData pgDataArchive = infoPgData(infoPg, pgIdx);
|
||||
|
||||
// If there is an exact match with the history, system and version then get the archiveId and stop
|
||||
if (historyId == pgDataArchive.id && pgSystemId == pgDataArchive.systemId && pgVersion == pgDataArchive.version)
|
||||
{
|
||||
archiveId = infoPgArchiveId(infoPg, pgIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If there was not an exact match, then search for the first matching database system-id and version
|
||||
if (archiveId == NULL)
|
||||
{
|
||||
for (unsigned int pgIdx = 0; pgIdx < infoPgDataTotal(infoPg); pgIdx++)
|
||||
{
|
||||
InfoPgData pgDataArchive = infoPgData(infoPg, pgIdx);
|
||||
|
||||
if (pgSystemId == pgDataArchive.systemId && pgVersion == pgDataArchive.version)
|
||||
{
|
||||
archiveId = infoPgArchiveId(infoPg, pgIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the archive id has not been found, then error
|
||||
if (archiveId == NULL)
|
||||
{
|
||||
THROW_FMT(
|
||||
ArchiveMismatchError,
|
||||
"unable to retrieve the archive id for database version '%s' and system-id '%" PRIu64 "'",
|
||||
strPtr(pgVersionToStr(pgVersion)), pgSystemId);
|
||||
}
|
||||
|
||||
FUNCTION_TEST_RESULT(STRING, archiveId);
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Get the current archive id
|
||||
***********************************************************************************************************************************/
|
||||
|
@ -29,6 +29,8 @@ InfoArchive *infoArchiveNew(
|
||||
Functions
|
||||
***********************************************************************************************************************************/
|
||||
void infoArchiveCheckPg(const InfoArchive *this, unsigned int pgVersion, uint64_t pgSystemId);
|
||||
const String *infoArchiveIdHistoryMatch(
|
||||
const InfoArchive *this, const unsigned int historyId, const unsigned int pgVersion, const uint64_t pgSystemId);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Getters
|
||||
|
@ -582,7 +582,7 @@ unit:
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------------------------
|
||||
- name: info-archive
|
||||
total: 1
|
||||
total: 2
|
||||
|
||||
coverage:
|
||||
info/infoArchive: full
|
||||
|
@ -8,13 +8,16 @@ Test Run
|
||||
void
|
||||
testRun(void)
|
||||
{
|
||||
// Initialize test variables
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
String *content = NULL;
|
||||
String *fileName = strNewFmt("%s/test.ini", testPath());
|
||||
InfoArchive *info = NULL;
|
||||
|
||||
// *****************************************************************************************************************************
|
||||
if (testBegin("infoArchiveNew(), infoArchiveCheckPg(), infoArchiveFree()"))
|
||||
{
|
||||
// Initialize test variables
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
String *content = NULL;
|
||||
String *fileName = strNewFmt("%s/test.ini", testPath());
|
||||
|
||||
|
||||
TEST_ERROR_FMT(
|
||||
infoArchiveNew(storageLocal(), fileName, true, cipherTypeNone, NULL), FileMissingError,
|
||||
@ -47,8 +50,6 @@ testRun(void)
|
||||
TEST_RESULT_VOID(
|
||||
storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), bufNewStr(content)), "put archive info to file");
|
||||
|
||||
InfoArchive *info = NULL;
|
||||
|
||||
TEST_ASSIGN(info, infoArchiveNew(storageLocal(), fileName, true, cipherTypeNone, NULL), " new archive info");
|
||||
TEST_RESULT_STR(strPtr(infoArchiveId(info)), "9.4-1", " archiveId set");
|
||||
TEST_RESULT_PTR(infoArchivePg(info), info->infoPg, " infoPg set");
|
||||
@ -73,4 +74,39 @@ testRun(void)
|
||||
TEST_RESULT_VOID(infoArchiveFree(info), "infoArchiveFree() - free archive info");
|
||||
TEST_RESULT_VOID(infoArchiveFree(NULL), " NULL ptr");
|
||||
}
|
||||
|
||||
// *****************************************************************************************************************************
|
||||
if (testBegin("infoArchiveIdHistoryMatch()"))
|
||||
{
|
||||
content = strNew
|
||||
(
|
||||
"[backrest]\n"
|
||||
"backrest-checksum=\"075a202d42c3b6a0257da5f73a68fa77b342f777\"\n"
|
||||
"backrest-format=5\n"
|
||||
"backrest-version=\"2.08dev\"\n"
|
||||
"\n"
|
||||
"[db]\n"
|
||||
"db-id=2\n"
|
||||
"db-system-id=6626363367545678089\n"
|
||||
"db-version=\"9.5\"\n"
|
||||
"\n"
|
||||
"[db:history]\n"
|
||||
"1={\"db-id\":6625592122879095702,\"db-version\":\"9.4\"}\n"
|
||||
"2={\"db-id\":6626363367545678089,\"db-version\":\"9.5\"}\n"
|
||||
);
|
||||
|
||||
TEST_RESULT_VOID(
|
||||
storagePutNP(storageNewWriteNP(storageLocalWrite(), fileName), bufNewStr(content)), "put archive info to file");
|
||||
|
||||
TEST_ASSIGN(info, infoArchiveNew(storageLocal(), fileName, true, cipherTypeNone, NULL), "new archive info");
|
||||
TEST_RESULT_STR(strPtr(infoArchiveIdHistoryMatch(info, 2, 90500, 6626363367545678089)), "9.5-2", " full match found");
|
||||
|
||||
TEST_RESULT_STR(strPtr(infoArchiveIdHistoryMatch(info, 2, 90400, 6625592122879095702)), "9.4-1", " partial match found");
|
||||
|
||||
TEST_ERROR(infoArchiveIdHistoryMatch(info, 2, 90400, 6625592122879095799), ArchiveMismatchError,
|
||||
"unable to retrieve the archive id for database version '9.4' and system-id '6625592122879095799'");
|
||||
|
||||
TEST_ERROR(infoArchiveIdHistoryMatch(info, 2, 90400, 6626363367545678089), ArchiveMismatchError,
|
||||
"unable to retrieve the archive id for database version '9.4' and system-id '6626363367545678089'");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user