mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-20 08:40:50 +00:00
Merge pull request #34907 from kssenii/sqlite-db-allow-no-existing-db
This commit is contained in:
commit
9bceba5aa8
@ -23,35 +23,36 @@ void processSQLiteError(const String & message, bool throw_on_error)
|
||||
LOG_ERROR(&Poco::Logger::get("SQLiteEngine"), fmt::runtime(message));
|
||||
}
|
||||
|
||||
|
||||
String validateSQLiteDatabasePath(const String & path, const String & user_files_path, bool throw_on_error)
|
||||
{
|
||||
String canonical_user_files_path = fs::canonical(user_files_path);
|
||||
|
||||
String canonical_path;
|
||||
std::error_code err;
|
||||
|
||||
if (fs::path(path).is_relative())
|
||||
canonical_path = fs::canonical(fs::path(user_files_path) / path, err);
|
||||
else
|
||||
canonical_path = fs::canonical(path, err);
|
||||
return fs::absolute(fs::path(user_files_path) / path).lexically_normal();
|
||||
|
||||
if (err)
|
||||
processSQLiteError(fmt::format("SQLite database path '{}' is invalid. Error: {}", path, err.message()), throw_on_error);
|
||||
String absolute_path = fs::absolute(path).lexically_normal();
|
||||
String absolute_user_files_path = fs::absolute(user_files_path).lexically_normal();
|
||||
|
||||
if (!canonical_path.starts_with(canonical_user_files_path))
|
||||
if (!absolute_path.starts_with(absolute_user_files_path))
|
||||
{
|
||||
processSQLiteError(fmt::format("SQLite database file path '{}' must be inside 'user_files' directory", path), throw_on_error);
|
||||
|
||||
return canonical_path;
|
||||
return "";
|
||||
}
|
||||
return absolute_path;
|
||||
}
|
||||
|
||||
|
||||
SQLitePtr openSQLiteDB(const String & database_path, ContextPtr context, bool throw_on_error)
|
||||
SQLitePtr openSQLiteDB(const String & path, ContextPtr context, bool throw_on_error)
|
||||
{
|
||||
auto validated_path = validateSQLiteDatabasePath(database_path, context->getUserFilesPath(), throw_on_error);
|
||||
auto user_files_path = context->getUserFilesPath();
|
||||
auto database_path = validateSQLiteDatabasePath(path, user_files_path, throw_on_error);
|
||||
|
||||
/// For attach database there is no throw mode.
|
||||
if (database_path.empty())
|
||||
return nullptr;
|
||||
|
||||
if (!fs::exists(database_path))
|
||||
LOG_DEBUG(&Poco::Logger::get("SQLite"), "SQLite database path {} does not exist, will create an empty SQLite database", database_path);
|
||||
|
||||
sqlite3 * tmp_sqlite_db = nullptr;
|
||||
int status = sqlite3_open(validated_path.c_str(), &tmp_sqlite_db);
|
||||
int status = sqlite3_open(database_path.c_str(), &tmp_sqlite_db);
|
||||
|
||||
if (status != SQLITE_OK)
|
||||
{
|
||||
|
@ -0,0 +1 @@
|
||||
table1
|
32
tests/queries/0_stateless/02227_test_create_empty_sqlite_db.sh
Executable file
32
tests/queries/0_stateless/02227_test_create_empty_sqlite_db.sh
Executable file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tags: no-fasttest
|
||||
|
||||
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
# shellcheck source=../shell_config.sh
|
||||
. "$CUR_DIR"/../shell_config.sh
|
||||
|
||||
# See 01658_read_file_to_string_column.sh
|
||||
user_files_path=$(clickhouse-client --query "select _path,_file from file('nonexist.txt', 'CSV', 'val1 char')" 2>&1 | grep Exception | awk '{gsub("/nonexist.txt","",$9); print $9}')
|
||||
|
||||
function cleanup()
|
||||
{
|
||||
${CLICKHOUSE_CLIENT} --query="DROP DATABASE IF EXISTS ${CURR_DATABASE}"
|
||||
rm -r "${DB_PATH}"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
export CURR_DATABASE="test_01889_sqllite_${CLICKHOUSE_DATABASE}"
|
||||
|
||||
DB_PATH=${user_files_path}/${CURR_DATABASE}_db1
|
||||
|
||||
${CLICKHOUSE_CLIENT} --multiquery --multiline --query="""
|
||||
DROP DATABASE IF EXISTS ${CURR_DATABASE};
|
||||
CREATE DATABASE ${CURR_DATABASE} ENGINE = SQLite('${DB_PATH}');
|
||||
SHOW TABLES FROM ${CURR_DATABASE};
|
||||
"""
|
||||
|
||||
sqlite3 "${DB_PATH}" 'CREATE TABLE table1 (col1 text, col2 smallint);'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --multiquery --multiline --query="""
|
||||
SHOW TABLES FROM ${CURR_DATABASE};
|
||||
"""
|
Loading…
Reference in New Issue
Block a user