rake-db 2.3.30 → 2.3.31

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.
Files changed (52) hide show
  1. package/package.json +11 -21
  2. package/.env +0 -1
  3. package/.env.local +0 -2
  4. package/.turbo/turbo-check.log +0 -23
  5. package/.turbo/turbo-test.log +0 -22
  6. package/.turbo/turbo-test:ci.log +0 -22
  7. package/CHANGELOG.md +0 -395
  8. package/app/dbScript.ts +0 -33
  9. package/app/migrations/20221017181504_createUser.ts +0 -14
  10. package/app/migrations/20221017200111_createProfile.ts +0 -10
  11. package/app/migrations/20221017200252_createChat.ts +0 -9
  12. package/app/migrations/20221017200326_createChatUser.ts +0 -10
  13. package/app/migrations/20221017200900_createMessage.ts +0 -12
  14. package/app/migrations/20221017201235_createGeoSchema.ts +0 -5
  15. package/app/migrations/20221017210011_createCountry.ts +0 -8
  16. package/app/migrations/20221017210133_createCity.ts +0 -9
  17. package/app/migrations/20221105202843_createUniqueTable.ts +0 -12
  18. package/jest-setup.ts +0 -3
  19. package/rollup.config.js +0 -3
  20. package/src/ast.ts +0 -130
  21. package/src/commands/createOrDrop.test.ts +0 -214
  22. package/src/commands/createOrDrop.ts +0 -151
  23. package/src/commands/generate.test.ts +0 -136
  24. package/src/commands/generate.ts +0 -93
  25. package/src/commands/migrateOrRollback.test.ts +0 -267
  26. package/src/commands/migrateOrRollback.ts +0 -190
  27. package/src/common.test.ts +0 -295
  28. package/src/common.ts +0 -353
  29. package/src/errors.ts +0 -3
  30. package/src/index.ts +0 -8
  31. package/src/migration/change.test.ts +0 -16
  32. package/src/migration/change.ts +0 -15
  33. package/src/migration/changeTable.test.ts +0 -897
  34. package/src/migration/changeTable.ts +0 -566
  35. package/src/migration/createTable.test.ts +0 -384
  36. package/src/migration/createTable.ts +0 -193
  37. package/src/migration/migration.test.ts +0 -430
  38. package/src/migration/migration.ts +0 -518
  39. package/src/migration/migrationUtils.ts +0 -307
  40. package/src/migration/tableMethods.ts +0 -8
  41. package/src/pull/astToMigration.test.ts +0 -275
  42. package/src/pull/astToMigration.ts +0 -173
  43. package/src/pull/dbStructure.test.ts +0 -180
  44. package/src/pull/dbStructure.ts +0 -413
  45. package/src/pull/pull.test.ts +0 -115
  46. package/src/pull/pull.ts +0 -22
  47. package/src/pull/structureToAst.test.ts +0 -841
  48. package/src/pull/structureToAst.ts +0 -372
  49. package/src/rakeDb.test.ts +0 -131
  50. package/src/rakeDb.ts +0 -84
  51. package/src/test-utils.ts +0 -64
  52. package/tsconfig.json +0 -12
@@ -1,518 +0,0 @@
1
- import {
2
- ColumnsShape,
3
- ColumnType,
4
- ColumnTypes,
5
- ForeignKeyOptions,
6
- IndexColumnOptions,
7
- IndexOptions,
8
- logParamToLogObject,
9
- MaybeArray,
10
- QueryInput,
11
- QueryLogObject,
12
- Sql,
13
- TransactionAdapter,
14
- raw,
15
- TextColumn,
16
- AdapterOptions,
17
- createDb,
18
- DbResult,
19
- DefaultColumnTypes,
20
- EnumColumn,
21
- quote,
22
- } from 'pqb';
23
- import { createTable } from './createTable';
24
- import { changeTable, TableChangeData, TableChanger } from './changeTable';
25
- import {
26
- RakeDbConfig,
27
- quoteWithSchema,
28
- getSchemaAndTableFromName,
29
- } from '../common';
30
- import { RakeDbAst } from '../ast';
31
-
32
- export type DropMode = 'CASCADE' | 'RESTRICT';
33
-
34
- export type TableOptions = {
35
- dropMode?: DropMode;
36
- comment?: string;
37
- noPrimaryKey?: boolean;
38
- };
39
-
40
- type TextColumnCreator = () => TextColumn;
41
-
42
- export type MigrationColumnTypes = Omit<
43
- ColumnTypes,
44
- 'text' | 'string' | 'enum'
45
- > & {
46
- text: TextColumnCreator;
47
- string: TextColumnCreator;
48
- enum: (name: string) => EnumColumn;
49
- };
50
-
51
- export type ColumnsShapeCallback = (
52
- t: MigrationColumnTypes & { raw: typeof raw },
53
- ) => ColumnsShape;
54
-
55
- export type ChangeTableOptions = { comment?: string | [string, string] | null };
56
- export type ChangeTableCallback = (t: TableChanger) => TableChangeData;
57
-
58
- export type ColumnComment = { column: string; comment: string | null };
59
-
60
- export type JoinTableOptions = {
61
- tableName?: string;
62
- comment?: string;
63
- dropMode?: DropMode;
64
- };
65
-
66
- export type Migration = DbResult<DefaultColumnTypes> & MigrationBase;
67
-
68
- export const createMigrationInterface = (
69
- tx: TransactionAdapter,
70
- up: boolean,
71
- options: RakeDbConfig,
72
- adapterOptions: AdapterOptions,
73
- appCodeUpdaterCache: object,
74
- ) => {
75
- const adapter = new TransactionAdapter(tx, tx.client, tx.types);
76
- const { query, arrays } = adapter;
77
- const log = logParamToLogObject(options.logger || console, options.log);
78
-
79
- adapter.query = ((q, types) => {
80
- return wrapWithLog(log, q, () => query.call(adapter, q, types));
81
- }) as typeof adapter.query;
82
-
83
- adapter.arrays = ((q, types) => {
84
- return wrapWithLog(log, q, () => arrays.call(adapter, q, types));
85
- }) as typeof adapter.arrays;
86
-
87
- const db = createDb({ adapter }) as unknown as Migration;
88
-
89
- const { prototype: proto } = MigrationBase;
90
- for (const key of Object.getOwnPropertyNames(proto)) {
91
- (db as unknown as Record<string, unknown>)[key] =
92
- proto[key as keyof typeof proto];
93
- }
94
-
95
- return Object.assign(db, {
96
- adapter,
97
- log,
98
- up,
99
- options,
100
- adapterOptions,
101
- appCodeUpdaterCache,
102
- });
103
- };
104
-
105
- export class MigrationBase {
106
- public adapter!: TransactionAdapter;
107
- public log?: QueryLogObject;
108
- public up!: boolean;
109
- public options!: RakeDbConfig;
110
- public adapterOptions!: AdapterOptions;
111
- public appCodeUpdaterCache!: object;
112
-
113
- createTable(
114
- tableName: string,
115
- options: TableOptions,
116
- fn: ColumnsShapeCallback,
117
- ): Promise<void>;
118
- createTable(tableName: string, fn: ColumnsShapeCallback): Promise<void>;
119
- createTable(
120
- tableName: string,
121
- cbOrOptions: ColumnsShapeCallback | TableOptions,
122
- cb?: ColumnsShapeCallback,
123
- ): Promise<void> {
124
- const options = typeof cbOrOptions === 'function' ? {} : cbOrOptions;
125
- const fn = (cb || cbOrOptions) as ColumnsShapeCallback;
126
-
127
- return createTable(this, this.up, tableName, options, fn);
128
- }
129
-
130
- dropTable(
131
- tableName: string,
132
- options: TableOptions,
133
- fn: ColumnsShapeCallback,
134
- ): Promise<void>;
135
- dropTable(tableName: string, fn: ColumnsShapeCallback): Promise<void>;
136
- dropTable(
137
- tableName: string,
138
- cbOrOptions: ColumnsShapeCallback | TableOptions,
139
- cb?: ColumnsShapeCallback,
140
- ) {
141
- const options = typeof cbOrOptions === 'function' ? {} : cbOrOptions;
142
- const fn = (cb || cbOrOptions) as ColumnsShapeCallback;
143
-
144
- return createTable(this, !this.up, tableName, options, fn);
145
- }
146
-
147
- changeTable(
148
- tableName: string,
149
- options: ChangeTableOptions,
150
- fn?: ChangeTableCallback,
151
- ): Promise<void>;
152
- changeTable(tableName: string, fn: ChangeTableCallback): Promise<void>;
153
- changeTable(
154
- tableName: string,
155
- cbOrOptions: ChangeTableCallback | ChangeTableOptions,
156
- cb?: ChangeTableCallback,
157
- ) {
158
- const [fn, options] =
159
- typeof cbOrOptions === 'function' ? [cbOrOptions, {}] : [cb, cbOrOptions];
160
-
161
- return changeTable(this, this.up, tableName, options, fn);
162
- }
163
-
164
- async renameTable(from: string, to: string): Promise<void> {
165
- const [fromSchema, f] = getSchemaAndTableFromName(this.up ? from : to);
166
- const [toSchema, t] = getSchemaAndTableFromName(this.up ? to : from);
167
- const ast: RakeDbAst.RenameTable = {
168
- type: 'renameTable',
169
- fromSchema,
170
- from: f,
171
- toSchema,
172
- to: t,
173
- };
174
-
175
- await this.adapter.query(
176
- `ALTER TABLE ${quoteWithSchema({
177
- schema: ast.fromSchema,
178
- name: ast.from,
179
- })} RENAME TO ${quoteWithSchema({
180
- schema: ast.toSchema,
181
- name: ast.to,
182
- })}`,
183
- );
184
-
185
- await runCodeUpdater(this, ast);
186
- }
187
-
188
- addColumn(
189
- tableName: string,
190
- columnName: string,
191
- fn: (t: MigrationColumnTypes) => ColumnType,
192
- ) {
193
- return addColumn(this, this.up, tableName, columnName, fn);
194
- }
195
-
196
- dropColumn(
197
- tableName: string,
198
- columnName: string,
199
- fn: (t: MigrationColumnTypes) => ColumnType,
200
- ) {
201
- return addColumn(this, !this.up, tableName, columnName, fn);
202
- }
203
-
204
- addIndex(
205
- tableName: string,
206
- columns: MaybeArray<string | IndexColumnOptions>,
207
- options?: IndexOptions,
208
- ) {
209
- return addIndex(this, this.up, tableName, columns, options);
210
- }
211
-
212
- dropIndex(
213
- tableName: string,
214
- columns: MaybeArray<string | IndexColumnOptions>,
215
- options?: IndexOptions,
216
- ) {
217
- return addIndex(this, !this.up, tableName, columns, options);
218
- }
219
-
220
- addForeignKey(
221
- tableName: string,
222
- columns: [string, ...string[]],
223
- foreignTable: string,
224
- foreignColumns: [string, ...string[]],
225
- options?: ForeignKeyOptions,
226
- ) {
227
- return addForeignKey(
228
- this,
229
- this.up,
230
- tableName,
231
- columns,
232
- foreignTable,
233
- foreignColumns,
234
- options,
235
- );
236
- }
237
-
238
- dropForeignKey(
239
- tableName: string,
240
- columns: [string, ...string[]],
241
- foreignTable: string,
242
- foreignColumns: [string, ...string[]],
243
- options?: ForeignKeyOptions,
244
- ) {
245
- return addForeignKey(
246
- this,
247
- !this.up,
248
- tableName,
249
- columns,
250
- foreignTable,
251
- foreignColumns,
252
- options,
253
- );
254
- }
255
-
256
- addPrimaryKey(
257
- tableName: string,
258
- columns: string[],
259
- options?: { name?: string },
260
- ) {
261
- return addPrimaryKey(this, this.up, tableName, columns, options);
262
- }
263
-
264
- dropPrimaryKey(
265
- tableName: string,
266
- columns: string[],
267
- options?: { name?: string },
268
- ) {
269
- return addPrimaryKey(this, !this.up, tableName, columns, options);
270
- }
271
-
272
- renameColumn(tableName: string, from: string, to: string) {
273
- return this.changeTable(tableName, (t) => ({
274
- [from]: t.rename(to),
275
- }));
276
- }
277
-
278
- createSchema(schemaName: string) {
279
- return createSchema(this, this.up, schemaName);
280
- }
281
-
282
- dropSchema(schemaName: string) {
283
- return createSchema(this, !this.up, schemaName);
284
- }
285
-
286
- createExtension(
287
- name: string,
288
- options: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name'> = {},
289
- ) {
290
- return createExtension(this, this.up, name, options);
291
- }
292
-
293
- dropExtension(
294
- name: string,
295
- options: Omit<
296
- RakeDbAst.Extension,
297
- 'type' | 'action' | 'name' | 'values'
298
- > = {},
299
- ) {
300
- return createExtension(this, !this.up, name, options);
301
- }
302
-
303
- createEnum(
304
- name: string,
305
- values: string[],
306
- options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values'>,
307
- ) {
308
- return createEnum(this, this.up, name, values, options);
309
- }
310
-
311
- dropEnum(
312
- name: string,
313
- values: string[],
314
- options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values'>,
315
- ) {
316
- return createEnum(this, !this.up, name, values, options);
317
- }
318
-
319
- async tableExists(tableName: string) {
320
- return queryExists(this, {
321
- text: `SELECT 1 FROM "information_schema"."tables" WHERE "table_name" = $1`,
322
- values: [tableName],
323
- });
324
- }
325
-
326
- async columnExists(tableName: string, columnName: string): Promise<boolean> {
327
- return queryExists(this, {
328
- text: `SELECT 1 FROM "information_schema"."columns" WHERE "table_name" = $1 AND "column_name" = $2`,
329
- values: [tableName, columnName],
330
- });
331
- }
332
-
333
- async constraintExists(constraintName: string): Promise<boolean> {
334
- return queryExists(this, {
335
- text: `SELECT 1 FROM "information_schema"."table_constraints" WHERE "constraint_name" = $1`,
336
- values: [constraintName],
337
- });
338
- }
339
- }
340
-
341
- const wrapWithLog = async <Result>(
342
- log: QueryLogObject | undefined,
343
- query: QueryInput,
344
- fn: () => Promise<Result>,
345
- ): Promise<Result> => {
346
- if (!log) {
347
- return fn();
348
- } else {
349
- const sql = (
350
- typeof query === 'string'
351
- ? { text: query, values: [] }
352
- : query.values
353
- ? query
354
- : { ...query, values: [] }
355
- ) as Sql;
356
-
357
- const logData = log.beforeQuery(sql);
358
-
359
- try {
360
- const result = await fn();
361
- log.afterQuery(sql, logData);
362
- return result;
363
- } catch (err) {
364
- log.onError(err as Error, sql, logData);
365
- throw err;
366
- }
367
- }
368
- };
369
-
370
- const addColumn = (
371
- migration: MigrationBase,
372
- up: boolean,
373
- tableName: string,
374
- columnName: string,
375
- fn: (t: MigrationColumnTypes) => ColumnType,
376
- ) => {
377
- return changeTable(migration, up, tableName, {}, (t) => ({
378
- [columnName]: t.add(fn(t)),
379
- }));
380
- };
381
-
382
- const addIndex = (
383
- migration: MigrationBase,
384
- up: boolean,
385
- tableName: string,
386
- columns: MaybeArray<string | IndexColumnOptions>,
387
- options?: IndexOptions,
388
- ) => {
389
- return changeTable(migration, up, tableName, {}, (t) => ({
390
- ...t.add(t.index(columns, options)),
391
- }));
392
- };
393
-
394
- const addForeignKey = (
395
- migration: MigrationBase,
396
- up: boolean,
397
- tableName: string,
398
- columns: [string, ...string[]],
399
- foreignTable: string,
400
- foreignColumns: [string, ...string[]],
401
- options?: ForeignKeyOptions,
402
- ) => {
403
- return changeTable(migration, up, tableName, {}, (t) => ({
404
- ...t.add(t.foreignKey(columns, foreignTable, foreignColumns, options)),
405
- }));
406
- };
407
-
408
- const addPrimaryKey = (
409
- migration: MigrationBase,
410
- up: boolean,
411
- tableName: string,
412
- columns: string[],
413
- options?: { name?: string },
414
- ) => {
415
- return changeTable(migration, up, tableName, {}, (t) => ({
416
- ...t.add(t.primaryKey(columns, options)),
417
- }));
418
- };
419
-
420
- const createSchema = async (
421
- migration: MigrationBase,
422
- up: boolean,
423
- name: string,
424
- ) => {
425
- const ast: RakeDbAst.Schema = {
426
- type: 'schema',
427
- action: up ? 'create' : 'drop',
428
- name,
429
- };
430
-
431
- await migration.adapter.query(
432
- `${ast.action === 'create' ? 'CREATE' : 'DROP'} SCHEMA "${name}"`,
433
- );
434
-
435
- await runCodeUpdater(migration, ast);
436
- };
437
-
438
- const createExtension = async (
439
- migration: MigrationBase,
440
- up: boolean,
441
- name: string,
442
- options: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name'>,
443
- ) => {
444
- const ast: RakeDbAst.Extension = {
445
- type: 'extension',
446
- action: up ? 'create' : 'drop',
447
- name,
448
- ...options,
449
- };
450
-
451
- let query;
452
- if (ast.action === 'drop') {
453
- query = `DROP EXTENSION${ast.dropIfExists ? ' IF EXISTS' : ''} "${
454
- ast.name
455
- }"${ast.cascade ? ' CASCADE' : ''}`;
456
- } else {
457
- query = `CREATE EXTENSION${
458
- ast.createIfNotExists ? ' IF NOT EXISTS' : ''
459
- } "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ''}${
460
- ast.version ? ` VERSION '${ast.version}'` : ''
461
- }${ast.cascade ? ' CASCADE' : ''}`;
462
- }
463
-
464
- await migration.adapter.query(query);
465
-
466
- await runCodeUpdater(migration, ast);
467
- };
468
-
469
- const createEnum = async (
470
- migration: MigrationBase,
471
- up: boolean,
472
- name: string,
473
- values: string[],
474
- options: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values'> = {},
475
- ) => {
476
- const [schema, enumName] = getSchemaAndTableFromName(name);
477
-
478
- const ast: RakeDbAst.Enum = {
479
- type: 'enum',
480
- action: up ? 'create' : 'drop',
481
- schema,
482
- name: enumName,
483
- values,
484
- ...options,
485
- };
486
-
487
- let query;
488
- const quotedName = quoteWithSchema(ast);
489
- if (ast.action === 'create') {
490
- query = `CREATE TYPE ${quotedName} AS ENUM (${values
491
- .map(quote)
492
- .join(', ')})`;
493
- } else {
494
- query = `DROP TYPE${ast.dropIfExists ? ' IF EXISTS' : ''} ${quotedName}${
495
- ast.cascade ? ' CASCADE' : ''
496
- }`;
497
- }
498
-
499
- await migration.adapter.query(query);
500
-
501
- await runCodeUpdater(migration, ast);
502
- };
503
-
504
- const queryExists = (
505
- db: MigrationBase,
506
- sql: { text: string; values: unknown[] },
507
- ) => {
508
- return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
509
- };
510
-
511
- export const runCodeUpdater = (migration: MigrationBase, ast: RakeDbAst) => {
512
- return migration.options.appCodeUpdater?.({
513
- ast,
514
- options: migration.adapterOptions,
515
- basePath: migration.options.basePath,
516
- cache: migration.appCodeUpdaterCache,
517
- });
518
- };