rake-db 2.27.3 → 2.27.4
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/dist/node-postgres.d.ts +1472 -0
- package/dist/node-postgres.js +5888 -0
- package/dist/node-postgres.js.map +1 -0
- package/dist/node-postgres.mjs +5885 -0
- package/dist/node-postgres.mjs.map +1 -0
- package/dist/postgres-js.d.ts +1472 -0
- package/dist/postgres-js.js +5888 -0
- package/dist/postgres-js.js.map +1 -0
- package/dist/postgres-js.mjs +5885 -0
- package/dist/postgres-js.mjs.map +1 -0
- package/package.json +14 -4
|
@@ -0,0 +1,1472 @@
|
|
|
1
|
+
import * as orchid_core from 'orchid-core';
|
|
2
|
+
import { MaybeArray, RawSQLBase, ColumnDataCheckBase, RecordString, ColumnTypeBase, EmptyObject, ColumnSchemaConfig, AdapterBase, QueryLogOptions, QueryLogObject } from 'orchid-core';
|
|
3
|
+
import * as pqb from 'pqb';
|
|
4
|
+
import { ColumnsShape, Db, TableData, NoPrimaryKeyOption, ColumnType, EnumColumn, DefaultColumnTypes, DefaultSchemaConfig, DbResult, TableDataFn, TableDataItem, DbDomainArg, raw } from 'pqb';
|
|
5
|
+
import { NodePostgresAdapterOptions } from 'pqb/node-postgres';
|
|
6
|
+
|
|
7
|
+
interface CreateTableResult<Table extends string, Shape extends ColumnsShape> {
|
|
8
|
+
table: Db<Table, Shape>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameType | RakeDbAst.Schema | RakeDbAst.RenameSchema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.EnumValues | RakeDbAst.RenameEnumValues | RakeDbAst.ChangeEnumValues | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.RenameTableItem | RakeDbAst.View;
|
|
12
|
+
declare namespace RakeDbAst {
|
|
13
|
+
interface Table extends TableData {
|
|
14
|
+
type: 'table';
|
|
15
|
+
action: 'create' | 'drop';
|
|
16
|
+
schema?: string;
|
|
17
|
+
name: string;
|
|
18
|
+
shape: ColumnsShape;
|
|
19
|
+
noPrimaryKey: NoPrimaryKeyOption;
|
|
20
|
+
createIfNotExists?: boolean;
|
|
21
|
+
dropIfExists?: boolean;
|
|
22
|
+
dropMode?: DropMode;
|
|
23
|
+
comment?: string;
|
|
24
|
+
}
|
|
25
|
+
interface ChangeTable {
|
|
26
|
+
type: 'changeTable';
|
|
27
|
+
schema?: string;
|
|
28
|
+
name: string;
|
|
29
|
+
comment?: string | [string, string] | null;
|
|
30
|
+
shape: ChangeTableShape;
|
|
31
|
+
add: TableData;
|
|
32
|
+
drop: TableData;
|
|
33
|
+
}
|
|
34
|
+
type ChangeTableShape = Record<string, MaybeArray<ChangeTableItem>>;
|
|
35
|
+
type ChangeTableItem = ChangeTableItem.Column | ChangeTableItem.Change | ChangeTableItem.Rename;
|
|
36
|
+
namespace ChangeTableItem {
|
|
37
|
+
interface Column {
|
|
38
|
+
type: 'add' | 'drop';
|
|
39
|
+
item: ColumnType;
|
|
40
|
+
dropMode?: DropMode;
|
|
41
|
+
}
|
|
42
|
+
interface Change {
|
|
43
|
+
type: 'change';
|
|
44
|
+
name?: string;
|
|
45
|
+
from: ColumnChange;
|
|
46
|
+
to: ColumnChange;
|
|
47
|
+
using?: ChangeUsing;
|
|
48
|
+
}
|
|
49
|
+
interface ChangeUsing {
|
|
50
|
+
usingUp?: RawSQLBase;
|
|
51
|
+
usingDown?: RawSQLBase;
|
|
52
|
+
}
|
|
53
|
+
interface Rename {
|
|
54
|
+
type: 'rename';
|
|
55
|
+
name: string;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
interface ColumnChange {
|
|
59
|
+
column?: ColumnType;
|
|
60
|
+
type?: string;
|
|
61
|
+
collate?: string;
|
|
62
|
+
default?: unknown | RawSQLBase;
|
|
63
|
+
nullable?: boolean;
|
|
64
|
+
comment?: string | null;
|
|
65
|
+
compression?: string;
|
|
66
|
+
primaryKey?: boolean;
|
|
67
|
+
checks?: ColumnDataCheckBase[];
|
|
68
|
+
foreignKeys?: TableData.ColumnReferences[];
|
|
69
|
+
indexes?: TableData.ColumnIndex[];
|
|
70
|
+
excludes?: TableData.ColumnExclude[];
|
|
71
|
+
identity?: TableData.Identity;
|
|
72
|
+
}
|
|
73
|
+
interface RenameType {
|
|
74
|
+
type: 'renameType';
|
|
75
|
+
kind: 'TABLE' | 'TYPE' | 'DOMAIN';
|
|
76
|
+
fromSchema?: string;
|
|
77
|
+
from: string;
|
|
78
|
+
toSchema?: string;
|
|
79
|
+
to: string;
|
|
80
|
+
}
|
|
81
|
+
interface Schema {
|
|
82
|
+
type: 'schema';
|
|
83
|
+
action: 'create' | 'drop';
|
|
84
|
+
name: string;
|
|
85
|
+
}
|
|
86
|
+
interface RenameSchema {
|
|
87
|
+
type: 'renameSchema';
|
|
88
|
+
from: string;
|
|
89
|
+
to: string;
|
|
90
|
+
}
|
|
91
|
+
interface ExtensionArg {
|
|
92
|
+
version?: string;
|
|
93
|
+
cascade?: boolean;
|
|
94
|
+
createIfNotExists?: boolean;
|
|
95
|
+
dropIfExists?: boolean;
|
|
96
|
+
}
|
|
97
|
+
interface Extension extends ExtensionArg {
|
|
98
|
+
type: 'extension';
|
|
99
|
+
action: 'create' | 'drop';
|
|
100
|
+
schema?: string;
|
|
101
|
+
name: string;
|
|
102
|
+
}
|
|
103
|
+
interface Enum {
|
|
104
|
+
type: 'enum';
|
|
105
|
+
action: 'create' | 'drop';
|
|
106
|
+
schema?: string;
|
|
107
|
+
name: string;
|
|
108
|
+
values: [string, ...string[]];
|
|
109
|
+
cascade?: boolean;
|
|
110
|
+
dropIfExists?: boolean;
|
|
111
|
+
}
|
|
112
|
+
interface EnumValues {
|
|
113
|
+
type: 'enumValues';
|
|
114
|
+
action: 'add' | 'drop';
|
|
115
|
+
schema?: string;
|
|
116
|
+
name: string;
|
|
117
|
+
values: string[];
|
|
118
|
+
place?: 'before' | 'after';
|
|
119
|
+
relativeTo?: string;
|
|
120
|
+
ifNotExists?: boolean;
|
|
121
|
+
}
|
|
122
|
+
interface RenameEnumValues {
|
|
123
|
+
type: 'renameEnumValues';
|
|
124
|
+
schema?: string;
|
|
125
|
+
name: string;
|
|
126
|
+
values: RecordString;
|
|
127
|
+
}
|
|
128
|
+
interface ChangeEnumValues {
|
|
129
|
+
type: 'changeEnumValues';
|
|
130
|
+
schema?: string;
|
|
131
|
+
name: string;
|
|
132
|
+
fromValues: string[];
|
|
133
|
+
toValues: string[];
|
|
134
|
+
}
|
|
135
|
+
interface Domain {
|
|
136
|
+
type: 'domain';
|
|
137
|
+
action: 'create' | 'drop';
|
|
138
|
+
schema?: string;
|
|
139
|
+
name: string;
|
|
140
|
+
baseType: ColumnType;
|
|
141
|
+
}
|
|
142
|
+
interface Collation {
|
|
143
|
+
type: 'collation';
|
|
144
|
+
action: 'create' | 'drop';
|
|
145
|
+
schema?: string;
|
|
146
|
+
name: string;
|
|
147
|
+
locale?: string;
|
|
148
|
+
lcCollate?: string;
|
|
149
|
+
lcCType?: string;
|
|
150
|
+
provider?: string;
|
|
151
|
+
deterministic?: boolean;
|
|
152
|
+
version?: string;
|
|
153
|
+
fromExisting?: string;
|
|
154
|
+
createIfNotExists?: boolean;
|
|
155
|
+
dropIfExists?: boolean;
|
|
156
|
+
cascade?: boolean;
|
|
157
|
+
}
|
|
158
|
+
interface EnumOptions {
|
|
159
|
+
createIfNotExists?: boolean;
|
|
160
|
+
dropIfExists?: boolean;
|
|
161
|
+
}
|
|
162
|
+
interface Constraint extends TableData.Constraint {
|
|
163
|
+
type: 'constraint';
|
|
164
|
+
action: 'create' | 'drop';
|
|
165
|
+
tableSchema?: string;
|
|
166
|
+
tableName: string;
|
|
167
|
+
}
|
|
168
|
+
interface RenameTableItem {
|
|
169
|
+
type: 'renameTableItem';
|
|
170
|
+
kind: 'INDEX' | 'CONSTRAINT';
|
|
171
|
+
tableSchema?: string;
|
|
172
|
+
tableName: string;
|
|
173
|
+
from: string;
|
|
174
|
+
to: string;
|
|
175
|
+
}
|
|
176
|
+
interface View {
|
|
177
|
+
type: 'view';
|
|
178
|
+
action: 'create' | 'drop';
|
|
179
|
+
schema?: string;
|
|
180
|
+
name: string;
|
|
181
|
+
shape: ColumnsShape;
|
|
182
|
+
sql: RawSQLBase;
|
|
183
|
+
options: ViewOptions;
|
|
184
|
+
deps: {
|
|
185
|
+
schemaName: string;
|
|
186
|
+
name: string;
|
|
187
|
+
}[];
|
|
188
|
+
}
|
|
189
|
+
interface ViewOptions {
|
|
190
|
+
createOrReplace?: boolean;
|
|
191
|
+
dropIfExists?: boolean;
|
|
192
|
+
dropMode?: DropMode;
|
|
193
|
+
temporary?: boolean;
|
|
194
|
+
recursive?: boolean;
|
|
195
|
+
columns?: string[];
|
|
196
|
+
with?: {
|
|
197
|
+
checkOption?: 'LOCAL' | 'CASCADED';
|
|
198
|
+
securityBarrier?: boolean;
|
|
199
|
+
securityInvoker?: boolean;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
declare function add(item: ColumnType, options?: {
|
|
205
|
+
dropMode?: DropMode;
|
|
206
|
+
}): SpecialChange;
|
|
207
|
+
declare function add(emptyObject: EmptyObject): SpecialChange;
|
|
208
|
+
declare function add(items: Record<string, ColumnType>, options?: {
|
|
209
|
+
dropMode?: DropMode;
|
|
210
|
+
}): Record<string, RakeDbAst.ChangeTableItem.Column>;
|
|
211
|
+
interface Change extends RakeDbAst.ChangeTableItem.Change, ChangeOptions {
|
|
212
|
+
}
|
|
213
|
+
type ChangeOptions = RakeDbAst.ChangeTableItem.ChangeUsing;
|
|
214
|
+
interface SpecialChange {
|
|
215
|
+
type: SpecialChange;
|
|
216
|
+
}
|
|
217
|
+
interface OneWayChange {
|
|
218
|
+
type: 'change';
|
|
219
|
+
name?: string;
|
|
220
|
+
to: RakeDbAst.ColumnChange;
|
|
221
|
+
using?: RakeDbAst.ChangeTableItem.ChangeUsing;
|
|
222
|
+
}
|
|
223
|
+
type TableChangeMethods = typeof tableChangeMethods;
|
|
224
|
+
declare const tableChangeMethods: {
|
|
225
|
+
name(name: string): any;
|
|
226
|
+
add: typeof add;
|
|
227
|
+
drop: typeof add;
|
|
228
|
+
change(from: ColumnType | OneWayChange, to: ColumnType | OneWayChange, using?: ChangeOptions): Change;
|
|
229
|
+
default(value: unknown | RawSQLBase): OneWayChange;
|
|
230
|
+
nullable(): OneWayChange;
|
|
231
|
+
nonNullable(): OneWayChange;
|
|
232
|
+
comment(comment: string | null): OneWayChange;
|
|
233
|
+
/**
|
|
234
|
+
* Rename a column:
|
|
235
|
+
*
|
|
236
|
+
* ```ts
|
|
237
|
+
* import { change } from '../dbScript';
|
|
238
|
+
*
|
|
239
|
+
* change(async (db) => {
|
|
240
|
+
* await db.changeTable('table', (t) => ({
|
|
241
|
+
* oldColumnName: t.rename('newColumnName'),
|
|
242
|
+
* }));
|
|
243
|
+
* });
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* Note that the renaming `ALTER TABLE` is executed before the rest of alterations,
|
|
247
|
+
* so if you're also adding a new constraint on this column inside the same `changeTable`,
|
|
248
|
+
* refer to it with a new name.
|
|
249
|
+
*
|
|
250
|
+
* @param name
|
|
251
|
+
*/
|
|
252
|
+
rename(name: string): RakeDbAst.ChangeTableItem.Rename;
|
|
253
|
+
primaryKey<Columns extends [string, ...string[]], Name extends string>(columns: Columns, name?: Name | undefined): {
|
|
254
|
+
tableDataItem: true;
|
|
255
|
+
columns: Columns;
|
|
256
|
+
name: string extends Name ? never : Name;
|
|
257
|
+
};
|
|
258
|
+
unique<Columns_1 extends [string | TableData.Index.ColumnOrExpressionOptions<string>, ...(string | TableData.Index.ColumnOrExpressionOptions<string>)[]], Name_1 extends string>(columns: Columns_1, ...args: [options?: TableData.Index.UniqueOptionsArg | undefined] | [name?: Name_1 | undefined, options?: TableData.Index.UniqueOptionsArg | undefined]): {
|
|
259
|
+
tableDataItem: true;
|
|
260
|
+
columns: Columns_1 extends (string | TableData.Index.ColumnOptionsForColumn<string>)[] ? { [I in keyof Columns_1]: "column" extends keyof Columns_1[I] ? Columns_1[I][keyof Columns_1[I] & "column"] : Columns_1[I]; } : never;
|
|
261
|
+
name: string extends Name_1 ? never : Name_1;
|
|
262
|
+
};
|
|
263
|
+
index(columns: (string | TableData.Index.ColumnOrExpressionOptions<string>)[], ...args: [options?: TableData.Index.OptionsArg | undefined] | [name?: string | undefined, options?: TableData.Index.OptionsArg | undefined]): pqb.NonUniqDataItem;
|
|
264
|
+
searchIndex(columns: (string | TableData.Index.ColumnOrExpressionOptions<string>)[], ...args: [options?: TableData.Index.TsVectorArg | undefined] | [name?: string | undefined, options?: TableData.Index.TsVectorArg | undefined]): pqb.NonUniqDataItem;
|
|
265
|
+
exclude(columns: TableData.Exclude.ColumnOrExpressionOptions<string>[], ...args: [options?: TableData.Exclude.Options | undefined] | [name?: string | undefined, options?: TableData.Exclude.Options | undefined]): pqb.NonUniqDataItem;
|
|
266
|
+
foreignKey<ForeignTable extends string | (() => orchid_core.ForeignKeyTable), ForeignColumns extends ForeignTable extends () => orchid_core.ForeignKeyTable ? [orchid_core.ColumnNameOfTable<ReturnType<ForeignTable>>, ...orchid_core.ColumnNameOfTable<ReturnType<ForeignTable>>[]] : [string, ...string[]]>(columns: [string, ...string[]], fnOrTable: ForeignTable, foreignColumns: ForeignColumns, options?: TableData.References.Options | undefined): pqb.NonUniqDataItem;
|
|
267
|
+
check(check: RawSQLBase<orchid_core.QueryColumn<unknown, any>, unknown>, name?: string | undefined): pqb.NonUniqDataItem;
|
|
268
|
+
sql: pqb.SqlFn;
|
|
269
|
+
enum(name: string): EnumColumn<pqb.DefaultSchemaConfig, undefined, [string, ...string[]]>;
|
|
270
|
+
};
|
|
271
|
+
type TableChanger<CT> = MigrationColumnTypes<CT> & TableChangeMethods;
|
|
272
|
+
type TableChangeData = Record<string, RakeDbAst.ChangeTableItem.Column | RakeDbAst.ChangeTableItem.Rename | Change | SpecialChange | ColumnTypeBase>;
|
|
273
|
+
|
|
274
|
+
interface MigrationItemHasLoad {
|
|
275
|
+
path?: string;
|
|
276
|
+
/**
|
|
277
|
+
* Function that loads the migration content,
|
|
278
|
+
* can store lazy import of a migration file.
|
|
279
|
+
* Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
|
|
280
|
+
*/
|
|
281
|
+
load(): Promise<unknown>;
|
|
282
|
+
}
|
|
283
|
+
interface MigrationItem extends MigrationItemHasLoad {
|
|
284
|
+
path: string;
|
|
285
|
+
version: string;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
interface CommandFn<SchemaConfig extends ColumnSchemaConfig, CT> {
|
|
289
|
+
(adapters: AdapterBase[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]): void | Promise<void>;
|
|
290
|
+
}
|
|
291
|
+
interface PickBasePath {
|
|
292
|
+
basePath: string;
|
|
293
|
+
}
|
|
294
|
+
interface PickImport {
|
|
295
|
+
import(path: string): Promise<unknown>;
|
|
296
|
+
}
|
|
297
|
+
interface PickMigrationId {
|
|
298
|
+
migrationId: RakeDbMigrationId;
|
|
299
|
+
}
|
|
300
|
+
interface PickMigrations {
|
|
301
|
+
migrations?: ModuleExportsRecord;
|
|
302
|
+
}
|
|
303
|
+
interface PickMigrationsPath {
|
|
304
|
+
migrationsPath: string;
|
|
305
|
+
}
|
|
306
|
+
interface PickOptionalMigrationsPath {
|
|
307
|
+
migrationsPath?: string;
|
|
308
|
+
}
|
|
309
|
+
interface PickRenameMigrations {
|
|
310
|
+
renameMigrations?: RakeDbRenameMigrationsInput;
|
|
311
|
+
}
|
|
312
|
+
interface PickMigrationsTable {
|
|
313
|
+
migrationsTable: string;
|
|
314
|
+
}
|
|
315
|
+
interface PickMigrationCallbacks {
|
|
316
|
+
beforeChange?: ChangeCallback$1;
|
|
317
|
+
afterChange?: ChangeCallback$1;
|
|
318
|
+
beforeMigrate?: MigrationCallback;
|
|
319
|
+
afterMigrate?: MigrationCallback;
|
|
320
|
+
beforeRollback?: MigrationCallback;
|
|
321
|
+
afterRollback?: MigrationCallback;
|
|
322
|
+
}
|
|
323
|
+
interface PickAfterChangeCommit {
|
|
324
|
+
afterChangeCommit?: ChangeCommitCallback;
|
|
325
|
+
}
|
|
326
|
+
interface PickForceDefaultExports {
|
|
327
|
+
forceDefaultExports?: boolean;
|
|
328
|
+
}
|
|
329
|
+
interface RakeDbBaseConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends QueryLogOptions, PickImport, PickMigrationId, PickMigrationsPath, PickMigrations, PickRenameMigrations, PickMigrationsTable, PickMigrationCallbacks, PickForceDefaultExports, PickAfterChangeCommit {
|
|
330
|
+
schemaConfig: SchemaConfig;
|
|
331
|
+
snakeCase: boolean;
|
|
332
|
+
language?: string;
|
|
333
|
+
commands: Record<string, CommandFn<SchemaConfig, CT>>;
|
|
334
|
+
noPrimaryKey?: NoPrimaryKeyOption;
|
|
335
|
+
baseTable?: RakeDbBaseTable<CT>;
|
|
336
|
+
}
|
|
337
|
+
interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>> extends RakeDbBaseConfig<SchemaConfig, CT>, PickBasePath {
|
|
338
|
+
columnTypes: CT;
|
|
339
|
+
dbScript: string;
|
|
340
|
+
recurrentPath: string;
|
|
341
|
+
}
|
|
342
|
+
interface InputRakeDbConfigBase<SchemaConfig extends ColumnSchemaConfig, CT> extends QueryLogOptions, PickOptionalMigrationsPath {
|
|
343
|
+
columnTypes?: CT | ((t: DefaultColumnTypes<DefaultSchemaConfig>) => CT);
|
|
344
|
+
baseTable?: RakeDbBaseTable<CT>;
|
|
345
|
+
schemaConfig?: SchemaConfig;
|
|
346
|
+
basePath?: string;
|
|
347
|
+
dbScript?: string;
|
|
348
|
+
migrationId?: 'serial' | RakeDbMigrationId;
|
|
349
|
+
recurrentPath?: string;
|
|
350
|
+
migrationsTable?: string;
|
|
351
|
+
snakeCase?: boolean;
|
|
352
|
+
language?: string;
|
|
353
|
+
commands?: Record<string, (adapter: AdapterBase[], config: RakeDbConfig<SchemaConfig, CT>, args: string[]) => void | Promise<void>>;
|
|
354
|
+
noPrimaryKey?: NoPrimaryKeyOption;
|
|
355
|
+
forceDefaultExports?: boolean;
|
|
356
|
+
/**
|
|
357
|
+
* Is called once per db before migrating or rolling back a set of migrations.
|
|
358
|
+
*
|
|
359
|
+
* @param arg.db - query builder
|
|
360
|
+
* @param arg.up - whether it's migrating up or down
|
|
361
|
+
* @param arg.redo - whether it's migrating down and then up for `redo` command
|
|
362
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
363
|
+
*/
|
|
364
|
+
beforeChange?: ChangeCallback$1;
|
|
365
|
+
/**
|
|
366
|
+
* Is called once per db after migrating or rolling back a set of migrations.
|
|
367
|
+
* Runs inside the same transaction as migrations,
|
|
368
|
+
* for running after commit use {@link afterChangeCommit}.
|
|
369
|
+
*
|
|
370
|
+
* @param arg.db - query builder
|
|
371
|
+
* @param arg.up - whether it's migrating up or down
|
|
372
|
+
* @param arg.redo - whether it's migrating down and then up for `redo` command
|
|
373
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
374
|
+
*/
|
|
375
|
+
afterChange?: ChangeCallback$1;
|
|
376
|
+
/**
|
|
377
|
+
* Is called once per db after migrating or rolling back a set of migrations.
|
|
378
|
+
* Runs **after** committing migrations transaction.
|
|
379
|
+
*
|
|
380
|
+
* @param arg.options - database connection options
|
|
381
|
+
* @param arg.up - whether it's migrating up or down
|
|
382
|
+
* @param arg.migrations - array of executed (up or down) migrations
|
|
383
|
+
*/
|
|
384
|
+
afterChangeCommit?: ChangeCommitCallback;
|
|
385
|
+
/**
|
|
386
|
+
* Is called once per db before migrating (up) a set of migrations.
|
|
387
|
+
*
|
|
388
|
+
* @param arg.db - query builder
|
|
389
|
+
* @param arg.migrations - applied migrations
|
|
390
|
+
*/
|
|
391
|
+
beforeMigrate?: MigrationCallback;
|
|
392
|
+
/**
|
|
393
|
+
* Is called once per db after migrating (up) a set of migrations.
|
|
394
|
+
*
|
|
395
|
+
* @param arg.db - query builder
|
|
396
|
+
* @param arg.migrations - applied migrations
|
|
397
|
+
*/
|
|
398
|
+
afterMigrate?: MigrationCallback;
|
|
399
|
+
/**
|
|
400
|
+
* Is called once per db before rolling back a set of migrations.
|
|
401
|
+
*
|
|
402
|
+
* @param arg.db - query builder
|
|
403
|
+
* @param arg.migrations - rolled back migrations
|
|
404
|
+
*/
|
|
405
|
+
beforeRollback?: MigrationCallback;
|
|
406
|
+
/**
|
|
407
|
+
* Is called once per db before rolling back a set of migrations.
|
|
408
|
+
*
|
|
409
|
+
* @param arg.db - query builder
|
|
410
|
+
* @param arg.migrations - rolled back migrations
|
|
411
|
+
*/
|
|
412
|
+
afterRollback?: MigrationCallback;
|
|
413
|
+
}
|
|
414
|
+
interface InputRakeDbConfigFileBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
|
|
415
|
+
/**
|
|
416
|
+
* It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
|
|
417
|
+
*/
|
|
418
|
+
import(path: string): Promise<unknown>;
|
|
419
|
+
}
|
|
420
|
+
interface InputRakeDbConfigCodeBased<SchemaConfig extends ColumnSchemaConfig, CT> extends InputRakeDbConfigBase<SchemaConfig, CT> {
|
|
421
|
+
/**
|
|
422
|
+
* To specify array of migrations explicitly, without loading them from files.
|
|
423
|
+
*/
|
|
424
|
+
migrations: ModuleExportsRecord;
|
|
425
|
+
renameMigrations?: RakeDbRenameMigrationsInput;
|
|
426
|
+
/**
|
|
427
|
+
* It may look odd, but it's required for `tsx` and other bundlers to have such `import` config specified explicitly.
|
|
428
|
+
*/
|
|
429
|
+
import?(path: string): Promise<unknown>;
|
|
430
|
+
}
|
|
431
|
+
type InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> = InputRakeDbConfigFileBased<SchemaConfig, CT> | InputRakeDbConfigCodeBased<SchemaConfig, CT>;
|
|
432
|
+
interface ChangeCallback$1 {
|
|
433
|
+
(arg: {
|
|
434
|
+
db: DbResult<unknown>;
|
|
435
|
+
up: boolean;
|
|
436
|
+
redo: boolean;
|
|
437
|
+
migrations: MigrationItem[];
|
|
438
|
+
}): void | Promise<void>;
|
|
439
|
+
}
|
|
440
|
+
interface ChangeCommitCallback {
|
|
441
|
+
(arg: {
|
|
442
|
+
adapter: AdapterBase;
|
|
443
|
+
up: boolean;
|
|
444
|
+
migrations: MigrationItem[];
|
|
445
|
+
}): void | Promise<void>;
|
|
446
|
+
}
|
|
447
|
+
interface MigrationCallback {
|
|
448
|
+
(arg: {
|
|
449
|
+
db: DbResult<unknown>;
|
|
450
|
+
migrations: MigrationItem[];
|
|
451
|
+
}): void | Promise<void>;
|
|
452
|
+
}
|
|
453
|
+
type AnyRakeDbConfig = RakeDbConfig<any, any>;
|
|
454
|
+
interface RakeDbBaseTable<CT> {
|
|
455
|
+
exportAs: string;
|
|
456
|
+
getFilePath(): string;
|
|
457
|
+
nowSQL?: string;
|
|
458
|
+
new (): {
|
|
459
|
+
types: CT;
|
|
460
|
+
snakeCase?: boolean;
|
|
461
|
+
language?: string;
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
interface ModuleExportsRecord {
|
|
465
|
+
[K: string]: () => Promise<unknown>;
|
|
466
|
+
}
|
|
467
|
+
type RakeDbMigrationId = 'timestamp' | {
|
|
468
|
+
serial: number;
|
|
469
|
+
};
|
|
470
|
+
interface RakeDbRenameMigrationsMap {
|
|
471
|
+
[K: string]: number;
|
|
472
|
+
}
|
|
473
|
+
interface RakeDbRenameMigrationsInput {
|
|
474
|
+
to: RakeDbMigrationId;
|
|
475
|
+
map: RakeDbRenameMigrationsMap;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
type DropMode = 'CASCADE' | 'RESTRICT';
|
|
479
|
+
type TableOptions = {
|
|
480
|
+
createIfNotExists?: boolean;
|
|
481
|
+
dropIfExists?: boolean;
|
|
482
|
+
dropMode?: DropMode;
|
|
483
|
+
comment?: string;
|
|
484
|
+
noPrimaryKey?: boolean;
|
|
485
|
+
snakeCase?: boolean;
|
|
486
|
+
language?: string;
|
|
487
|
+
};
|
|
488
|
+
type MigrationColumnTypes<CT> = Omit<CT, 'enum'> & {
|
|
489
|
+
enum: (name: string) => EnumColumn<ColumnSchemaConfig, unknown, readonly string[]>;
|
|
490
|
+
};
|
|
491
|
+
type ColumnsShapeCallback<CT, Shape extends ColumnsShape = ColumnsShape> = (t: MigrationColumnTypes<CT> & {
|
|
492
|
+
raw: typeof raw;
|
|
493
|
+
}) => Shape;
|
|
494
|
+
type ChangeTableOptions = {
|
|
495
|
+
snakeCase?: boolean;
|
|
496
|
+
language?: string;
|
|
497
|
+
comment?: string | [string, string] | null;
|
|
498
|
+
};
|
|
499
|
+
type ChangeTableCallback<CT> = (t: TableChanger<CT>) => TableChangeData;
|
|
500
|
+
type SilentQueries = {
|
|
501
|
+
silentQuery: AdapterBase['query'];
|
|
502
|
+
silentArrays: AdapterBase['arrays'];
|
|
503
|
+
};
|
|
504
|
+
type DbMigration<CT> = DbResult<CT> & Migration<CT> & {
|
|
505
|
+
adapter: SilentQueries;
|
|
506
|
+
};
|
|
507
|
+
interface MigrationAdapter extends AdapterBase {
|
|
508
|
+
schema: string;
|
|
509
|
+
}
|
|
510
|
+
declare class Migration<CT> {
|
|
511
|
+
adapter: MigrationAdapter;
|
|
512
|
+
log?: QueryLogObject;
|
|
513
|
+
up: boolean;
|
|
514
|
+
options: RakeDbConfig<ColumnSchemaConfig>;
|
|
515
|
+
columnTypes: CT;
|
|
516
|
+
/**
|
|
517
|
+
* `createTable` accepts a string for a table name, optional options, and a callback to specify columns.
|
|
518
|
+
*
|
|
519
|
+
* `dropTable` accepts the same arguments, it will drop the table when migrating and create a table when rolling back.
|
|
520
|
+
*
|
|
521
|
+
* To create an empty table, the callback with columns may be omitted.
|
|
522
|
+
*
|
|
523
|
+
* When creating a table within a specific schema, write the table name with schema name: `'schemaName.tableName'`.
|
|
524
|
+
*
|
|
525
|
+
* Returns object `{ table: TableInterface }` that allows to insert records right after creating a table.
|
|
526
|
+
*
|
|
527
|
+
* Options are:
|
|
528
|
+
*
|
|
529
|
+
* ```ts
|
|
530
|
+
* type TableOptions = {
|
|
531
|
+
* // create the table only if it not exists already
|
|
532
|
+
* createIfNotExists?: boolean;
|
|
533
|
+
*
|
|
534
|
+
* // drop the table only if it exists
|
|
535
|
+
* dropIfExists?: boolean;
|
|
536
|
+
*
|
|
537
|
+
* // used when reverting a `createTable`
|
|
538
|
+
* dropMode?: 'CASCADE' | 'RESTRICT';
|
|
539
|
+
*
|
|
540
|
+
* // add a database comment on the table
|
|
541
|
+
* comment?: string;
|
|
542
|
+
*
|
|
543
|
+
* // by default, it will throw an error when the table has no primary key
|
|
544
|
+
* // set `noPrimaryKey` to `true` to bypass it
|
|
545
|
+
* noPrimaryKey?: boolean;
|
|
546
|
+
*
|
|
547
|
+
* // override rakeDb `snakeCase` option for only this table
|
|
548
|
+
* snakeCase?: boolean;
|
|
549
|
+
* };
|
|
550
|
+
* ```
|
|
551
|
+
*
|
|
552
|
+
* Example:
|
|
553
|
+
*
|
|
554
|
+
* ```ts
|
|
555
|
+
* import { change } from '../dbScript';
|
|
556
|
+
*
|
|
557
|
+
* change(async (db, up) => {
|
|
558
|
+
* // call `createTable` with options
|
|
559
|
+
* await db.createTable(
|
|
560
|
+
* 'table',
|
|
561
|
+
* {
|
|
562
|
+
* comment: 'Table comment',
|
|
563
|
+
* dropMode: 'CASCADE',
|
|
564
|
+
* noPrimaryKey: true,
|
|
565
|
+
* },
|
|
566
|
+
* (t) => ({
|
|
567
|
+
* // ...
|
|
568
|
+
* }),
|
|
569
|
+
* );
|
|
570
|
+
*
|
|
571
|
+
* // call without options
|
|
572
|
+
* const { table } = await db.createTable('user', (t) => ({
|
|
573
|
+
* id: t.identity().primaryKey(),
|
|
574
|
+
* email: t.text().unique(),
|
|
575
|
+
* name: t.text(),
|
|
576
|
+
* active: t.boolean().nullable(),
|
|
577
|
+
* ...t.timestamps(),
|
|
578
|
+
* }));
|
|
579
|
+
*
|
|
580
|
+
* // create records only when migrating up
|
|
581
|
+
* if (up) {
|
|
582
|
+
* // table is a db table interface, all query methods are available
|
|
583
|
+
* await table.createMany([...data]);
|
|
584
|
+
* }
|
|
585
|
+
* });
|
|
586
|
+
* ```
|
|
587
|
+
*
|
|
588
|
+
* @param tableName - name of the table to create
|
|
589
|
+
* @param fn - create table callback
|
|
590
|
+
* @param dataFn - callback for creating composite indexes, primary keys, foreign keys
|
|
591
|
+
*/
|
|
592
|
+
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn?: ColumnsShapeCallback<CT, Shape>, dataFn?: TableDataFn<Shape, MaybeArray<TableDataItem>>): Promise<CreateTableResult<Table, Shape>>;
|
|
593
|
+
/**
|
|
594
|
+
* See {@link createTable}
|
|
595
|
+
*
|
|
596
|
+
* @param tableName - name of the table to create
|
|
597
|
+
* @param options - {@link TableOptions}
|
|
598
|
+
* @param fn - create table callback
|
|
599
|
+
* @param dataFn - callback for creating composite indexes, primary keys, foreign keys
|
|
600
|
+
*/
|
|
601
|
+
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn?: ColumnsShapeCallback<CT, Shape>, dataFn?: TableDataFn<Shape, MaybeArray<TableDataItem>>): Promise<CreateTableResult<Table, Shape>>;
|
|
602
|
+
/**
|
|
603
|
+
* Drop the table, create it on rollback. See {@link createTable}.
|
|
604
|
+
*
|
|
605
|
+
* @param tableName - name of the table to drop
|
|
606
|
+
* @param fn - create table callback
|
|
607
|
+
* @param dataFn - callback for creating composite indexes, primary keys, foreign keys
|
|
608
|
+
*/
|
|
609
|
+
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn?: ColumnsShapeCallback<CT, Shape>, dataFn?: TableDataFn<Shape, MaybeArray<TableDataItem>>): Promise<CreateTableResult<Table, Shape>>;
|
|
610
|
+
/**
|
|
611
|
+
* Drop the table, create it on rollback. See {@link createTable}.
|
|
612
|
+
*
|
|
613
|
+
* @param tableName - name of the table to drop
|
|
614
|
+
* @param options - {@link TableOptions}
|
|
615
|
+
* @param fn - create table callback
|
|
616
|
+
* @param dataFn - callback for creating composite indexes, primary keys, foreign keys
|
|
617
|
+
*/
|
|
618
|
+
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn?: ColumnsShapeCallback<CT, Shape>, dataFn?: TableDataFn<Shape, MaybeArray<TableDataItem>>): Promise<CreateTableResult<Table, Shape>>;
|
|
619
|
+
/**
|
|
620
|
+
* `changeTable` accepts a table name, optional options, and a special callback with column changes.
|
|
621
|
+
*
|
|
622
|
+
* When changing a table within a specific schema, write the table name with schema name: `'schemaName.tableName'`.
|
|
623
|
+
*
|
|
624
|
+
* Options are:
|
|
625
|
+
*
|
|
626
|
+
* ```ts
|
|
627
|
+
* type ChangeTableOptions = {
|
|
628
|
+
* comment?:
|
|
629
|
+
* | // add a comment to the table on migrating, remove a comment on rollback
|
|
630
|
+
* string // change comment from first to second on migrating, from second to first on rollback
|
|
631
|
+
* | [string, string] // remove a comment on both migrate and rollback
|
|
632
|
+
* | null;
|
|
633
|
+
*
|
|
634
|
+
* // override rakeDb `snakeCase` option for only this table
|
|
635
|
+
* snakeCase?: boolean;
|
|
636
|
+
* };
|
|
637
|
+
* ```
|
|
638
|
+
*
|
|
639
|
+
* The callback of the `changeTable` is different from `createTable` in the way that it expects columns to be wrapped in change methods such as `add`, `drop`, and `change`.
|
|
640
|
+
*
|
|
641
|
+
* @param tableName - name of the table to change (ALTER)
|
|
642
|
+
* @param fn - change table callback
|
|
643
|
+
*/
|
|
644
|
+
changeTable(tableName: string, fn: ChangeTableCallback<CT>): Promise<void>;
|
|
645
|
+
/**
|
|
646
|
+
* See {@link changeTable}
|
|
647
|
+
*
|
|
648
|
+
* @param tableName - name of the table to change (ALTER)
|
|
649
|
+
* @param options - change table options
|
|
650
|
+
* @param fn - change table callback
|
|
651
|
+
*/
|
|
652
|
+
changeTable(tableName: string, options: ChangeTableOptions, fn?: ChangeTableCallback<CT>): Promise<void>;
|
|
653
|
+
/**
|
|
654
|
+
* Rename a table:
|
|
655
|
+
*
|
|
656
|
+
* ```ts
|
|
657
|
+
* import { change } from '../dbScript';
|
|
658
|
+
*
|
|
659
|
+
* change(async (db) => {
|
|
660
|
+
* await db.renameTable('oldTableName', 'newTableName');
|
|
661
|
+
* });
|
|
662
|
+
* ```
|
|
663
|
+
*
|
|
664
|
+
* Prefix table name with a schema to set a different schema:
|
|
665
|
+
*
|
|
666
|
+
* ```ts
|
|
667
|
+
* import { change } from '../dbScript';
|
|
668
|
+
*
|
|
669
|
+
* change(async (db) => {
|
|
670
|
+
* await db.renameTable('fromSchema.oldTable', 'toSchema.newTable');
|
|
671
|
+
* });
|
|
672
|
+
* ```
|
|
673
|
+
*
|
|
674
|
+
* @param from - rename the table from
|
|
675
|
+
* @param to - rename the table to
|
|
676
|
+
*/
|
|
677
|
+
renameTable(from: string, to: string): Promise<void>;
|
|
678
|
+
/**
|
|
679
|
+
* Set a different schema to the table:
|
|
680
|
+
*
|
|
681
|
+
* ```ts
|
|
682
|
+
* import { change } from '../dbScript';
|
|
683
|
+
*
|
|
684
|
+
* change(async (db) => {
|
|
685
|
+
* await db.changeTableSchema('tableName', 'fromSchema', 'toSchema');
|
|
686
|
+
* });
|
|
687
|
+
* ```
|
|
688
|
+
*
|
|
689
|
+
* @param table - table name
|
|
690
|
+
* @param from - current table schema
|
|
691
|
+
* @param to - desired table schema
|
|
692
|
+
*/
|
|
693
|
+
changeTableSchema(table: string, from: string, to: string): Promise<void>;
|
|
694
|
+
/**
|
|
695
|
+
* Add a column to the table on migrating, and remove it on rollback.
|
|
696
|
+
*
|
|
697
|
+
* `dropColumn` takes the same arguments, removes a column on migrate, and adds it on rollback.
|
|
698
|
+
*
|
|
699
|
+
* ```ts
|
|
700
|
+
* import { change } from '../dbScript';
|
|
701
|
+
*
|
|
702
|
+
* change(async (db) => {
|
|
703
|
+
* await db.addColumn('tableName', 'columnName', (t) =>
|
|
704
|
+
* t.integer().index().nullable(),
|
|
705
|
+
* );
|
|
706
|
+
* });
|
|
707
|
+
* ```
|
|
708
|
+
*
|
|
709
|
+
* @param tableName - name of the table to add the column to
|
|
710
|
+
* @param columnName - name of the column to add
|
|
711
|
+
* @param fn - function returning a type of the column
|
|
712
|
+
*/
|
|
713
|
+
addColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
714
|
+
/**
|
|
715
|
+
* Drop the schema, create it on rollback. See {@link addColumn}.
|
|
716
|
+
*
|
|
717
|
+
* @param tableName - name of the table to add the column to
|
|
718
|
+
* @param columnName - name of the column to add
|
|
719
|
+
* @param fn - function returning a type of the column
|
|
720
|
+
*/
|
|
721
|
+
dropColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
722
|
+
/**
|
|
723
|
+
* Add an index to the table on migrating, and remove it on rollback.
|
|
724
|
+
*
|
|
725
|
+
* `dropIndex` takes the same arguments, removes the index on migrate, and adds it on rollback.
|
|
726
|
+
*
|
|
727
|
+
* The first argument is the table name, other arguments are the same as in [composite index](#composite-index).
|
|
728
|
+
*
|
|
729
|
+
* ```ts
|
|
730
|
+
* import { change } from '../dbScript';
|
|
731
|
+
*
|
|
732
|
+
* change(async (db) => {
|
|
733
|
+
* await db.addIndex(
|
|
734
|
+
* 'tableName',
|
|
735
|
+
* ['column1', { column: 'column2', order: 'DESC' }],
|
|
736
|
+
* {
|
|
737
|
+
* name: 'indexName',
|
|
738
|
+
* },
|
|
739
|
+
* );
|
|
740
|
+
* });
|
|
741
|
+
* ```
|
|
742
|
+
*
|
|
743
|
+
* @param tableName - name of the table to add the index for
|
|
744
|
+
* @param columns - indexed columns
|
|
745
|
+
* @param args - index options, or an index name and then options
|
|
746
|
+
*/
|
|
747
|
+
addIndex(tableName: string, columns: (string | TableData.Index.ColumnOrExpressionOptions<string>)[], ...args: [options?: TableData.Index.OptionsArg] | [name?: string, options?: TableData.Index.OptionsArg]): Promise<void>;
|
|
748
|
+
/**
|
|
749
|
+
* Drop the schema, create it on rollback. See {@link addIndex}.
|
|
750
|
+
*
|
|
751
|
+
* @param tableName - name of the table to add the index for
|
|
752
|
+
* @param columns - indexed columns
|
|
753
|
+
* @param args - index options, or an index name and then options
|
|
754
|
+
*/
|
|
755
|
+
dropIndex(tableName: string, columns: (string | TableData.Index.ColumnOrExpressionOptions<string>)[], ...args: [options?: TableData.Index.OptionsArg] | [name?: string, options?: TableData.Index.OptionsArg]): Promise<void>;
|
|
756
|
+
/**
|
|
757
|
+
* Rename index:
|
|
758
|
+
*
|
|
759
|
+
* ```ts
|
|
760
|
+
* import { change } from '../dbScript';
|
|
761
|
+
*
|
|
762
|
+
* change(async (db) => {
|
|
763
|
+
* // tableName can be prefixed with a schema
|
|
764
|
+
* await db.renameIndex('tableName', 'oldIndexName', 'newIndexName');
|
|
765
|
+
* });
|
|
766
|
+
* ```
|
|
767
|
+
*
|
|
768
|
+
* @param tableName - table which this index belongs to
|
|
769
|
+
* @param from - rename the index from
|
|
770
|
+
* @param to - rename the index to
|
|
771
|
+
*/
|
|
772
|
+
renameIndex(tableName: string, from: string, to: string): Promise<void>;
|
|
773
|
+
/**
|
|
774
|
+
* Add a foreign key to a table on migrating, and remove it on rollback.
|
|
775
|
+
*
|
|
776
|
+
* `dropForeignKey` takes the same arguments, removes the foreign key on migrate, and adds it on rollback.
|
|
777
|
+
*
|
|
778
|
+
* Arguments:
|
|
779
|
+
*
|
|
780
|
+
* - table name
|
|
781
|
+
* - column names in the table
|
|
782
|
+
* - other table name
|
|
783
|
+
* - column names in the other table
|
|
784
|
+
* - options:
|
|
785
|
+
* - `name`: constraint name
|
|
786
|
+
* - `match`: 'FULL', 'PARTIAL', or 'SIMPLE'
|
|
787
|
+
* - `onUpdate` and `onDelete`: 'NO ACTION', 'RESTRICT', 'CASCADE', 'SET NULL', or 'SET DEFAULT'
|
|
788
|
+
*
|
|
789
|
+
* The first argument is the table name, other arguments are the same as in [composite foreign key](#composite-foreign-key).
|
|
790
|
+
*
|
|
791
|
+
* ```ts
|
|
792
|
+
* import { change } from '../dbScript';
|
|
793
|
+
*
|
|
794
|
+
* change(async (db) => {
|
|
795
|
+
* await db.addForeignKey(
|
|
796
|
+
* 'tableName',
|
|
797
|
+
* ['id', 'name'],
|
|
798
|
+
* 'otherTable',
|
|
799
|
+
* ['foreignId', 'foreignName'],
|
|
800
|
+
* {
|
|
801
|
+
* name: 'constraintName',
|
|
802
|
+
* match: 'FULL',
|
|
803
|
+
* onUpdate: 'RESTRICT',
|
|
804
|
+
* onDelete: 'CASCADE',
|
|
805
|
+
* },
|
|
806
|
+
* );
|
|
807
|
+
* });
|
|
808
|
+
* ```
|
|
809
|
+
*
|
|
810
|
+
* @param tableName - table name
|
|
811
|
+
* @param columns - column names in the table
|
|
812
|
+
* @param foreignTable - other table name
|
|
813
|
+
* @param foreignColumns - column names in the other table
|
|
814
|
+
* @param options - foreign key options
|
|
815
|
+
*/
|
|
816
|
+
addForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: TableData.References.Options): Promise<void>;
|
|
817
|
+
/**
|
|
818
|
+
* Drop the schema, create it on rollback. See {@link addForeignKey}.
|
|
819
|
+
*
|
|
820
|
+
* @param tableName - table name
|
|
821
|
+
* @param columns - column names in the table
|
|
822
|
+
* @param foreignTable - other table name
|
|
823
|
+
* @param foreignColumns - column names in the other table
|
|
824
|
+
* @param options - foreign key options
|
|
825
|
+
*/
|
|
826
|
+
dropForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: TableData.References.Options): Promise<void>;
|
|
827
|
+
/**
|
|
828
|
+
* Add a primary key to a table on migrate, and remove it on rollback.
|
|
829
|
+
*
|
|
830
|
+
* `dropPrimaryKey` takes the same arguments, removes the primary key on migrate, and adds it on rollback.
|
|
831
|
+
*
|
|
832
|
+
* First argument is a table name, second argument is an array of columns.
|
|
833
|
+
* The optional third argument may have a name for the primary key constraint.
|
|
834
|
+
*
|
|
835
|
+
* ```ts
|
|
836
|
+
* import { change } from '../dbScript';
|
|
837
|
+
*
|
|
838
|
+
* change(async (db) => {
|
|
839
|
+
* await db.addPrimaryKey('tableName', ['id', 'name'], {
|
|
840
|
+
* name: 'tablePkeyName',
|
|
841
|
+
* });
|
|
842
|
+
* });
|
|
843
|
+
* ```
|
|
844
|
+
*
|
|
845
|
+
* @param tableName - name of the table
|
|
846
|
+
* @param columns - array of the columns
|
|
847
|
+
* @param name - optionally, set a primary key constraint name
|
|
848
|
+
*/
|
|
849
|
+
addPrimaryKey(tableName: string, columns: [string, ...string[]], name?: string): Promise<void>;
|
|
850
|
+
/**
|
|
851
|
+
* Drop the schema, create it on rollback. See {@link addPrimaryKey}.
|
|
852
|
+
*
|
|
853
|
+
* @param tableName - name of the table
|
|
854
|
+
* @param columns - array of the columns
|
|
855
|
+
* @param name - optionally, set a primary key constraint name
|
|
856
|
+
*/
|
|
857
|
+
dropPrimaryKey(tableName: string, columns: [string, ...string[]], name?: string): Promise<void>;
|
|
858
|
+
/**
|
|
859
|
+
* Add or drop a check for multiple columns.
|
|
860
|
+
*
|
|
861
|
+
* ```ts
|
|
862
|
+
* import { change } from '../dbScript';
|
|
863
|
+
*
|
|
864
|
+
* change(async (db) => {
|
|
865
|
+
* await db.addCheck('tableName', t.sql`column > 123`);
|
|
866
|
+
* });
|
|
867
|
+
* ```
|
|
868
|
+
*
|
|
869
|
+
* @param tableName - name of the table to add the check into
|
|
870
|
+
* @param check - raw SQL for the check
|
|
871
|
+
*/
|
|
872
|
+
addCheck(tableName: string, check: RawSQLBase): Promise<void>;
|
|
873
|
+
/**
|
|
874
|
+
* Drop the schema, create it on rollback. See {@link addCheck}.
|
|
875
|
+
*
|
|
876
|
+
* @param tableName - name of the table to add the check into
|
|
877
|
+
* @param check - raw SQL for the check
|
|
878
|
+
*/
|
|
879
|
+
dropCheck(tableName: string, check: RawSQLBase): Promise<void>;
|
|
880
|
+
/**
|
|
881
|
+
* Rename a table constraint such as a primary key or a database check.
|
|
882
|
+
*
|
|
883
|
+
* ```ts
|
|
884
|
+
* import { change } from '../dbScript';
|
|
885
|
+
*
|
|
886
|
+
* change(async (db) => {
|
|
887
|
+
* await db.renameConstraint(
|
|
888
|
+
* 'tableName', // may include schema: 'schema.table'
|
|
889
|
+
* 'oldConstraintName',
|
|
890
|
+
* 'newConstraintName',
|
|
891
|
+
* );
|
|
892
|
+
* });
|
|
893
|
+
* ```
|
|
894
|
+
*
|
|
895
|
+
* @param tableName - name of the table containing the constraint, may include schema name, may include schema name
|
|
896
|
+
* @param from - current name of the constraint
|
|
897
|
+
* @param to - desired name
|
|
898
|
+
*/
|
|
899
|
+
renameConstraint(tableName: string, from: string, to: string): Promise<void>;
|
|
900
|
+
/**
|
|
901
|
+
* Rename a column:
|
|
902
|
+
*
|
|
903
|
+
* ```ts
|
|
904
|
+
* import { change } from '../dbScript';
|
|
905
|
+
*
|
|
906
|
+
* change(async (db) => {
|
|
907
|
+
* await db.renameColumn('tableName', 'oldColumnName', 'newColumnName');
|
|
908
|
+
* });
|
|
909
|
+
* ```
|
|
910
|
+
*
|
|
911
|
+
* @param tableName - name of the table to rename the column in
|
|
912
|
+
* @param from - rename column from
|
|
913
|
+
* @param to - rename column to
|
|
914
|
+
*/
|
|
915
|
+
renameColumn(tableName: string, from: string, to: string): Promise<void>;
|
|
916
|
+
/**
|
|
917
|
+
* `createSchema` creates a database schema, and removes it on rollback.
|
|
918
|
+
*
|
|
919
|
+
* `dropSchema` takes the same arguments, removes schema on migration, and adds it on rollback.
|
|
920
|
+
*
|
|
921
|
+
* ```ts
|
|
922
|
+
* import { change } from '../dbScript';
|
|
923
|
+
*
|
|
924
|
+
* change(async (db) => {
|
|
925
|
+
* await db.createSchema('schemaName');
|
|
926
|
+
* });
|
|
927
|
+
* ```
|
|
928
|
+
*
|
|
929
|
+
* @param schemaName - name of the schema
|
|
930
|
+
*/
|
|
931
|
+
createSchema(schemaName: string): Promise<void>;
|
|
932
|
+
/**
|
|
933
|
+
* Renames a database schema, renames it backwards on roll back.
|
|
934
|
+
*
|
|
935
|
+
* ```ts
|
|
936
|
+
* import { change } from '../dbScript';
|
|
937
|
+
*
|
|
938
|
+
* change(async (db) => {
|
|
939
|
+
* await db.renameSchema('from', 'to');
|
|
940
|
+
* });
|
|
941
|
+
* ```
|
|
942
|
+
*
|
|
943
|
+
* @param from - existing schema to rename
|
|
944
|
+
* @param to - desired schema name
|
|
945
|
+
*/
|
|
946
|
+
renameSchema(from: string, to: string): Promise<void>;
|
|
947
|
+
/**
|
|
948
|
+
* Drop the schema, create it on rollback. See {@link createSchema}.
|
|
949
|
+
*
|
|
950
|
+
* @param schemaName - name of the schema
|
|
951
|
+
*/
|
|
952
|
+
dropSchema(schemaName: string): Promise<void>;
|
|
953
|
+
/**
|
|
954
|
+
* `createExtension` creates a database extension, and removes it on rollback.
|
|
955
|
+
*
|
|
956
|
+
* `dropExtension` takes the same arguments, removes the extension on migrate, and adds it on rollback.
|
|
957
|
+
*
|
|
958
|
+
* ```ts
|
|
959
|
+
* import { change } from '../dbScript';
|
|
960
|
+
*
|
|
961
|
+
* change(async (db) => {
|
|
962
|
+
* await db.createExtension('pg_trgm');
|
|
963
|
+
* });
|
|
964
|
+
* ```
|
|
965
|
+
*
|
|
966
|
+
* @param name - name of the extension
|
|
967
|
+
* @param options - extension options
|
|
968
|
+
*/
|
|
969
|
+
createExtension(name: string, options?: RakeDbAst.ExtensionArg): Promise<void>;
|
|
970
|
+
/**
|
|
971
|
+
* Drop the extension, create it on rollback. See {@link createExtension}.
|
|
972
|
+
*
|
|
973
|
+
* @param name - name of the extension
|
|
974
|
+
* @param options - extension options
|
|
975
|
+
*/
|
|
976
|
+
dropExtension(name: string, options?: RakeDbAst.ExtensionArg): Promise<void>;
|
|
977
|
+
/**
|
|
978
|
+
* `createEnum` creates an enum on migrate, drops it on rollback.
|
|
979
|
+
*
|
|
980
|
+
* `dropEnum` does the opposite.
|
|
981
|
+
*
|
|
982
|
+
* Third argument for options is optional.
|
|
983
|
+
*
|
|
984
|
+
* ```ts
|
|
985
|
+
* import { change } from '../dbScript';
|
|
986
|
+
*
|
|
987
|
+
* change(async (db) => {
|
|
988
|
+
* await db.createEnum('number', ['one', 'two', 'three']);
|
|
989
|
+
*
|
|
990
|
+
* // use `schemaName.enumName` format to specify a schema
|
|
991
|
+
* await db.createEnum('customSchema.mood', ['sad', 'ok', 'happy'], {
|
|
992
|
+
* // following options are used when dropping enum
|
|
993
|
+
* dropIfExists: true,
|
|
994
|
+
* cascade: true,
|
|
995
|
+
* });
|
|
996
|
+
* });
|
|
997
|
+
* ```
|
|
998
|
+
*
|
|
999
|
+
* @param name - name of the enum
|
|
1000
|
+
* @param values - possible enum values
|
|
1001
|
+
* @param options - enum options
|
|
1002
|
+
*/
|
|
1003
|
+
createEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
1004
|
+
/**
|
|
1005
|
+
* Drop the enum, create it on rollback. See {@link createEnum}.
|
|
1006
|
+
*
|
|
1007
|
+
* @param name - name of the enum
|
|
1008
|
+
* @param values - possible enum values
|
|
1009
|
+
* @param options - enum options
|
|
1010
|
+
*/
|
|
1011
|
+
dropEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
1012
|
+
/**
|
|
1013
|
+
* Use these methods to add or drop one or multiple values from an existing enum.
|
|
1014
|
+
*
|
|
1015
|
+
* `addEnumValues` will drop values when rolling back the migration.
|
|
1016
|
+
*
|
|
1017
|
+
* Dropping a value internally acts in multiple steps:
|
|
1018
|
+
*
|
|
1019
|
+
* 1. Select all columns from the database that depends on the enum;
|
|
1020
|
+
* 2. Alter all these columns to have text type;
|
|
1021
|
+
* 3. Drop the enum;
|
|
1022
|
+
* 4. Re-create the enum without the value given;
|
|
1023
|
+
* 5. Alter all columns from the first step to have the enum type;
|
|
1024
|
+
*
|
|
1025
|
+
* In the case when the value is used by some table,
|
|
1026
|
+
* migrating `dropEnumValue` or rolling back `addEnumValue` will throw an error with a descriptive message,
|
|
1027
|
+
* in such case you'd need to manually resolve the issue by deleting rows with the value, or changing such values.
|
|
1028
|
+
*
|
|
1029
|
+
* ```ts
|
|
1030
|
+
* import { change } from '../dbScript';
|
|
1031
|
+
*
|
|
1032
|
+
* change(async (db) => {
|
|
1033
|
+
* await db.addEnumValue('numbers', 'four');
|
|
1034
|
+
*
|
|
1035
|
+
* // you can pass options
|
|
1036
|
+
* await db.addEnumValue('numbers', 'three', {
|
|
1037
|
+
* // where to insert
|
|
1038
|
+
* before: 'four',
|
|
1039
|
+
* // skip if already exists
|
|
1040
|
+
* ifNotExists: true,
|
|
1041
|
+
* });
|
|
1042
|
+
*
|
|
1043
|
+
* // enum name can be prefixed with schema
|
|
1044
|
+
* await db.addEnumValue('public.numbers', 'five', {
|
|
1045
|
+
* after: 'four',
|
|
1046
|
+
* });
|
|
1047
|
+
* });
|
|
1048
|
+
* ```
|
|
1049
|
+
*
|
|
1050
|
+
* @param enumName - target enum name
|
|
1051
|
+
* @param values - array of values to add
|
|
1052
|
+
* @param options - optional object with options
|
|
1053
|
+
* @param options.before - insert before the specified value
|
|
1054
|
+
* @param options.after - insert after the specified value
|
|
1055
|
+
* @param options.ifNotExists - skip adding if already exists
|
|
1056
|
+
*/
|
|
1057
|
+
addEnumValues(enumName: string, values: string[], options?: AddEnumValueOptions): Promise<void>;
|
|
1058
|
+
/**
|
|
1059
|
+
* See {@link addEnumValues}
|
|
1060
|
+
*/
|
|
1061
|
+
dropEnumValues(enumName: string, values: string[], options?: AddEnumValueOptions): Promise<void>;
|
|
1062
|
+
/**
|
|
1063
|
+
* Rename one or multiple enum values using this method:
|
|
1064
|
+
*
|
|
1065
|
+
* ```ts
|
|
1066
|
+
* import { change } from '../dbScript';
|
|
1067
|
+
*
|
|
1068
|
+
* change(async (db) => {
|
|
1069
|
+
* // rename value "from" to "to"
|
|
1070
|
+
* await db.rename('numbers', { from: 'to' });
|
|
1071
|
+
*
|
|
1072
|
+
* // enum name can be prefixed with schema
|
|
1073
|
+
* await db.rename('public.numbers', { from: 'to' });
|
|
1074
|
+
* });
|
|
1075
|
+
* ```
|
|
1076
|
+
*
|
|
1077
|
+
* @param enumName - target enum name, can be prefixed with schema
|
|
1078
|
+
* @param values - object where keys are for old names, values are for new names
|
|
1079
|
+
*/
|
|
1080
|
+
renameEnumValues(enumName: string, values: RecordString): Promise<void>;
|
|
1081
|
+
/**
|
|
1082
|
+
* Drops the enum and re-creates it with a new set of values.
|
|
1083
|
+
* Before dropping, changes all related column types to text, and after creating changes types back to the enum,
|
|
1084
|
+
* in the same way as [dropEnumValues](/guide/migration-writing.html#addenumvalues,-dropenumvalues) works.
|
|
1085
|
+
*
|
|
1086
|
+
* ```ts
|
|
1087
|
+
* import { change } from '../dbScript';
|
|
1088
|
+
*
|
|
1089
|
+
* change(async (db) => {
|
|
1090
|
+
* await db.changeEnumValues(
|
|
1091
|
+
* // can be prefixed with schema: 'public.numbers'
|
|
1092
|
+
* 'numbers',
|
|
1093
|
+
* // change from:
|
|
1094
|
+
* ['one', 'two'],
|
|
1095
|
+
* // change to:
|
|
1096
|
+
* ['three', 'four'],
|
|
1097
|
+
* );
|
|
1098
|
+
* });
|
|
1099
|
+
* ```
|
|
1100
|
+
*
|
|
1101
|
+
* @param enumName - target enum name, can be prefixed with schema
|
|
1102
|
+
* @param fromValues - array of values before the change
|
|
1103
|
+
* @param toValues - array of values to set
|
|
1104
|
+
*/
|
|
1105
|
+
changeEnumValues(enumName: string, fromValues: string[], toValues: string[]): Promise<void>;
|
|
1106
|
+
/**
|
|
1107
|
+
* Rename a type (such as enum):
|
|
1108
|
+
*
|
|
1109
|
+
* ```ts
|
|
1110
|
+
* import { change } from '../dbScript';
|
|
1111
|
+
*
|
|
1112
|
+
* change(async (db) => {
|
|
1113
|
+
* await db.renameType('oldTypeName', 'newTypeName');
|
|
1114
|
+
* });
|
|
1115
|
+
* ```
|
|
1116
|
+
*
|
|
1117
|
+
* Prefix the type name with a schema to set a different schema:
|
|
1118
|
+
*
|
|
1119
|
+
* ```ts
|
|
1120
|
+
* import { change } from '../dbScript';
|
|
1121
|
+
*
|
|
1122
|
+
* change(async (db) => {
|
|
1123
|
+
* await db.renameType('fromSchema.oldType', 'toSchema.newType');
|
|
1124
|
+
* });
|
|
1125
|
+
* ```
|
|
1126
|
+
*
|
|
1127
|
+
* @param from - rename the type from
|
|
1128
|
+
* @param to - rename the type to
|
|
1129
|
+
*/
|
|
1130
|
+
renameType(from: string, to: string): Promise<void>;
|
|
1131
|
+
/**
|
|
1132
|
+
* Set a different schema to the type (such as enum):
|
|
1133
|
+
*
|
|
1134
|
+
* ```ts
|
|
1135
|
+
* import { change } from '../dbScript';
|
|
1136
|
+
*
|
|
1137
|
+
* change(async (db) => {
|
|
1138
|
+
* await db.changeTypeSchema('typeName', 'fromSchema', 'toSchema');
|
|
1139
|
+
* });
|
|
1140
|
+
* ```
|
|
1141
|
+
*
|
|
1142
|
+
* @param name - type name
|
|
1143
|
+
* @param from - current table schema
|
|
1144
|
+
* @param to - desired table schema
|
|
1145
|
+
*/
|
|
1146
|
+
changeTypeSchema(name: string, from: string, to: string): Promise<void>;
|
|
1147
|
+
/**
|
|
1148
|
+
* Domain is a custom database type that is based on other type and can include `NOT NULL` and a `CHECK` (see [postgres tutorial](https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-user-defined-data-types/)).
|
|
1149
|
+
*
|
|
1150
|
+
* Construct a column type in the function as the second argument.
|
|
1151
|
+
*
|
|
1152
|
+
* Specifiers [nullable](/guide/common-column-methods.html#nullable), [default](/guide/common-column-methods.html#default), [check](/guide/migration-column-methods.html#check), [collate](/guide/migration-column-methods.html#collate)
|
|
1153
|
+
* will be saved to the domain type on database level.
|
|
1154
|
+
*
|
|
1155
|
+
* ```ts
|
|
1156
|
+
* import { change } from '../dbScript';
|
|
1157
|
+
*
|
|
1158
|
+
* change(async (db) => {
|
|
1159
|
+
* await db.createDomain('domainName', (t) =>
|
|
1160
|
+
* t.integer().check(t.sql`value = 42`),
|
|
1161
|
+
* );
|
|
1162
|
+
*
|
|
1163
|
+
* // use `schemaName.domainName` format to specify a schema
|
|
1164
|
+
* await db.createDomain('schemaName.domainName', (t) =>
|
|
1165
|
+
* t
|
|
1166
|
+
* .text()
|
|
1167
|
+
* .nullable()
|
|
1168
|
+
* .collate('C')
|
|
1169
|
+
* .default('default text')
|
|
1170
|
+
* .check(t.sql`length(value) > 10`),
|
|
1171
|
+
* );
|
|
1172
|
+
* });
|
|
1173
|
+
* ```
|
|
1174
|
+
*
|
|
1175
|
+
* @param name - name of the domain
|
|
1176
|
+
* @param fn - function returning a column type. Options `nullable`, `collate`, `default`, `check` will be applied to domain
|
|
1177
|
+
*/
|
|
1178
|
+
createDomain(name: string, fn: DbDomainArg<CT>): Promise<void>;
|
|
1179
|
+
/**
|
|
1180
|
+
* Drop the domain, create it on rollback. See {@link dropDomain}.
|
|
1181
|
+
*
|
|
1182
|
+
* @param name - name of the domain
|
|
1183
|
+
* @param fn - function returning a column type. Options `nullable`, `collate`, `default`, `check` will be applied to domain
|
|
1184
|
+
*/
|
|
1185
|
+
dropDomain(name: string, fn: DbDomainArg<CT>): Promise<void>;
|
|
1186
|
+
/**
|
|
1187
|
+
* To rename a domain:
|
|
1188
|
+
*
|
|
1189
|
+
* ```ts
|
|
1190
|
+
* import { change } from '../dbScript';
|
|
1191
|
+
*
|
|
1192
|
+
* change(async (db) => {
|
|
1193
|
+
* await db.renameDomain('oldName', 'newName');
|
|
1194
|
+
*
|
|
1195
|
+
* // to move domain to a different schema
|
|
1196
|
+
* await db.renameDomain('oldSchema.domain', 'newSchema.domain');
|
|
1197
|
+
* });
|
|
1198
|
+
* ```
|
|
1199
|
+
*
|
|
1200
|
+
* @param from - old domain name (can include schema)
|
|
1201
|
+
* @param to - new domain name (can include schema)
|
|
1202
|
+
*/
|
|
1203
|
+
renameDomain(from: string, to: string): Promise<void>;
|
|
1204
|
+
/**
|
|
1205
|
+
* Create and drop a database collation, (see [Postgres docs](https://www.postgresql.org/docs/current/sql-createcollation.html)).
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* import { change } from '../dbScript';
|
|
1209
|
+
*
|
|
1210
|
+
* change(async (db) => {
|
|
1211
|
+
* await db.createCollation('myCollation', {
|
|
1212
|
+
* // This is a shortcut for setting lcCollate and lcCType at once.
|
|
1213
|
+
* locale: 'en-u-kn-true',
|
|
1214
|
+
*
|
|
1215
|
+
* // set `lcType` and `lcCType` only if the `locale` is not set.
|
|
1216
|
+
* // lcType: 'C',
|
|
1217
|
+
* // lcCType: 'C',
|
|
1218
|
+
*
|
|
1219
|
+
* // provider can be 'icu' or 'libc'. 'libc' is a default.
|
|
1220
|
+
* provider: 'icu',
|
|
1221
|
+
*
|
|
1222
|
+
* // true by default, false is only supported with 'icu' provider.
|
|
1223
|
+
* deterministic: true,
|
|
1224
|
+
*
|
|
1225
|
+
* // Is intended to by used by `pg_upgrade`. Normally, it should be omitted.
|
|
1226
|
+
* version: '1.2.3',
|
|
1227
|
+
*
|
|
1228
|
+
* // For `CREATE IF NOT EXISTS` when creating.
|
|
1229
|
+
* createIfNotExists: true,
|
|
1230
|
+
*
|
|
1231
|
+
* // For `DROP IF EXISTS` when dropping.
|
|
1232
|
+
* dropIfExists: true,
|
|
1233
|
+
*
|
|
1234
|
+
* // For `DROP ... CASCADE` when dropping.
|
|
1235
|
+
* cascase: true,
|
|
1236
|
+
* });
|
|
1237
|
+
* });
|
|
1238
|
+
* ```
|
|
1239
|
+
*
|
|
1240
|
+
* Instead of specifying the collation options, you can specify a collation to copy options from.
|
|
1241
|
+
*
|
|
1242
|
+
* ```ts
|
|
1243
|
+
* import { change } from '../dbScript';
|
|
1244
|
+
*
|
|
1245
|
+
* change(async (db) => {
|
|
1246
|
+
* await db.createCollation('myCollation', {
|
|
1247
|
+
* fromExisting: 'otherCollation',
|
|
1248
|
+
* });
|
|
1249
|
+
* });
|
|
1250
|
+
* ```
|
|
1251
|
+
*
|
|
1252
|
+
* To create a collation withing a specific database schema, prepend it to the collation name:
|
|
1253
|
+
*
|
|
1254
|
+
* ```ts
|
|
1255
|
+
* import { change } from '../dbScript';
|
|
1256
|
+
*
|
|
1257
|
+
* change(async (db) => {
|
|
1258
|
+
* await db.createCollation('schemaName.myCollation', {
|
|
1259
|
+
* // `fromExisting` also can accept a collation name with a schema.
|
|
1260
|
+
* fromExisting: 'schemaName.otherCollation',
|
|
1261
|
+
* });
|
|
1262
|
+
* });
|
|
1263
|
+
* ```
|
|
1264
|
+
*
|
|
1265
|
+
* @param name - name of the collation, can contain a name of schema separated with a dot.
|
|
1266
|
+
* @param options - options to create and drop the collation.
|
|
1267
|
+
*/
|
|
1268
|
+
createCollation(name: string, options: Omit<RakeDbAst.Collation, 'type' | 'action' | 'schema' | 'name'>): Promise<void>;
|
|
1269
|
+
/**
|
|
1270
|
+
* Drop the collation, create it on rollback. See {@link createCollation}.
|
|
1271
|
+
*
|
|
1272
|
+
* @param name - name of the collation, can contain a name of schema separated with a dot.
|
|
1273
|
+
* @param options - options to create and drop the collation.
|
|
1274
|
+
*/
|
|
1275
|
+
dropCollation(name: string, options: Omit<RakeDbAst.Collation, 'type' | 'action' | 'schema' | 'name'>): Promise<void>;
|
|
1276
|
+
/**
|
|
1277
|
+
* Create and drop database views.
|
|
1278
|
+
*
|
|
1279
|
+
* Provide SQL as a string or via `t.sql` that can accept variables.
|
|
1280
|
+
*
|
|
1281
|
+
* ```ts
|
|
1282
|
+
* import { change } from '../dbScript';
|
|
1283
|
+
*
|
|
1284
|
+
* change(async (db) => {
|
|
1285
|
+
* await db.createView(
|
|
1286
|
+
* 'simpleView',
|
|
1287
|
+
* `
|
|
1288
|
+
* SELECT a.one, b.two
|
|
1289
|
+
* FROM a
|
|
1290
|
+
* JOIN b ON b."aId" = a.id
|
|
1291
|
+
* `,
|
|
1292
|
+
* );
|
|
1293
|
+
*
|
|
1294
|
+
* // view can accept t.sql with variables in such way:
|
|
1295
|
+
* const value = 'some value';
|
|
1296
|
+
* await db.createView(
|
|
1297
|
+
* 'viewWithVariables',
|
|
1298
|
+
* t.sql`
|
|
1299
|
+
* SELECT * FROM a WHERE key = ${value}
|
|
1300
|
+
* `,
|
|
1301
|
+
* );
|
|
1302
|
+
*
|
|
1303
|
+
* // view with options
|
|
1304
|
+
* await db.createView(
|
|
1305
|
+
* 'schemaName.recursiveView',
|
|
1306
|
+
* {
|
|
1307
|
+
* // createOrReplace has effect when creating the view
|
|
1308
|
+
* createOrReplace: true,
|
|
1309
|
+
*
|
|
1310
|
+
* // dropIfExists and dropMode have effect when dropping the view
|
|
1311
|
+
* dropIfExists: true,
|
|
1312
|
+
* dropMode: 'CASCADE',
|
|
1313
|
+
*
|
|
1314
|
+
* // for details, check Postgres docs for CREATE VIEW,
|
|
1315
|
+
* // these options are matching CREATE VIEW options
|
|
1316
|
+
* temporary: true,
|
|
1317
|
+
* recursive: true,
|
|
1318
|
+
* columns: ['n'],
|
|
1319
|
+
* with: {
|
|
1320
|
+
* checkOption: 'LOCAL', // or 'CASCADED'
|
|
1321
|
+
* securityBarrier: true,
|
|
1322
|
+
* securityInvoker: true,
|
|
1323
|
+
* },
|
|
1324
|
+
* },
|
|
1325
|
+
* `
|
|
1326
|
+
* VALUES (1)
|
|
1327
|
+
* UNION ALL
|
|
1328
|
+
* SELECT n + 1 FROM "schemaName"."recursiveView" WHERE n < 100;
|
|
1329
|
+
* `,
|
|
1330
|
+
* );
|
|
1331
|
+
* });
|
|
1332
|
+
* ```
|
|
1333
|
+
*
|
|
1334
|
+
* @param name - name of the view
|
|
1335
|
+
* @param options - view options
|
|
1336
|
+
* @param sql - SQL to create the view with
|
|
1337
|
+
*/
|
|
1338
|
+
createView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawSQLBase): Promise<void>;
|
|
1339
|
+
/**
|
|
1340
|
+
* See {@link createView}
|
|
1341
|
+
*
|
|
1342
|
+
* @param name - name of the view
|
|
1343
|
+
* @param sql - SQL to create the view with
|
|
1344
|
+
*/
|
|
1345
|
+
createView(name: string, sql: string | RawSQLBase): Promise<void>;
|
|
1346
|
+
/**
|
|
1347
|
+
* Drop the view, create it on rollback. See {@link createView}.
|
|
1348
|
+
*
|
|
1349
|
+
* @param name - name of the view
|
|
1350
|
+
* @param options - view options
|
|
1351
|
+
* @param sql - SQL to create the view with
|
|
1352
|
+
*/
|
|
1353
|
+
dropView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawSQLBase): Promise<void>;
|
|
1354
|
+
/**
|
|
1355
|
+
* Drop the view, create it on rollback. See {@link createView}.
|
|
1356
|
+
*
|
|
1357
|
+
* @param name - name of the view
|
|
1358
|
+
* @param sql - SQL to create the view with
|
|
1359
|
+
*/
|
|
1360
|
+
dropView(name: string, sql: string | RawSQLBase): Promise<void>;
|
|
1361
|
+
/**
|
|
1362
|
+
* Returns boolean to know if table exists:
|
|
1363
|
+
*
|
|
1364
|
+
* ```ts
|
|
1365
|
+
* import { change } from '../dbScript';
|
|
1366
|
+
*
|
|
1367
|
+
* change(async (db) => {
|
|
1368
|
+
* if (await db.tableExists('tableName')) {
|
|
1369
|
+
* // ...do something
|
|
1370
|
+
* }
|
|
1371
|
+
* });
|
|
1372
|
+
* ```
|
|
1373
|
+
*
|
|
1374
|
+
* @param tableName - name of the table
|
|
1375
|
+
*/
|
|
1376
|
+
tableExists(tableName: string): Promise<boolean>;
|
|
1377
|
+
/**
|
|
1378
|
+
* Returns boolean to know if a column exists:
|
|
1379
|
+
*
|
|
1380
|
+
* Note that when `snakeCase` option is set to true, this method won't translate column to snake case, unlike other parts.
|
|
1381
|
+
*
|
|
1382
|
+
* ```ts
|
|
1383
|
+
* import { change } from '../dbScript';
|
|
1384
|
+
*
|
|
1385
|
+
* change(async (db) => {
|
|
1386
|
+
* if (await db.columnExists('tableName', 'columnName')) {
|
|
1387
|
+
* // ...do something
|
|
1388
|
+
* }
|
|
1389
|
+
* });
|
|
1390
|
+
* ```
|
|
1391
|
+
*
|
|
1392
|
+
* @param tableName - name of the table to check for the column in
|
|
1393
|
+
* @param columnName - name of the column
|
|
1394
|
+
*/
|
|
1395
|
+
columnExists(tableName: string, columnName: string): Promise<boolean>;
|
|
1396
|
+
/**
|
|
1397
|
+
* Returns boolean to know if constraint exists:
|
|
1398
|
+
*
|
|
1399
|
+
* ```ts
|
|
1400
|
+
* import { change } from '../dbScript';
|
|
1401
|
+
*
|
|
1402
|
+
* change(async (db) => {
|
|
1403
|
+
* if (await db.constraintExists('constraintName')) {
|
|
1404
|
+
* // ...do something
|
|
1405
|
+
* }
|
|
1406
|
+
* });
|
|
1407
|
+
* ```
|
|
1408
|
+
*
|
|
1409
|
+
* @param constraintName - name of the constraint
|
|
1410
|
+
*/
|
|
1411
|
+
constraintExists(constraintName: string): Promise<boolean>;
|
|
1412
|
+
}
|
|
1413
|
+
interface AddEnumValueOptions {
|
|
1414
|
+
ifNotExists?: boolean;
|
|
1415
|
+
before?: string;
|
|
1416
|
+
after?: string;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
interface MigrationChange {
|
|
1420
|
+
fn: ChangeCallback<unknown>;
|
|
1421
|
+
config: RakeDbConfig<ColumnSchemaConfig, unknown>;
|
|
1422
|
+
}
|
|
1423
|
+
type ChangeCallback<CT> = (db: DbMigration<CT>, up: boolean) => Promise<void>;
|
|
1424
|
+
|
|
1425
|
+
/**
|
|
1426
|
+
* Type of {@link rakeDbWithAdapters} function
|
|
1427
|
+
*/
|
|
1428
|
+
interface RakeDbFn<Options> {
|
|
1429
|
+
<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: Options, partialConfig: InputRakeDbConfig<SchemaConfig, CT>, args?: string[]): RakeDbChangeFnWithPromise<CT>;
|
|
1430
|
+
/**
|
|
1431
|
+
* Unlike the original `rakeDb` that executes immediately,
|
|
1432
|
+
* `rakeDb.lazy` returns the `run` function to be later called programmatically.
|
|
1433
|
+
*
|
|
1434
|
+
* @param options - array of connection adapters for migrating multiple dbs
|
|
1435
|
+
* @param config - {@link RakeDbConfig}
|
|
1436
|
+
* @returns `change` is to be used in migrations, `run` takes an array cli args to execute a command
|
|
1437
|
+
*/
|
|
1438
|
+
lazy<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<DefaultSchemaConfig>>(options: Options, config: InputRakeDbConfig<SchemaConfig, CT>): {
|
|
1439
|
+
change: RakeDbChangeFn<CT>;
|
|
1440
|
+
run(args: string[], config?: Partial<RakeDbConfig<SchemaConfig, CT>>): Promise<RakeDbResult>;
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
interface RakeDbResult {
|
|
1444
|
+
adapters: AdapterBase[];
|
|
1445
|
+
config: AnyRakeDbConfig;
|
|
1446
|
+
args: string[];
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Function to use in migrations to wrap database changes
|
|
1450
|
+
* Saves the given callback to an internal queue,
|
|
1451
|
+
* and also returns the callback in case you want to export it from migration.
|
|
1452
|
+
*/
|
|
1453
|
+
interface RakeDbChangeFn<CT> {
|
|
1454
|
+
(fn: ChangeCallback<CT>): MigrationChange;
|
|
1455
|
+
}
|
|
1456
|
+
interface RakeDbChangeFnWithPromise<CT> extends RakeDbChangeFn<CT> {
|
|
1457
|
+
promise: Promise<RakeDbResult>;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
interface MigrateFnConfig extends MigrateOrRollbackConfig, PickAfterChangeCommit, PickBasePath, PickImport, PickMigrationsPath {
|
|
1461
|
+
}
|
|
1462
|
+
interface MigrateOrRollbackConfig extends PickMigrationCallbacks, PickMigrationId, QueryLogOptions, PickForceDefaultExports, PickMigrationsTable {
|
|
1463
|
+
columnTypes: unknown;
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
declare const rakeDb: RakeDbFn<MaybeArray<NodePostgresAdapterOptions>>;
|
|
1467
|
+
declare const makeConnectAndMigrate: (config?: Partial<MigrateFnConfig>) => ((options: MaybeArray<NodePostgresAdapterOptions>, params?: {
|
|
1468
|
+
count?: number;
|
|
1469
|
+
force?: boolean;
|
|
1470
|
+
}) => Promise<void>);
|
|
1471
|
+
|
|
1472
|
+
export { makeConnectAndMigrate, rakeDb };
|