Skip to content

Commit c396d2a

Browse files
Pierre-Luc Gagnéclaude
andcommitted
fix: qualify table names with db prefix in create_all_tables
When chaining create_database().then().create_all_tables<DB>(), the generated CREATE TABLE statements now use <db_name>.<table_name> so MySQL can resolve the target schema without a current database selected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 0f873ac commit c396d2a

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

lib/include/ds_mysql/sql_ddl.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,17 @@ concept BuildsSql = requires(T const& t) {
11141114
};
11151115

11161116
template <typename Table>
1117-
void append_create_table_sql(std::string& sql, bool if_not_exists) {
1117+
void append_create_table_sql(std::string& sql, bool if_not_exists, std::string_view db_name = {}) {
11181118
const auto table_name = table_name_for<Table>::value().to_string_view();
11191119
const auto column_defs = make_column_defs<Table>();
11201120
sql += "CREATE TABLE ";
11211121
if (if_not_exists) {
11221122
sql += "IF NOT EXISTS ";
11231123
}
1124+
if (!db_name.empty()) {
1125+
sql += db_name;
1126+
sql += '.';
1127+
}
11241128
sql += table_name;
11251129
sql += " (\n";
11261130
sql += column_defs;
@@ -1130,7 +1134,8 @@ void append_create_table_sql(std::string& sql, bool if_not_exists) {
11301134
template <Database DB, bool IfNotExists, std::size_t... Is>
11311135
std::string build_create_all_tables_sql_impl(std::string prior_sql, std::index_sequence<Is...>) {
11321136
using tables_tuple = typename database_tables<DB>::type;
1133-
(append_create_table_sql<std::tuple_element_t<Is, tables_tuple>>(prior_sql, IfNotExists), ...);
1137+
const auto db_name = database_name_for<DB>::value();
1138+
(append_create_table_sql<std::tuple_element_t<Is, tables_tuple>>(prior_sql, IfNotExists, db_name), ...);
11341139
return prior_sql;
11351140
}
11361141

tests/unit/test_ddl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,10 @@ suite<"DDL CREATE DATABASE"> ddl_create_database_suite = [] {
917917
.build_sql();
918918
expect(sql ==
919919
"CREATE DATABASE IF NOT EXISTS schema_bootstrap_db;\n"
920-
"CREATE TABLE account (\n"
920+
"CREATE TABLE schema_bootstrap_db.account (\n"
921921
" id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT\n"
922922
");\n"
923-
"CREATE TABLE trade (\n"
923+
"CREATE TABLE schema_bootstrap_db.trade (\n"
924924
" id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT\n"
925925
");\n"s)
926926
<< sql;
@@ -935,10 +935,10 @@ suite<"DDL CREATE DATABASE"> ddl_create_database_suite = [] {
935935
.build_sql();
936936
expect(sql ==
937937
"CREATE DATABASE IF NOT EXISTS schema_bootstrap_db;\n"
938-
"CREATE TABLE IF NOT EXISTS account (\n"
938+
"CREATE TABLE IF NOT EXISTS schema_bootstrap_db.account (\n"
939939
" id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT\n"
940940
");\n"
941-
"CREATE TABLE IF NOT EXISTS trade (\n"
941+
"CREATE TABLE IF NOT EXISTS schema_bootstrap_db.trade (\n"
942942
" id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT\n"
943943
");\n"s)
944944
<< sql;

0 commit comments

Comments
 (0)