0

Push migrations.

by
Published Apr 8, 2025

The `schema/push` API accepts a list of migrations to be applied to the current branch.

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
 * Push migrations.
8
 * The `schema/push` API accepts a list of migrations to be applied to the
9
current branch.
10
 */
11
export async function main(
12
	auth: Xata,
13
	db_branch_name: string,
14
	body: {
15
		migrations: {
16
			title?: string
17
			message?: string
18
			id: string
19
			parentID?: string
20
			checksum: string
21
			operations:
22
				| { addTable: { table: string } }
23
				| { removeTable: { table: string } }
24
				| { renameTable: { oldName: string; newName: string } }
25
				| {
26
						addColumn: {
27
							table: string
28
							column: {
29
								name: string
30
								type:
31
									| 'string'
32
									| 'object'
33
									| 'bool'
34
									| 'int'
35
									| 'float'
36
									| 'text'
37
									| 'email'
38
									| 'multiple'
39
									| 'link'
40
									| 'datetime'
41
									| 'vector'
42
									| 'file[]'
43
									| 'file'
44
									| 'json'
45
								link?: { table: string }
46
								vector?: { dimension: number }
47
								file?: { defaultPublicAccess?: false | true }
48
								'file[]'?: { defaultPublicAccess?: false | true }
49
								notNull?: false | true
50
								defaultValue?: string
51
								unique?: false | true
52
								columns?: {}[]
53
							}
54
						}
55
				  }
56
				| { removeColumn: { table: string; column: string } }
57
				| {
58
						renameColumn: { table: string; oldName: string; newName: string }
59
				  }[]
60
		}[]
61
	}
62
) {
63
	const url = new URL(`${auth.workspaceUrl}/db/${db_branch_name}/schema/push`)
64

65
	const response = await fetch(url, {
66
		method: 'POST',
67
		headers: {
68
			'Content-Type': 'application/json',
69
			Authorization: 'Bearer ' + auth.apiKey
70
		},
71
		body: JSON.stringify(body)
72
	})
73
	if (!response.ok) {
74
		const text = await response.text()
75
		throw new Error(`${response.status} ${text}`)
76
	}
77
	return await response.json()
78
}
79