Skip to content

Commit 6c69d5e

Browse files
Backport #87392 to 25.9: Fix EmbeddedRocksDB upgrade
1 parent 35d0660 commit 6c69d5e

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

src/Storages/RocksDB/StorageEmbeddedRocksDB.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,25 @@ StorageEmbeddedRocksDB::StorageEmbeddedRocksDB(const StorageID & table_id_,
213213
setSettings(std::move(settings_));
214214

215215
if (rocksdb_dir.empty())
216-
rocksdb_dir = fs::path{getContext()->getUserFilesPath()} / relative_data_path_;
217-
218-
bool is_local = context_->getApplicationType() == Context::ApplicationType::LOCAL;
219-
fs::path user_files_path = is_local ? "" : fs::canonical(getContext()->getUserFilesPath());
220-
if (fs::path(rocksdb_dir).is_relative())
221-
rocksdb_dir = user_files_path / rocksdb_dir;
222-
rocksdb_dir = fs::absolute(rocksdb_dir).lexically_normal();
223-
224-
if (!is_local && !pathStartsWith(fs::path(rocksdb_dir), user_files_path))
225-
throw Exception(ErrorCodes::BAD_ARGUMENTS,
226-
"Path must be inside user-files path: {}", user_files_path.string());
216+
{
217+
/// We used to create databases under the database directory by default instead of user files. Check first if it exists there
218+
auto old_path = context_->getPath() + relative_data_path_;
219+
if (mode >= LoadingStrictnessLevel::ATTACH && fs::exists(old_path))
220+
rocksdb_dir = old_path;
221+
else
222+
rocksdb_dir = fs::path{getContext()->getUserFilesPath()} / relative_data_path_;
223+
}
224+
else
225+
{
226+
bool is_local = context_->getApplicationType() == Context::ApplicationType::LOCAL;
227+
fs::path user_files_path = is_local ? "" : fs::canonical(getContext()->getUserFilesPath());
228+
if (fs::path(rocksdb_dir).is_relative())
229+
rocksdb_dir = user_files_path / rocksdb_dir;
230+
rocksdb_dir = fs::absolute(rocksdb_dir).lexically_normal();
231+
232+
if (!is_local && !pathStartsWith(fs::path(rocksdb_dir), user_files_path))
233+
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Path must be inside user-files path: {}", user_files_path.string());
234+
}
227235

228236
if (mode < LoadingStrictnessLevel::ATTACH)
229237
{
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
3+
from helpers.cluster import CLICKHOUSE_CI_MIN_TESTED_VERSION, ClickHouseCluster
4+
5+
cluster = ClickHouseCluster(__file__)
6+
node = cluster.add_instance(
7+
"node",
8+
with_zookeeper=False,
9+
image="clickhouse/clickhouse-server",
10+
tag=CLICKHOUSE_CI_MIN_TESTED_VERSION,
11+
stay_alive=True,
12+
with_installed_binary=True,
13+
)
14+
15+
16+
@pytest.fixture(scope="module")
17+
def start_cluster():
18+
try:
19+
cluster.start()
20+
yield cluster
21+
22+
finally:
23+
cluster.shutdown()
24+
25+
26+
@pytest.fixture(autouse=True)
27+
def cleanup():
28+
yield
29+
node.restart_with_original_version(clear_data_dir=True)
30+
31+
32+
def test_rocksdb_upgrade(start_cluster):
33+
node.query(
34+
"""
35+
CREATE TABLE vt
36+
(
37+
`sha256` FixedString(64),
38+
`amount` Nullable(Int8)
39+
)
40+
ENGINE = EmbeddedRocksDB
41+
PRIMARY KEY sha256
42+
"""
43+
)
44+
45+
assert node.query("SELECT count() FROM vt") == "0\n"
46+
47+
node.restart_with_latest_version()
48+
49+
assert node.query("SELECT count() FROM vt") == "0\n"
50+
51+
node.query("""DROP TABLE vt""")

0 commit comments

Comments
 (0)