Skip to content

Commit 22bc30c

Browse files
authored
cleanup(spanner): new version time in spanner_create_backup sample (#5943)
A changed requirement on the PITR `CreateBackup()` sample: use the current time (from Spanner) as the `version_time` instead of using the `earliest_version_time` of the database.
1 parent 4680478 commit 22bc30c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

google/cloud/spanner/samples/samples.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,20 @@ void CreateBackup(google::cloud::spanner::DatabaseAdminClient client,
685685
std::string const& backup_id) {
686686
google::cloud::spanner::Database database(project_id, instance_id,
687687
database_id);
688-
auto db = client.GetDatabase(database);
689-
if (!db) throw std::runtime_error(db.status().message());
690688
auto expire_time =
691689
google::cloud::spanner::MakeTimestamp(absl::Now() + absl::Hours(7))
692690
.value();
693-
auto version_time =
694-
google::cloud::spanner::MakeTimestamp(db->earliest_version_time())
695-
.value();
691+
auto version_time = [&database]() {
692+
auto client = google::cloud::spanner::Client(
693+
google::cloud::spanner::MakeConnection(database));
694+
auto rows = client.ExecuteQuery(
695+
google::cloud::spanner::SqlStatement("SELECT CURRENT_TIMESTAMP()"));
696+
using RowType = std::tuple<google::cloud::spanner::Timestamp>;
697+
auto row = google::cloud::spanner::GetSingularRow(
698+
google::cloud::spanner::StreamOf<RowType>(rows));
699+
if (!row) throw std::runtime_error(row.status().message());
700+
return std::get<0>(*row);
701+
}();
696702
auto backup =
697703
client.CreateBackup(database, backup_id, expire_time, version_time).get();
698704
if (!backup) throw std::runtime_error(backup.status().message());

0 commit comments

Comments
 (0)