Merge pull request #29429 from Enmk/Governance/database_comment

Implemented creating databases with comments
This commit is contained in:
Anton Popov 2021-10-04 15:12:33 +03:00 committed by GitHub
commit 362e84a336
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 100 additions and 1 deletions

View File

@ -126,6 +126,8 @@ ASTPtr DatabaseDictionary::getCreateDatabaseQuery() const
{
WriteBufferFromString buffer(query);
buffer << "CREATE DATABASE " << backQuoteIfNeed(getDatabaseName()) << " ENGINE = Dictionary";
if (const auto comment_value = getDatabaseComment(); !comment_value.empty())
buffer << " COMMENT " << backQuote(comment_value);
}
auto settings = getContext()->getSettingsRef();
ParserCreateQuery parser;

View File

@ -78,8 +78,11 @@ DatabasePtr DatabaseFactory::get(const ASTCreateQuery & create, const String & m
if (impl && context->hasQueryContext() && context->getSettingsRef().log_queries)
context->getQueryContext()->addQueryFactoriesInfo(Context::QueryLogFactories::Database, impl->getEngineName());
return impl;
// Attach database metadata
if (impl && create.storage && create.storage->comment)
impl->setDatabaseComment(create.storage->comment->as<ASTLiteral>()->value.safeGet<String>());
return impl;
}
catch (...)
{

View File

@ -69,6 +69,10 @@ ASTPtr DatabaseMemory::getCreateDatabaseQuery() const
create_query->database = getDatabaseName();
create_query->set(create_query->storage, std::make_shared<ASTStorage>());
create_query->storage->set(create_query->storage->engine, makeASTFunction(getEngineName()));
if (const auto comment_value = getDatabaseComment(); !comment_value.empty())
create_query->storage->set(create_query->storage->comment, std::make_shared<ASTLiteral>(comment_value));
return create_query;
}

View File

@ -527,6 +527,16 @@ ASTPtr DatabaseOnDisk::getCreateDatabaseQuery() const
ast = parseQuery(parser, query.data(), query.data() + query.size(), "", 0, settings.max_parser_depth);
}
if (const auto database_comment = getDatabaseComment(); !database_comment.empty())
{
auto & ast_create_query = ast->as<ASTCreateQuery &>();
// TODO(nemkov): this is a precaution and should never happen, remove if there are no failed tests on CI/CD.
if (!ast_create_query.storage)
throw Exception(ErrorCodes::LOGICAL_ERROR, "ASTCreateQuery lacks engine clause, but a comment is present.");
ast_create_query.storage->set(ast_create_query.storage->comment, std::make_shared<ASTLiteral>(database_comment));
}
return ast;
}

View File

@ -259,6 +259,17 @@ public:
/// Get the CREATE DATABASE query for current database.
virtual ASTPtr getCreateDatabaseQuery() const = 0;
String getDatabaseComment() const
{
std::lock_guard lock{mutex};
return comment;
}
void setDatabaseComment(String new_comment)
{
std::lock_guard lock{mutex};
comment = std::move(new_comment);
}
/// Get name of database.
String getDatabaseName() const
{
@ -324,6 +335,7 @@ protected:
mutable std::mutex mutex;
String database_name;
String comment;
};
using DatabasePtr = std::shared_ptr<IDatabase>;

View File

@ -196,6 +196,10 @@ ASTPtr DatabaseMySQL::getCreateDatabaseQuery() const
const auto & create_query = std::make_shared<ASTCreateQuery>();
create_query->database = getDatabaseName();
create_query->set(create_query->storage, database_engine_define);
if (const auto comment_value = getDatabaseComment(); !comment_value.empty())
create_query->storage->set(create_query->storage->comment, std::make_shared<ASTLiteral>(comment_value));
return create_query;
}

View File

@ -347,6 +347,10 @@ ASTPtr DatabasePostgreSQL::getCreateDatabaseQuery() const
const auto & create_query = std::make_shared<ASTCreateQuery>();
create_query->database = getDatabaseName();
create_query->set(create_query->storage, database_engine_define);
if (const auto comment_value = getDatabaseComment(); !comment_value.empty())
create_query->storage->set(create_query->storage->comment, std::make_shared<ASTLiteral>(comment_value));
return create_query;
}

View File

@ -162,6 +162,10 @@ ASTPtr DatabaseSQLite::getCreateDatabaseQuery() const
const auto & create_query = std::make_shared<ASTCreateQuery>();
create_query->database = getDatabaseName();
create_query->set(create_query->storage, database_engine_define);
if (const auto comment_value = getDatabaseComment(); !comment_value.empty())
create_query->storage->set(create_query->storage->comment, std::make_shared<ASTLiteral>(comment_value));
return create_query;
}

View File

@ -17,6 +17,7 @@ NamesAndTypesList StorageSystemDatabases::getNamesAndTypes()
{"data_path", std::make_shared<DataTypeString>()},
{"metadata_path", std::make_shared<DataTypeString>()},
{"uuid", std::make_shared<DataTypeUUID>()},
{"comment", std::make_shared<DataTypeString>()}
};
}
@ -39,6 +40,7 @@ void StorageSystemDatabases::fillData(MutableColumns & res_columns, ContextPtr c
res_columns[2]->insert(context->getPath() + database->getDataPath());
res_columns[3]->insert(database->getMetadataPath());
res_columns[4]->insert(database->getUUID());
res_columns[5]->insert(database->getDatabaseComment());
}
}

View File

@ -0,0 +1,16 @@
engine : Atomic
CREATE DATABASE default\nENGINE = Atomic\nCOMMENT \'Test DB with comment\'
comment= Test DB with comment
engine : Ordinary
CREATE DATABASE default\nENGINE = Ordinary\nCOMMENT \'Test DB with comment\'
comment= Test DB with comment
engine : Lazy(1)
CREATE DATABASE default\nENGINE = Lazy(1)\nCOMMENT \'Test DB with comment\'
comment= Test DB with comment
engine : Memory
CREATE DATABASE default\nENGINE = Memory()\nCOMMENT \'Test DB with comment\'
comment= Test DB with comment

View File

@ -0,0 +1,38 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
DB_NAME="${CLICKHOUSE_DATABASE}"
function get_db_comment_info()
{
$CLICKHOUSE_CLIENT --query="SHOW CREATE DATABASE ${DB_NAME};"
$CLICKHOUSE_CLIENT --query="SELECT 'comment=', comment FROM system.databases WHERE name='${DB_NAME}'"
echo # just a newline
}
function test_db_comments()
{
local ENGINE_NAME="$1"
echo "engine : ${ENGINE_NAME}"
$CLICKHOUSE_CLIENT -nm <<EOF
DROP DATABASE IF EXISTS ${DB_NAME};
CREATE DATABASE ${DB_NAME} ENGINE = ${ENGINE_NAME} COMMENT 'Test DB with comment';
EOF
get_db_comment_info
}
# For some reason order seems important, putting Atomic after Memory makes test fail every time
# due to DB metadata SQL file still being present after "DROP DATABASE"
test_db_comments "Atomic"
test_db_comments "Ordinary"
test_db_comments "Lazy(1)"
test_db_comments "Memory"
# test_db_comments "MySQL('127.0.0.1:9004', 'default', 'default', '')" # fails due to CH internal reasons
# test_db_comments "SQLite('dummy_sqlitedb')"
## needs to be explicitly enabled with `SET allow_experimental_database_replicated=1`
# test_db_comments "Replicated('/clickhouse/$CLICKHOUSE_TEST_ZOOKEEPER_PREFIX', '1') ORDER BY k"