-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigration.zod.ts
More file actions
87 lines (70 loc) · 3.71 KB
/
migration.zod.ts
File metadata and controls
87 lines (70 loc) · 3.71 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
85
86
87
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { FieldSchema } from '../data/field.zod';
import { ObjectSchema } from '../data/object.zod';
// --- Atomic Operations ---
export const AddFieldOperation = z.object({
type: z.literal('add_field'),
objectName: z.string().describe('Target object name'),
fieldName: z.string().describe('Name of the field to add'),
field: FieldSchema.describe('Full field definition to add')
}).describe('Add a new field to an existing object');
export const ModifyFieldOperation = z.object({
type: z.literal('modify_field'),
objectName: z.string().describe('Target object name'),
fieldName: z.string().describe('Name of the field to modify'),
changes: z.record(z.string(), z.unknown()).describe('Partial field definition updates')
}).describe('Modify properties of an existing field');
export const RemoveFieldOperation = z.object({
type: z.literal('remove_field'),
objectName: z.string().describe('Target object name'),
fieldName: z.string().describe('Name of the field to remove')
}).describe('Remove a field from an existing object');
export const CreateObjectOperation = z.object({
type: z.literal('create_object'),
object: ObjectSchema.describe('Full object definition to create')
}).describe('Create a new object');
export const RenameObjectOperation = z.object({
type: z.literal('rename_object'),
oldName: z.string().describe('Current object name'),
newName: z.string().describe('New object name')
}).describe('Rename an existing object');
export const DeleteObjectOperation = z.object({
type: z.literal('delete_object'),
objectName: z.string().describe('Name of the object to delete')
}).describe('Delete an existing object');
export const ExecuteSqlOperation = z.object({
type: z.literal('execute_sql'),
sql: z.string().describe('Raw SQL statement to execute'),
description: z.string().optional().describe('Human-readable description of the SQL')
}).describe('Execute a raw SQL statement');
// Union of all possible operations
export const MigrationOperationSchema = z.discriminatedUnion('type', [
AddFieldOperation,
ModifyFieldOperation,
RemoveFieldOperation,
CreateObjectOperation,
RenameObjectOperation,
DeleteObjectOperation,
ExecuteSqlOperation
]);
// --- Migration & ChangeSet ---
export const MigrationDependencySchema = z.object({
migrationId: z.string().describe('ID of the migration this depends on'),
package: z.string().optional().describe('Package that owns the dependency migration')
}).describe('Dependency reference to another migration that must run first');
export const ChangeSetSchema = z.object({
id: z.string().uuid().describe('Unique identifier for this change set'),
name: z.string().describe('Human readable name for the migration'),
description: z.string().optional().describe('Detailed description of what this migration does'),
author: z.string().optional().describe('Author who created this migration'),
createdAt: z.string().datetime().optional().describe('ISO 8601 timestamp when the migration was created'),
// Dependencies ensure migrations run in order
dependencies: z.array(MigrationDependencySchema).optional().describe('Migrations that must run before this one'),
// The actual atomic operations
operations: z.array(MigrationOperationSchema).describe('Ordered list of atomic migration operations'),
// Rollback operations (AI should generate these too)
rollback: z.array(MigrationOperationSchema).optional().describe('Operations to reverse this migration')
}).describe('A versioned set of atomic schema migration operations');
export type ChangeSet = z.infer<typeof ChangeSetSchema>;
export type MigrationOperation = z.infer<typeof MigrationOperationSchema>;