-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmigration.ts
More file actions
216 lines (213 loc) · 6.57 KB
/
Copy pathmigration.ts
File metadata and controls
216 lines (213 loc) · 6.57 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env -S node
import {
col,
fn,
lit,
Migration,
MigrationCLI,
primaryKey,
rawSql,
unique,
} from '@prisma-next/target-postgres/migration';
import type { Contract as End } from './end-contract';
import endContract from './end-contract.json' with { type: 'json' };
export default class M extends Migration<never, End> {
override readonly endContractJson = endContract;
override get operations() {
return [
rawSql({
id: 'extension.vector',
label: 'Enable extension "vector"',
summary: 'Ensures the vector extension is available for pgvector operations',
operationClass: 'additive',
target: { id: 'postgres' },
precheck: [
{
description: 'verify extension "vector" is not already enabled',
sql: "SELECT NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')",
},
],
execute: [
{
description: 'create extension "vector"',
sql: 'CREATE EXTENSION IF NOT EXISTS vector',
},
],
postcheck: [
{
description: 'confirm extension "vector" is enabled',
sql: "SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'vector')",
},
],
}),
this.createTable({
schema: 'public',
table: 'bug',
columns: [
col('id', 'uuid', { notNull: true }),
col('severity', 'text', { notNull: true }),
col('stepsToRepro', 'text'),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'feature',
columns: [
col('id', 'uuid', { notNull: true }),
col('priority', 'text', { notNull: true }),
col('targetRelease', 'text'),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'tag',
columns: [col('id', 'uuid', { notNull: true }), col('label', 'text', { notNull: true })],
constraints: [primaryKey(['id']), unique(['label'], { name: 'tag_label_key' })],
}),
this.createTable({
schema: 'public',
table: 'user',
columns: [
col('address', 'jsonb'),
col('createdAt', 'timestamptz', { notNull: true, default: fn('now()') }),
col('displayName', 'text', { notNull: true }),
col('email', 'text', { notNull: true }),
col('id', 'uuid', { notNull: true }),
col('kind', 'text', { notNull: true }),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'post',
columns: [
col('createdAt', 'timestamptz', { notNull: true, default: fn('now()') }),
col('embedding', 'vector(1536)'),
col('id', 'uuid', { notNull: true }),
col('priority', 'text', { notNull: true, default: lit('low') }),
col('title', 'text', { notNull: true }),
col('userId', 'uuid', { notNull: true }),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'task',
columns: [
col('createdAt', 'timestamptz', { notNull: true, default: fn('now()') }),
col('description', 'text'),
col('id', 'uuid', { notNull: true }),
col('status', 'text', { notNull: true, default: lit('open') }),
col('title', 'text', { notNull: true }),
col('type', 'text', { notNull: true }),
col('userId', 'uuid', { notNull: true }),
],
constraints: [primaryKey(['id'])],
}),
this.createTable({
schema: 'public',
table: 'post_tag',
columns: [
col('postId', 'uuid', { notNull: true }),
col('tagId', 'uuid', { notNull: true }),
],
constraints: [primaryKey(['postId', 'tagId'])],
}),
this.addCheckConstraint({
schema: 'public',
table: 'user',
constraint: 'user_kind_check',
payload: { kind: 'valueSet', column: 'kind', values: ['admin', 'user'] },
}),
this.addCheckConstraint({
schema: 'public',
table: 'post',
constraint: 'post_priority_check',
payload: { kind: 'valueSet', column: 'priority', values: ['low', 'high', 'urgent'] },
}),
this.addForeignKey({
schema: 'public',
table: 'post',
foreignKey: {
name: 'post_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'user', columns: ['id'] },
},
}),
this.createIndex({
schema: 'public',
table: 'post',
index: 'post_userId_idx',
columns: ['userId'],
}),
this.addForeignKey({
schema: 'public',
table: 'task',
foreignKey: {
name: 'task_userId_fkey',
columns: ['userId'],
references: { schema: 'public', table: 'user', columns: ['id'] },
},
}),
this.createIndex({
schema: 'public',
table: 'task',
index: 'task_userId_idx',
columns: ['userId'],
}),
this.addForeignKey({
schema: 'public',
table: 'bug',
foreignKey: {
name: 'bug_id_fkey',
columns: ['id'],
references: { schema: 'public', table: 'task', columns: ['id'] },
onDelete: 'cascade',
},
}),
this.addForeignKey({
schema: 'public',
table: 'feature',
foreignKey: {
name: 'feature_id_fkey',
columns: ['id'],
references: { schema: 'public', table: 'task', columns: ['id'] },
onDelete: 'cascade',
},
}),
this.createIndex({
schema: 'public',
table: 'post_tag',
index: 'post_tag_postId_idx',
columns: ['postId'],
}),
this.createIndex({
schema: 'public',
table: 'post_tag',
index: 'post_tag_tagId_idx',
columns: ['tagId'],
}),
this.addForeignKey({
schema: 'public',
table: 'post_tag',
foreignKey: {
name: 'post_tag_postId_fkey',
columns: ['postId'],
references: { schema: 'public', table: 'post', columns: ['id'] },
},
}),
this.addForeignKey({
schema: 'public',
table: 'post_tag',
foreignKey: {
name: 'post_tag_tagId_fkey',
columns: ['tagId'],
references: { schema: 'public', table: 'tag', columns: ['id'] },
},
}),
];
}
}
MigrationCLI.run(import.meta.url, M);