-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaMigration.h
More file actions
84 lines (66 loc) · 2.09 KB
/
Copy pathSchemaMigration.h
File metadata and controls
84 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef BLANG_SCHEMA_MIGRATION_H_
#define BLANG_SCHEMA_MIGRATION_H_
#include <string>
#include <vector>
#include <map>
#include "Type.h"
namespace QLang
{
// Represents a single field in the stored schema
struct SchemaField
{
std::string name;
std::string type;
};
// Represents a table in the stored schema
struct SchemaTable
{
std::string name;
std::vector<SchemaField> fields;
};
// A migration step to apply
struct MigrationStep
{
enum StepType {
CREATE_TABLE,
ADD_COLUMN,
DROP_COLUMN, // destructive — requires @drop annotation
DROP_TABLE // destructive — requires @drop annotation
};
StepType type;
std::string sql;
std::string description;
bool isDestructive = false;
};
// Schema migration engine.
// Compares current table struct definitions against a stored schema snapshot
// and generates migration SQL.
class SchemaMigration
{
public:
// Load stored schema from a JSON file (into the stored snapshot).
// Returns true on success (or if file doesn't exist — treated as empty schema).
bool loadSchema( const std::string &path );
// Load the current schema from a JSON file (into the current snapshot).
// Used by `bcc migrate`, which obtains the current schema by running
// `qcc --emit-schema` rather than parsing in-process.
bool loadCurrentSchema( const std::string &path );
// Save current schema to a JSON file.
bool saveSchema( const std::string &path );
// Extract schema from parsed table struct definitions
void extractSchema( const std::vector<SmartPtr<StructDefinition>> &structs );
// Compute the diff between stored and current schema.
// Returns list of migration steps needed.
std::vector<MigrationStep> computeDiff();
// Generate SQL for all steps
std::string generateSQL();
// Preview: show what would change (human-readable)
std::string preview();
// Check if any destructive changes need confirmation
bool hasDestructiveChanges();
private:
std::vector<SchemaTable> mStoredSchema; // from .blang/schema.json
std::vector<SchemaTable> mCurrentSchema; // from parsed table structs
};
} // namespace QLang
#endif // BLANG_SCHEMA_MIGRATION_H_