Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support BACKUP ALL command #48189

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/Parsers/ParserBackupQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace
});
}

bool parseElement(IParser::Pos & pos, Expected & expected, bool allow_all, Element & element)
bool parseElement(IParser::Pos & pos, Expected & expected, Element & element)
{
return IParserBase::wrapParseImpl(pos, [&]
{
Expand Down Expand Up @@ -169,7 +169,7 @@ namespace
return true;
}

if (allow_all && ParserKeyword{"ALL"}.ignore(pos, expected))
if (ParserKeyword{"ALL"}.ignore(pos, expected))
{
element.type = ElementType::ALL;
parseExceptDatabases(pos, expected, element.except_databases);
Expand All @@ -181,7 +181,7 @@ namespace
});
}

bool parseElements(IParser::Pos & pos, Expected & expected, bool allow_all, std::vector<Element> & elements)
bool parseElements(IParser::Pos & pos, Expected & expected, std::vector<Element> & elements)
{
return IParserBase::wrapParseImpl(pos, [&]
{
Expand All @@ -190,7 +190,7 @@ namespace
auto parse_element = [&]
{
Element element;
if (parseElement(pos, expected, allow_all, element))
if (parseElement(pos, expected, element))
{
result.emplace_back(std::move(element));
return true;
Expand Down Expand Up @@ -334,11 +334,8 @@ bool ParserBackupQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
else
return false;

/// Disable "ALL" if this is a RESTORE command.
bool allow_all = (kind == Kind::RESTORE);

std::vector<Element> elements;
if (!parseElements(pos, expected, allow_all, elements))
if (!parseElements(pos, expected, elements))
return false;

String cluster;
Expand Down
66 changes: 66 additions & 0 deletions tests/integration/test_backup_restore_new/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,72 @@ def test_restore_partition():
)


@pytest.mark.parametrize("exclude_system_log_tables", [False, True])
def test_backup_all(exclude_system_log_tables):
create_and_fill_table()

session_id = new_session_id()
instance.http_query(
"CREATE TEMPORARY TABLE temp_tbl(s String)", params={"session_id": session_id}
)
instance.http_query(
"INSERT INTO temp_tbl VALUES ('q'), ('w'), ('e')",
params={"session_id": session_id},
)

instance.query("CREATE FUNCTION two_and_half AS (x) -> x * 2.5")

instance.query("CREATE USER u1 IDENTIFIED BY 'qwe123' SETTINGS custom_a = 1")

backup_name = new_backup_name()

exclude_from_backup = []
if exclude_system_log_tables:
system_log_tables = (
instance.query(
"SELECT concat('system.', table) FROM system.tables WHERE (database = 'system') AND (table LIKE '%_log')"
)
.rstrip("\n")
.split("\n")
)
exclude_from_backup += system_log_tables

backup_command = f"BACKUP ALL {'EXCEPT TABLES ' + ','.join(exclude_from_backup) if exclude_from_backup else ''} TO {backup_name}"

instance.http_query(backup_command, params={"session_id": session_id})

instance.query("DROP TABLE test.table")
instance.query("DROP FUNCTION two_and_half")
instance.query("DROP USER u1")

restore_settings = []
if not exclude_system_log_tables:
restore_settings.append("allow_non_empty_tables=true")
restore_command = f"RESTORE ALL FROM {backup_name} {'SETTINGS '+ ', '.join(restore_settings) if restore_settings else ''}"

session_id = new_session_id()
instance.http_query(
restore_command, params={"session_id": session_id}, method="POST"
)

assert instance.query("SELECT count(), sum(x) FROM test.table") == "100\t4950\n"

assert instance.http_query(
"SELECT * FROM temp_tbl ORDER BY s", params={"session_id": session_id}
) == TSV([["e"], ["q"], ["w"]])

assert instance.query("SELECT two_and_half(6)") == "15\n"

assert (
instance.query("SHOW CREATE USER u1")
== "CREATE USER u1 IDENTIFIED WITH sha256_password SETTINGS custom_a = 1\n"
)

instance.query("DROP TABLE test.table")
instance.query("DROP FUNCTION two_and_half")
instance.query("DROP USER u1")


def test_operation_id():
create_and_fill_table(n=30)

Expand Down