rake-db 2.3.25 → 2.3.26
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/CHANGELOG.md +8 -0
- package/dist/index.d.ts +27 -21
- package/dist/index.js +30 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/ast.ts +18 -2
- package/src/migration/changeTable.test.ts +6 -0
- package/src/migration/createTable.test.ts +2 -0
- package/src/migration/migration.test.ts +48 -2
- package/src/migration/migration.ts +70 -16
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import { EmptyObject, RawExpression, ColumnType, ColumnTypes, raw, ColumnsShape, DbResult, DefaultColumnTypes, TransactionAdapter, AdapterOptions, TextColumn, MaybeArray, IndexColumnOptions, IndexOptions, ForeignKeyOptions, QueryLogObject, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, QueryLogOptions } from 'pqb';
|
|
2
|
+
import { EmptyObject, RawExpression, ColumnType, ColumnTypes, EnumColumn, raw, ColumnsShape, DbResult, DefaultColumnTypes, TransactionAdapter, AdapterOptions, TextColumn, MaybeArray, IndexColumnOptions, IndexOptions, ForeignKeyOptions, QueryLogObject, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, QueryLogOptions } from 'pqb';
|
|
3
3
|
|
|
4
4
|
declare function add(item: ColumnType, options?: {
|
|
5
5
|
dropMode?: DropMode;
|
|
@@ -35,9 +35,10 @@ declare type TableOptions = {
|
|
|
35
35
|
noPrimaryKey?: boolean;
|
|
36
36
|
};
|
|
37
37
|
declare type TextColumnCreator = () => TextColumn;
|
|
38
|
-
declare type MigrationColumnTypes = Omit<ColumnTypes, 'text' | 'string'> & {
|
|
38
|
+
declare type MigrationColumnTypes = Omit<ColumnTypes, 'text' | 'string' | 'enum'> & {
|
|
39
39
|
text: TextColumnCreator;
|
|
40
40
|
string: TextColumnCreator;
|
|
41
|
+
enum: (name: string) => EnumColumn;
|
|
41
42
|
};
|
|
42
43
|
declare type ColumnsShapeCallback = (t: MigrationColumnTypes & {
|
|
43
44
|
raw: typeof raw;
|
|
@@ -55,11 +56,6 @@ declare type JoinTableOptions = {
|
|
|
55
56
|
comment?: string;
|
|
56
57
|
dropMode?: DropMode;
|
|
57
58
|
};
|
|
58
|
-
declare type ExtensionOptions = {
|
|
59
|
-
schema?: string;
|
|
60
|
-
version?: string;
|
|
61
|
-
cascade?: boolean;
|
|
62
|
-
};
|
|
63
59
|
declare type Migration = DbResult<DefaultColumnTypes> & MigrationBase;
|
|
64
60
|
declare const createMigrationInterface: (tx: TransactionAdapter, up: boolean, options: RakeDbConfig, adapterOptions: AdapterOptions, appCodeUpdaterCache: object) => pqb.Db<string, Record<string, never>, pqb.RelationsBase, {
|
|
65
61
|
smallint: () => pqb.SmallIntColumn;
|
|
@@ -85,7 +81,7 @@ declare const createMigrationInterface: (tx: TransactionAdapter, up: boolean, op
|
|
|
85
81
|
timeWithTimeZone: <Precision_5 extends number | undefined = undefined>(precision?: Precision_5 | undefined) => pqb.TimeWithTimeZoneColumn<Precision_5>;
|
|
86
82
|
interval: <Fields extends string | undefined = undefined, Precision_6 extends number | undefined = undefined>(fields?: Fields | undefined, precision?: Precision_6 | undefined) => pqb.IntervalColumn<Fields, Precision_6>;
|
|
87
83
|
boolean: () => pqb.BooleanColumn;
|
|
88
|
-
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) =>
|
|
84
|
+
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) => EnumColumn<U, T>;
|
|
89
85
|
point: () => pqb.PointColumn;
|
|
90
86
|
line: () => pqb.LineColumn;
|
|
91
87
|
lseg: () => pqb.LsegColumn;
|
|
@@ -184,7 +180,7 @@ declare const createMigrationInterface: (tx: TransactionAdapter, up: boolean, op
|
|
|
184
180
|
timeWithTimeZone: <Precision_5 extends number | undefined = undefined>(precision?: Precision_5 | undefined) => pqb.TimeWithTimeZoneColumn<Precision_5>;
|
|
185
181
|
interval: <Fields extends string | undefined = undefined, Precision_6 extends number | undefined = undefined>(fields?: Fields | undefined, precision?: Precision_6 | undefined) => pqb.IntervalColumn<Fields, Precision_6>;
|
|
186
182
|
boolean: () => pqb.BooleanColumn;
|
|
187
|
-
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) =>
|
|
183
|
+
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) => EnumColumn<U, T>;
|
|
188
184
|
point: () => pqb.PointColumn;
|
|
189
185
|
line: () => pqb.LineColumn;
|
|
190
186
|
lseg: () => pqb.LsegColumn;
|
|
@@ -282,7 +278,7 @@ declare const createMigrationInterface: (tx: TransactionAdapter, up: boolean, op
|
|
|
282
278
|
timeWithTimeZone: <Precision_5 extends number | undefined = undefined>(precision?: Precision_5 | undefined) => pqb.TimeWithTimeZoneColumn<Precision_5>;
|
|
283
279
|
interval: <Fields extends string | undefined = undefined, Precision_6 extends number | undefined = undefined>(fields?: Fields | undefined, precision?: Precision_6 | undefined) => pqb.IntervalColumn<Fields, Precision_6>;
|
|
284
280
|
boolean: () => pqb.BooleanColumn;
|
|
285
|
-
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) =>
|
|
281
|
+
enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) => EnumColumn<U, T>;
|
|
286
282
|
point: () => pqb.PointColumn;
|
|
287
283
|
line: () => pqb.LineColumn;
|
|
288
284
|
lseg: () => pqb.LsegColumn;
|
|
@@ -396,20 +392,17 @@ declare class MigrationBase {
|
|
|
396
392
|
renameColumn(tableName: string, from: string, to: string): Promise<void>;
|
|
397
393
|
createSchema(schemaName: string): Promise<void>;
|
|
398
394
|
dropSchema(schemaName: string): Promise<void>;
|
|
399
|
-
createExtension(name: string, options?:
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
ifExists?: boolean;
|
|
404
|
-
cascade?: boolean;
|
|
405
|
-
}): Promise<void>;
|
|
395
|
+
createExtension(name: string, options?: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name'>): Promise<void>;
|
|
396
|
+
dropExtension(name: string, options?: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name' | 'values'>): Promise<void>;
|
|
397
|
+
createEnum(name: string, values: string[], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values'>): Promise<void>;
|
|
398
|
+
dropEnum(name: string, values: string[], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values'>): Promise<void>;
|
|
406
399
|
tableExists(tableName: string): Promise<boolean>;
|
|
407
400
|
columnExists(tableName: string, columnName: string): Promise<boolean>;
|
|
408
401
|
constraintExists(constraintName: string): Promise<boolean>;
|
|
409
402
|
}
|
|
410
403
|
declare const runCodeUpdater: (migration: MigrationBase, ast: RakeDbAst) => Promise<void> | undefined;
|
|
411
404
|
|
|
412
|
-
declare type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.ForeignKey;
|
|
405
|
+
declare type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.ForeignKey;
|
|
413
406
|
declare namespace RakeDbAst {
|
|
414
407
|
type Table = {
|
|
415
408
|
type: 'table';
|
|
@@ -482,8 +475,21 @@ declare namespace RakeDbAst {
|
|
|
482
475
|
schema?: string;
|
|
483
476
|
version?: string;
|
|
484
477
|
cascade?: boolean;
|
|
485
|
-
|
|
486
|
-
|
|
478
|
+
createIfNotExists?: boolean;
|
|
479
|
+
dropIfExists?: boolean;
|
|
480
|
+
};
|
|
481
|
+
type Enum = {
|
|
482
|
+
type: 'enum';
|
|
483
|
+
action: 'create' | 'drop';
|
|
484
|
+
schema?: string;
|
|
485
|
+
name: string;
|
|
486
|
+
values: string[];
|
|
487
|
+
cascade?: boolean;
|
|
488
|
+
dropIfExists?: boolean;
|
|
489
|
+
};
|
|
490
|
+
type EnumOptions = {
|
|
491
|
+
createIfNotExists?: boolean;
|
|
492
|
+
dropIfExists?: boolean;
|
|
487
493
|
};
|
|
488
494
|
type ForeignKey = {
|
|
489
495
|
type: 'foreignKey';
|
|
@@ -530,4 +536,4 @@ declare const change: (fn: ChangeCallback) => void;
|
|
|
530
536
|
|
|
531
537
|
declare const rakeDb: (options: MaybeArray<AdapterOptions>, partialConfig?: Partial<RakeDbConfig>, args?: string[]) => Promise<void>;
|
|
532
538
|
|
|
533
|
-
export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode,
|
|
539
|
+
export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, JoinTableOptions, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, createDb, createMigrationInterface, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -1077,6 +1077,12 @@ class MigrationBase {
|
|
|
1077
1077
|
dropExtension(name, options = {}) {
|
|
1078
1078
|
return createExtension$1(this, !this.up, name, options);
|
|
1079
1079
|
}
|
|
1080
|
+
createEnum(name, values, options) {
|
|
1081
|
+
return createEnum(this, this.up, name, values, options);
|
|
1082
|
+
}
|
|
1083
|
+
dropEnum(name, values, options) {
|
|
1084
|
+
return createEnum(this, !this.up, name, values, options);
|
|
1085
|
+
}
|
|
1080
1086
|
async tableExists(tableName) {
|
|
1081
1087
|
return queryExists(this, {
|
|
1082
1088
|
text: `SELECT 1 FROM "information_schema"."tables" WHERE "table_name" = $1`,
|
|
@@ -1145,13 +1151,35 @@ const createExtension$1 = async (migration, up, name, options) => {
|
|
|
1145
1151
|
}, options);
|
|
1146
1152
|
let query;
|
|
1147
1153
|
if (ast.action === "drop") {
|
|
1148
|
-
query = `DROP EXTENSION${ast.
|
|
1154
|
+
query = `DROP EXTENSION${ast.dropIfExists ? " IF EXISTS" : ""} "${ast.name}"${ast.cascade ? " CASCADE" : ""}`;
|
|
1149
1155
|
} else {
|
|
1150
|
-
query = `CREATE EXTENSION${ast.
|
|
1156
|
+
query = `CREATE EXTENSION${ast.createIfNotExists ? " IF NOT EXISTS" : ""} "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ""}${ast.version ? ` VERSION '${ast.version}'` : ""}${ast.cascade ? " CASCADE" : ""}`;
|
|
1151
1157
|
}
|
|
1152
1158
|
await migration.adapter.query(query);
|
|
1153
1159
|
await runCodeUpdater(migration, ast);
|
|
1154
1160
|
};
|
|
1161
|
+
const createEnum = async (migration, up, name, values, options = {}) => {
|
|
1162
|
+
const [schema, enumName] = getSchemaAndTableFromName(name);
|
|
1163
|
+
const ast = __spreadValues$2({
|
|
1164
|
+
type: "enum",
|
|
1165
|
+
action: up ? "create" : "drop",
|
|
1166
|
+
schema,
|
|
1167
|
+
name: enumName,
|
|
1168
|
+
values
|
|
1169
|
+
}, options);
|
|
1170
|
+
let text;
|
|
1171
|
+
const quotedName = quoteWithSchema(ast);
|
|
1172
|
+
if (ast.action === "create") {
|
|
1173
|
+
text = `CREATE TYPE ${quotedName} AS ENUM (${values.map((_, i) => `$${i + 1}`).join(", ")})`;
|
|
1174
|
+
} else {
|
|
1175
|
+
text = `DROP TYPE${ast.dropIfExists ? " IF EXISTS" : ""} ${quotedName}${ast.cascade ? " CASCADE" : ""}`;
|
|
1176
|
+
}
|
|
1177
|
+
await migration.adapter.query({
|
|
1178
|
+
text,
|
|
1179
|
+
values
|
|
1180
|
+
});
|
|
1181
|
+
await runCodeUpdater(migration, ast);
|
|
1182
|
+
};
|
|
1155
1183
|
const queryExists = (db, sql) => {
|
|
1156
1184
|
return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
1157
1185
|
};
|