ensure proper table creation in session::create_schema including foreign key constraints
This commit is contained in:
+18
-60
@@ -25,12 +25,8 @@ utils::result<void, utils::error> session::create_schema() const {
|
||||
|
||||
for (const auto &node: *schema_) {
|
||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
||||
std::cout << "Dependency graph " << node->name() << " (" << node.get() << ")" << " -> " << it->second->node_ptr()->name() << " (" << it->second->node_ptr().get() << ")" << std::endl;
|
||||
dependency_graph[node->name()].push_back(it->second->node().name());
|
||||
|
||||
// if (it->second->is_has_many()) {
|
||||
// continue;
|
||||
// }
|
||||
if (const auto dit = in_degree.find(it->second->node().name()); dit == in_degree.end()) {
|
||||
in_degree[it->second->node().name()] = std::make_pair(1, it->second->node_ptr());
|
||||
} else {
|
||||
@@ -51,67 +47,29 @@ utils::result<void, utils::error> session::create_schema() const {
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
// Step 2: Perform topological sorting (Kahn's Algorithm)
|
||||
std::queue<object::schema::node_ptr> zero_in_degree;
|
||||
std::vector<object::schema::node_ptr> sorted_order;
|
||||
|
||||
for (const auto &[table, degree]: in_degree) {
|
||||
if (degree.first == 0) {
|
||||
zero_in_degree.push(degree.second);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &it : in_degree) {
|
||||
std::cout << "In degree table " << it.second.second->name() << " (" << it.second.first << ")" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
while (!zero_in_degree.empty()) {
|
||||
auto current = zero_in_degree.front();
|
||||
zero_in_degree.pop();
|
||||
sorted_order.push_back(current);
|
||||
|
||||
for (const auto &neighbor: dependency_graph[current->name()]) {
|
||||
in_degree[neighbor].first--;
|
||||
|
||||
if (in_degree[neighbor].first == 0) {
|
||||
zero_in_degree.push(in_degree[neighbor].second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Check for cycles
|
||||
|
||||
if (sorted_order.size() != in_degree.size()) {
|
||||
std::cout << "Cycle detected in table dependencies (sorted order size: " << sorted_order.size() << ", in degree size: " << in_degree.size() << ")" << std::endl;
|
||||
// throw std::logic_error("Cycle detected in table dependencies");
|
||||
}
|
||||
|
||||
// Step 4: Create tables in the sorted order
|
||||
|
||||
std::vector<std::string> fk_sql_commands;
|
||||
auto c = pool_.acquire();
|
||||
for (const auto &node : sorted_order) {
|
||||
std::cout << "Creating table " << node->name() << std::endl;
|
||||
// auto result = query::query::create()
|
||||
// .table(node->name(), node->info().definition().columns())
|
||||
// .execute(*c);
|
||||
// if (!result) {
|
||||
// return utils::failure(result.err());
|
||||
// }
|
||||
for (const auto &node: *schema_) {
|
||||
auto ctx = query::query::create()
|
||||
.table(node->name(), node->info().definition().columns())
|
||||
.compile(*c);
|
||||
|
||||
for ( const auto& [sql, command] : ctx.additional_commands ) {
|
||||
fk_sql_commands.push_back( sql );
|
||||
}
|
||||
if (auto result = c->execute(ctx.sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
|
||||
// auto c = pool_.acquire();
|
||||
// for (const auto &node: *schema_) {
|
||||
// auto result = query::query::create()
|
||||
// .table(node->name(), node->info().definition().columns())
|
||||
// .execute(*c);
|
||||
// if (!result) {
|
||||
// return utils::failure(result.err());
|
||||
// }
|
||||
// }
|
||||
|
||||
// execute additional commands (e.g. ALTER TABLE ADD FK)
|
||||
for (const auto &sql: fk_sql_commands) {
|
||||
if (auto result = c->execute(sql); !result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
}
|
||||
return utils::ok<void>();
|
||||
|
||||
}
|
||||
|
||||
utils::result<void, utils::error> session::drop_table(const std::string &table_name) const {
|
||||
|
||||
Reference in New Issue
Block a user