Skip to content

Commit 6692348

Browse files
authored
Merge pull request #1118 from Altinity/backport/25.3.8/75622
25.3.8 Backport of ClickHouse#75622: Allow altering database comment
2 parents 86d4e0d + 1866030 commit 6692348

32 files changed

+432
-7
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
description: 'Documentation for ALTER DATABASE ... MODIFY COMMENT Statements'
3+
slug: /sql-reference/statements/alter/database-comment
4+
sidebar_position: 51
5+
sidebar_label: 'UPDATE'
6+
title: 'ALTER DATABASE ... MODIFY COMMENT Statements'
7+
---
8+
9+
# ALTER DATABASE ... MODIFY COMMENT
10+
11+
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.
12+
13+
**Syntax**
14+
15+
``` sql
16+
ALTER DATABASE [db].name [ON CLUSTER cluster] MODIFY COMMENT 'Comment'
17+
```
18+
19+
**Examples**
20+
21+
Creating a DATABASE with comment (for more information, see the [COMMENT](/sql-reference/statements/create/table#comment-clause) clause):
22+
23+
``` sql
24+
CREATE DATABASE database_with_comment ENGINE = Memory COMMENT 'The temporary database';
25+
```
26+
27+
Modifying the table comment:
28+
29+
``` sql
30+
ALTER DATABASE database_with_comment MODIFY COMMENT 'new comment on a database';
31+
SELECT comment FROM system.databases WHERE name = 'database_with_comment';
32+
```
33+
34+
Output of a new comment:
35+
36+
```text
37+
┌─comment─────────────────┐
38+
│ new comment on database │
39+
└─────────────────────────┘
40+
```
41+
42+
Removing the database comment:
43+
44+
``` sql
45+
ALTER DATABASE database_with_comment MODIFY COMMENT '';
46+
SELECT comment FROM system.databases WHERE name = 'database_with_comment';
47+
```
48+
49+
Output of a removed comment:
50+
51+
```text
52+
┌─comment─┐
53+
│ │
54+
└─────────┘
55+
```

src/Access/Common/AccessType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ enum class AccessType : uint8_t
4343
M(ALTER_MATERIALIZE_COLUMN, "MATERIALIZE COLUMN", COLUMN, ALTER_COLUMN) \
4444
M(ALTER_COLUMN, "", GROUP, ALTER_TABLE) /* allow to execute ALTER {ADD|DROP|MODIFY...} COLUMN */\
4545
M(ALTER_MODIFY_COMMENT, "MODIFY COMMENT", TABLE, ALTER_TABLE) /* modify table comment */\
46+
M(ALTER_MODIFY_DATABASE_COMMENT, "MODIFY DATABASE COMMENT", DATABASE, ALTER_DATABASE) /* modify database comment*/\
4647
\
4748
M(ALTER_ORDER_BY, "ALTER MODIFY ORDER BY, MODIFY ORDER BY", TABLE, ALTER_INDEX) \
4849
M(ALTER_SAMPLE_BY, "ALTER MODIFY SAMPLE BY, MODIFY SAMPLE BY", TABLE, ALTER_INDEX) \

src/Databases/DatabaseMemory.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ std::vector<std::pair<ASTPtr, StoragePtr>> DatabaseMemory::getTablesForBackup(co
213213
return res;
214214
}
215215

216+
void DatabaseMemory::alterDatabaseComment(const AlterCommand & command)
217+
{
218+
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
219+
}
220+
216221
void registerDatabaseMemory(DatabaseFactory & factory)
217222
{
218223
auto create_fn = [](const DatabaseFactory::Arguments & args)

src/Databases/DatabaseMemory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class DatabaseMemory final : public DatabaseWithOwnTablesBase
5252

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

55+
void alterDatabaseComment(const AlterCommand & alter_command) override;
56+
5557
private:
5658
void removeDataPath(ContextPtr local_context);
5759

src/Databases/DatabaseOnDisk.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <Parsers/ASTFunction.h>
2424
#include <Parsers/ParserCreateQuery.h>
2525
#include <Parsers/parseQuery.h>
26+
#include <Storages/AlterCommands.h>
2627
#include <Storages/IStorage.h>
2728
#include <Storages/StorageFactory.h>
2829
#include <TableFunctions/TableFunctionFactory.h>
@@ -916,4 +917,9 @@ void DatabaseOnDisk::checkTableNameLengthUnlocked(const String & database_name_,
916917
}
917918
}
918919

920+
void DatabaseOnDisk::alterDatabaseComment(const AlterCommand & command)
921+
{
922+
DB::updateDatabaseCommentWithMetadataFile(shared_from_this(), command);
923+
}
924+
919925
}

src/Databases/DatabaseOnDisk.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace DB
1212
{
1313

1414
class Context;
15+
struct AlterCommand;
16+
1517
std::pair<String, StoragePtr> createTableFromAST(
1618
ASTCreateQuery ast_create_query,
1719
const String & database_name,
@@ -82,6 +84,8 @@ class DatabaseOnDisk : public DatabaseWithOwnTablesBase
8284

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

87+
void alterDatabaseComment(const AlterCommand & alter_command) override;
88+
8589
protected:
8690
static constexpr const char * create_suffix = ".tmp";
8791
static constexpr const char * drop_suffix = ".tmp_drop";

src/Databases/DatabasesCommon.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <Parsers/ASTSelectWithUnionQuery.h>
1313
#include <Parsers/ParserCreateQuery.h>
1414
#include <Parsers/parseQuery.h>
15+
#include <Storages/AlterCommands.h>
1516
#include <Storages/ColumnsDescription.h>
1617
#include <Storages/KeyDescription.h>
1718
#include <Storages/StorageDictionary.h>
@@ -42,6 +43,7 @@ namespace ErrorCodes
4243
extern const int NOT_IMPLEMENTED;
4344
extern const int LOGICAL_ERROR;
4445
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
46+
extern const int BAD_ARGUMENTS;
4547
}
4648
namespace
4749
{
@@ -324,6 +326,24 @@ void writeMetadataFile(std::shared_ptr<IDisk> db_disk, const String & file_path,
324326
out.reset();
325327
}
326328

329+
void updateDatabaseCommentWithMetadataFile(DatabasePtr db, const AlterCommand & command)
330+
{
331+
if (!command.comment)
332+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unable to obtain database comment from query");
333+
334+
String old_database_comment = db->getDatabaseComment();
335+
db->setDatabaseComment(command.comment.value());
336+
337+
try
338+
{
339+
DatabaseCatalog::instance().updateMetadataFile(db);
340+
}
341+
catch (...)
342+
{
343+
db->setDatabaseComment(old_database_comment);
344+
throw;
345+
}
346+
}
327347

328348
DatabaseWithOwnTablesBase::DatabaseWithOwnTablesBase(const String & name_, const String & logger, ContextPtr context_)
329349
: IDatabase(name_), WithContext(context_->getGlobalContext()), db_disk(context_->getDatabaseDisk()), log(getLogger(logger))

src/Databases/DatabasesCommon.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void cleanupObjectDefinitionFromTemporaryFlags(ASTCreateQuery & query);
2323
String readMetadataFile(std::shared_ptr<IDisk> db_disk, const String & file_path);
2424
void writeMetadataFile(std::shared_ptr<IDisk> db_disk, const String & file_path, std::string_view content, bool fsync_metadata);
2525

26+
void updateDatabaseCommentWithMetadataFile(DatabasePtr db, const AlterCommand & command);
27+
2628
/// A base class for databases that manage their own list of tables.
2729
class DatabaseWithOwnTablesBase : public IDatabase, protected WithContext
2830
{

src/Databases/IDatabase.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ IDatabase::~IDatabase()
5858
CurrentMetrics::sub(CurrentMetrics::AttachedDatabase, 1);
5959
}
6060

61+
void IDatabase::alterDatabaseComment(const AlterCommand & /*command*/)
62+
{
63+
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "{}: ALTER DATABASE COMMENT is not supported", getEngineName());
64+
}
65+
6166
std::vector<std::pair<ASTPtr, StoragePtr>> IDatabase::getTablesForBackup(const FilterByNameFunction &, const ContextPtr &) const
6267
{
6368
/// Cannot backup any table because IDatabase doesn't own any tables.

src/Databases/IDatabase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
namespace DB
2222
{
2323

24+
2425
struct Settings;
2526
struct ConstraintsDescription;
2627
struct IndicesDescription;
2728
struct StorageInMemoryMetadata;
2829
struct StorageID;
2930
class ASTCreateQuery;
31+
struct AlterCommand;
3032
class AlterCommands;
3133
class SettingsChanges;
3234
using DictionariesWithID = std::vector<std::pair<String, UUID>>;
@@ -376,6 +378,9 @@ class IDatabase : public std::enable_shared_from_this<IDatabase>
376378
return database_name;
377379
}
378380

381+
// Alter comment of database.
382+
virtual void alterDatabaseComment(const AlterCommand &);
383+
379384
/// Get UUID of database.
380385
virtual UUID getUUID() const { return UUIDHelpers::Nil; }
381386

0 commit comments

Comments
 (0)