Skip to content

Commit

Permalink
dbcopy: update sequence at the end of each table copy
Browse files Browse the repository at this point in the history
  • Loading branch information
franku authored and pstorz committed Jan 31, 2020
1 parent 0a18b2a commit 425425c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 17 additions & 9 deletions core/src/dird/dbcopy/database_export_postgresql.cc
Expand Up @@ -107,35 +107,41 @@ void DatabaseExportPostgresql::CopyRow(RowData& source_data_row)
}
}

void DatabaseExportPostgresql::CopyStart() { SelectSequenceSchema(); }
void DatabaseExportPostgresql::CopyStart()
{
// runs before first table
SelectSequenceSchema();
}

static void UpdateSequences(
BareosDb* db,
const DatabaseExportPostgresql::SequenceSchemaVector&
sequence_schema_vector)
sequence_schema_vector,
const std::string& table_name)
{
std::cout << "Updating sequence for tables: " << std::endl;
for (const auto& s : sequence_schema_vector) {
std::cout << "--> " << s.table_name << std::endl;
std::string table_name_lower_case;
std::transform(table_name.cbegin(), table_name.cend(),
std::back_inserter(table_name_lower_case), ::tolower);
if (s.table_name == table_name_lower_case) {
std::string sequence_schema_query{"select setval(' "};
sequence_schema_query += s.sequence_name;
sequence_schema_query += "', (select max(";
sequence_schema_query += s.column_name;
sequence_schema_query += ") from ";
sequence_schema_query += s.table_name;
sequence_schema_query += table_name_lower_case;
sequence_schema_query += "))";
std::cout << "--> updating sequence" << std::endl;
if (!db->SqlQuery(sequence_schema_query.c_str())) {
throw std::runtime_error(
"DatabaseExportPostgresql: Could not set sequence");
}
}
}

void DatabaseExportPostgresql::CopyEnd()
{
UpdateSequences(db_, sequence_schema_vector_);
}

void DatabaseExportPostgresql::CopyEnd() {}

void DatabaseExportPostgresql::CursorStartTable(const std::string& table_name)
{
const DatabaseTableDescriptions::TableDescription* table{
Expand Down Expand Up @@ -224,6 +230,8 @@ void DatabaseExportPostgresql::EndTable(const std::string& table_name)
}
}

UpdateSequences(db_, sequence_schema_vector_, table_name);

if (transaction_) {
db_->SqlQuery("COMMIT");
transaction_ = false;
Expand Down
2 changes: 1 addition & 1 deletion core/src/dird/dbcopy/database_import_mysql.cc
Expand Up @@ -198,7 +198,7 @@ void DatabaseImportMysql::FillRowWithDatabaseResult(ResultHandlerContext* r,
"Number of database fields does not match description");
}

RowData& row_data = r->row_data;
RowData& row_data = r->row_data;

if (r->is_restore_object) {
std::size_t field_index_longblob = fields - 1;
Expand Down

0 comments on commit 425425c

Please sign in to comment.