2019-10-02 10:10:45 +00:00
|
|
|
#include <Databases/DatabaseOnDisk.h>
|
2019-10-03 09:31:59 +00:00
|
|
|
|
2019-10-02 10:10:45 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteBufferFromFile.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2019-10-10 17:33:01 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
2019-10-15 18:04:17 +00:00
|
|
|
#include <Interpreters/ExternalDictionariesLoader.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2019-10-03 09:31:59 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
2019-10-10 17:33:01 +00:00
|
|
|
#include <Parsers/formatAST.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2019-10-10 17:33:01 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
|
|
|
|
#include <common/logger_useful.h>
|
2019-10-03 09:31:59 +00:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-26 19:06:19 +00:00
|
|
|
#include <Databases/DatabaseOrdinary.h>
|
|
|
|
#include <Databases/DatabaseAtomic.h>
|
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-10-02 10:10:45 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-10-10 17:33:01 +00:00
|
|
|
static constexpr size_t METADATA_FILE_BUFFER_SIZE = 32768;
|
|
|
|
|
2019-10-02 10:10:45 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2019-10-03 08:27:43 +00:00
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
extern const int INCORRECT_FILE_NAME;
|
2019-10-02 10:10:45 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
2019-10-03 08:27:43 +00:00
|
|
|
extern const int TABLE_ALREADY_EXISTS;
|
|
|
|
extern const int UNKNOWN_TABLE;
|
2019-10-10 17:33:01 +00:00
|
|
|
extern const int DICTIONARY_ALREADY_EXISTS;
|
|
|
|
extern const int EMPTY_LIST_OF_COLUMNS_PASSED;
|
2019-10-02 10:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-10 17:33:01 +00:00
|
|
|
ASTPtr parseCreateQueryFromMetadataFile(const String & filepath, Poco::Logger * log)
|
|
|
|
{
|
|
|
|
String definition;
|
|
|
|
{
|
|
|
|
char in_buf[METADATA_FILE_BUFFER_SIZE];
|
|
|
|
ReadBufferFromFile in(filepath, METADATA_FILE_BUFFER_SIZE, -1, in_buf);
|
|
|
|
readStringUntilEOF(definition, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Empty files with metadata are generated after a rough restart of the server.
|
|
|
|
* Remove these files to slightly reduce the work of the admins on startup.
|
|
|
|
*/
|
|
|
|
if (definition.empty())
|
|
|
|
{
|
|
|
|
LOG_ERROR(log, "File " << filepath << " is empty. Removing.");
|
|
|
|
Poco::File(filepath).remove();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-11 15:22:30 +00:00
|
|
|
ParserCreateQuery parser_create;
|
|
|
|
ASTPtr result = parseQuery(parser_create, definition, "in file " + filepath, 0);
|
2019-10-10 17:33:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<String, StoragePtr> createTableFromAST(
|
|
|
|
ASTCreateQuery ast_create_query,
|
|
|
|
const String & database_name,
|
2019-11-01 12:47:55 +00:00
|
|
|
const String & table_data_path_relative,
|
2019-10-10 17:33:01 +00:00
|
|
|
Context & context,
|
|
|
|
bool has_force_restore_data_flag)
|
|
|
|
{
|
|
|
|
ast_create_query.attach = true;
|
|
|
|
ast_create_query.database = database_name;
|
|
|
|
|
|
|
|
if (ast_create_query.as_table_function)
|
|
|
|
{
|
|
|
|
const auto & table_function = ast_create_query.as_table_function->as<ASTFunction &>();
|
|
|
|
const auto & factory = TableFunctionFactory::instance();
|
|
|
|
StoragePtr storage = factory.get(table_function.name, context)->execute(ast_create_query.as_table_function, context, ast_create_query.table);
|
|
|
|
return {ast_create_query.table, storage};
|
|
|
|
}
|
|
|
|
/// We do not directly use `InterpreterCreateQuery::execute`, because
|
|
|
|
/// - the database has not been created yet;
|
|
|
|
/// - the code is simpler, since the query is already brought to a suitable form.
|
|
|
|
if (!ast_create_query.columns_list || !ast_create_query.columns_list->columns)
|
|
|
|
throw Exception("Missing definition of columns.", ErrorCodes::EMPTY_LIST_OF_COLUMNS_PASSED);
|
|
|
|
|
|
|
|
ColumnsDescription columns = InterpreterCreateQuery::getColumnsDescription(*ast_create_query.columns_list->columns, context);
|
|
|
|
ConstraintsDescription constraints = InterpreterCreateQuery::getConstraintsDescription(ast_create_query.columns_list->constraints);
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
ast_create_query.table,
|
|
|
|
StorageFactory::instance().get(
|
|
|
|
ast_create_query,
|
2019-10-30 12:17:52 +00:00
|
|
|
table_data_path_relative, ast_create_query.table, database_name, context, context.getGlobalContext(),
|
2019-10-10 17:33:01 +00:00
|
|
|
columns, constraints,
|
|
|
|
true, has_force_restore_data_flag)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String getObjectDefinitionFromCreateQuery(const ASTPtr & query)
|
|
|
|
{
|
|
|
|
ASTPtr query_clone = query->clone();
|
2019-10-22 10:47:43 +00:00
|
|
|
auto * create = query_clone->as<ASTCreateQuery>();
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-10-22 10:47:43 +00:00
|
|
|
if (!create)
|
|
|
|
{
|
|
|
|
std::ostringstream query_stream;
|
2019-11-07 08:52:22 +00:00
|
|
|
formatAST(*query, query_stream, true);
|
2019-10-22 10:47:43 +00:00
|
|
|
throw Exception("Query '" + query_stream.str() + "' is not CREATE query", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create->is_dictionary)
|
|
|
|
create->attach = true;
|
2019-10-11 15:22:30 +00:00
|
|
|
|
2019-10-10 17:33:01 +00:00
|
|
|
/// We remove everything that is not needed for ATTACH from the query.
|
2019-10-22 10:47:43 +00:00
|
|
|
create->database.clear();
|
|
|
|
create->as_database.clear();
|
|
|
|
create->as_table.clear();
|
|
|
|
create->if_not_exists = false;
|
|
|
|
create->is_populate = false;
|
|
|
|
create->replace_view = false;
|
2019-10-10 17:33:01 +00:00
|
|
|
|
|
|
|
/// For views it is necessary to save the SELECT query itself, for the rest - on the contrary
|
2019-10-22 10:47:43 +00:00
|
|
|
if (!create->is_view && !create->is_materialized_view && !create->is_live_view)
|
|
|
|
create->select = nullptr;
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-10-22 10:47:43 +00:00
|
|
|
create->format = nullptr;
|
|
|
|
create->out_file = nullptr;
|
2019-10-10 17:33:01 +00:00
|
|
|
|
|
|
|
std::ostringstream statement_stream;
|
2019-10-22 10:47:43 +00:00
|
|
|
formatAST(*create, statement_stream, false);
|
2019-10-10 17:33:01 +00:00
|
|
|
statement_stream << '\n';
|
|
|
|
return statement_stream.str();
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:40:51 +00:00
|
|
|
DatabaseOnDisk::DatabaseOnDisk(const String & name, const String & metadata_path_, const String & logger, const Context & context_)
|
|
|
|
: DatabaseWithOwnTablesBase(name, logger)
|
|
|
|
, metadata_path(metadata_path_)
|
|
|
|
, data_path("data/" + escapeForFileName(database_name) + "/")
|
|
|
|
{
|
|
|
|
Poco::File(context_.getPath() + getDataPath()).createDirectories();
|
|
|
|
Poco::File(getMetadataPath()).createDirectories();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
void DatabaseOnDisk::createTable(
|
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
const StoragePtr & table,
|
|
|
|
const ASTPtr & query)
|
|
|
|
{
|
|
|
|
const auto & settings = context.getSettingsRef();
|
|
|
|
|
|
|
|
/// Create a file with metadata if necessary - if the query is not ATTACH.
|
|
|
|
/// Write the query of `ATTACH table` to it.
|
|
|
|
|
|
|
|
/** The code is based on the assumption that all threads share the same order of operations
|
|
|
|
* - creating the .sql.tmp file;
|
|
|
|
* - adding a table to `tables`;
|
|
|
|
* - rename .sql.tmp to .sql.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// A race condition would be possible if a table with the same name is simultaneously created using CREATE and using ATTACH.
|
|
|
|
/// But there is protection from it - see using DDLGuard in InterpreterCreateQuery.
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
if (isDictionaryExist(context, table_name))
|
|
|
|
throw Exception("Dictionary " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " already exists.",
|
2019-10-10 17:33:01 +00:00
|
|
|
ErrorCodes::DICTIONARY_ALREADY_EXISTS);
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
if (isTableExist(context, table_name))
|
|
|
|
throw Exception("Table " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " already exists.", ErrorCodes::TABLE_ALREADY_EXISTS);
|
2019-10-03 07:59:48 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String table_metadata_path = getObjectMetadataPath(table_name);
|
2019-10-03 07:59:48 +00:00
|
|
|
String table_metadata_tmp_path = table_metadata_path + ".tmp";
|
|
|
|
String statement;
|
|
|
|
|
|
|
|
{
|
2019-10-10 17:33:01 +00:00
|
|
|
statement = getObjectDefinitionFromCreateQuery(query);
|
2019-10-03 07:59:48 +00:00
|
|
|
|
|
|
|
/// Exclusive flags guarantees, that table is not created right now in another thread. Otherwise, exception will be thrown.
|
|
|
|
WriteBufferFromFile out(table_metadata_tmp_path, statement.size(), O_WRONLY | O_CREAT | O_EXCL);
|
|
|
|
writeString(statement, out);
|
|
|
|
out.next();
|
|
|
|
if (settings.fsync_metadata)
|
|
|
|
out.sync();
|
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/// Add a table to the map of known tables.
|
2019-11-11 11:34:03 +00:00
|
|
|
attachTable(table_name, table, getDataPath(query->as<ASTCreateQuery &>()));
|
2019-10-03 07:59:48 +00:00
|
|
|
|
|
|
|
/// If it was ATTACH query and file with table metadata already exist
|
|
|
|
/// (so, ATTACH is done after DETACH), then rename atomically replaces old file with new one.
|
|
|
|
Poco::File(table_metadata_tmp_path).renameTo(table_metadata_path);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Poco::File(table_metadata_tmp_path).remove();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
void DatabaseOnDisk::removeTable(const Context & /* context */, const String & table_name)
|
2019-10-03 07:59:48 +00:00
|
|
|
{
|
2019-11-11 11:34:03 +00:00
|
|
|
String table_data = getDataPath(table_name);
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
StoragePtr res = detachTable(table_name);
|
2019-10-03 07:59:48 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String table_metadata_path = getObjectMetadataPath(table_name);
|
2019-10-03 07:59:48 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Poco::File(table_metadata_path).remove();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Poco::File(table_metadata_path + ".tmp_drop").remove();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, getCurrentExceptionMessage(__PRETTY_FUNCTION__));
|
|
|
|
}
|
2019-11-11 11:34:03 +00:00
|
|
|
attachTable(table_name, res, data_path);
|
2019-10-03 07:59:48 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
void DatabaseOnDisk::renameTable(
|
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
IDatabase & to_database,
|
|
|
|
const String & to_table_name,
|
|
|
|
TableStructureWriteLockHolder & lock)
|
2019-10-10 17:33:01 +00:00
|
|
|
{
|
2019-11-26 19:06:19 +00:00
|
|
|
bool from_ordinary_to_atomic = false;
|
2019-11-06 16:05:04 +00:00
|
|
|
if (typeid(*this) != typeid(to_database))
|
2019-11-26 19:06:19 +00:00
|
|
|
{
|
|
|
|
if (typeid_cast<DatabaseOrdinary *>(this) && typeid_cast<DatabaseAtomic *>(&to_database))
|
|
|
|
from_ordinary_to_atomic = true;
|
|
|
|
else
|
|
|
|
throw Exception("Moving tables between databases of different engines is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
2019-11-06 16:05:04 +00:00
|
|
|
|
|
|
|
StoragePtr table = tryGetTable(context, table_name);
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
if (!table)
|
|
|
|
throw Exception("Table " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-25 18:46:02 +00:00
|
|
|
ASTPtr ast = getQueryFromMetadata(getObjectMetadataPath(table_name));
|
|
|
|
if (!ast)
|
|
|
|
throw Exception("There is no metadata file for table " + backQuote(table_name) + ".", ErrorCodes::FILE_DOESNT_EXIST);
|
|
|
|
auto & create = ast->as<ASTCreateQuery &>();
|
|
|
|
create.table = to_table_name;
|
2019-11-26 19:06:19 +00:00
|
|
|
if (from_ordinary_to_atomic)
|
|
|
|
create.uuid = boost::uuids::to_string(boost::uuids::random_generator()());
|
2019-11-25 18:46:02 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
/// Notify the table that it is renamed. If the table does not support renaming, exception is thrown.
|
2019-10-10 17:33:01 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-25 18:46:02 +00:00
|
|
|
table->rename(to_database.getDataPath(create),
|
2019-11-06 16:05:04 +00:00
|
|
|
to_database.getDatabaseName(),
|
|
|
|
to_table_name, lock);
|
2019-10-10 17:33:01 +00:00
|
|
|
}
|
2019-11-06 16:05:04 +00:00
|
|
|
catch (const Exception &)
|
2019-10-10 17:33:01 +00:00
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2019-11-06 16:05:04 +00:00
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
/// Better diagnostics.
|
|
|
|
throw Exception{Exception::CreateFromPoco, e};
|
|
|
|
}
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
/// NOTE Non-atomic.
|
|
|
|
to_database.createTable(context, to_table_name, table, ast);
|
|
|
|
removeTable(context, table_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr DatabaseOnDisk::getCreateTableQueryImpl(const Context & context, const String & table_name, bool throw_on_error) const
|
2019-10-03 07:59:48 +00:00
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
auto table_metadata_path = getObjectMetadataPath(table_name);
|
|
|
|
ast = getCreateQueryFromMetadata(table_metadata_path, throw_on_error);
|
2019-10-03 07:59:48 +00:00
|
|
|
if (!ast && throw_on_error)
|
|
|
|
{
|
|
|
|
/// Handle system.* tables for which there are no table.sql files.
|
2019-11-06 16:05:04 +00:00
|
|
|
bool has_table = tryGetTable(context, table_name) != nullptr;
|
2019-10-03 07:59:48 +00:00
|
|
|
|
|
|
|
auto msg = has_table
|
|
|
|
? "There is no CREATE TABLE query for table "
|
|
|
|
: "There is no metadata file for table ";
|
|
|
|
|
2019-10-08 08:10:55 +00:00
|
|
|
throw Exception(msg + backQuote(table_name), ErrorCodes::CANNOT_GET_CREATE_TABLE_QUERY);
|
2019-10-03 07:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
ASTPtr DatabaseOnDisk::getCreateDatabaseQuery(const Context & /*context*/) const
|
2019-10-03 07:59:48 +00:00
|
|
|
{
|
|
|
|
ASTPtr ast;
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
auto database_metadata_path = getDatabaseMetadataPath(getMetadataPath());
|
|
|
|
ast = getCreateQueryFromMetadata(database_metadata_path, true);
|
2019-10-03 07:59:48 +00:00
|
|
|
if (!ast)
|
|
|
|
{
|
|
|
|
/// Handle databases (such as default) for which there are no database.sql files.
|
2019-11-06 16:05:04 +00:00
|
|
|
//FIXME WTF
|
|
|
|
String query = "CREATE DATABASE " + backQuoteIfNeed(getDatabaseName()) + " ENGINE = Lazy";
|
2019-10-03 07:59:48 +00:00
|
|
|
ParserCreateQuery parser;
|
|
|
|
ast = parseQuery(parser, query.data(), query.data() + query.size(), "", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
void DatabaseOnDisk::drop(const Context & context)
|
2019-10-03 07:59:48 +00:00
|
|
|
{
|
2019-11-06 16:05:04 +00:00
|
|
|
Poco::File(context.getPath() + getDataPath()).remove(false);
|
|
|
|
Poco::File(getMetadataPath()).remove(false);
|
2019-10-03 07:59:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String DatabaseOnDisk::getObjectMetadataPath(const String & table_name) const
|
2019-10-03 07:59:48 +00:00
|
|
|
{
|
2019-11-06 16:05:04 +00:00
|
|
|
String base_path = getMetadataPath();
|
|
|
|
//FIXME
|
|
|
|
return base_path + (endsWith(base_path, "/") ? "" : "/") + escapeForFileName(table_name) + ".sql";
|
2019-10-02 10:10:45 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
time_t DatabaseOnDisk::getObjectMetadataModificationTime(const String & table_name) const
|
2019-10-03 08:35:58 +00:00
|
|
|
{
|
2019-11-06 16:05:04 +00:00
|
|
|
String table_metadata_path = getObjectMetadataPath(table_name);
|
2019-10-03 08:35:58 +00:00
|
|
|
Poco::File meta_file(table_metadata_path);
|
|
|
|
|
|
|
|
if (meta_file.exists())
|
|
|
|
return meta_file.getLastModified().epochTime();
|
|
|
|
else
|
|
|
|
return static_cast<time_t>(0);
|
|
|
|
}
|
|
|
|
|
2019-11-11 11:34:03 +00:00
|
|
|
void DatabaseOnDisk::iterateMetadataFiles(const Context & /*context*/, const IteratingFunction & iterating_function) const
|
2019-10-03 08:27:43 +00:00
|
|
|
{
|
|
|
|
Poco::DirectoryIterator dir_end;
|
2019-11-06 16:05:04 +00:00
|
|
|
for (Poco::DirectoryIterator dir_it(getMetadataPath()); dir_it != dir_end; ++dir_it)
|
2019-10-03 08:27:43 +00:00
|
|
|
{
|
|
|
|
/// For '.svn', '.gitignore' directory and similar.
|
|
|
|
if (dir_it.name().at(0) == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/// There are .sql.bak files - skip them.
|
|
|
|
if (endsWith(dir_it.name(), ".sql.bak"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// There are files that we tried to delete previously
|
|
|
|
static const char * tmp_drop_ext = ".sql.tmp_drop";
|
|
|
|
if (endsWith(dir_it.name(), tmp_drop_ext))
|
|
|
|
{
|
2019-11-11 11:34:03 +00:00
|
|
|
//const std::string object_name = dir_it.name().substr(0, dir_it.name().size() - strlen(tmp_drop_ext));
|
|
|
|
|
|
|
|
//FIXME
|
|
|
|
//if (Poco::File(context.getPath() + getDataPath(object_name)).exists())
|
|
|
|
//{
|
|
|
|
// Poco::File(dir_it->path()).renameTo(object_name + ".sql");
|
|
|
|
// LOG_WARNING(log, "Object " << backQuote(object_name) << " was not dropped previously");
|
|
|
|
//}
|
|
|
|
//else
|
|
|
|
//{
|
|
|
|
// LOG_INFO(log, "Removing file " << dir_it->path());
|
|
|
|
// Poco::File(dir_it->path()).remove();
|
|
|
|
//}
|
2019-10-03 08:27:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// There are files .sql.tmp - delete
|
|
|
|
if (endsWith(dir_it.name(), ".sql.tmp"))
|
|
|
|
{
|
|
|
|
LOG_INFO(log, "Removing file " << dir_it->path());
|
|
|
|
Poco::File(dir_it->path()).remove();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The required files have names like `table_name.sql`
|
|
|
|
if (endsWith(dir_it.name(), ".sql"))
|
|
|
|
{
|
|
|
|
iterating_function(dir_it.name());
|
|
|
|
}
|
|
|
|
else
|
2019-11-06 16:05:04 +00:00
|
|
|
throw Exception("Incorrect file extension: " + dir_it.name() + " in metadata directory " + getMetadataPath(),
|
2019-10-03 08:27:43 +00:00
|
|
|
ErrorCodes::INCORRECT_FILE_NAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String DatabaseOnDisk::getDatabaseMetadataPath(const String & base_path) const
|
|
|
|
{
|
|
|
|
//FIXME
|
|
|
|
return (endsWith(base_path, "/") ? base_path.substr(0, base_path.size() - 1) : base_path) + ".sql";
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr DatabaseOnDisk::getQueryFromMetadata(const String & database_metadata_path, bool throw_on_error) const
|
|
|
|
{
|
|
|
|
String query;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ReadBufferFromFile in(database_metadata_path, 4096);
|
|
|
|
readStringUntilEOF(query, in);
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (!throw_on_error && e.code() == ErrorCodes::FILE_DOESNT_EXIST)
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParserCreateQuery parser;
|
|
|
|
const char * pos = query.data();
|
|
|
|
std::string error_message;
|
|
|
|
auto ast = tryParseQuery(parser, pos, pos + query.size(), error_message, /* hilite = */ false,
|
|
|
|
"in file " + metadata_path, /* allow_multi_statements = */ false, 0);
|
|
|
|
|
|
|
|
if (!ast && throw_on_error)
|
|
|
|
throw Exception(error_message, ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr DatabaseOnDisk::getCreateQueryFromMetadata(const String & database_metadata_path, bool throw_on_error) const
|
|
|
|
{
|
|
|
|
ASTPtr ast = getQueryFromMetadata(database_metadata_path, throw_on_error);
|
|
|
|
|
|
|
|
if (ast)
|
|
|
|
{
|
|
|
|
auto & ast_create_query = ast->as<ASTCreateQuery &>();
|
|
|
|
ast_create_query.attach = false;
|
|
|
|
ast_create_query.database = database_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2019-11-11 11:34:03 +00:00
|
|
|
String DatabaseOnDisk::getDataPath(const String & table_name) const
|
|
|
|
{
|
|
|
|
return data_path + escapeForFileName(table_name) + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
String DatabaseOnDisk::getDataPath(const ASTCreateQuery & query) const
|
|
|
|
{
|
|
|
|
return getDataPath(query.table);
|
|
|
|
}
|
|
|
|
|
2019-10-05 10:21:47 +00:00
|
|
|
}
|