Skip to content

Commit 733354c

Browse files
committed
PGPRO-533: Reformat json output, add program-version to backup.control
1 parent 6df8c2a commit 733354c

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

src/catalog.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
395395
fprintf(out, "block-size = %u\n", backup->block_size);
396396
fprintf(out, "xlog-block-size = %u\n", backup->wal_block_size);
397397
fprintf(out, "checksum-version = %u\n", backup->checksum_version);
398+
fprintf(out, "program-version = %s\n", PROGRAM_VERSION);
398399
if (backup->server_version[0] != '\0')
399400
fprintf(out, "server-version = %s\n", backup->server_version);
400401

@@ -476,6 +477,7 @@ readBackupControlFile(const char *path)
476477
char *stop_lsn = NULL;
477478
char *status = NULL;
478479
char *parent_backup = NULL;
480+
char *program_version = NULL;
479481
char *server_version = NULL;
480482
char *compress_alg = NULL;
481483

@@ -494,6 +496,7 @@ readBackupControlFile(const char *path)
494496
{'u', 0, "block-size", &backup->block_size, SOURCE_FILE_STRICT},
495497
{'u', 0, "xlog-block-size", &backup->wal_block_size, SOURCE_FILE_STRICT},
496498
{'u', 0, "checksum-version", &backup->checksum_version, SOURCE_FILE_STRICT},
499+
{'s', 0, "program-version", &program_version, SOURCE_FILE_STRICT},
497500
{'s', 0, "server-version", &server_version, SOURCE_FILE_STRICT},
498501
{'b', 0, "stream", &backup->stream, SOURCE_FILE_STRICT},
499502
{'s', 0, "status", &status, SOURCE_FILE_STRICT},
@@ -570,6 +573,13 @@ readBackupControlFile(const char *path)
570573
free(parent_backup);
571574
}
572575

576+
if (program_version)
577+
{
578+
StrNCpy(backup->program_version, program_version,
579+
sizeof(backup->program_version));
580+
pfree(program_version);
581+
}
582+
573583
if (server_version)
574584
{
575585
StrNCpy(backup->server_version, server_version,

src/pg_probackup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ typedef struct pgBackup
228228
uint32 wal_block_size;
229229
uint32 checksum_version;
230230

231+
char program_version[100];
231232
char server_version[100];
232233

233234
bool stream; /* Was this backup taken in stream mode?

src/show.c

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ static int show_backup(time_t requested_backup_id);
2626
static void show_instance_plain(parray *backup_list, bool show_name);
2727
static void show_instance_json(parray *backup_list);
2828

29+
/* Json output functions */
30+
31+
typedef enum
32+
{
33+
JT_BEGIN_ARRAY,
34+
JT_END_ARRAY,
35+
JT_BEGIN_OBJECT,
36+
JT_END_OBJECT
37+
} JsonToken;
38+
39+
static void json_add(PQExpBuffer buf, JsonToken type);
40+
static void json_add_key(PQExpBuffer buf, const char *name, bool add_comma);
41+
static void json_add_value(PQExpBuffer buf, const char *name, const char *value,
42+
bool add_comma);
43+
2944
static PQExpBufferData show_buf;
3045
static bool first_instance = true;
3146
static uint8 json_level = 0;
@@ -219,8 +234,9 @@ show_instance_start(void)
219234
first_instance = true;
220235
json_level = 0;
221236

222-
appendPQExpBufferChar(&show_buf, '[');
223-
json_level++;
237+
json_add(&show_buf, JT_BEGIN_OBJECT);
238+
json_add_key(&show_buf, "instances", false);
239+
json_add(&show_buf, JT_BEGIN_ARRAY);
224240
}
225241

226242
/*
@@ -230,7 +246,11 @@ static void
230246
show_instance_end(void)
231247
{
232248
if (show_format == SHOW_JSON)
233-
appendPQExpBufferStr(&show_buf, "\n]\n");
249+
{
250+
json_add(&show_buf, JT_END_ARRAY);
251+
json_add(&show_buf, JT_END_OBJECT);
252+
appendPQExpBufferChar(&show_buf, '\n');
253+
}
234254

235255
fputs(show_buf.data, stdout);
236256
termPQExpBuffer(&show_buf);
@@ -375,14 +395,6 @@ json_add_indent(PQExpBuffer buf)
375395
appendPQExpBufferStr(buf, " ");
376396
}
377397

378-
typedef enum
379-
{
380-
JT_BEGIN_ARRAY,
381-
JT_END_ARRAY,
382-
JT_BEGIN_OBJECT,
383-
JT_END_OBJECT
384-
} JsonToken;
385-
386398
static void
387399
json_add(PQExpBuffer buf, JsonToken type)
388400
{
@@ -493,10 +505,10 @@ show_instance_json(parray *backup_list)
493505

494506
/* Begin of instance object */
495507
json_add(buf, JT_BEGIN_OBJECT);
508+
json_add_key(buf, instance_name, false);
496509

497-
json_add_value(buf, "instance", instance_name, false);
498-
499-
json_add_key(buf, "backups", true);
510+
json_add(buf, JT_BEGIN_OBJECT);
511+
json_add_key(buf, "backups", false);
500512

501513
/*
502514
* List backups.
@@ -516,14 +528,19 @@ show_instance_json(parray *backup_list)
516528
appendPQExpBufferChar(buf, ',');
517529

518530
json_add(buf, JT_BEGIN_OBJECT);
531+
json_add_key(buf, base36enc(backup->start_time), false);
519532

520-
json_add_value(buf, "id", base36enc(backup->start_time), false);
533+
/* Show backup attributes */
534+
json_add(buf, JT_BEGIN_OBJECT);
521535

522536
if (backup->parent_backup != 0)
537+
{
523538
json_add_value(buf, "parent-backup-id",
524-
base36enc(backup->parent_backup), true);
525-
526-
json_add_value(buf, "backup-mode", pgBackupGetBackupMode(backup), true);
539+
base36enc(backup->parent_backup), false);
540+
json_add_value(buf, "backup-mode", pgBackupGetBackupMode(backup), true);
541+
}
542+
else
543+
json_add_value(buf, "backup-mode", pgBackupGetBackupMode(backup), false);
527544

528545
json_add_value(buf, "wal", backup->stream ? "STREAM": "ARCHIVE", true);
529546

@@ -545,6 +562,7 @@ show_instance_json(parray *backup_list)
545562
json_add_key(buf, "checksum-version", true);
546563
appendPQExpBuffer(buf, "%u", backup->checksum_version);
547564

565+
json_add_value(buf, "program-version", backup->program_version, true);
548566
json_add_value(buf, "server-version", backup->server_version, true);
549567

550568
json_add_key(buf, "current-tli", true);
@@ -594,11 +612,15 @@ show_instance_json(parray *backup_list)
594612

595613
json_add_value(buf, "status", status2str(backup->status), true);
596614

615+
json_add(buf, JT_END_OBJECT);
616+
/* End of backup attributes */
617+
597618
json_add(buf, JT_END_OBJECT);
598619
}
599620

600621
/* End of backups */
601622
json_add(buf, JT_END_ARRAY);
623+
json_add(buf, JT_END_OBJECT);
602624

603625
/* End of instance object */
604626
json_add(buf, JT_END_OBJECT);

src/util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,6 @@ pgBackup_init(pgBackup *backup)
325325
backup->from_replica = false;
326326
backup->parent_backup = 0;
327327
backup->primary_conninfo = NULL;
328+
backup->program_version[0] = '\0';
328329
backup->server_version[0] = '\0';
329330
}

0 commit comments

Comments
 (0)