relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
constraint_operations.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "core.hpp"
4#include "diff.hpp"
5
6#include <string>
7
8namespace relx::migrations {
9
12private:
13 std::string table_name_;
14 ConstraintMetadata constraint_;
15
16public:
17 AddConstraintOperation(std::string table_name, const ConstraintMetadata& constraint)
18 : table_name_(std::move(table_name)), constraint_(constraint) {}
19
21 if (constraint_.sql_definition.empty()) {
22 return std::unexpected(MigrationError::make(
23 MigrationErrorType::VALIDATION_FAILED, "Constraint SQL definition cannot be empty",
24 table_name_ + " constraint: " + constraint_.name));
25 }
26
27 if (constraint_.type == "INDEX") {
28 // Indexes use CREATE INDEX syntax, not ALTER TABLE
29 return "CREATE " + constraint_.sql_definition + ";";
30 } else {
31 return "ALTER TABLE " + table_name_ + " ADD " + constraint_.sql_definition + ";";
32 }
33 }
34
36 if (constraint_.name.empty()) {
38 "Constraint name cannot be empty for rollback",
39 table_name_));
40 }
41
42 if (constraint_.type == "INDEX") {
43 return "DROP INDEX IF EXISTS " + constraint_.name + ";";
44 } else if (constraint_.type == "PRIMARY_KEY") {
45 return "ALTER TABLE " + table_name_ + " DROP PRIMARY KEY;";
46 } else {
47 return "ALTER TABLE " + table_name_ + " DROP CONSTRAINT " + constraint_.name + ";";
48 }
49 }
50
51 OperationType type() const override {
52 return constraint_.type == "INDEX" ? OperationType::ADD_INDEX : OperationType::ADD_CONSTRAINT;
53 }
54};
55
58private:
59 std::string table_name_;
60 ConstraintMetadata constraint_;
61
62public:
63 DropConstraintOperation(std::string table_name, const ConstraintMetadata& constraint)
64 : table_name_(std::move(table_name)), constraint_(constraint) {}
65
67 if (constraint_.name.empty()) {
69 "Constraint name cannot be empty", table_name_));
70 }
71
72 if (constraint_.type == "INDEX") {
73 return "DROP INDEX IF EXISTS " + constraint_.name + ";";
74 } else if (constraint_.type == "PRIMARY_KEY") {
75 return "ALTER TABLE " + table_name_ + " DROP PRIMARY KEY;";
76 } else {
77 return "ALTER TABLE " + table_name_ + " DROP CONSTRAINT " + constraint_.name + ";";
78 }
79 }
80
82 if (constraint_.sql_definition.empty()) {
83 return std::unexpected(
85 "Constraint SQL definition cannot be empty for rollback",
86 table_name_ + " constraint: " + constraint_.name));
87 }
88
89 if (constraint_.type == "INDEX") {
90 return "CREATE " + constraint_.sql_definition + ";";
91 } else {
92 return "ALTER TABLE " + table_name_ + " ADD " + constraint_.sql_definition + ";";
93 }
94 }
95
96 OperationType type() const override {
97 return constraint_.type == "INDEX" ? OperationType::DROP_INDEX : OperationType::DROP_CONSTRAINT;
98 }
99};
100
103private:
104 std::string table_name_;
105 ColumnMetadata old_column_;
106 ColumnMetadata new_column_;
107
108public:
109 ModifyColumnOperation(std::string table_name, const ColumnMetadata& old_column,
110 const ColumnMetadata& new_column)
111 : table_name_(std::move(table_name)), old_column_(old_column), new_column_(new_column) {}
112
114 if (new_column_.name.empty() || new_column_.sql_type.empty()) {
116 "Column name and SQL type cannot be empty",
117 table_name_ + "." + new_column_.name));
118 }
119
120 // PostgreSQL syntax - other databases may vary
121 return "ALTER TABLE " + table_name_ + " ALTER COLUMN " + new_column_.name + " TYPE " +
122 new_column_.sql_type + ";";
123 }
124
126 if (old_column_.name.empty() || old_column_.sql_type.empty()) {
127 return std::unexpected(
129 "Original column name and SQL type cannot be empty",
130 table_name_ + "." + old_column_.name));
131 }
132
133 return "ALTER TABLE " + table_name_ + " ALTER COLUMN " + old_column_.name + " TYPE " +
134 old_column_.sql_type + ";";
135 }
136
138};
139
140} // namespace relx::migrations
ADD CONSTRAINT migration operation.
MigrationResult< std::string > to_sql() const override
AddConstraintOperation(std::string table_name, const ConstraintMetadata &constraint)
MigrationResult< std::string > rollback_sql() const override
DROP CONSTRAINT migration operation.
MigrationResult< std::string > to_sql() const override
DropConstraintOperation(std::string table_name, const ConstraintMetadata &constraint)
MigrationResult< std::string > rollback_sql() const override
Base class for migration operations.
Definition core.hpp:72
ModifyColumnOperation(std::string table_name, const ColumnMetadata &old_column, const ColumnMetadata &new_column)
MigrationResult< std::string > to_sql() const override
MigrationResult< std::string > rollback_sql() const override
std::expected< T, MigrationError > MigrationResult
Result type for migration operations.
Definition core.hpp:53
OperationType
Enum for migration operation types.
Definition core.hpp:56
STL namespace.
Metadata about a column extracted from PFR analysis.
Definition diff.hpp:44
Metadata about a constraint extracted from PFR analysis.
Definition diff.hpp:58
static MigrationError make(MigrationErrorType type, const std::string &message, const std::string &context="")
Create a MigrationError with automatic formatting.
Definition core.hpp:37