@anthonyalayo/drizzle-kit 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +25 -0
- package/README.md +79 -0
- package/build.dev.ts +51 -0
- package/build.ts +112 -0
- package/imports-checker/analyze.ts +78 -0
- package/imports-checker/checker.ts +296 -0
- package/imports-checker/grammar/grammar.ohm +121 -0
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
- package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
- package/imports-checker/index.ts +48 -0
- package/package.json +142 -0
- package/patches/difflib@0.2.4.patch +31 -0
- package/src/@types/utils.ts +58 -0
- package/src/api.ts +504 -0
- package/src/cli/commands/_es5.ts +2 -0
- package/src/cli/commands/check.ts +52 -0
- package/src/cli/commands/drop.ts +60 -0
- package/src/cli/commands/introspect.ts +772 -0
- package/src/cli/commands/libSqlPushUtils.ts +355 -0
- package/src/cli/commands/migrate.ts +1526 -0
- package/src/cli/commands/mysqlIntrospect.ts +53 -0
- package/src/cli/commands/mysqlPushUtils.ts +352 -0
- package/src/cli/commands/mysqlUp.ts +102 -0
- package/src/cli/commands/pgIntrospect.ts +55 -0
- package/src/cli/commands/pgPushUtils.ts +269 -0
- package/src/cli/commands/pgUp.ts +217 -0
- package/src/cli/commands/push.ts +637 -0
- package/src/cli/commands/singlestoreIntrospect.ts +53 -0
- package/src/cli/commands/singlestorePushUtils.ts +456 -0
- package/src/cli/commands/singlestoreUp.ts +1 -0
- package/src/cli/commands/sqliteIntrospect.ts +96 -0
- package/src/cli/commands/sqlitePushUtils.ts +322 -0
- package/src/cli/commands/sqliteUp.ts +52 -0
- package/src/cli/commands/utils.ts +858 -0
- package/src/cli/connections.ts +914 -0
- package/src/cli/index.ts +48 -0
- package/src/cli/schema.ts +794 -0
- package/src/cli/selector-ui.ts +38 -0
- package/src/cli/utils.ts +112 -0
- package/src/cli/validations/cli.ts +79 -0
- package/src/cli/validations/common.ts +193 -0
- package/src/cli/validations/libsql.ts +27 -0
- package/src/cli/validations/mysql.ts +61 -0
- package/src/cli/validations/outputs.ts +91 -0
- package/src/cli/validations/postgres.ts +83 -0
- package/src/cli/validations/singlestore.ts +61 -0
- package/src/cli/validations/sqlite.ts +104 -0
- package/src/cli/validations/studio.ts +24 -0
- package/src/cli/views.ts +658 -0
- package/src/extensions/getTablesFilterByExtensions.ts +16 -0
- package/src/extensions/vector.ts +10 -0
- package/src/global.ts +61 -0
- package/src/index.ts +326 -0
- package/src/introspect-mysql.ts +1030 -0
- package/src/introspect-pg.ts +1435 -0
- package/src/introspect-singlestore.ts +913 -0
- package/src/introspect-sqlite.ts +533 -0
- package/src/jsonDiffer.js +882 -0
- package/src/jsonStatements.ts +3590 -0
- package/src/loader.mjs +57 -0
- package/src/migrationPreparator.ts +217 -0
- package/src/schemaValidator.ts +23 -0
- package/src/serializer/index.ts +142 -0
- package/src/serializer/mysqlImports.ts +38 -0
- package/src/serializer/mysqlSchema.ts +423 -0
- package/src/serializer/mysqlSerializer.ts +999 -0
- package/src/serializer/pgImports.ts +106 -0
- package/src/serializer/pgSchema.ts +937 -0
- package/src/serializer/pgSerializer.ts +2223 -0
- package/src/serializer/singlestoreImports.ts +38 -0
- package/src/serializer/singlestoreSchema.ts +257 -0
- package/src/serializer/singlestoreSerializer.ts +767 -0
- package/src/serializer/sqliteImports.ts +41 -0
- package/src/serializer/sqliteSchema.ts +352 -0
- package/src/serializer/sqliteSerializer.ts +931 -0
- package/src/serializer/studio.ts +694 -0
- package/src/serializer/utils.ts +45 -0
- package/src/simulator.ts +157 -0
- package/src/snapshotsDiffer.ts +4398 -0
- package/src/sqlgenerator.ts +4212 -0
- package/src/statementCombiner.ts +597 -0
- package/src/utils/certs.ts +44 -0
- package/src/utils/words.ts +1333 -0
- package/src/utils.ts +370 -0
- package/tests/bin.test.ts +58 -0
- package/tests/cli/d1http.config.ts +12 -0
- package/tests/cli/drizzle.config.ts +9 -0
- package/tests/cli/durable-sqlite.config.ts +7 -0
- package/tests/cli/expo.config.ts +7 -0
- package/tests/cli/postgres.config.ts +13 -0
- package/tests/cli/postgres2.config.ts +17 -0
- package/tests/cli/schema.ts +1 -0
- package/tests/cli/turso.config.ts +10 -0
- package/tests/cli-export.test.ts +79 -0
- package/tests/cli-generate.test.ts +259 -0
- package/tests/cli-migrate.test.ts +103 -0
- package/tests/cli-push.test.ts +126 -0
- package/tests/common.ts +15 -0
- package/tests/indexes/common.ts +21 -0
- package/tests/indexes/pg.test.ts +245 -0
- package/tests/introspect/libsql.test.ts +35 -0
- package/tests/introspect/mysql.test.ts +319 -0
- package/tests/introspect/pg.test.ts +894 -0
- package/tests/introspect/singlestore.test.ts +275 -0
- package/tests/introspect/sqlite.test.ts +122 -0
- package/tests/libsql-checks.test.ts +308 -0
- package/tests/libsql-statements.test.ts +989 -0
- package/tests/libsql-views.test.ts +218 -0
- package/tests/migrate/libsq-schema.ts +6 -0
- package/tests/migrate/libsql-migrate.test.ts +58 -0
- package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
- package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
- package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/_journal.json +20 -0
- package/tests/mysql-checks.test.ts +291 -0
- package/tests/mysql-generated.test.ts +1290 -0
- package/tests/mysql-schemas.test.ts +156 -0
- package/tests/mysql-views.test.ts +553 -0
- package/tests/mysql.test.ts +863 -0
- package/tests/pg-array.test.ts +368 -0
- package/tests/pg-checks.test.ts +329 -0
- package/tests/pg-columns.test.ts +484 -0
- package/tests/pg-domains.test.ts +306 -0
- package/tests/pg-enums.test.ts +751 -0
- package/tests/pg-generated.test.ts +529 -0
- package/tests/pg-identity.test.ts +447 -0
- package/tests/pg-schemas.test.ts +105 -0
- package/tests/pg-sequences.test.ts +298 -0
- package/tests/pg-tables.test.ts +1021 -0
- package/tests/pg-views.test.ts +1929 -0
- package/tests/push/common.ts +58 -0
- package/tests/push/libsql.test.ts +1400 -0
- package/tests/push/mysql-push.test.ts +345 -0
- package/tests/push/mysql.test.ts +788 -0
- package/tests/push/pg.test.ts +4341 -0
- package/tests/push/singlestore-push.test.ts +894 -0
- package/tests/push/singlestore.test.ts +444 -0
- package/tests/push/sqlite.test.ts +1613 -0
- package/tests/rls/pg-policy.test.ts +1656 -0
- package/tests/rls/pg-role.test.ts +234 -0
- package/tests/schemaDiffer.ts +2852 -0
- package/tests/singlestore-generated.test.ts +1290 -0
- package/tests/singlestore-schemas.test.ts +155 -0
- package/tests/singlestore.test.ts +978 -0
- package/tests/sqlite-checks.test.ts +308 -0
- package/tests/sqlite-columns.test.ts +1049 -0
- package/tests/sqlite-generated.test.ts +1726 -0
- package/tests/sqlite-tables.test.ts +637 -0
- package/tests/sqlite-views.test.ts +218 -0
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
- package/tests/test/sqlite.test.ts +39 -0
- package/tests/testsinglestore.ts +29 -0
- package/tests/validations.test.ts +869 -0
- package/tests/wrap-param.test.ts +16 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.cli-types.json +9 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
import { mapValues, originUUID, snapshotVersion } from '../global';
|
|
2
|
+
|
|
3
|
+
import { any, array, boolean, enum as enumType, literal, number, object, record, string, TypeOf, union } from 'zod';
|
|
4
|
+
|
|
5
|
+
const indexV2 = object({
|
|
6
|
+
name: string(),
|
|
7
|
+
columns: record(
|
|
8
|
+
string(),
|
|
9
|
+
object({
|
|
10
|
+
name: string(),
|
|
11
|
+
}),
|
|
12
|
+
),
|
|
13
|
+
isUnique: boolean(),
|
|
14
|
+
}).strict();
|
|
15
|
+
|
|
16
|
+
const columnV2 = object({
|
|
17
|
+
name: string(),
|
|
18
|
+
type: string(),
|
|
19
|
+
primaryKey: boolean(),
|
|
20
|
+
notNull: boolean(),
|
|
21
|
+
default: any().optional(),
|
|
22
|
+
references: string().optional(),
|
|
23
|
+
}).strict();
|
|
24
|
+
|
|
25
|
+
const tableV2 = object({
|
|
26
|
+
name: string(),
|
|
27
|
+
columns: record(string(), columnV2),
|
|
28
|
+
indexes: record(string(), indexV2),
|
|
29
|
+
}).strict();
|
|
30
|
+
|
|
31
|
+
const checkConstraint = object({
|
|
32
|
+
name: string().optional(),
|
|
33
|
+
value: string(),
|
|
34
|
+
}).strict();
|
|
35
|
+
|
|
36
|
+
const domainSchema = object({
|
|
37
|
+
name: string(),
|
|
38
|
+
schema: string(),
|
|
39
|
+
baseType: string(),
|
|
40
|
+
notNull: boolean().optional(),
|
|
41
|
+
defaultValue: any().optional(),
|
|
42
|
+
checkConstraints: record(string(), checkConstraint).optional(),
|
|
43
|
+
}).strict();
|
|
44
|
+
|
|
45
|
+
const enumSchemaV1 = object({
|
|
46
|
+
name: string(),
|
|
47
|
+
values: record(string(), string()),
|
|
48
|
+
}).strict();
|
|
49
|
+
|
|
50
|
+
const enumSchema = object({
|
|
51
|
+
name: string(),
|
|
52
|
+
schema: string(),
|
|
53
|
+
values: string().array(),
|
|
54
|
+
}).strict();
|
|
55
|
+
|
|
56
|
+
export const pgSchemaV2 = object({
|
|
57
|
+
version: literal('2'),
|
|
58
|
+
tables: record(string(), tableV2),
|
|
59
|
+
enums: record(string(), enumSchemaV1),
|
|
60
|
+
}).strict();
|
|
61
|
+
|
|
62
|
+
// ------- V1 --------
|
|
63
|
+
const references = object({
|
|
64
|
+
foreignKeyName: string(),
|
|
65
|
+
table: string(),
|
|
66
|
+
column: string(),
|
|
67
|
+
onDelete: string().optional(),
|
|
68
|
+
onUpdate: string().optional(),
|
|
69
|
+
}).strict();
|
|
70
|
+
|
|
71
|
+
const columnV1 = object({
|
|
72
|
+
name: string(),
|
|
73
|
+
type: string(),
|
|
74
|
+
primaryKey: boolean(),
|
|
75
|
+
notNull: boolean(),
|
|
76
|
+
default: any().optional(),
|
|
77
|
+
references: references.optional(),
|
|
78
|
+
}).strict();
|
|
79
|
+
|
|
80
|
+
const tableV1 = object({
|
|
81
|
+
name: string(),
|
|
82
|
+
columns: record(string(), columnV1),
|
|
83
|
+
indexes: record(string(), indexV2),
|
|
84
|
+
}).strict();
|
|
85
|
+
|
|
86
|
+
export const pgSchemaV1 = object({
|
|
87
|
+
version: literal('1'),
|
|
88
|
+
tables: record(string(), tableV1),
|
|
89
|
+
enums: record(string(), enumSchemaV1),
|
|
90
|
+
}).strict();
|
|
91
|
+
|
|
92
|
+
const indexColumn = object({
|
|
93
|
+
expression: string(),
|
|
94
|
+
isExpression: boolean(),
|
|
95
|
+
asc: boolean(),
|
|
96
|
+
nulls: string().optional(),
|
|
97
|
+
opclass: string().optional(),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
export type IndexColumnType = TypeOf<typeof indexColumn>;
|
|
101
|
+
|
|
102
|
+
const index = object({
|
|
103
|
+
name: string(),
|
|
104
|
+
columns: indexColumn.array(),
|
|
105
|
+
isUnique: boolean(),
|
|
106
|
+
with: record(string(), any()).optional(),
|
|
107
|
+
method: string().default('btree'),
|
|
108
|
+
where: string().optional(),
|
|
109
|
+
concurrently: boolean().default(false),
|
|
110
|
+
}).strict();
|
|
111
|
+
|
|
112
|
+
const indexV4 = object({
|
|
113
|
+
name: string(),
|
|
114
|
+
columns: string().array(),
|
|
115
|
+
isUnique: boolean(),
|
|
116
|
+
with: record(string(), string()).optional(),
|
|
117
|
+
method: string().default('btree'),
|
|
118
|
+
where: string().optional(),
|
|
119
|
+
concurrently: boolean().default(false),
|
|
120
|
+
}).strict();
|
|
121
|
+
|
|
122
|
+
const indexV5 = object({
|
|
123
|
+
name: string(),
|
|
124
|
+
columns: string().array(),
|
|
125
|
+
isUnique: boolean(),
|
|
126
|
+
with: record(string(), string()).optional(),
|
|
127
|
+
method: string().default('btree'),
|
|
128
|
+
where: string().optional(),
|
|
129
|
+
concurrently: boolean().default(false),
|
|
130
|
+
}).strict();
|
|
131
|
+
|
|
132
|
+
const indexV6 = object({
|
|
133
|
+
name: string(),
|
|
134
|
+
columns: string().array(),
|
|
135
|
+
isUnique: boolean(),
|
|
136
|
+
with: record(string(), string()).optional(),
|
|
137
|
+
method: string().default('btree'),
|
|
138
|
+
where: string().optional(),
|
|
139
|
+
concurrently: boolean().default(false),
|
|
140
|
+
}).strict();
|
|
141
|
+
|
|
142
|
+
const fk = object({
|
|
143
|
+
name: string(),
|
|
144
|
+
tableFrom: string(),
|
|
145
|
+
columnsFrom: string().array(),
|
|
146
|
+
tableTo: string(),
|
|
147
|
+
schemaTo: string().optional(),
|
|
148
|
+
columnsTo: string().array(),
|
|
149
|
+
onUpdate: string().optional(),
|
|
150
|
+
onDelete: string().optional(),
|
|
151
|
+
}).strict();
|
|
152
|
+
|
|
153
|
+
export const sequenceSchema = object({
|
|
154
|
+
name: string(),
|
|
155
|
+
increment: string().optional(),
|
|
156
|
+
minValue: string().optional(),
|
|
157
|
+
maxValue: string().optional(),
|
|
158
|
+
startWith: string().optional(),
|
|
159
|
+
cache: string().optional(),
|
|
160
|
+
cycle: boolean().optional(),
|
|
161
|
+
schema: string(),
|
|
162
|
+
}).strict();
|
|
163
|
+
|
|
164
|
+
export const roleSchema = object({
|
|
165
|
+
name: string(),
|
|
166
|
+
createDb: boolean().optional(),
|
|
167
|
+
createRole: boolean().optional(),
|
|
168
|
+
inherit: boolean().optional(),
|
|
169
|
+
}).strict();
|
|
170
|
+
|
|
171
|
+
export const sequenceSquashed = object({
|
|
172
|
+
name: string(),
|
|
173
|
+
schema: string(),
|
|
174
|
+
values: string(),
|
|
175
|
+
}).strict();
|
|
176
|
+
|
|
177
|
+
const columnV7 = object({
|
|
178
|
+
name: string(),
|
|
179
|
+
type: string(),
|
|
180
|
+
typeSchema: string().optional(),
|
|
181
|
+
domainTypeSchema: string().optional(),
|
|
182
|
+
primaryKey: boolean(),
|
|
183
|
+
notNull: boolean(),
|
|
184
|
+
default: any().optional(),
|
|
185
|
+
isUnique: any().optional(),
|
|
186
|
+
uniqueName: string().optional(),
|
|
187
|
+
nullsNotDistinct: boolean().optional(),
|
|
188
|
+
}).strict();
|
|
189
|
+
|
|
190
|
+
const column = object({
|
|
191
|
+
name: string(),
|
|
192
|
+
type: string(),
|
|
193
|
+
typeSchema: string().optional(),
|
|
194
|
+
domainTypeSchema: string().optional(),
|
|
195
|
+
primaryKey: boolean(),
|
|
196
|
+
notNull: boolean(),
|
|
197
|
+
default: any().optional(),
|
|
198
|
+
isUnique: any().optional(),
|
|
199
|
+
uniqueName: string().optional(),
|
|
200
|
+
nullsNotDistinct: boolean().optional(),
|
|
201
|
+
generated: object({
|
|
202
|
+
type: literal('stored'),
|
|
203
|
+
as: string(),
|
|
204
|
+
}).optional(),
|
|
205
|
+
identity: sequenceSchema
|
|
206
|
+
.merge(object({ type: enumType(['always', 'byDefault']) }))
|
|
207
|
+
.optional(),
|
|
208
|
+
}).strict();
|
|
209
|
+
|
|
210
|
+
const columnSquashed = object({
|
|
211
|
+
name: string(),
|
|
212
|
+
type: string(),
|
|
213
|
+
typeSchema: string().optional(),
|
|
214
|
+
domainTypeSchema: string().optional(),
|
|
215
|
+
primaryKey: boolean(),
|
|
216
|
+
notNull: boolean(),
|
|
217
|
+
default: any().optional(),
|
|
218
|
+
isUnique: any().optional(),
|
|
219
|
+
uniqueName: string().optional(),
|
|
220
|
+
nullsNotDistinct: boolean().optional(),
|
|
221
|
+
generated: object({
|
|
222
|
+
type: literal('stored'),
|
|
223
|
+
as: string(),
|
|
224
|
+
}).optional(),
|
|
225
|
+
identity: string().optional(),
|
|
226
|
+
}).strict();
|
|
227
|
+
|
|
228
|
+
const tableV3 = object({
|
|
229
|
+
name: string(),
|
|
230
|
+
columns: record(string(), column),
|
|
231
|
+
indexes: record(string(), index),
|
|
232
|
+
foreignKeys: record(string(), fk),
|
|
233
|
+
}).strict();
|
|
234
|
+
|
|
235
|
+
const compositePK = object({
|
|
236
|
+
name: string(),
|
|
237
|
+
columns: string().array(),
|
|
238
|
+
}).strict();
|
|
239
|
+
|
|
240
|
+
const uniqueConstraint = object({
|
|
241
|
+
name: string(),
|
|
242
|
+
columns: string().array(),
|
|
243
|
+
nullsNotDistinct: boolean(),
|
|
244
|
+
}).strict();
|
|
245
|
+
|
|
246
|
+
export const policy = object({
|
|
247
|
+
name: string(),
|
|
248
|
+
as: enumType(['PERMISSIVE', 'RESTRICTIVE']).optional(),
|
|
249
|
+
for: enumType(['ALL', 'SELECT', 'INSERT', 'UPDATE', 'DELETE']).optional(),
|
|
250
|
+
to: string().array().optional(),
|
|
251
|
+
using: string().optional(),
|
|
252
|
+
withCheck: string().optional(),
|
|
253
|
+
on: string().optional(),
|
|
254
|
+
schema: string().optional(),
|
|
255
|
+
}).strict();
|
|
256
|
+
|
|
257
|
+
export const policySquashed = object({
|
|
258
|
+
name: string(),
|
|
259
|
+
values: string(),
|
|
260
|
+
}).strict();
|
|
261
|
+
|
|
262
|
+
const viewWithOption = object({
|
|
263
|
+
checkOption: enumType(['local', 'cascaded']).optional(),
|
|
264
|
+
securityBarrier: boolean().optional(),
|
|
265
|
+
securityInvoker: boolean().optional(),
|
|
266
|
+
}).strict();
|
|
267
|
+
|
|
268
|
+
const matViewWithOption = object({
|
|
269
|
+
fillfactor: number().optional(),
|
|
270
|
+
toastTupleTarget: number().optional(),
|
|
271
|
+
parallelWorkers: number().optional(),
|
|
272
|
+
autovacuumEnabled: boolean().optional(),
|
|
273
|
+
vacuumIndexCleanup: enumType(['auto', 'off', 'on']).optional(),
|
|
274
|
+
vacuumTruncate: boolean().optional(),
|
|
275
|
+
autovacuumVacuumThreshold: number().optional(),
|
|
276
|
+
autovacuumVacuumScaleFactor: number().optional(),
|
|
277
|
+
autovacuumVacuumCostDelay: number().optional(),
|
|
278
|
+
autovacuumVacuumCostLimit: number().optional(),
|
|
279
|
+
autovacuumFreezeMinAge: number().optional(),
|
|
280
|
+
autovacuumFreezeMaxAge: number().optional(),
|
|
281
|
+
autovacuumFreezeTableAge: number().optional(),
|
|
282
|
+
autovacuumMultixactFreezeMinAge: number().optional(),
|
|
283
|
+
autovacuumMultixactFreezeMaxAge: number().optional(),
|
|
284
|
+
autovacuumMultixactFreezeTableAge: number().optional(),
|
|
285
|
+
logAutovacuumMinDuration: number().optional(),
|
|
286
|
+
userCatalogTable: boolean().optional(),
|
|
287
|
+
}).strict();
|
|
288
|
+
|
|
289
|
+
export const mergedViewWithOption = viewWithOption.merge(matViewWithOption).strict();
|
|
290
|
+
|
|
291
|
+
export const view = object({
|
|
292
|
+
name: string(),
|
|
293
|
+
schema: string(),
|
|
294
|
+
columns: record(string(), column),
|
|
295
|
+
definition: string().optional(),
|
|
296
|
+
materialized: boolean(),
|
|
297
|
+
with: mergedViewWithOption.optional(),
|
|
298
|
+
isExisting: boolean(),
|
|
299
|
+
withNoData: boolean().optional(),
|
|
300
|
+
using: string().optional(),
|
|
301
|
+
tablespace: string().optional(),
|
|
302
|
+
}).strict();
|
|
303
|
+
|
|
304
|
+
const tableV4 = object({
|
|
305
|
+
name: string(),
|
|
306
|
+
schema: string(),
|
|
307
|
+
columns: record(string(), column),
|
|
308
|
+
indexes: record(string(), indexV4),
|
|
309
|
+
foreignKeys: record(string(), fk),
|
|
310
|
+
}).strict();
|
|
311
|
+
|
|
312
|
+
const tableV5 = object({
|
|
313
|
+
name: string(),
|
|
314
|
+
schema: string(),
|
|
315
|
+
columns: record(string(), column),
|
|
316
|
+
indexes: record(string(), indexV5),
|
|
317
|
+
foreignKeys: record(string(), fk),
|
|
318
|
+
compositePrimaryKeys: record(string(), compositePK),
|
|
319
|
+
uniqueConstraints: record(string(), uniqueConstraint).default({}),
|
|
320
|
+
}).strict();
|
|
321
|
+
|
|
322
|
+
const tableV6 = object({
|
|
323
|
+
name: string(),
|
|
324
|
+
schema: string(),
|
|
325
|
+
columns: record(string(), column),
|
|
326
|
+
indexes: record(string(), indexV6),
|
|
327
|
+
foreignKeys: record(string(), fk),
|
|
328
|
+
compositePrimaryKeys: record(string(), compositePK),
|
|
329
|
+
uniqueConstraints: record(string(), uniqueConstraint).default({}),
|
|
330
|
+
}).strict();
|
|
331
|
+
|
|
332
|
+
const tableV7 = object({
|
|
333
|
+
name: string(),
|
|
334
|
+
schema: string(),
|
|
335
|
+
columns: record(string(), columnV7),
|
|
336
|
+
indexes: record(string(), index),
|
|
337
|
+
foreignKeys: record(string(), fk),
|
|
338
|
+
compositePrimaryKeys: record(string(), compositePK),
|
|
339
|
+
uniqueConstraints: record(string(), uniqueConstraint).default({}),
|
|
340
|
+
}).strict();
|
|
341
|
+
|
|
342
|
+
const table = object({
|
|
343
|
+
name: string(),
|
|
344
|
+
schema: string(),
|
|
345
|
+
columns: record(string(), column),
|
|
346
|
+
indexes: record(string(), index),
|
|
347
|
+
foreignKeys: record(string(), fk),
|
|
348
|
+
compositePrimaryKeys: record(string(), compositePK),
|
|
349
|
+
uniqueConstraints: record(string(), uniqueConstraint).default({}),
|
|
350
|
+
policies: record(string(), policy).default({}),
|
|
351
|
+
checkConstraints: record(string(), checkConstraint).default({}),
|
|
352
|
+
isRLSEnabled: boolean().default(false),
|
|
353
|
+
}).strict();
|
|
354
|
+
|
|
355
|
+
const schemaHash = object({
|
|
356
|
+
id: string(),
|
|
357
|
+
prevId: string(),
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
export const kitInternals = object({
|
|
361
|
+
tables: record(
|
|
362
|
+
string(),
|
|
363
|
+
object({
|
|
364
|
+
columns: record(
|
|
365
|
+
string(),
|
|
366
|
+
object({
|
|
367
|
+
isArray: boolean().optional(),
|
|
368
|
+
dimensions: number().optional(),
|
|
369
|
+
rawType: string().optional(),
|
|
370
|
+
isDefaultAnExpression: boolean().optional(),
|
|
371
|
+
}).optional(),
|
|
372
|
+
),
|
|
373
|
+
}).optional(),
|
|
374
|
+
),
|
|
375
|
+
}).optional();
|
|
376
|
+
|
|
377
|
+
export const pgSchemaInternalV3 = object({
|
|
378
|
+
version: literal('3'),
|
|
379
|
+
dialect: literal('pg'),
|
|
380
|
+
tables: record(string(), tableV3),
|
|
381
|
+
enums: record(string(), enumSchemaV1),
|
|
382
|
+
}).strict();
|
|
383
|
+
|
|
384
|
+
export const pgSchemaInternalV4 = object({
|
|
385
|
+
version: literal('4'),
|
|
386
|
+
dialect: literal('pg'),
|
|
387
|
+
tables: record(string(), tableV4),
|
|
388
|
+
enums: record(string(), enumSchemaV1),
|
|
389
|
+
schemas: record(string(), string()),
|
|
390
|
+
}).strict();
|
|
391
|
+
|
|
392
|
+
// "table" -> "schema.table" for schema proper support
|
|
393
|
+
export const pgSchemaInternalV5 = object({
|
|
394
|
+
version: literal('5'),
|
|
395
|
+
dialect: literal('pg'),
|
|
396
|
+
tables: record(string(), tableV5),
|
|
397
|
+
enums: record(string(), enumSchemaV1),
|
|
398
|
+
schemas: record(string(), string()),
|
|
399
|
+
_meta: object({
|
|
400
|
+
schemas: record(string(), string()),
|
|
401
|
+
tables: record(string(), string()),
|
|
402
|
+
columns: record(string(), string()),
|
|
403
|
+
}),
|
|
404
|
+
internal: kitInternals,
|
|
405
|
+
}).strict();
|
|
406
|
+
|
|
407
|
+
export const pgSchemaInternalV6 = object({
|
|
408
|
+
version: literal('6'),
|
|
409
|
+
dialect: literal('postgresql'),
|
|
410
|
+
tables: record(string(), tableV6),
|
|
411
|
+
enums: record(string(), enumSchema),
|
|
412
|
+
schemas: record(string(), string()),
|
|
413
|
+
_meta: object({
|
|
414
|
+
schemas: record(string(), string()),
|
|
415
|
+
tables: record(string(), string()),
|
|
416
|
+
columns: record(string(), string()),
|
|
417
|
+
}),
|
|
418
|
+
internal: kitInternals,
|
|
419
|
+
}).strict();
|
|
420
|
+
|
|
421
|
+
export const pgSchemaExternal = object({
|
|
422
|
+
version: literal('5'),
|
|
423
|
+
dialect: literal('pg'),
|
|
424
|
+
tables: array(table),
|
|
425
|
+
enums: array(enumSchemaV1),
|
|
426
|
+
schemas: array(object({ name: string() })),
|
|
427
|
+
_meta: object({
|
|
428
|
+
schemas: record(string(), string()),
|
|
429
|
+
tables: record(string(), string()),
|
|
430
|
+
columns: record(string(), string()),
|
|
431
|
+
}),
|
|
432
|
+
}).strict();
|
|
433
|
+
|
|
434
|
+
export const pgSchemaInternalV7 = object({
|
|
435
|
+
version: literal('7'),
|
|
436
|
+
dialect: literal('postgresql'),
|
|
437
|
+
tables: record(string(), tableV7),
|
|
438
|
+
enums: record(string(), enumSchema),
|
|
439
|
+
schemas: record(string(), string()),
|
|
440
|
+
views: record(string(), view).default({}),
|
|
441
|
+
sequences: record(string(), sequenceSchema).default({}),
|
|
442
|
+
roles: record(string(), roleSchema).default({}),
|
|
443
|
+
policies: record(string(), policy).default({}),
|
|
444
|
+
_meta: object({
|
|
445
|
+
schemas: record(string(), string()),
|
|
446
|
+
tables: record(string(), string()),
|
|
447
|
+
columns: record(string(), string()),
|
|
448
|
+
}),
|
|
449
|
+
internal: kitInternals,
|
|
450
|
+
}).strict();
|
|
451
|
+
|
|
452
|
+
export const pgSchemaInternal = object({
|
|
453
|
+
version: literal(snapshotVersion),
|
|
454
|
+
dialect: literal('postgresql'),
|
|
455
|
+
tables: record(string(), table),
|
|
456
|
+
enums: record(string(), enumSchema),
|
|
457
|
+
schemas: record(string(), string()),
|
|
458
|
+
domains: record(string(), domainSchema),
|
|
459
|
+
views: record(string(), view).default({}),
|
|
460
|
+
sequences: record(string(), sequenceSchema).default({}),
|
|
461
|
+
roles: record(string(), roleSchema).default({}),
|
|
462
|
+
policies: record(string(), policy).default({}),
|
|
463
|
+
_meta: object({
|
|
464
|
+
schemas: record(string(), string()),
|
|
465
|
+
tables: record(string(), string()),
|
|
466
|
+
columns: record(string(), string()),
|
|
467
|
+
}),
|
|
468
|
+
internal: kitInternals,
|
|
469
|
+
}).strict();
|
|
470
|
+
|
|
471
|
+
const domainSquashed = object({
|
|
472
|
+
name: string(),
|
|
473
|
+
schema: string(),
|
|
474
|
+
baseType: string(),
|
|
475
|
+
notNull: boolean().optional(),
|
|
476
|
+
defaultValue: string().optional(),
|
|
477
|
+
checkConstraints: record(string(), string()).optional(),
|
|
478
|
+
}).strict();
|
|
479
|
+
|
|
480
|
+
const tableSquashed = object({
|
|
481
|
+
name: string(),
|
|
482
|
+
schema: string(),
|
|
483
|
+
columns: record(string(), columnSquashed),
|
|
484
|
+
indexes: record(string(), string()),
|
|
485
|
+
foreignKeys: record(string(), string()),
|
|
486
|
+
compositePrimaryKeys: record(string(), string()),
|
|
487
|
+
uniqueConstraints: record(string(), string()),
|
|
488
|
+
policies: record(string(), string()),
|
|
489
|
+
checkConstraints: record(string(), string()),
|
|
490
|
+
isRLSEnabled: boolean().default(false),
|
|
491
|
+
}).strict();
|
|
492
|
+
|
|
493
|
+
const tableSquashedV4 = object({
|
|
494
|
+
name: string(),
|
|
495
|
+
schema: string(),
|
|
496
|
+
columns: record(string(), column),
|
|
497
|
+
indexes: record(string(), string()),
|
|
498
|
+
foreignKeys: record(string(), string()),
|
|
499
|
+
}).strict();
|
|
500
|
+
|
|
501
|
+
export const pgSchemaSquashedV4 = object({
|
|
502
|
+
version: literal('4'),
|
|
503
|
+
dialect: literal('pg'),
|
|
504
|
+
tables: record(string(), tableSquashedV4),
|
|
505
|
+
enums: record(string(), enumSchemaV1),
|
|
506
|
+
schemas: record(string(), string()),
|
|
507
|
+
}).strict();
|
|
508
|
+
|
|
509
|
+
export const pgSchemaSquashedV6 = object({
|
|
510
|
+
version: literal('6'),
|
|
511
|
+
dialect: literal('postgresql'),
|
|
512
|
+
tables: record(string(), tableSquashed),
|
|
513
|
+
enums: record(string(), enumSchema),
|
|
514
|
+
schemas: record(string(), string()),
|
|
515
|
+
}).strict();
|
|
516
|
+
|
|
517
|
+
export const pgSchemaSquashedV7 = object({
|
|
518
|
+
version: literal('7'),
|
|
519
|
+
dialect: literal('postgresql'),
|
|
520
|
+
tables: record(string(), tableSquashed),
|
|
521
|
+
enums: record(string(), enumSchema),
|
|
522
|
+
schemas: record(string(), string()),
|
|
523
|
+
views: record(string(), view),
|
|
524
|
+
sequences: record(string(), sequenceSquashed),
|
|
525
|
+
roles: record(string(), roleSchema).default({}),
|
|
526
|
+
policies: record(string(), policySquashed).default({}),
|
|
527
|
+
}).strict();
|
|
528
|
+
|
|
529
|
+
export const pgSchemaSquashed = object({
|
|
530
|
+
version: literal(snapshotVersion),
|
|
531
|
+
dialect: literal('postgresql'),
|
|
532
|
+
tables: record(string(), tableSquashed),
|
|
533
|
+
domains: record(string(), domainSquashed),
|
|
534
|
+
enums: record(string(), enumSchema),
|
|
535
|
+
schemas: record(string(), string()),
|
|
536
|
+
views: record(string(), view),
|
|
537
|
+
sequences: record(string(), sequenceSquashed),
|
|
538
|
+
roles: record(string(), roleSchema).default({}),
|
|
539
|
+
policies: record(string(), policySquashed).default({}),
|
|
540
|
+
}).strict();
|
|
541
|
+
|
|
542
|
+
export const pgSchemaV3 = pgSchemaInternalV3.merge(schemaHash);
|
|
543
|
+
export const pgSchemaV4 = pgSchemaInternalV4.merge(schemaHash);
|
|
544
|
+
export const pgSchemaV5 = pgSchemaInternalV5.merge(schemaHash);
|
|
545
|
+
export const pgSchemaV6 = pgSchemaInternalV6.merge(schemaHash);
|
|
546
|
+
export const pgSchemaV7 = pgSchemaInternalV7.merge(schemaHash);
|
|
547
|
+
export const pgSchema = pgSchemaInternal.merge(schemaHash);
|
|
548
|
+
|
|
549
|
+
export type Enum = TypeOf<typeof enumSchema>;
|
|
550
|
+
export type Domain = TypeOf<typeof domainSchema>;
|
|
551
|
+
export type Sequence = TypeOf<typeof sequenceSchema>;
|
|
552
|
+
export type Role = TypeOf<typeof roleSchema>;
|
|
553
|
+
export type Column = TypeOf<typeof column>;
|
|
554
|
+
export type TableV3 = TypeOf<typeof tableV3>;
|
|
555
|
+
export type TableV4 = TypeOf<typeof tableV4>;
|
|
556
|
+
export type TableV5 = TypeOf<typeof tableV5>;
|
|
557
|
+
export type Table = TypeOf<typeof table>;
|
|
558
|
+
export type PgSchema = TypeOf<typeof pgSchema>;
|
|
559
|
+
export type PgSchemaInternal = TypeOf<typeof pgSchemaInternal>;
|
|
560
|
+
export type PgSchemaV6Internal = TypeOf<typeof pgSchemaInternalV6>;
|
|
561
|
+
export type PgSchemaExternal = TypeOf<typeof pgSchemaExternal>;
|
|
562
|
+
export type PgSchemaSquashed = TypeOf<typeof pgSchemaSquashed>;
|
|
563
|
+
export type PgSchemaSquashedV4 = TypeOf<typeof pgSchemaSquashedV4>;
|
|
564
|
+
export type PgSchemaSquashedV6 = TypeOf<typeof pgSchemaSquashedV6>;
|
|
565
|
+
export type Index = TypeOf<typeof index>;
|
|
566
|
+
export type ForeignKey = TypeOf<typeof fk>;
|
|
567
|
+
export type PrimaryKey = TypeOf<typeof compositePK>;
|
|
568
|
+
export type UniqueConstraint = TypeOf<typeof uniqueConstraint>;
|
|
569
|
+
export type Policy = TypeOf<typeof policy>;
|
|
570
|
+
export type View = TypeOf<typeof view>;
|
|
571
|
+
export type MatViewWithOption = TypeOf<typeof matViewWithOption>;
|
|
572
|
+
export type ViewWithOption = TypeOf<typeof viewWithOption>;
|
|
573
|
+
|
|
574
|
+
export type PgKitInternals = TypeOf<typeof kitInternals>;
|
|
575
|
+
export type CheckConstraint = TypeOf<typeof checkConstraint>;
|
|
576
|
+
|
|
577
|
+
export type PgSchemaV1 = TypeOf<typeof pgSchemaV1>;
|
|
578
|
+
export type PgSchemaV2 = TypeOf<typeof pgSchemaV2>;
|
|
579
|
+
export type PgSchemaV3 = TypeOf<typeof pgSchemaV3>;
|
|
580
|
+
export type PgSchemaV4 = TypeOf<typeof pgSchemaV4>;
|
|
581
|
+
export type PgSchemaV5 = TypeOf<typeof pgSchemaV5>;
|
|
582
|
+
export type PgSchemaV6 = TypeOf<typeof pgSchemaV6>;
|
|
583
|
+
export type PgSchemaV7 = TypeOf<typeof pgSchemaV7>;
|
|
584
|
+
|
|
585
|
+
export const backwardCompatiblePgSchema = union([
|
|
586
|
+
pgSchemaV5,
|
|
587
|
+
pgSchemaV6,
|
|
588
|
+
pgSchema,
|
|
589
|
+
]);
|
|
590
|
+
|
|
591
|
+
export const PgSquasher = {
|
|
592
|
+
squashIdx: (idx: Index) => {
|
|
593
|
+
index.parse(idx);
|
|
594
|
+
return `${idx.name};${
|
|
595
|
+
idx.columns
|
|
596
|
+
.map(
|
|
597
|
+
(c) => `${c.expression}--${c.isExpression}--${c.asc}--${c.nulls}--${c.opclass ? c.opclass : ''}`,
|
|
598
|
+
)
|
|
599
|
+
.join(',,')
|
|
600
|
+
};${idx.isUnique};${idx.concurrently};${idx.method};${idx.where};${JSON.stringify(idx.with)}`;
|
|
601
|
+
},
|
|
602
|
+
unsquashIdx: (input: string): Index => {
|
|
603
|
+
const [
|
|
604
|
+
name,
|
|
605
|
+
columnsString,
|
|
606
|
+
isUnique,
|
|
607
|
+
concurrently,
|
|
608
|
+
method,
|
|
609
|
+
where,
|
|
610
|
+
idxWith,
|
|
611
|
+
] = input.split(';');
|
|
612
|
+
|
|
613
|
+
const columnString = columnsString.split(',,');
|
|
614
|
+
const columns: IndexColumnType[] = [];
|
|
615
|
+
|
|
616
|
+
for (const column of columnString) {
|
|
617
|
+
const [expression, isExpression, asc, nulls, opclass] = column.split('--');
|
|
618
|
+
columns.push({
|
|
619
|
+
nulls: nulls as IndexColumnType['nulls'],
|
|
620
|
+
isExpression: isExpression === 'true',
|
|
621
|
+
asc: asc === 'true',
|
|
622
|
+
expression: expression,
|
|
623
|
+
opclass: opclass === 'undefined' ? undefined : opclass,
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const result: Index = index.parse({
|
|
628
|
+
name,
|
|
629
|
+
columns: columns,
|
|
630
|
+
isUnique: isUnique === 'true',
|
|
631
|
+
concurrently: concurrently === 'true',
|
|
632
|
+
method,
|
|
633
|
+
where: where === 'undefined' ? undefined : where,
|
|
634
|
+
with: !idxWith || idxWith === 'undefined' ? undefined : JSON.parse(idxWith),
|
|
635
|
+
});
|
|
636
|
+
return result;
|
|
637
|
+
},
|
|
638
|
+
squashIdxPush: (idx: Index) => {
|
|
639
|
+
index.parse(idx);
|
|
640
|
+
return `${idx.name};${
|
|
641
|
+
idx.columns
|
|
642
|
+
.map((c) => `${c.isExpression ? '' : c.expression}--${c.asc}--${c.nulls}`)
|
|
643
|
+
.join(',,')
|
|
644
|
+
};${idx.isUnique};${idx.method};${JSON.stringify(idx.with)}`;
|
|
645
|
+
},
|
|
646
|
+
unsquashIdxPush: (input: string): Index => {
|
|
647
|
+
const [name, columnsString, isUnique, method, idxWith] = input.split(';');
|
|
648
|
+
|
|
649
|
+
const columnString = columnsString.split('--');
|
|
650
|
+
const columns: IndexColumnType[] = [];
|
|
651
|
+
|
|
652
|
+
for (const column of columnString) {
|
|
653
|
+
const [expression, asc, nulls, opclass] = column.split(',');
|
|
654
|
+
columns.push({
|
|
655
|
+
nulls: nulls as IndexColumnType['nulls'],
|
|
656
|
+
isExpression: expression === '',
|
|
657
|
+
asc: asc === 'true',
|
|
658
|
+
expression: expression,
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const result: Index = index.parse({
|
|
663
|
+
name,
|
|
664
|
+
columns: columns,
|
|
665
|
+
isUnique: isUnique === 'true',
|
|
666
|
+
concurrently: false,
|
|
667
|
+
method,
|
|
668
|
+
with: idxWith === 'undefined' ? undefined : JSON.parse(idxWith),
|
|
669
|
+
});
|
|
670
|
+
return result;
|
|
671
|
+
},
|
|
672
|
+
squashFK: (fk: ForeignKey) => {
|
|
673
|
+
return `${fk.name};${fk.tableFrom};${fk.columnsFrom.join(',')};${fk.tableTo};${fk.columnsTo.join(',')};${
|
|
674
|
+
fk.onUpdate ?? ''
|
|
675
|
+
};${fk.onDelete ?? ''};${fk.schemaTo || 'public'}`;
|
|
676
|
+
},
|
|
677
|
+
squashPolicy: (policy: Policy) => {
|
|
678
|
+
return `${policy.name}--${policy.as}--${policy.for}--${
|
|
679
|
+
policy.to?.join(',')
|
|
680
|
+
}--${policy.using}--${policy.withCheck}--${policy.on}`;
|
|
681
|
+
},
|
|
682
|
+
unsquashPolicy: (policy: string): Policy => {
|
|
683
|
+
const splitted = policy.split('--');
|
|
684
|
+
return {
|
|
685
|
+
name: splitted[0],
|
|
686
|
+
as: splitted[1] as Policy['as'],
|
|
687
|
+
for: splitted[2] as Policy['for'],
|
|
688
|
+
to: splitted[3].split(','),
|
|
689
|
+
using: splitted[4] !== 'undefined' ? splitted[4] : undefined,
|
|
690
|
+
withCheck: splitted[5] !== 'undefined' ? splitted[5] : undefined,
|
|
691
|
+
on: splitted[6] !== 'undefined' ? splitted[6] : undefined,
|
|
692
|
+
};
|
|
693
|
+
},
|
|
694
|
+
squashPolicyPush: (policy: Policy) => {
|
|
695
|
+
return `${policy.name}--${policy.as}--${policy.for}--${policy.to?.join(',')}--${policy.on}`;
|
|
696
|
+
},
|
|
697
|
+
unsquashPolicyPush: (policy: string): Policy => {
|
|
698
|
+
const splitted = policy.split('--');
|
|
699
|
+
return {
|
|
700
|
+
name: splitted[0],
|
|
701
|
+
as: splitted[1] as Policy['as'],
|
|
702
|
+
for: splitted[2] as Policy['for'],
|
|
703
|
+
to: splitted[3].split(','),
|
|
704
|
+
on: splitted[4] !== 'undefined' ? splitted[4] : undefined,
|
|
705
|
+
};
|
|
706
|
+
},
|
|
707
|
+
squashPK: (pk: PrimaryKey) => {
|
|
708
|
+
return `${pk.columns.join(',')};${pk.name}`;
|
|
709
|
+
},
|
|
710
|
+
unsquashPK: (pk: string): PrimaryKey => {
|
|
711
|
+
const splitted = pk.split(';');
|
|
712
|
+
return { name: splitted[1], columns: splitted[0].split(',') };
|
|
713
|
+
},
|
|
714
|
+
squashUnique: (unq: UniqueConstraint) => {
|
|
715
|
+
return `${unq.name};${unq.columns.join(',')};${unq.nullsNotDistinct}`;
|
|
716
|
+
},
|
|
717
|
+
unsquashUnique: (unq: string): UniqueConstraint => {
|
|
718
|
+
const [name, columns, nullsNotDistinct] = unq.split(';');
|
|
719
|
+
return {
|
|
720
|
+
name,
|
|
721
|
+
columns: columns.split(','),
|
|
722
|
+
nullsNotDistinct: nullsNotDistinct === 'true',
|
|
723
|
+
};
|
|
724
|
+
},
|
|
725
|
+
unsquashFK: (input: string): ForeignKey => {
|
|
726
|
+
const [
|
|
727
|
+
name,
|
|
728
|
+
tableFrom,
|
|
729
|
+
columnsFromStr,
|
|
730
|
+
tableTo,
|
|
731
|
+
columnsToStr,
|
|
732
|
+
onUpdate,
|
|
733
|
+
onDelete,
|
|
734
|
+
schemaTo,
|
|
735
|
+
] = input.split(';');
|
|
736
|
+
|
|
737
|
+
const result: ForeignKey = fk.parse({
|
|
738
|
+
name,
|
|
739
|
+
tableFrom,
|
|
740
|
+
columnsFrom: columnsFromStr.split(','),
|
|
741
|
+
schemaTo: schemaTo,
|
|
742
|
+
tableTo,
|
|
743
|
+
columnsTo: columnsToStr.split(','),
|
|
744
|
+
onUpdate,
|
|
745
|
+
onDelete,
|
|
746
|
+
});
|
|
747
|
+
return result;
|
|
748
|
+
},
|
|
749
|
+
squashSequence: (seq: Omit<Sequence, 'name' | 'schema'>) => {
|
|
750
|
+
return `${seq.minValue};${seq.maxValue};${seq.increment};${seq.startWith};${seq.cache};${seq.cycle ?? ''}`;
|
|
751
|
+
},
|
|
752
|
+
unsquashSequence: (seq: string): Omit<Sequence, 'name' | 'schema'> => {
|
|
753
|
+
const splitted = seq.split(';');
|
|
754
|
+
return {
|
|
755
|
+
minValue: splitted[0] !== 'undefined' ? splitted[0] : undefined,
|
|
756
|
+
maxValue: splitted[1] !== 'undefined' ? splitted[1] : undefined,
|
|
757
|
+
increment: splitted[2] !== 'undefined' ? splitted[2] : undefined,
|
|
758
|
+
startWith: splitted[3] !== 'undefined' ? splitted[3] : undefined,
|
|
759
|
+
cache: splitted[4] !== 'undefined' ? splitted[4] : undefined,
|
|
760
|
+
cycle: splitted[5] === 'true',
|
|
761
|
+
};
|
|
762
|
+
},
|
|
763
|
+
squashIdentity: (
|
|
764
|
+
seq: Omit<Sequence, 'schema'> & { type: 'always' | 'byDefault' },
|
|
765
|
+
) => {
|
|
766
|
+
return `${seq.name};${seq.type};${seq.minValue};${seq.maxValue};${seq.increment};${seq.startWith};${seq.cache};${
|
|
767
|
+
seq.cycle ?? ''
|
|
768
|
+
}`;
|
|
769
|
+
},
|
|
770
|
+
unsquashIdentity: (
|
|
771
|
+
seq: string,
|
|
772
|
+
): Omit<Sequence, 'schema'> & { type: 'always' | 'byDefault' } => {
|
|
773
|
+
const splitted = seq.split(';');
|
|
774
|
+
return {
|
|
775
|
+
name: splitted[0],
|
|
776
|
+
type: splitted[1] as 'always' | 'byDefault',
|
|
777
|
+
minValue: splitted[2] !== 'undefined' ? splitted[2] : undefined,
|
|
778
|
+
maxValue: splitted[3] !== 'undefined' ? splitted[3] : undefined,
|
|
779
|
+
increment: splitted[4] !== 'undefined' ? splitted[4] : undefined,
|
|
780
|
+
startWith: splitted[5] !== 'undefined' ? splitted[5] : undefined,
|
|
781
|
+
cache: splitted[6] !== 'undefined' ? splitted[6] : undefined,
|
|
782
|
+
cycle: splitted[7] === 'true',
|
|
783
|
+
};
|
|
784
|
+
},
|
|
785
|
+
squashCheck: (check: CheckConstraint) => {
|
|
786
|
+
return `${check.name};${check.value}`;
|
|
787
|
+
},
|
|
788
|
+
unsquashCheck: (input: string): CheckConstraint => {
|
|
789
|
+
const [
|
|
790
|
+
name,
|
|
791
|
+
value,
|
|
792
|
+
] = input.split(';');
|
|
793
|
+
|
|
794
|
+
return { name, value };
|
|
795
|
+
},
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
export const squashPgScheme = (
|
|
799
|
+
json: PgSchema,
|
|
800
|
+
action?: 'push' | undefined,
|
|
801
|
+
): PgSchemaSquashed => {
|
|
802
|
+
const mappedTables = Object.fromEntries(
|
|
803
|
+
Object.entries(json.tables).map((it) => {
|
|
804
|
+
const squashedIndexes = mapValues(it[1].indexes, (index) => {
|
|
805
|
+
return action === 'push'
|
|
806
|
+
? PgSquasher.squashIdxPush(index)
|
|
807
|
+
: PgSquasher.squashIdx(index);
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
const squashedFKs = mapValues(it[1].foreignKeys, (fk) => {
|
|
811
|
+
return PgSquasher.squashFK(fk);
|
|
812
|
+
});
|
|
813
|
+
|
|
814
|
+
const squashedPKs = mapValues(it[1].compositePrimaryKeys, (pk) => {
|
|
815
|
+
return PgSquasher.squashPK(pk);
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
const mappedColumns = Object.fromEntries(
|
|
819
|
+
Object.entries(it[1].columns).map((it) => {
|
|
820
|
+
const mappedIdentity = it[1].identity
|
|
821
|
+
? PgSquasher.squashIdentity(it[1].identity)
|
|
822
|
+
: undefined;
|
|
823
|
+
return [
|
|
824
|
+
it[0],
|
|
825
|
+
{
|
|
826
|
+
...it[1],
|
|
827
|
+
identity: mappedIdentity,
|
|
828
|
+
},
|
|
829
|
+
];
|
|
830
|
+
}),
|
|
831
|
+
);
|
|
832
|
+
|
|
833
|
+
const squashedUniqueConstraints = mapValues(
|
|
834
|
+
it[1].uniqueConstraints,
|
|
835
|
+
(unq) => {
|
|
836
|
+
return PgSquasher.squashUnique(unq);
|
|
837
|
+
},
|
|
838
|
+
);
|
|
839
|
+
|
|
840
|
+
const squashedPolicies = mapValues(it[1].policies, (policy) => {
|
|
841
|
+
return action === 'push'
|
|
842
|
+
? PgSquasher.squashPolicyPush(policy)
|
|
843
|
+
: PgSquasher.squashPolicy(policy);
|
|
844
|
+
});
|
|
845
|
+
const squashedChecksContraints = mapValues(
|
|
846
|
+
it[1].checkConstraints,
|
|
847
|
+
(check) => {
|
|
848
|
+
return PgSquasher.squashCheck(check);
|
|
849
|
+
},
|
|
850
|
+
);
|
|
851
|
+
|
|
852
|
+
return [
|
|
853
|
+
it[0],
|
|
854
|
+
{
|
|
855
|
+
name: it[1].name,
|
|
856
|
+
schema: it[1].schema,
|
|
857
|
+
columns: mappedColumns,
|
|
858
|
+
indexes: squashedIndexes,
|
|
859
|
+
foreignKeys: squashedFKs,
|
|
860
|
+
compositePrimaryKeys: squashedPKs,
|
|
861
|
+
uniqueConstraints: squashedUniqueConstraints,
|
|
862
|
+
policies: squashedPolicies,
|
|
863
|
+
checkConstraints: squashedChecksContraints,
|
|
864
|
+
isRLSEnabled: it[1].isRLSEnabled ?? false,
|
|
865
|
+
},
|
|
866
|
+
];
|
|
867
|
+
}),
|
|
868
|
+
);
|
|
869
|
+
|
|
870
|
+
const mappedSequences = Object.fromEntries(
|
|
871
|
+
Object.entries(json.sequences).map((it) => {
|
|
872
|
+
return [
|
|
873
|
+
it[0],
|
|
874
|
+
{
|
|
875
|
+
name: it[1].name,
|
|
876
|
+
schema: it[1].schema,
|
|
877
|
+
values: PgSquasher.squashSequence(it[1]),
|
|
878
|
+
},
|
|
879
|
+
];
|
|
880
|
+
}),
|
|
881
|
+
);
|
|
882
|
+
|
|
883
|
+
const mappedPolicies = Object.fromEntries(
|
|
884
|
+
Object.entries(json.policies).map((it) => {
|
|
885
|
+
return [
|
|
886
|
+
it[0],
|
|
887
|
+
{
|
|
888
|
+
name: it[1].name,
|
|
889
|
+
values: action === 'push'
|
|
890
|
+
? PgSquasher.squashPolicyPush(it[1])
|
|
891
|
+
: PgSquasher.squashPolicy(it[1]),
|
|
892
|
+
},
|
|
893
|
+
];
|
|
894
|
+
}),
|
|
895
|
+
);
|
|
896
|
+
|
|
897
|
+
const mappedDomains = mapValues(json.domains, (domain) => {
|
|
898
|
+
const squashedDomainChecks = mapValues(
|
|
899
|
+
domain.checkConstraints ?? {},
|
|
900
|
+
(check) => PgSquasher.squashCheck(check),
|
|
901
|
+
);
|
|
902
|
+
|
|
903
|
+
return { ...domain, checkConstraints: squashedDomainChecks };
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
return {
|
|
907
|
+
version: snapshotVersion,
|
|
908
|
+
dialect: json.dialect,
|
|
909
|
+
tables: mappedTables,
|
|
910
|
+
domains: mappedDomains,
|
|
911
|
+
enums: json.enums,
|
|
912
|
+
schemas: json.schemas,
|
|
913
|
+
views: json.views,
|
|
914
|
+
policies: mappedPolicies,
|
|
915
|
+
sequences: mappedSequences,
|
|
916
|
+
roles: json.roles,
|
|
917
|
+
};
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
export const dryPg = pgSchema.parse({
|
|
921
|
+
version: snapshotVersion,
|
|
922
|
+
dialect: 'postgresql',
|
|
923
|
+
id: originUUID,
|
|
924
|
+
prevId: '',
|
|
925
|
+
tables: {},
|
|
926
|
+
domains: {},
|
|
927
|
+
enums: {},
|
|
928
|
+
schemas: {},
|
|
929
|
+
policies: {},
|
|
930
|
+
roles: {},
|
|
931
|
+
sequences: {},
|
|
932
|
+
_meta: {
|
|
933
|
+
schemas: {},
|
|
934
|
+
tables: {},
|
|
935
|
+
columns: {},
|
|
936
|
+
},
|
|
937
|
+
});
|