@@ -45,34 +45,34 @@ function stringFromIdentityProperty(
45
45
return typeof field === 'string'
46
46
? ( field as string )
47
47
: typeof field === 'undefined'
48
- ? undefined
49
- : String ( field ) ;
48
+ ? undefined
49
+ : String ( field ) ;
50
50
}
51
51
52
52
function maxRangeForIdentityBasedOn ( columnType : string ) {
53
53
return columnType === 'integer'
54
54
? '2147483647'
55
55
: columnType === 'bigint'
56
- ? '9223372036854775807'
57
- : '32767' ;
56
+ ? '9223372036854775807'
57
+ : '32767' ;
58
58
}
59
59
60
60
function minRangeForIdentityBasedOn ( columnType : string ) {
61
61
return columnType === 'integer'
62
62
? '-2147483648'
63
63
: columnType === 'bitint'
64
- ? '-9223372036854775808'
65
- : '-32768' ;
64
+ ? '-9223372036854775808'
65
+ : '-32768' ;
66
66
}
67
67
68
68
function stringFromDatabaseIdentityProperty ( field : any ) : string | undefined {
69
69
return typeof field === 'string'
70
70
? ( field as string )
71
71
: typeof field === 'undefined'
72
- ? undefined
73
- : typeof field === 'bigint'
74
- ? field . toString ( )
75
- : String ( field ) ;
72
+ ? undefined
73
+ : typeof field === 'bigint'
74
+ ? field . toString ( )
75
+ : String ( field ) ;
76
76
}
77
77
78
78
export const generatePgSnapshot = (
@@ -146,8 +146,8 @@ export const generatePgSnapshot = (
146
146
as : is ( generated . as , SQL )
147
147
? dialect . sqlToQuery ( generated . as as SQL ) . sql
148
148
: typeof generated . as === 'function'
149
- ? dialect . sqlToQuery ( generated . as ( ) as SQL ) . sql
150
- : ( generated . as as any ) ,
149
+ ? dialect . sqlToQuery ( generated . as ( ) as SQL ) . sql
150
+ : ( generated . as as any ) ,
151
151
type : 'stored' ,
152
152
}
153
153
: undefined ,
@@ -170,24 +170,19 @@ export const generatePgSnapshot = (
170
170
const existingUnique = uniqueConstraintObject [ column . uniqueName ! ] ;
171
171
if ( typeof existingUnique !== 'undefined' ) {
172
172
console . log (
173
- `\n${
174
- withStyle . errorWarning ( `We\'ve found duplicated unique constraint names in ${
175
- chalk . underline . blue (
176
- tableName ,
177
- )
173
+ `\n${ withStyle . errorWarning ( `We\'ve found duplicated unique constraint names in ${ chalk . underline . blue (
174
+ tableName ,
175
+ )
178
176
} table.
179
- The unique constraint ${
180
- chalk . underline . blue (
181
- column . uniqueName ,
182
- )
183
- } on the ${
184
- chalk . underline . blue (
185
- column . name ,
186
- )
187
- } column is confilcting with a unique constraint name already defined for ${
188
- chalk . underline . blue (
189
- existingUnique . columns . join ( ',' ) ,
190
- )
177
+ The unique constraint ${ chalk . underline . blue (
178
+ column . uniqueName ,
179
+ )
180
+ } on the ${ chalk . underline . blue (
181
+ column . name ,
182
+ )
183
+ } column is confilcting with a unique constraint name already defined for ${ chalk . underline . blue (
184
+ existingUnique . columns . join ( ',' ) ,
185
+ )
191
186
} columns\n`)
192
187
} `,
193
188
) ;
@@ -208,21 +203,19 @@ export const generatePgSnapshot = (
208
203
columnToSet . default = `'${ column . default } '` ;
209
204
} else {
210
205
if ( sqlTypeLowered === 'jsonb' || sqlTypeLowered === 'json' ) {
211
- columnToSet . default = `'${
212
- JSON . stringify (
213
- column . default ,
214
- )
215
- } '::${ sqlTypeLowered } `;
206
+ columnToSet . default = `'${ JSON . stringify (
207
+ column . default ,
208
+ )
209
+ } '::${ sqlTypeLowered } `;
216
210
} else if ( column . default instanceof Date ) {
217
211
if ( sqlTypeLowered === 'date' ) {
218
212
columnToSet . default = `'${ column . default . toISOString ( ) . split ( 'T' ) [ 0 ] } '` ;
219
213
} else if ( sqlTypeLowered === 'timestamp' ) {
220
- columnToSet . default = `'${
221
- column . default
222
- . toISOString ( )
223
- . replace ( 'T' , ' ' )
224
- . slice ( 0 , 23 )
225
- } '`;
214
+ columnToSet . default = `'${ column . default
215
+ . toISOString ( )
216
+ . replace ( 'T' , ' ' )
217
+ . slice ( 0 , 23 )
218
+ } '`;
226
219
} else {
227
220
columnToSet . default = `'${ column . default . toISOString ( ) } '` ;
228
221
}
@@ -253,24 +246,19 @@ export const generatePgSnapshot = (
253
246
const existingUnique = uniqueConstraintObject [ name ] ;
254
247
if ( typeof existingUnique !== 'undefined' ) {
255
248
console . log (
256
- `\n${
257
- withStyle . errorWarning ( `We\'ve found duplicated unique constraint names in ${
258
- chalk . underline . blue (
259
- tableName ,
260
- )
249
+ `\n${ withStyle . errorWarning ( `We\'ve found duplicated unique constraint names in ${ chalk . underline . blue (
250
+ tableName ,
251
+ )
261
252
} table.
262
- The unique constraint ${
263
- chalk . underline . blue (
264
- name ,
265
- )
266
- } on the ${
267
- chalk . underline . blue (
268
- columnNames . join ( ',' ) ,
269
- )
270
- } columns is confilcting with a unique constraint name already defined for ${
271
- chalk . underline . blue (
272
- existingUnique . columns . join ( ',' ) ,
273
- )
253
+ The unique constraint ${ chalk . underline . blue (
254
+ name ,
255
+ )
256
+ } on the ${ chalk . underline . blue (
257
+ columnNames . join ( ',' ) ,
258
+ )
259
+ } columns is confilcting with a unique constraint name already defined for ${ chalk . underline . blue (
260
+ existingUnique . columns . join ( ',' ) ,
261
+ )
274
262
} columns\n`)
275
263
} `,
276
264
) ;
@@ -323,16 +311,13 @@ export const generatePgSnapshot = (
323
311
if ( is ( it , SQL ) ) {
324
312
if ( typeof value . config . name === 'undefined' ) {
325
313
console . log (
326
- `\n${
327
- withStyle . errorWarning (
328
- `Please specify an index name in ${
329
- getTableName (
330
- value . config . table ,
331
- )
332
- } table that has "${
333
- dialect . sqlToQuery ( it ) . sql
334
- } " expression. We can generate index names for indexes on columns only; for expressions in indexes, you need to specify the name yourself.`,
314
+ `\n${ withStyle . errorWarning (
315
+ `Please specify an index name in ${ getTableName (
316
+ value . config . table ,
335
317
)
318
+ } table that has "${ dialect . sqlToQuery ( it ) . sql
319
+ } " expression. We can generate index names for indexes on columns only; for expressions in indexes, you need to specify the name yourself.`,
320
+ )
336
321
} `,
337
322
) ;
338
323
process . exit ( 1 ) ;
@@ -345,34 +330,27 @@ export const generatePgSnapshot = (
345
330
&& typeof it . indexConfig ! . opClass === 'undefined'
346
331
) {
347
332
console . log (
348
- `\n${
349
- withStyle . errorWarning (
350
- `You are specifying an index on the ${
351
- chalk . blueBright (
352
- it . name ,
353
- )
354
- } column inside the ${
355
- chalk . blueBright (
356
- tableName ,
357
- )
358
- } table with the ${
359
- chalk . blueBright (
360
- 'vector' ,
361
- )
362
- } type without specifying an operator class. Vector extension doesn't have a default operator class, so you need to specify one of the available options. Here is a list of available op classes for the vector extension: [${
363
- vectorOps
364
- . map ( ( it ) => `${ chalk . underline ( `${ it } ` ) } ` )
365
- . join (
366
- ', ' ,
367
- )
368
- } ].\n\nYou can specify it using current syntax: ${
369
- chalk . underline (
370
- `index("${ value . config . name } ").using("${ value . config . method } ", table.${ it . name } .op("${
371
- vectorOps [ 0 ]
372
- } "))`,
373
- )
374
- } \n\nYou can check the "pg_vector" docs for more info: https://github.com/pgvector/pgvector?tab=readme-ov-file#indexing\n`,
333
+ `\n${ withStyle . errorWarning (
334
+ `You are specifying an index on the ${ chalk . blueBright (
335
+ it . name ,
336
+ )
337
+ } column inside the ${ chalk . blueBright (
338
+ tableName ,
339
+ )
340
+ } table with the ${ chalk . blueBright (
341
+ 'vector' ,
375
342
)
343
+ } type without specifying an operator class. Vector extension doesn't have a default operator class, so you need to specify one of the available options. Here is a list of available op classes for the vector extension: [${ vectorOps
344
+ . map ( ( it ) => `${ chalk . underline ( `${ it } ` ) } ` )
345
+ . join (
346
+ ', ' ,
347
+ )
348
+ } ].\n\nYou can specify it using current syntax: ${ chalk . underline (
349
+ `index("${ value . config . name } ").using("${ value . config . method } ", table.${ it . name } .op("${ vectorOps [ 0 ]
350
+ } "))`,
351
+ )
352
+ } \n\nYou can check the "pg_vector" docs for more info: https://github.com/pgvector/pgvector?tab=readme-ov-file#indexing\n`,
353
+ )
376
354
} `,
377
355
) ;
378
356
process . exit ( 1 ) ;
@@ -402,8 +380,8 @@ export const generatePgSnapshot = (
402
380
nulls : it . indexConfig ?. nulls
403
381
? it . indexConfig ?. nulls
404
382
: it . indexConfig ?. order === 'desc'
405
- ? 'first'
406
- : 'last' ,
383
+ ? 'first'
384
+ : 'last' ,
407
385
opclass : it . indexConfig ?. opClass ,
408
386
} ;
409
387
}
@@ -414,18 +392,15 @@ export const generatePgSnapshot = (
414
392
if ( typeof indexesInSchema [ schema ?? 'public' ] !== 'undefined' ) {
415
393
if ( indexesInSchema [ schema ?? 'public' ] . includes ( name ) ) {
416
394
console . log (
417
- `\n${
418
- withStyle . errorWarning (
419
- `We\'ve found duplicated index name across ${
420
- chalk . underline . blue (
421
- schema ?? 'public' ,
422
- )
423
- } schema. Please rename your index in either the ${
424
- chalk . underline . blue (
425
- tableName ,
426
- )
427
- } table or the table with the duplicated index name`,
395
+ `\n${ withStyle . errorWarning (
396
+ `We\'ve found duplicated index name across ${ chalk . underline . blue (
397
+ schema ?? 'public' ,
398
+ )
399
+ } schema. Please rename your index in either the ${ chalk . underline . blue (
400
+ tableName ,
428
401
)
402
+ } table or the table with the duplicated index name`,
403
+ )
429
404
} `,
430
405
) ;
431
406
process . exit ( 1 ) ;
@@ -465,7 +440,7 @@ export const generatePgSnapshot = (
465
440
const name = sequence . seqName ! ;
466
441
if (
467
442
typeof sequencesToReturn [ `${ sequence . schema ?? 'public' } .${ name } ` ]
468
- === 'undefined'
443
+ === 'undefined'
469
444
) {
470
445
const increment = stringFromIdentityProperty ( sequence ?. seqOptions ?. increment ) ?? '1' ;
471
446
const minValue = stringFromIdentityProperty ( sequence ?. seqOptions ?. minValue )
@@ -594,8 +569,7 @@ export const fromDatabase = async (
594
569
const seqWhere = schemaFilters . map ( ( t ) => `schemaname = '${ t } '` ) . join ( ' or ' ) ;
595
570
596
571
const allSequences = await db . query (
597
- `select schemaname, sequencename, start_value, min_value, max_value, increment_by, cycle, cache_size from pg_sequences as seq${
598
- seqWhere === '' ? '' : ` WHERE ${ seqWhere } `
572
+ `select schemaname, sequencename, start_value, min_value, max_value, increment_by, cycle, cache_size from pg_sequences as seq${ seqWhere === '' ? '' : ` WHERE ${ seqWhere } `
599
573
} ;`,
600
574
) ;
601
575
@@ -622,6 +596,10 @@ export const fromDatabase = async (
622
596
} ;
623
597
}
624
598
599
+ const whereEnums = schemaFilters
600
+ . map ( ( t ) => `n.nspname = '${ t } '` )
601
+ . join ( " or " ) ;
602
+
625
603
const allEnums = await db . query (
626
604
`select n.nspname as enum_schema,
627
605
t.typname as enum_name,
@@ -630,6 +608,7 @@ export const fromDatabase = async (
630
608
from pg_type t
631
609
join pg_enum e on t.oid = e.enumtypid
632
610
join pg_catalog.pg_namespace n ON n.oid = t.typnamespace
611
+ ${ whereEnums === "" ? "" : ` WHERE ${ whereEnums } ` }
633
612
order by enum_schema, enum_name, sort_order;` ,
634
613
) ;
635
614
@@ -881,7 +860,7 @@ export const fromDatabase = async (
881
860
} else {
882
861
if (
883
862
typeof internals . tables [ tableName ] ! . columns [ columnName ]
884
- === 'undefined'
863
+ === 'undefined'
885
864
) {
886
865
internals . tables [ tableName ] ! . columns [ columnName ] = {
887
866
isArray : true ,
@@ -937,8 +916,8 @@ export const fromDatabase = async (
937
916
cache : sequencesToReturn [ identityName ] ?. cache
938
917
? sequencesToReturn [ identityName ] ?. cache
939
918
: sequencesToReturn [ `${ tableSchema } .${ identityName } ` ] ?. cache
940
- ? sequencesToReturn [ `${ tableSchema } .${ identityName } ` ] ?. cache
941
- : undefined ,
919
+ ? sequencesToReturn [ `${ tableSchema } .${ identityName } ` ] ?. cache
920
+ : undefined ,
942
921
cycle : identityCycle ,
943
922
schema : tableSchema ,
944
923
}
@@ -1184,11 +1163,10 @@ const defaultForColumn = (column: any) => {
1184
1163
return Number ( rt ) ;
1185
1164
} else if ( column . data_type === 'json' || column . data_type === 'jsonb' ) {
1186
1165
const jsonWithoutSpaces = JSON . stringify ( JSON . parse ( rt ) ) ;
1187
- return `'${ jsonWithoutSpaces } '${
1188
- hasDifferentDefaultCast
1189
- ? columnToDefault [ hasDifferentDefaultCast ]
1190
- : `::${ column . data_type as string } `
1191
- } `;
1166
+ return `'${ jsonWithoutSpaces } '${ hasDifferentDefaultCast
1167
+ ? columnToDefault [ hasDifferentDefaultCast ]
1168
+ : `::${ column . data_type as string } `
1169
+ } `;
1192
1170
} else if ( column . data_type === 'boolean' ) {
1193
1171
return column . column_default === 'true' ;
1194
1172
} else {
0 commit comments