0

Compare branch schemas.

by
Published Apr 8, 2025
Script xata Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Xata = {
3
	apiKey: string
4
	workspaceUrl: string
5
}
6
/**
7
 * Compare branch schemas.
8
 *
9
 */
10
export async function main(
11
	auth: Xata,
12
	db_branch_name: string,
13
	branch_name: string,
14
	body: {
15
		sourceBranchOperations?:
16
			| { addTable: { table: string } }
17
			| { removeTable: { table: string } }
18
			| { renameTable: { oldName: string; newName: string } }
19
			| {
20
					addColumn: {
21
						table: string
22
						column: {
23
							name: string
24
							type:
25
								| 'string'
26
								| 'object'
27
								| 'bool'
28
								| 'int'
29
								| 'float'
30
								| 'text'
31
								| 'email'
32
								| 'multiple'
33
								| 'link'
34
								| 'datetime'
35
								| 'vector'
36
								| 'file[]'
37
								| 'file'
38
								| 'json'
39
							link?: { table: string }
40
							vector?: { dimension: number }
41
							file?: { defaultPublicAccess?: false | true }
42
							'file[]'?: { defaultPublicAccess?: false | true }
43
							notNull?: false | true
44
							defaultValue?: string
45
							unique?: false | true
46
							columns?: {}[]
47
						}
48
					}
49
			  }
50
			| { removeColumn: { table: string; column: string } }
51
			| { renameColumn: { table: string; oldName: string; newName: string } }[]
52
		targetBranchOperations?:
53
			| { addTable: { table: string } }
54
			| { removeTable: { table: string } }
55
			| { renameTable: { oldName: string; newName: string } }
56
			| {
57
					addColumn: {
58
						table: string
59
						column: {
60
							name: string
61
							type:
62
								| 'string'
63
								| 'object'
64
								| 'bool'
65
								| 'int'
66
								| 'float'
67
								| 'text'
68
								| 'email'
69
								| 'multiple'
70
								| 'link'
71
								| 'datetime'
72
								| 'vector'
73
								| 'file[]'
74
								| 'file'
75
								| 'json'
76
							link?: { table: string }
77
							vector?: { dimension: number }
78
							file?: { defaultPublicAccess?: false | true }
79
							'file[]'?: { defaultPublicAccess?: false | true }
80
							notNull?: false | true
81
							defaultValue?: string
82
							unique?: false | true
83
							columns?: {}[]
84
						}
85
					}
86
			  }
87
			| { removeColumn: { table: string; column: string } }
88
			| { renameColumn: { table: string; oldName: string; newName: string } }[]
89
	}
90
) {
91
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/schema/compare/${branch_name}`)
92

93
	const response = await fetch(url, {
94
		method: 'POST',
95
		headers: {
96
			'Content-Type': 'application/json',
97
			Authorization: 'Bearer ' + auth.apiKey
98
		},
99
		body: JSON.stringify(body)
100
	})
101
	if (!response.ok) {
102
		const text = await response.text()
103
		throw new Error(`${response.status} ${text}`)
104
	}
105
	return await response.json()
106
}
107