rake-db 2.12.7 → 2.14.0
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/index.d.ts +56 -52
- package/dist/index.js +27 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +27 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import * as pqb from 'pqb';
|
|
2
|
-
import { ColumnsShape, Db as Db$1, ColumnType, EnumColumn, raw, Adapter, DbResult, TransactionAdapter, QueryLogObject, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn,
|
|
2
|
+
import { ColumnsShape, Db as Db$1, ColumnType, EnumColumn, raw, Adapter, DbResult, TransactionAdapter, QueryLogObject, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, TableData, NoPrimaryKeyOption, SingleColumnIndexOptions, DefaultColumnTypes, QueryLogOptions, AdapterOptions } from 'pqb';
|
|
3
3
|
import { EmptyObject, RawSQLBase, ColumnSchemaConfig, MaybeArray, ColumnTypeBase } from 'orchid-core';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
interface CreateTableResult<Table extends string, Shape extends ColumnsShape> {
|
|
6
6
|
table: Db$1<Table, Shape>;
|
|
7
|
-
}
|
|
7
|
+
}
|
|
8
8
|
|
|
9
9
|
declare function add(item: ColumnType, options?: {
|
|
10
10
|
dropMode?: DropMode;
|
|
@@ -786,7 +786,7 @@ declare class Migration<CT extends RakeDbColumnTypes> {
|
|
|
786
786
|
|
|
787
787
|
type RakeDbAst = RakeDbAst.Table | RakeDbAst.ChangeTable | RakeDbAst.RenameTable | RakeDbAst.Schema | RakeDbAst.Extension | RakeDbAst.Enum | RakeDbAst.Domain | RakeDbAst.Collation | RakeDbAst.Constraint | RakeDbAst.View;
|
|
788
788
|
declare namespace RakeDbAst {
|
|
789
|
-
|
|
789
|
+
interface Table extends TableData {
|
|
790
790
|
type: 'table';
|
|
791
791
|
action: 'create' | 'drop';
|
|
792
792
|
schema?: string;
|
|
@@ -797,8 +797,8 @@ declare namespace RakeDbAst {
|
|
|
797
797
|
dropIfExists?: boolean;
|
|
798
798
|
dropMode?: DropMode;
|
|
799
799
|
comment?: string;
|
|
800
|
-
}
|
|
801
|
-
|
|
800
|
+
}
|
|
801
|
+
interface ChangeTable {
|
|
802
802
|
type: 'changeTable';
|
|
803
803
|
schema?: string;
|
|
804
804
|
name: string;
|
|
@@ -806,27 +806,27 @@ declare namespace RakeDbAst {
|
|
|
806
806
|
shape: Record<string, ChangeTableItem>;
|
|
807
807
|
add: TableData;
|
|
808
808
|
drop: TableData;
|
|
809
|
-
}
|
|
809
|
+
}
|
|
810
810
|
type ChangeTableItem = ChangeTableItem.Column | ChangeTableItem.Change | ChangeTableItem.Rename;
|
|
811
811
|
namespace ChangeTableItem {
|
|
812
|
-
|
|
812
|
+
interface Column {
|
|
813
813
|
type: 'add' | 'drop';
|
|
814
814
|
item: ColumnType;
|
|
815
815
|
dropMode?: DropMode;
|
|
816
|
-
}
|
|
817
|
-
|
|
816
|
+
}
|
|
817
|
+
interface Change {
|
|
818
818
|
type: 'change';
|
|
819
819
|
name?: string;
|
|
820
820
|
from: ColumnChange;
|
|
821
821
|
to: ColumnChange;
|
|
822
822
|
using?: RawSQLBase;
|
|
823
|
-
}
|
|
824
|
-
|
|
823
|
+
}
|
|
824
|
+
interface Rename {
|
|
825
825
|
type: 'rename';
|
|
826
826
|
name: string;
|
|
827
|
-
}
|
|
827
|
+
}
|
|
828
828
|
}
|
|
829
|
-
|
|
829
|
+
interface ColumnChange {
|
|
830
830
|
column?: ColumnType;
|
|
831
831
|
type?: string;
|
|
832
832
|
collate?: string;
|
|
@@ -842,20 +842,20 @@ declare namespace RakeDbAst {
|
|
|
842
842
|
} & ForeignKeyOptions)[];
|
|
843
843
|
indexes?: Omit<SingleColumnIndexOptions, 'column' | 'expression'>[];
|
|
844
844
|
identity?: TableData.Identity;
|
|
845
|
-
}
|
|
846
|
-
|
|
845
|
+
}
|
|
846
|
+
interface RenameTable {
|
|
847
847
|
type: 'renameTable';
|
|
848
848
|
fromSchema?: string;
|
|
849
849
|
from: string;
|
|
850
850
|
toSchema?: string;
|
|
851
851
|
to: string;
|
|
852
|
-
}
|
|
853
|
-
|
|
852
|
+
}
|
|
853
|
+
interface Schema {
|
|
854
854
|
type: 'schema';
|
|
855
855
|
action: 'create' | 'drop';
|
|
856
856
|
name: string;
|
|
857
|
-
}
|
|
858
|
-
|
|
857
|
+
}
|
|
858
|
+
interface Extension {
|
|
859
859
|
type: 'extension';
|
|
860
860
|
action: 'create' | 'drop';
|
|
861
861
|
name: string;
|
|
@@ -864,8 +864,8 @@ declare namespace RakeDbAst {
|
|
|
864
864
|
cascade?: boolean;
|
|
865
865
|
createIfNotExists?: boolean;
|
|
866
866
|
dropIfExists?: boolean;
|
|
867
|
-
}
|
|
868
|
-
|
|
867
|
+
}
|
|
868
|
+
interface Enum {
|
|
869
869
|
type: 'enum';
|
|
870
870
|
action: 'create' | 'drop';
|
|
871
871
|
schema?: string;
|
|
@@ -873,8 +873,8 @@ declare namespace RakeDbAst {
|
|
|
873
873
|
values: [string, ...string[]];
|
|
874
874
|
cascade?: boolean;
|
|
875
875
|
dropIfExists?: boolean;
|
|
876
|
-
}
|
|
877
|
-
|
|
876
|
+
}
|
|
877
|
+
interface Domain {
|
|
878
878
|
type: 'domain';
|
|
879
879
|
action: 'create' | 'drop';
|
|
880
880
|
schema?: string;
|
|
@@ -885,8 +885,8 @@ declare namespace RakeDbAst {
|
|
|
885
885
|
default?: RawSQLBase;
|
|
886
886
|
check?: RawSQLBase;
|
|
887
887
|
cascade?: boolean;
|
|
888
|
-
}
|
|
889
|
-
|
|
888
|
+
}
|
|
889
|
+
interface Collation {
|
|
890
890
|
type: 'collation';
|
|
891
891
|
action: 'create' | 'drop';
|
|
892
892
|
schema?: string;
|
|
@@ -901,18 +901,18 @@ declare namespace RakeDbAst {
|
|
|
901
901
|
createIfNotExists?: boolean;
|
|
902
902
|
dropIfExists?: boolean;
|
|
903
903
|
cascade?: boolean;
|
|
904
|
-
}
|
|
905
|
-
|
|
904
|
+
}
|
|
905
|
+
interface EnumOptions {
|
|
906
906
|
createIfNotExists?: boolean;
|
|
907
907
|
dropIfExists?: boolean;
|
|
908
|
-
}
|
|
909
|
-
|
|
908
|
+
}
|
|
909
|
+
interface Constraint extends TableData.Constraint {
|
|
910
910
|
type: 'constraint';
|
|
911
911
|
action: 'create';
|
|
912
912
|
tableSchema?: string;
|
|
913
913
|
tableName: string;
|
|
914
|
-
}
|
|
915
|
-
|
|
914
|
+
}
|
|
915
|
+
interface View {
|
|
916
916
|
type: 'view';
|
|
917
917
|
action: 'create' | 'drop';
|
|
918
918
|
schema?: string;
|
|
@@ -920,8 +920,8 @@ declare namespace RakeDbAst {
|
|
|
920
920
|
shape: ColumnsShape;
|
|
921
921
|
sql: RawSQLBase;
|
|
922
922
|
options: ViewOptions;
|
|
923
|
-
}
|
|
924
|
-
|
|
923
|
+
}
|
|
924
|
+
interface ViewOptions {
|
|
925
925
|
createOrReplace?: boolean;
|
|
926
926
|
dropIfExists?: boolean;
|
|
927
927
|
dropMode?: DropMode;
|
|
@@ -933,10 +933,10 @@ declare namespace RakeDbAst {
|
|
|
933
933
|
securityBarrier?: boolean;
|
|
934
934
|
securityInvoker?: boolean;
|
|
935
935
|
};
|
|
936
|
-
}
|
|
936
|
+
}
|
|
937
937
|
}
|
|
938
938
|
|
|
939
|
-
|
|
939
|
+
interface RakeDbColumnTypes {
|
|
940
940
|
index(columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): EmptyObject;
|
|
941
941
|
foreignKey(columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): EmptyObject;
|
|
942
942
|
primaryKey(columns: string[], options?: {
|
|
@@ -944,8 +944,8 @@ type RakeDbColumnTypes = {
|
|
|
944
944
|
}): EmptyObject;
|
|
945
945
|
check(check: RawSQLBase): EmptyObject;
|
|
946
946
|
constraint(arg: ConstraintArg): EmptyObject;
|
|
947
|
-
}
|
|
948
|
-
|
|
947
|
+
}
|
|
948
|
+
interface ConstraintArg {
|
|
949
949
|
name?: string;
|
|
950
950
|
references?: [
|
|
951
951
|
columns: [string, ...string[]],
|
|
@@ -955,9 +955,9 @@ type ConstraintArg = {
|
|
|
955
955
|
];
|
|
956
956
|
check?: RawSQLBase;
|
|
957
957
|
dropMode?: DropMode;
|
|
958
|
-
}
|
|
958
|
+
}
|
|
959
959
|
type Db = DbResult<RakeDbColumnTypes>;
|
|
960
|
-
|
|
960
|
+
interface BaseTable<CT> {
|
|
961
961
|
exportAs: string;
|
|
962
962
|
getFilePath(): string;
|
|
963
963
|
nowSQL?: string;
|
|
@@ -966,13 +966,13 @@ type BaseTable<CT> = {
|
|
|
966
966
|
snakeCase?: boolean;
|
|
967
967
|
language?: string;
|
|
968
968
|
};
|
|
969
|
-
}
|
|
969
|
+
}
|
|
970
970
|
type InputRakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT> = Partial<Omit<RakeDbConfig<SchemaConfig, CT>, 'columnTypes'>> & ({
|
|
971
971
|
columnTypes?: CT | ((t: DefaultColumnTypes<ColumnSchemaConfig>) => CT);
|
|
972
972
|
} | {
|
|
973
973
|
baseTable?: BaseTable<CT>;
|
|
974
974
|
});
|
|
975
|
-
|
|
975
|
+
interface RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTypes<ColumnSchemaConfig>> extends QueryLogOptions {
|
|
976
976
|
schemaConfig: SchemaConfig;
|
|
977
977
|
columnTypes: CT;
|
|
978
978
|
basePath: string;
|
|
@@ -994,9 +994,11 @@ type RakeDbConfig<SchemaConfig extends ColumnSchemaConfig, CT = DefaultColumnTyp
|
|
|
994
994
|
afterMigrate?(db: Db): Promise<void>;
|
|
995
995
|
beforeRollback?(db: Db): Promise<void>;
|
|
996
996
|
afterRollback?(db: Db): Promise<void>;
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
|
|
997
|
+
}
|
|
998
|
+
interface ModuleExportsRecord {
|
|
999
|
+
[K: string]: () => Promise<unknown>;
|
|
1000
|
+
}
|
|
1001
|
+
interface AppCodeUpdaterParams {
|
|
1000
1002
|
options: AdapterOptions;
|
|
1001
1003
|
basePath: string;
|
|
1002
1004
|
cache: object;
|
|
@@ -1006,14 +1008,14 @@ type AppCodeUpdaterParams = {
|
|
|
1006
1008
|
exportAs: string;
|
|
1007
1009
|
};
|
|
1008
1010
|
import(path: string): Promise<unknown>;
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
+
}
|
|
1012
|
+
interface AppCodeUpdater {
|
|
1011
1013
|
process(params: AppCodeUpdaterParams & {
|
|
1012
1014
|
ast: RakeDbAst;
|
|
1013
1015
|
}): Promise<void>;
|
|
1014
1016
|
afterAll(params: AppCodeUpdaterParams): Promise<void>;
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
+
}
|
|
1018
|
+
interface MigrationItem {
|
|
1017
1019
|
path: string;
|
|
1018
1020
|
version: string;
|
|
1019
1021
|
/**
|
|
@@ -1022,7 +1024,7 @@ type MigrationItem = {
|
|
|
1022
1024
|
* Promise can return `{ default: x }` where `x` is a return of `change` or an array of such returns.
|
|
1023
1025
|
*/
|
|
1024
1026
|
load(): Promise<unknown>;
|
|
1025
|
-
}
|
|
1027
|
+
}
|
|
1026
1028
|
|
|
1027
1029
|
declare const createDb: <SchemaConfig extends ColumnSchemaConfig, CT>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1028
1030
|
declare const dropDb: <SchemaConfig extends ColumnSchemaConfig, CT>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
@@ -1058,7 +1060,7 @@ declare const rollback: MigrateFn;
|
|
|
1058
1060
|
* Takes the same options as {@link migrate}.
|
|
1059
1061
|
*/
|
|
1060
1062
|
declare const redo: MigrateFn;
|
|
1061
|
-
declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, files: MigrationItem[], count: number, asts: RakeDbAst[], up: boolean) => Promise<void>;
|
|
1063
|
+
declare const migrateOrRollback: (trx: TransactionAdapter, config: RakeDbConfig<ColumnSchemaConfig, RakeDbColumnTypes>, files: MigrationItem[], count: number, asts: RakeDbAst[], up: boolean, skipLock?: boolean) => Promise<void>;
|
|
1062
1064
|
declare const changeCache: Record<string, ChangeCallback<RakeDbColumnTypes>[] | undefined>;
|
|
1063
1065
|
|
|
1064
1066
|
/**
|
|
@@ -1101,6 +1103,8 @@ declare const rakeDb: RakeDbFn;
|
|
|
1101
1103
|
|
|
1102
1104
|
declare const saveMigratedVersion: <SchemaConfig extends ColumnSchemaConfig, CT>(db: SilentQueries, version: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1103
1105
|
declare const removeMigratedVersion: <SchemaConfig extends ColumnSchemaConfig, CT>(db: SilentQueries, version: string, config: RakeDbConfig<SchemaConfig, CT>) => Promise<void>;
|
|
1106
|
+
declare class NoMigrationsTableError extends Error {
|
|
1107
|
+
}
|
|
1104
1108
|
declare const getMigratedVersionsMap: <SchemaConfig extends ColumnSchemaConfig, CT>(db: TransactionAdapter, config: RakeDbConfig<SchemaConfig, CT>) => Promise<Record<string, boolean>>;
|
|
1105
1109
|
|
|
1106
|
-
export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DbMigration, DropMode, Migration, MigrationColumnTypes, RAKE_DB_LOCK_KEY, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
|
1110
|
+
export { AppCodeUpdater, AppCodeUpdaterParams, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DbMigration, DropMode, Migration, MigrationColumnTypes, NoMigrationsTableError, RAKE_DB_LOCK_KEY, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -2205,6 +2205,8 @@ const removeMigratedVersion = async (db, version, config) => {
|
|
|
2205
2205
|
})} WHERE version = '${version}'`
|
|
2206
2206
|
);
|
|
2207
2207
|
};
|
|
2208
|
+
class NoMigrationsTableError extends Error {
|
|
2209
|
+
}
|
|
2208
2210
|
const getMigratedVersionsMap = async (db, config) => {
|
|
2209
2211
|
try {
|
|
2210
2212
|
const result = await db.arrays(
|
|
@@ -2213,10 +2215,10 @@ const getMigratedVersionsMap = async (db, config) => {
|
|
|
2213
2215
|
return Object.fromEntries(result.rows.map((row) => [row[0], true]));
|
|
2214
2216
|
} catch (err) {
|
|
2215
2217
|
if (err.code === "42P01") {
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
+
throw new NoMigrationsTableError();
|
|
2219
|
+
} else {
|
|
2220
|
+
throw err;
|
|
2218
2221
|
}
|
|
2219
|
-
throw err;
|
|
2220
2222
|
}
|
|
2221
2223
|
};
|
|
2222
2224
|
|
|
@@ -2251,9 +2253,6 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2251
2253
|
const adapter = new pqb.Adapter(opts);
|
|
2252
2254
|
try {
|
|
2253
2255
|
await adapter.transaction(begin, async (trx) => {
|
|
2254
|
-
await trx.query(
|
|
2255
|
-
`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`
|
|
2256
|
-
);
|
|
2257
2256
|
await fn(
|
|
2258
2257
|
trx,
|
|
2259
2258
|
conf,
|
|
@@ -2262,6 +2261,16 @@ function makeMigrateFn(defaultCount, up, fn) {
|
|
|
2262
2261
|
localAsts
|
|
2263
2262
|
);
|
|
2264
2263
|
});
|
|
2264
|
+
} catch (err) {
|
|
2265
|
+
if (err instanceof NoMigrationsTableError) {
|
|
2266
|
+
await adapter.transaction(begin, async (trx) => {
|
|
2267
|
+
const config2 = conf;
|
|
2268
|
+
await createSchemaMigrations(trx, config2);
|
|
2269
|
+
await fn(trx, config2, files, count != null ? count : defaultCount, localAsts);
|
|
2270
|
+
});
|
|
2271
|
+
} else {
|
|
2272
|
+
throw err;
|
|
2273
|
+
}
|
|
2265
2274
|
} finally {
|
|
2266
2275
|
await adapter.close();
|
|
2267
2276
|
}
|
|
@@ -2292,7 +2301,7 @@ const redo = makeMigrateFn(
|
|
|
2292
2301
|
async (trx, config, files, count, asts) => {
|
|
2293
2302
|
await migrateOrRollback(trx, config, files, count, asts, false);
|
|
2294
2303
|
files.reverse();
|
|
2295
|
-
await migrateOrRollback(trx, config, files, count, asts, true);
|
|
2304
|
+
await migrateOrRollback(trx, config, files, count, asts, true, true);
|
|
2296
2305
|
files.reverse();
|
|
2297
2306
|
}
|
|
2298
2307
|
);
|
|
@@ -2312,8 +2321,11 @@ function prepareConfig(config, args, count) {
|
|
|
2312
2321
|
delete config.appCodeUpdater;
|
|
2313
2322
|
return config;
|
|
2314
2323
|
}
|
|
2315
|
-
const migrateOrRollback = async (trx, config, files, count, asts, up) => {
|
|
2324
|
+
const migrateOrRollback = async (trx, config, files, count, asts, up, skipLock) => {
|
|
2316
2325
|
var _a, _b, _c;
|
|
2326
|
+
if (!skipLock) {
|
|
2327
|
+
await trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
|
|
2328
|
+
}
|
|
2317
2329
|
let db;
|
|
2318
2330
|
await ((_a = config[up ? "beforeMigrate" : "beforeRollback"]) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(trx)));
|
|
2319
2331
|
const migratedVersions = await getMigratedVersionsMap(trx, config);
|
|
@@ -3249,15 +3261,12 @@ const pushTableAst = (ctx, ast, data, domains, table, pendingTables, innerConstr
|
|
|
3249
3261
|
},
|
|
3250
3262
|
[]
|
|
3251
3263
|
);
|
|
3252
|
-
const columnChecks = innerConstraints.reduce(
|
|
3253
|
-
(
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
},
|
|
3259
|
-
{}
|
|
3260
|
-
);
|
|
3264
|
+
const columnChecks = innerConstraints.reduce((acc, item) => {
|
|
3265
|
+
if (belongsToTable(item) && isColumnCheck(item)) {
|
|
3266
|
+
acc[item.check.columns[0]] = item.check.expression;
|
|
3267
|
+
}
|
|
3268
|
+
return acc;
|
|
3269
|
+
}, {});
|
|
3261
3270
|
const shape = makeColumnsShape(
|
|
3262
3271
|
ctx,
|
|
3263
3272
|
data,
|
|
@@ -3911,6 +3920,7 @@ Migrate and rollback common arguments:
|
|
|
3911
3920
|
`;
|
|
3912
3921
|
|
|
3913
3922
|
exports.Migration = Migration;
|
|
3923
|
+
exports.NoMigrationsTableError = NoMigrationsTableError;
|
|
3914
3924
|
exports.RAKE_DB_LOCK_KEY = RAKE_DB_LOCK_KEY;
|
|
3915
3925
|
exports.changeCache = changeCache;
|
|
3916
3926
|
exports.createDb = createDb;
|