Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions docs/en/sql-reference/statements/alter/database-comment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
description: 'Documentation for ALTER DATABASE ... MODIFY COMMENT Statements'
slug: /sql-reference/statements/alter/database-comment
sidebar_position: 51
sidebar_label: 'UPDATE'
title: 'ALTER DATABASE ... MODIFY COMMENT Statements'
---

# ALTER DATABASE ... MODIFY COMMENT

Adds, modifies, or removes comment to the table, regardless if it was set before or not. Comment change is reflected in both [system.databases](/operations/system-tables/databases.md) and `SHOW CREATE DATABASE` query.

**Syntax**

``` sql
ALTER DATABASE [db].name [ON CLUSTER cluster] MODIFY COMMENT 'Comment'
```

**Examples**

Creating a DATABASE with comment (for more information, see the [COMMENT](/sql-reference/statements/create/table#comment-clause) clause):

``` sql
CREATE DATABASE database_with_comment ENGINE = Memory COMMENT 'The temporary database';
```

Modifying the table comment:

``` sql
ALTER DATABASE database_with_comment MODIFY COMMENT 'new comment on a database';
SELECT comment FROM system.databases WHERE name = 'database_with_comment';
```

Output of a new comment:

```text
┌─comment─────────────────┐
│ new comment on database │
└─────────────────────────┘
```

Removing the database comment:

``` sql
ALTER DATABASE database_with_comment MODIFY COMMENT '';
SELECT comment FROM system.databases WHERE name = 'database_with_comment';
```

Output of a removed comment:

```text
┌─comment─┐
│ │
└─────────┘
```
1 change: 1 addition & 0 deletions src/Access/Common/AccessType.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum class AccessType : uint8_t
M(ALTER_MATERIALIZE_COLUMN, "MATERIALIZE COLUMN", COLUMN, ALTER_COLUMN) \
M(ALTER_COLUMN, "", GROUP, ALTER_TABLE) /* allow to execute ALTER {ADD|DROP|MODIFY...} COLUMN */\
M(ALTER_MODIFY_COMMENT, "MODIFY COMMENT", TABLE, ALTER_TABLE) /* modify table comment */\
M(ALTER_MODIFY_DATABASE_COMMENT, "MODIFY DATABASE COMMENT", DATABASE, ALTER_DATABASE) /* modify database comment*/\
\
M(ALTER_ORDER_BY, "ALTER MODIFY ORDER BY, MODIFY ORDER BY", TABLE, ALTER_INDEX) \
M(ALTER_SAMPLE_BY, "ALTER MODIFY SAMPLE BY, MODIFY SAMPLE BY", TABLE, ALTER_INDEX) \
Expand Down
5 changes: 5 additions & 0 deletions src/Databases/DatabaseMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ std::vector<std::pair<ASTPtr, StoragePtr>> DatabaseMemory::getTablesForBackup(co
return res;
}

void DatabaseMemory::alterDatabaseComment(const AlterCommand & command)
{
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
}

void registerDatabaseMemory(DatabaseFactory & factory)
{
auto create_fn = [](const DatabaseFactory::Arguments & args)
Expand Down
2 changes: 2 additions & 0 deletions src/Databases/DatabaseMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class DatabaseMemory final : public DatabaseWithOwnTablesBase

std::vector<std::pair<ASTPtr, StoragePtr>> getTablesForBackup(const FilterByNameFunction & filter, const ContextPtr & local_context) const override;

void alterDatabaseComment(const AlterCommand & alter_command) override;

private:
void removeDataPath(ContextPtr local_context);

Expand Down
8 changes: 8 additions & 0 deletions src/Databases/DatabaseOnDisk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/formatAST.h>
#include <Parsers/parseQuery.h>
#include <Storages/AlterCommands.h>
#include <Storages/IStorage.h>
#include <Storages/StorageFactory.h>
#include <TableFunctions/TableFunctionFactory.h>
Expand Down Expand Up @@ -860,4 +861,11 @@ void DatabaseOnDisk::modifySettingsMetadata(const SettingsChanges & settings_cha

fs::rename(metadata_file_tmp_path, metadata_file_path);
}

void DatabaseOnDisk::alterDatabaseComment(const AlterCommand & command)
{
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
}


}
4 changes: 4 additions & 0 deletions src/Databases/DatabaseOnDisk.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace DB
{

class Context;
struct AlterCommand;

std::pair<String, StoragePtr> createTableFromAST(
ASTCreateQuery ast_create_query,
const String & database_name,
Expand Down Expand Up @@ -76,6 +78,8 @@ class DatabaseOnDisk : public DatabaseWithOwnTablesBase

void modifySettingsMetadata(const SettingsChanges & settings_changes, ContextPtr query_context);

void alterDatabaseComment(const AlterCommand & alter_command) override;

protected:
static constexpr const char * create_suffix = ".tmp";
static constexpr const char * drop_suffix = ".tmp_drop";
Expand Down
23 changes: 23 additions & 0 deletions src/Databases/DatabasesCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ParserCreateQuery.h>
#include <Parsers/formatAST.h>
#include <Parsers/parseQuery.h>
#include <Storages/AlterCommands.h>
#include <Storages/ColumnsDescription.h>
#include <Storages/KeyDescription.h>
#include <Storages/StorageDictionary.h>
#include <Storages/StorageFactory.h>
#include <Storages/Utils.h>
Expand All @@ -30,6 +34,7 @@ namespace ErrorCodes
extern const int NOT_IMPLEMENTED;
extern const int LOGICAL_ERROR;
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
extern const int BAD_ARGUMENTS;
}

void applyMetadataChangesToCreateQuery(const ASTPtr & query, const StorageInMemoryMetadata & metadata)
Expand Down Expand Up @@ -206,6 +211,24 @@ void cleanupObjectDefinitionFromTemporaryFlags(ASTCreateQuery & query)
query.out_file = nullptr;
}

void updateDatabaseCommentWithMetadataFile(DatabasePtr db, const AlterCommand & command)
{
if (!command.comment)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unable to obtain database comment from query");

String old_database_comment = db->getDatabaseComment();
db->setDatabaseComment(command.comment.value());

try
{
DatabaseCatalog::instance().updateMetadataFile(db);
}
catch (...)
{
db->setDatabaseComment(old_database_comment);
throw;
}
}

DatabaseWithOwnTablesBase::DatabaseWithOwnTablesBase(const String & name_, const String & logger, ContextPtr context_)
: IDatabase(name_), WithContext(context_->getGlobalContext()), log(getLogger(logger))
Expand Down
2 changes: 2 additions & 0 deletions src/Databases/DatabasesCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void cleanupObjectDefinitionFromTemporaryFlags(ASTCreateQuery & query);

class Context;

void updateDatabaseCommentWithMetadataFile(DatabasePtr db, const AlterCommand & command);

/// A base class for databases that manage their own list of tables.
class DatabaseWithOwnTablesBase : public IDatabase, protected WithContext
{
Expand Down
5 changes: 5 additions & 0 deletions src/Databases/IDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ IDatabase::~IDatabase()
CurrentMetrics::sub(CurrentMetrics::AttachedDatabase, 1);
}

void IDatabase::alterDatabaseComment(const AlterCommand & /*command*/)
{
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "{}: ALTER DATABASE COMMENT is not supported", getEngineName());
}

std::vector<std::pair<ASTPtr, StoragePtr>> IDatabase::getTablesForBackup(const FilterByNameFunction &, const ContextPtr &) const
{
/// Cannot backup any table because IDatabase doesn't own any tables.
Expand Down
6 changes: 6 additions & 0 deletions src/Databases/IDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
namespace DB
{


struct Settings;
struct ConstraintsDescription;
struct IndicesDescription;
struct StorageInMemoryMetadata;
struct StorageID;
class ASTCreateQuery;
struct AlterCommand;
class AlterCommands;
class SettingsChanges;
using DictionariesWithID = std::vector<std::pair<String, UUID>>;
Expand Down Expand Up @@ -416,6 +418,10 @@ class IDatabase : public std::enable_shared_from_this<IDatabase>
std::lock_guard lock{mutex};
return database_name;
}

// Alter comment of database.
virtual void alterDatabaseComment(const AlterCommand &);

/// Get UUID of database.
virtual UUID getUUID() const { return UUIDHelpers::Nil; }

Expand Down
13 changes: 13 additions & 0 deletions src/Databases/MySQL/DatabaseMySQL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
# include <QueryPipeline/QueryPipelineBuilder.h>
# include <IO/Operators.h>
# include <Interpreters/Context.h>
# include <Interpreters/DatabaseCatalog.h>
# include <Interpreters/evaluateConstantExpression.h>
# include <Parsers/ASTCreateQuery.h>
# include <Parsers/ASTFunction.h>
# include <Parsers/ParserCreateQuery.h>
# include <Parsers/parseQuery.h>
# include <Parsers/queryToString.h>
# include <Processors/Executors/PullingPipelineExecutor.h>
# include <Processors/Sources/MySQLSource.h>
# include <QueryPipeline/QueryPipelineBuilder.h>
# include <Storages/AlterCommands.h>
# include <Storages/MySQL/MySQLHelpers.h>
# include <Storages/MySQL/MySQLSettings.h>
# include <Storages/NamedCollectionsHelpers.h>
# include <Storages/StorageMySQL.h>
# include <Storages/MySQL/MySQLSettings.h>
# include <Storages/MySQL/MySQLHelpers.h>
Expand Down Expand Up @@ -411,6 +419,11 @@ StoragePtr DatabaseMySQL::detachTable(ContextPtr /* context */, const String & t
return local_tables_cache[table_name].second;
}

void DatabaseMySQL::alterDatabaseComment(const AlterCommand & command)
{
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
}

String DatabaseMySQL::getMetadataPath() const
{
return metadata_path;
Expand Down
5 changes: 4 additions & 1 deletion src/Databases/MySQL/DatabaseMySQL.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace DB
{

class Context;

struct AlterCommand;
struct MySQLSettings;
enum class MySQLDataTypesSupport : uint8_t;

/** Real-time access to table list and table structure from remote MySQL
Expand Down Expand Up @@ -86,6 +87,8 @@ class DatabaseMySQL final : public IDatabase, WithContext

void attachTable(ContextPtr context, const String & table_name, const StoragePtr & storage, const String & relative_table_path) override;

void alterDatabaseComment(const AlterCommand & command) override;

protected:
ASTPtr getCreateTableQueryImpl(const String & name, ContextPtr context, bool throw_on_error) const override;

Expand Down
6 changes: 6 additions & 0 deletions src/Databases/PostgreSQL/DatabasePostgreSQL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeArray.h>
#include <Storages/AlterCommands.h>
#include <Storages/NamedCollectionsHelpers.h>
#include <Storages/StoragePostgreSQL.h>
#include <Interpreters/Context.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTFunction.h>
Expand Down Expand Up @@ -385,6 +387,10 @@ void DatabasePostgreSQL::shutdown()
cleaner_task->deactivate();
}

void DatabasePostgreSQL::alterDatabaseComment(const AlterCommand & command)
{
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
}

ASTPtr DatabasePostgreSQL::getCreateDatabaseQuery() const
{
Expand Down
3 changes: 3 additions & 0 deletions src/Databases/PostgreSQL/DatabasePostgreSQL.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace DB
{

class Context;
struct AlterCommand;


/** Real-time access to table list and table structure from remote PostgreSQL.
Expand Down Expand Up @@ -60,6 +61,8 @@ class DatabasePostgreSQL final : public IDatabase, WithContext
void drop(ContextPtr /*context*/) override;
void shutdown() override;

void alterDatabaseComment(const AlterCommand & command) override;

protected:
ASTPtr getCreateTableQueryImpl(const String & table_name, ContextPtr context, bool throw_on_error) const override;

Expand Down
6 changes: 6 additions & 0 deletions src/Databases/SQLite/DatabaseSQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#if USE_SQLITE

#include <Storages/AlterCommands.h>
#include <Common/logger_useful.h>
#include <Core/Settings.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeNullable.h>
#include <Databases/DatabaseFactory.h>
#include <Databases/SQLite/fetchSQLiteTableStructure.h>
#include <Interpreters/DatabaseCatalog.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTColumnDeclaration.h>
#include <Parsers/ASTFunction.h>
Expand Down Expand Up @@ -173,6 +175,10 @@ ASTPtr DatabaseSQLite::getCreateDatabaseQuery() const
return create_query;
}

void DatabaseSQLite::alterDatabaseComment(const AlterCommand & command)
{
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
}

ASTPtr DatabaseSQLite::getCreateTableQueryImpl(const String & table_name, ContextPtr local_context, bool throw_on_error) const
{
Expand Down
4 changes: 4 additions & 0 deletions src/Databases/SQLite/DatabaseSQLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace DB
{
struct AlterCommand;

class DatabaseSQLite final : public IDatabase, WithContext
{
public:
Expand Down Expand Up @@ -40,6 +42,8 @@ class DatabaseSQLite final : public IDatabase, WithContext

void shutdown() override {}

void alterDatabaseComment(const AlterCommand & command) override;

protected:
ASTPtr getCreateTableQueryImpl(const String & table_name, ContextPtr context, bool throw_on_error) const override;

Expand Down
Loading
Loading