Skip to content

Commit

Permalink
Make test of CreateTables using affected opts (#4239)
Browse files Browse the repository at this point in the history
close #4235
  • Loading branch information
CalvinNeo authored Mar 14, 2022
1 parent 3d75154 commit 18b4b04
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion dbms/src/Debug/MockTiDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,45 @@ int MockTiDB::newTables(
Timestamp tso,
const String & engine_type)
{
std::lock_guard lock(tables_mutex);
if (databases.find(database_name) == databases.end())
{
throw Exception("MockTiDB not found db: " + database_name, ErrorCodes::LOGICAL_ERROR);
}

version++;
SchemaDiff diff;
diff.type = SchemaActionType::CreateTables;
for (const auto & [table_name, columns, handle_pk_name] : tables)
{
newTable(database_name, table_name, columns, tso, handle_pk_name, engine_type);
String qualified_name = database_name + "." + table_name;
if (tables_by_name.find(qualified_name) != tables_by_name.end())
{
throw Exception("Mock TiDB table " + qualified_name + " already exists", ErrorCodes::TABLE_ALREADY_EXISTS);
}

auto table_info = *parseColumns(table_name, columns, handle_pk_name, engine_type);
table_info.id = table_id_allocator++;
table_info.update_timestamp = tso;

auto table = std::make_shared<Table>(database_name, databases[database_name], table_info.name, std::move(table_info));
tables_by_id.emplace(table->table_info.id, table);
tables_by_name.emplace(qualified_name, table);

AffectedOption opt;
opt.schema_id = table->database_id;
opt.table_id = table->id();
opt.old_schema_id = table->database_id;
opt.old_table_id = table->id();
diff.affected_opts.push_back(std::move(opt));
}

if (diff.affected_opts.empty())
throw Exception("MockTiDB CreateTables should have at lease 1 table", ErrorCodes::LOGICAL_ERROR);

diff.schema_id = diff.affected_opts[0].schema_id;
diff.version = version;
version_diff[version] = diff;
return 0;
}

Expand Down

0 comments on commit 18b4b04

Please sign in to comment.