rake-db 2.3.26 → 2.3.28
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 +6 -0
- package/dist/index.d.ts +9 -6
- package/dist/index.js +224 -112
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +224 -114
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/commands/migrateOrRollback.test.ts +43 -1
- package/src/commands/migrateOrRollback.ts +13 -15
- package/src/common.ts +16 -0
- package/src/errors.ts +3 -0
- package/src/migration/change.test.ts +16 -0
- package/src/migration/change.ts +5 -16
- package/src/migration/changeTable.test.ts +236 -58
- package/src/migration/changeTable.ts +34 -14
- package/src/migration/createTable.test.ts +31 -7
- package/src/migration/createTable.ts +44 -25
- package/src/migration/migration.test.ts +1 -1
- package/src/migration/migration.ts +6 -8
- package/src/migration/tableMethods.ts +8 -0
- package/src/pull/astToMigration.test.ts +97 -28
- package/src/pull/astToMigration.ts +55 -12
- package/src/pull/dbStructure.test.ts +14 -0
- package/src/pull/dbStructure.ts +21 -0
- package/src/pull/pull.test.ts +4 -0
- package/src/pull/structureToAst.test.ts +37 -0
- package/src/pull/structureToAst.ts +24 -10
- package/src/rakeDb.test.ts +20 -0
- package/src/rakeDb.ts +27 -18
- package/src/test-utils.ts +1 -0
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,
|
|
2
|
+
import { EmptyObject, RawExpression, ColumnType, EnumColumn, ColumnTypes, 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;
|
|
@@ -15,7 +15,6 @@ declare type ChangeOptions = {
|
|
|
15
15
|
};
|
|
16
16
|
declare type TableChangeMethods = typeof tableChangeMethods;
|
|
17
17
|
declare const tableChangeMethods: {
|
|
18
|
-
raw: (sql: string, values?: false | Record<string, unknown> | undefined) => RawExpression<ColumnType<unknown, pqb.BaseOperators, unknown>>;
|
|
19
18
|
add: typeof add;
|
|
20
19
|
drop: typeof add;
|
|
21
20
|
change(from: ColumnType | Change, to: ColumnType | Change, options?: ChangeOptions): Change;
|
|
@@ -24,6 +23,8 @@ declare const tableChangeMethods: {
|
|
|
24
23
|
nonNullable(): Change;
|
|
25
24
|
comment(comment: string | null): Change;
|
|
26
25
|
rename(name: string): RakeDbAst.ChangeTableItem.Rename;
|
|
26
|
+
raw: (sql: string, values?: false | Record<string, unknown> | undefined) => RawExpression<ColumnType<unknown, pqb.BaseOperators, unknown>>;
|
|
27
|
+
enum: (name: string) => EnumColumn<string, [string, ...string[]]>;
|
|
27
28
|
};
|
|
28
29
|
declare type TableChanger = MigrationColumnTypes & TableChangeMethods;
|
|
29
30
|
declare type TableChangeData = Record<string, RakeDbAst.ChangeTableItem.Column | RakeDbAst.ChangeTableItem.Rename | Change | EmptyObject>;
|
|
@@ -528,12 +529,14 @@ declare const resetDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) =
|
|
|
528
529
|
declare const writeMigrationFile: (config: RakeDbConfig, name: string, content: string) => Promise<void>;
|
|
529
530
|
declare const generate: (config: RakeDbConfig, args: string[]) => Promise<void>;
|
|
530
531
|
|
|
531
|
-
declare const migrate: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig, args?: string[]) => Promise<void>;
|
|
532
|
-
declare const rollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig, args?: string[]) => Promise<void>;
|
|
533
|
-
|
|
534
532
|
declare type ChangeCallback = (db: Migration, up: boolean) => Promise<void>;
|
|
535
533
|
declare const change: (fn: ChangeCallback) => void;
|
|
536
534
|
|
|
535
|
+
declare const migrateOrRollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig, args: string[], up: boolean) => Promise<void>;
|
|
536
|
+
declare const changeCache: Record<string, ChangeCallback[] | undefined>;
|
|
537
|
+
declare const migrate: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig, args?: string[]) => Promise<void>;
|
|
538
|
+
declare const rollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig, args?: string[]) => Promise<void>;
|
|
539
|
+
|
|
537
540
|
declare const rakeDb: (options: MaybeArray<AdapterOptions>, partialConfig?: Partial<RakeDbConfig>, args?: string[]) => Promise<void>;
|
|
538
541
|
|
|
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 };
|
|
542
|
+
export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, JoinTableOptions, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, changeCache, createDb, createMigrationInterface, dropDb, generate, migrate, migrateOrRollback, rakeDb, resetDb, rollback, runCodeUpdater, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -252,25 +252,27 @@ const quoteSchemaTable = ({
|
|
|
252
252
|
}) => {
|
|
253
253
|
return pqb.singleQuote(schema ? `${schema}.${name}` : name);
|
|
254
254
|
};
|
|
255
|
+
const makePopulateEnumQuery = (item) => {
|
|
256
|
+
const [schema, name] = getSchemaAndTableFromName(item.enumName);
|
|
257
|
+
return {
|
|
258
|
+
text: `SELECT unnest(enum_range(NULL::${quoteWithSchema({
|
|
259
|
+
schema,
|
|
260
|
+
name
|
|
261
|
+
})}))::text`,
|
|
262
|
+
then(result) {
|
|
263
|
+
item.options.push(...result.rows.map(([value]) => value));
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
};
|
|
255
267
|
|
|
256
|
-
|
|
257
|
-
let currentPromise;
|
|
258
|
-
let currentUp = true;
|
|
259
|
-
let currentChangeCallback;
|
|
268
|
+
const currentChanges = [];
|
|
260
269
|
const change = (fn) => {
|
|
261
|
-
|
|
262
|
-
throw new Error("Database instance is not set");
|
|
263
|
-
currentPromise = fn(currentMigration, currentUp);
|
|
264
|
-
currentChangeCallback = fn;
|
|
265
|
-
};
|
|
266
|
-
const setCurrentMigration = (db) => {
|
|
267
|
-
currentMigration = db;
|
|
270
|
+
currentChanges.push(fn);
|
|
268
271
|
};
|
|
269
|
-
const
|
|
270
|
-
|
|
272
|
+
const clearChanges = () => {
|
|
273
|
+
currentChanges.length = 0;
|
|
271
274
|
};
|
|
272
|
-
const
|
|
273
|
-
const getCurrentChangeCallback = () => currentChangeCallback;
|
|
275
|
+
const getCurrentChanges = () => currentChanges;
|
|
274
276
|
|
|
275
277
|
var __defProp$5 = Object.defineProperty;
|
|
276
278
|
var __defProps$4 = Object.defineProperties;
|
|
@@ -461,6 +463,16 @@ const primaryKeyToSql = (primaryKey) => {
|
|
|
461
463
|
)})`;
|
|
462
464
|
};
|
|
463
465
|
|
|
466
|
+
const tableMethods = {
|
|
467
|
+
raw: pqb.raw,
|
|
468
|
+
enum: (name) => new pqb.EnumColumn(name, [])
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
class RakeDbError extends Error {
|
|
472
|
+
}
|
|
473
|
+
class NoPrimaryKey extends RakeDbError {
|
|
474
|
+
}
|
|
475
|
+
|
|
464
476
|
var __defProp$4 = Object.defineProperty;
|
|
465
477
|
var __defProps$3 = Object.defineProperties;
|
|
466
478
|
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
@@ -480,9 +492,19 @@ var __spreadValues$4 = (a, b) => {
|
|
|
480
492
|
return a;
|
|
481
493
|
};
|
|
482
494
|
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
495
|
+
var __objRest = (source, exclude) => {
|
|
496
|
+
var target = {};
|
|
497
|
+
for (var prop in source)
|
|
498
|
+
if (__hasOwnProp$4.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
499
|
+
target[prop] = source[prop];
|
|
500
|
+
if (source != null && __getOwnPropSymbols$4)
|
|
501
|
+
for (var prop of __getOwnPropSymbols$4(source)) {
|
|
502
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum$4.call(source, prop))
|
|
503
|
+
target[prop] = source[prop];
|
|
504
|
+
}
|
|
505
|
+
return target;
|
|
506
|
+
};
|
|
507
|
+
const types = Object.assign(Object.create(pqb.columnTypes), tableMethods);
|
|
486
508
|
const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
487
509
|
const shape = pqb.getColumnTypes(types, fn);
|
|
488
510
|
const tableData = pqb.getTableData();
|
|
@@ -496,8 +518,10 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
|
496
518
|
);
|
|
497
519
|
validatePrimaryKey(ast);
|
|
498
520
|
const queries = astToQueries$1(ast);
|
|
499
|
-
for (const
|
|
500
|
-
|
|
521
|
+
for (const _a of queries) {
|
|
522
|
+
const _b = _a, { then } = _b, query = __objRest(_b, ["then"]);
|
|
523
|
+
const result = await migration.adapter.arrays(query);
|
|
524
|
+
then == null ? void 0 : then(result);
|
|
501
525
|
}
|
|
502
526
|
await runCodeUpdater(migration, ast);
|
|
503
527
|
};
|
|
@@ -535,23 +559,31 @@ const validatePrimaryKey = (ast) => {
|
|
|
535
559
|
}
|
|
536
560
|
}
|
|
537
561
|
if (!hasPrimaryKey) {
|
|
538
|
-
const
|
|
562
|
+
const error = new NoPrimaryKey(
|
|
563
|
+
`Table ${ast.name} has no primary key.
|
|
564
|
+
You can suppress this error by setting { noPrimaryKey: true } after a table name.`
|
|
565
|
+
);
|
|
539
566
|
if (ast.noPrimaryKey === "error") {
|
|
540
|
-
throw
|
|
567
|
+
throw error;
|
|
541
568
|
} else {
|
|
542
|
-
console.warn(message);
|
|
569
|
+
console.warn(error.message);
|
|
543
570
|
}
|
|
544
571
|
}
|
|
545
572
|
}
|
|
546
573
|
};
|
|
547
574
|
const astToQueries$1 = (ast) => {
|
|
575
|
+
const queries = [];
|
|
576
|
+
for (const key in ast.shape) {
|
|
577
|
+
const item = ast.shape[key];
|
|
578
|
+
if (!(item instanceof pqb.EnumColumn))
|
|
579
|
+
continue;
|
|
580
|
+
queries.push(makePopulateEnumQuery(item));
|
|
581
|
+
}
|
|
548
582
|
if (ast.action === "drop") {
|
|
549
|
-
|
|
550
|
-
{
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
}
|
|
554
|
-
];
|
|
583
|
+
queries.push({
|
|
584
|
+
text: `DROP TABLE ${quoteWithSchema(ast)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`
|
|
585
|
+
});
|
|
586
|
+
return queries;
|
|
555
587
|
}
|
|
556
588
|
const lines = [];
|
|
557
589
|
const values = [];
|
|
@@ -573,7 +605,7 @@ const astToQueries$1 = (ast) => {
|
|
|
573
605
|
${constraintToSql(ast, true, foreignKey)}`);
|
|
574
606
|
});
|
|
575
607
|
indexes.push(...ast.indexes);
|
|
576
|
-
|
|
608
|
+
queries.push(
|
|
577
609
|
{
|
|
578
610
|
text: `CREATE TABLE ${quoteWithSchema(ast)} (${lines.join(",")}
|
|
579
611
|
)`,
|
|
@@ -581,14 +613,13 @@ const astToQueries$1 = (ast) => {
|
|
|
581
613
|
},
|
|
582
614
|
...indexesToQuery(true, ast, indexes),
|
|
583
615
|
...commentsToQuery(ast, comments)
|
|
584
|
-
|
|
616
|
+
);
|
|
585
617
|
if (ast.comment) {
|
|
586
|
-
|
|
587
|
-
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${pqb.quote(ast.comment)}
|
|
588
|
-
values: []
|
|
618
|
+
queries.push({
|
|
619
|
+
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${pqb.quote(ast.comment)}`
|
|
589
620
|
});
|
|
590
621
|
}
|
|
591
|
-
return
|
|
622
|
+
return queries;
|
|
592
623
|
};
|
|
593
624
|
|
|
594
625
|
var __defProp$3 = Object.defineProperty;
|
|
@@ -687,8 +718,7 @@ const columnTypeToColumnChange = (item) => {
|
|
|
687
718
|
}
|
|
688
719
|
return item.to;
|
|
689
720
|
};
|
|
690
|
-
const tableChangeMethods = {
|
|
691
|
-
raw: pqb.raw,
|
|
721
|
+
const tableChangeMethods = __spreadProps$2(__spreadValues$3({}, tableMethods), {
|
|
692
722
|
add,
|
|
693
723
|
drop,
|
|
694
724
|
change(from, to, options) {
|
|
@@ -721,8 +751,9 @@ const tableChangeMethods = {
|
|
|
721
751
|
rename(name) {
|
|
722
752
|
return { type: "rename", name };
|
|
723
753
|
}
|
|
724
|
-
};
|
|
754
|
+
});
|
|
725
755
|
const changeTable = async (migration, up, tableName, options, fn) => {
|
|
756
|
+
var _a;
|
|
726
757
|
pqb.resetTableData();
|
|
727
758
|
resetChangeTableData();
|
|
728
759
|
const tableChanger = Object.create(pqb.columnTypes);
|
|
@@ -731,7 +762,8 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
731
762
|
const ast = makeAst(up, tableName, changeData, changeTableData, options);
|
|
732
763
|
const queries = astToQueries(ast);
|
|
733
764
|
for (const query of queries) {
|
|
734
|
-
await migration.adapter.
|
|
765
|
+
const result = await migration.adapter.arrays(query);
|
|
766
|
+
(_a = query.then) == null ? void 0 : _a.call(query, result);
|
|
735
767
|
}
|
|
736
768
|
await runCodeUpdater(migration, ast);
|
|
737
769
|
};
|
|
@@ -763,11 +795,10 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
|
|
|
763
795
|
};
|
|
764
796
|
const astToQueries = (ast) => {
|
|
765
797
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
766
|
-
const
|
|
798
|
+
const queries = [];
|
|
767
799
|
if (ast.comment !== void 0) {
|
|
768
|
-
|
|
769
|
-
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${pqb.quote(ast.comment)}
|
|
770
|
-
values: []
|
|
800
|
+
queries.push({
|
|
801
|
+
text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${pqb.quote(ast.comment)}`
|
|
771
802
|
});
|
|
772
803
|
}
|
|
773
804
|
const addPrimaryKeys = ast.add.primaryKey ? __spreadValues$3({}, ast.add.primaryKey) : {
|
|
@@ -778,6 +809,12 @@ const astToQueries = (ast) => {
|
|
|
778
809
|
};
|
|
779
810
|
for (const key in ast.shape) {
|
|
780
811
|
const item = ast.shape[key];
|
|
812
|
+
if ("item" in item) {
|
|
813
|
+
const { item: column } = item;
|
|
814
|
+
if (column instanceof pqb.EnumColumn) {
|
|
815
|
+
queries.push(makePopulateEnumQuery(column));
|
|
816
|
+
}
|
|
817
|
+
}
|
|
781
818
|
if (item.type === "add") {
|
|
782
819
|
if (item.item.isPrimaryKey) {
|
|
783
820
|
addPrimaryKeys.columns.push(key);
|
|
@@ -787,6 +824,12 @@ const astToQueries = (ast) => {
|
|
|
787
824
|
dropPrimaryKeys.columns.push(key);
|
|
788
825
|
}
|
|
789
826
|
} else if (item.type === "change") {
|
|
827
|
+
if (item.from.column instanceof pqb.EnumColumn) {
|
|
828
|
+
queries.push(makePopulateEnumQuery(item.from.column));
|
|
829
|
+
}
|
|
830
|
+
if (item.to.column instanceof pqb.EnumColumn) {
|
|
831
|
+
queries.push(makePopulateEnumQuery(item.to.column));
|
|
832
|
+
}
|
|
790
833
|
if (item.from.primaryKey) {
|
|
791
834
|
dropPrimaryKeys.columns.push(key);
|
|
792
835
|
dropPrimaryKeys.change = true;
|
|
@@ -929,16 +972,16 @@ const astToQueries = (ast) => {
|
|
|
929
972
|
)
|
|
930
973
|
);
|
|
931
974
|
if (alterTable.length) {
|
|
932
|
-
|
|
975
|
+
queries.push({
|
|
933
976
|
text: `ALTER TABLE ${quoteWithSchema(ast)}
|
|
934
977
|
${alterTable.join(",\n ")}`,
|
|
935
978
|
values
|
|
936
979
|
});
|
|
937
980
|
}
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
return
|
|
981
|
+
queries.push(...indexesToQuery(false, ast, dropIndexes));
|
|
982
|
+
queries.push(...indexesToQuery(true, ast, addIndexes));
|
|
983
|
+
queries.push(...commentsToQuery(ast, comments));
|
|
984
|
+
return queries;
|
|
942
985
|
};
|
|
943
986
|
|
|
944
987
|
var __defProp$2 = Object.defineProperty;
|
|
@@ -1078,10 +1121,10 @@ class MigrationBase {
|
|
|
1078
1121
|
return createExtension$1(this, !this.up, name, options);
|
|
1079
1122
|
}
|
|
1080
1123
|
createEnum(name, values, options) {
|
|
1081
|
-
return createEnum(this, this.up, name, values, options);
|
|
1124
|
+
return createEnum$1(this, this.up, name, values, options);
|
|
1082
1125
|
}
|
|
1083
1126
|
dropEnum(name, values, options) {
|
|
1084
|
-
return createEnum(this, !this.up, name, values, options);
|
|
1127
|
+
return createEnum$1(this, !this.up, name, values, options);
|
|
1085
1128
|
}
|
|
1086
1129
|
async tableExists(tableName) {
|
|
1087
1130
|
return queryExists(this, {
|
|
@@ -1158,7 +1201,7 @@ const createExtension$1 = async (migration, up, name, options) => {
|
|
|
1158
1201
|
await migration.adapter.query(query);
|
|
1159
1202
|
await runCodeUpdater(migration, ast);
|
|
1160
1203
|
};
|
|
1161
|
-
const createEnum = async (migration, up, name, values, options = {}) => {
|
|
1204
|
+
const createEnum$1 = async (migration, up, name, values, options = {}) => {
|
|
1162
1205
|
const [schema, enumName] = getSchemaAndTableFromName(name);
|
|
1163
1206
|
const ast = __spreadValues$2({
|
|
1164
1207
|
type: "enum",
|
|
@@ -1167,17 +1210,14 @@ const createEnum = async (migration, up, name, values, options = {}) => {
|
|
|
1167
1210
|
name: enumName,
|
|
1168
1211
|
values
|
|
1169
1212
|
}, options);
|
|
1170
|
-
let
|
|
1213
|
+
let query;
|
|
1171
1214
|
const quotedName = quoteWithSchema(ast);
|
|
1172
1215
|
if (ast.action === "create") {
|
|
1173
|
-
|
|
1216
|
+
query = `CREATE TYPE ${quotedName} AS ENUM (${values.map(pqb.quote).join(", ")})`;
|
|
1174
1217
|
} else {
|
|
1175
|
-
|
|
1218
|
+
query = `DROP TYPE${ast.dropIfExists ? " IF EXISTS" : ""} ${quotedName}${ast.cascade ? " CASCADE" : ""}`;
|
|
1176
1219
|
}
|
|
1177
|
-
await migration.adapter.query(
|
|
1178
|
-
text,
|
|
1179
|
-
values
|
|
1180
|
-
});
|
|
1220
|
+
await migration.adapter.query(query);
|
|
1181
1221
|
await runCodeUpdater(migration, ast);
|
|
1182
1222
|
};
|
|
1183
1223
|
const queryExists = (db, sql) => {
|
|
@@ -1275,16 +1315,16 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
|
|
|
1275
1315
|
options,
|
|
1276
1316
|
appCodeUpdaterCache
|
|
1277
1317
|
);
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
if (callback) {
|
|
1282
|
-
change(callback);
|
|
1283
|
-
} else {
|
|
1318
|
+
clearChanges();
|
|
1319
|
+
let changes = changeCache[file.path];
|
|
1320
|
+
if (!changes) {
|
|
1284
1321
|
await config.import(url.pathToFileURL(file.path).pathname);
|
|
1285
|
-
|
|
1322
|
+
changes = getCurrentChanges();
|
|
1323
|
+
changeCache[file.path] = changes;
|
|
1324
|
+
}
|
|
1325
|
+
for (const fn of up ? changes : changes.reverse()) {
|
|
1326
|
+
await fn(db2, up);
|
|
1286
1327
|
}
|
|
1287
|
-
await getCurrentPromise();
|
|
1288
1328
|
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
1289
1329
|
db2.adapter,
|
|
1290
1330
|
file.version,
|
|
@@ -1769,6 +1809,20 @@ JOIN pg_catalog.pg_namespace n ON n.oid = extnamespace
|
|
|
1769
1809
|
);
|
|
1770
1810
|
return rows;
|
|
1771
1811
|
}
|
|
1812
|
+
async getEnums() {
|
|
1813
|
+
const { rows } = await this.db.query(
|
|
1814
|
+
`SELECT
|
|
1815
|
+
n.nspname as "schemaName",
|
|
1816
|
+
t.typname as name,
|
|
1817
|
+
json_agg(e.enumlabel) as values
|
|
1818
|
+
FROM pg_type t
|
|
1819
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
1820
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
1821
|
+
WHERE ${filterSchema("n.nspname")}
|
|
1822
|
+
GROUP BY n.nspname, t.typname`
|
|
1823
|
+
);
|
|
1824
|
+
return rows;
|
|
1825
|
+
}
|
|
1772
1826
|
}
|
|
1773
1827
|
|
|
1774
1828
|
var __defProp = Object.defineProperty;
|
|
@@ -1835,6 +1889,24 @@ const structureToAst = async (db) => {
|
|
|
1835
1889
|
}
|
|
1836
1890
|
}
|
|
1837
1891
|
const outerFKeys = [];
|
|
1892
|
+
for (const it of data.extensions) {
|
|
1893
|
+
ast.push({
|
|
1894
|
+
type: "extension",
|
|
1895
|
+
action: "create",
|
|
1896
|
+
name: it.name,
|
|
1897
|
+
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1898
|
+
version: it.version
|
|
1899
|
+
});
|
|
1900
|
+
}
|
|
1901
|
+
for (const it of data.enums) {
|
|
1902
|
+
ast.push({
|
|
1903
|
+
type: "enum",
|
|
1904
|
+
action: "create",
|
|
1905
|
+
name: it.name,
|
|
1906
|
+
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1907
|
+
values: it.values
|
|
1908
|
+
});
|
|
1909
|
+
}
|
|
1838
1910
|
for (const key in pendingTables) {
|
|
1839
1911
|
const innerFKeys = [];
|
|
1840
1912
|
const { table } = pendingTables[key];
|
|
@@ -1858,15 +1930,6 @@ const structureToAst = async (db) => {
|
|
|
1858
1930
|
tableName: fkey.tableName
|
|
1859
1931
|
}));
|
|
1860
1932
|
}
|
|
1861
|
-
for (const it of data.extensions) {
|
|
1862
|
-
ast.push({
|
|
1863
|
-
type: "extension",
|
|
1864
|
-
action: "create",
|
|
1865
|
-
name: it.name,
|
|
1866
|
-
schema: it.schemaName === "public" ? void 0 : it.schemaName,
|
|
1867
|
-
version: it.version
|
|
1868
|
-
});
|
|
1869
|
-
}
|
|
1870
1933
|
return ast;
|
|
1871
1934
|
};
|
|
1872
1935
|
const getData = async (db) => {
|
|
@@ -1877,7 +1940,8 @@ const getData = async (db) => {
|
|
|
1877
1940
|
primaryKeys,
|
|
1878
1941
|
indexes,
|
|
1879
1942
|
foreignKeys,
|
|
1880
|
-
extensions
|
|
1943
|
+
extensions,
|
|
1944
|
+
enums
|
|
1881
1945
|
] = await Promise.all([
|
|
1882
1946
|
db.getSchemas(),
|
|
1883
1947
|
db.getTables(),
|
|
@@ -1885,7 +1949,8 @@ const getData = async (db) => {
|
|
|
1885
1949
|
db.getPrimaryKeys(),
|
|
1886
1950
|
db.getIndexes(),
|
|
1887
1951
|
db.getForeignKeys(),
|
|
1888
|
-
db.getExtensions()
|
|
1952
|
+
db.getExtensions(),
|
|
1953
|
+
db.getEnums()
|
|
1889
1954
|
]);
|
|
1890
1955
|
return {
|
|
1891
1956
|
schemas,
|
|
@@ -1894,7 +1959,8 @@ const getData = async (db) => {
|
|
|
1894
1959
|
primaryKeys,
|
|
1895
1960
|
indexes,
|
|
1896
1961
|
foreignKeys,
|
|
1897
|
-
extensions
|
|
1962
|
+
extensions,
|
|
1963
|
+
enums
|
|
1898
1964
|
};
|
|
1899
1965
|
};
|
|
1900
1966
|
const makeBelongsToTable = (schema, table) => (item) => item.schemaName === schema && item.tableName === table;
|
|
@@ -2026,32 +2092,56 @@ const foreignKeyToAst = (fkey) => ({
|
|
|
2026
2092
|
});
|
|
2027
2093
|
|
|
2028
2094
|
const astToMigration = (ast) => {
|
|
2029
|
-
const
|
|
2095
|
+
const first = [];
|
|
2096
|
+
const tables = [];
|
|
2097
|
+
const foreignKeys = [];
|
|
2030
2098
|
for (const item of ast) {
|
|
2031
2099
|
if (item.type === "schema" && item.action === "create") {
|
|
2032
|
-
|
|
2100
|
+
first.push(createSchema(item));
|
|
2033
2101
|
} else if (item.type === "extension" && item.action === "create") {
|
|
2034
|
-
if (
|
|
2035
|
-
|
|
2036
|
-
|
|
2102
|
+
if (first.length)
|
|
2103
|
+
first.push([]);
|
|
2104
|
+
first.push(...createExtension(item));
|
|
2105
|
+
} else if (item.type === "enum" && item.action === "create") {
|
|
2106
|
+
if (first.length)
|
|
2107
|
+
first.push([]);
|
|
2108
|
+
first.push(...createEnum(item));
|
|
2037
2109
|
} else if (item.type === "table" && item.action === "create") {
|
|
2038
|
-
|
|
2039
|
-
code.push([]);
|
|
2040
|
-
code.push(...createTable(item));
|
|
2110
|
+
tables.push(createTable(item));
|
|
2041
2111
|
} else if (item.type === "foreignKey") {
|
|
2042
|
-
if (
|
|
2043
|
-
|
|
2044
|
-
|
|
2112
|
+
if (foreignKeys.length)
|
|
2113
|
+
foreignKeys.push([]);
|
|
2114
|
+
foreignKeys.push(...createForeignKey(item));
|
|
2045
2115
|
}
|
|
2046
2116
|
}
|
|
2047
|
-
if (!
|
|
2117
|
+
if (!first.length && !tables.length && !foreignKeys.length)
|
|
2048
2118
|
return;
|
|
2049
|
-
|
|
2050
|
-
|
|
2119
|
+
let code = `import { change } from 'rake-db';
|
|
2120
|
+
`;
|
|
2121
|
+
if (first.length) {
|
|
2122
|
+
code += `
|
|
2051
2123
|
change(async (db) => {
|
|
2052
|
-
${pqb.codeToString(
|
|
2124
|
+
${pqb.codeToString(first, " ", " ")}
|
|
2053
2125
|
});
|
|
2054
2126
|
`;
|
|
2127
|
+
}
|
|
2128
|
+
if (tables.length) {
|
|
2129
|
+
for (const table of tables) {
|
|
2130
|
+
code += `
|
|
2131
|
+
change(async (db) => {
|
|
2132
|
+
${pqb.codeToString(table, " ", " ")}
|
|
2133
|
+
});
|
|
2134
|
+
`;
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
if (foreignKeys.length) {
|
|
2138
|
+
code += `
|
|
2139
|
+
change(async (db) => {
|
|
2140
|
+
${pqb.codeToString(foreignKeys, " ", " ")}
|
|
2141
|
+
});
|
|
2142
|
+
`;
|
|
2143
|
+
}
|
|
2144
|
+
return code;
|
|
2055
2145
|
};
|
|
2056
2146
|
const createSchema = (ast) => {
|
|
2057
2147
|
return `await db.createSchema(${pqb.singleQuote(ast.name)});`;
|
|
@@ -2068,7 +2158,19 @@ const createExtension = (ast) => {
|
|
|
2068
2158
|
}
|
|
2069
2159
|
pqb.addCode(code, "}");
|
|
2070
2160
|
}
|
|
2071
|
-
pqb.addCode(code, ")");
|
|
2161
|
+
pqb.addCode(code, ");");
|
|
2162
|
+
return code;
|
|
2163
|
+
};
|
|
2164
|
+
const createEnum = (ast) => {
|
|
2165
|
+
const code = [
|
|
2166
|
+
`await db.createEnum(${pqb.singleQuote(ast.name)}, [${ast.values.map(pqb.singleQuote).join(", ")}]`
|
|
2167
|
+
];
|
|
2168
|
+
if (ast.schema) {
|
|
2169
|
+
pqb.addCode(code, ", {");
|
|
2170
|
+
code.push([`schema: ${pqb.singleQuote(ast.schema)},`]);
|
|
2171
|
+
pqb.addCode(code, "}");
|
|
2172
|
+
}
|
|
2173
|
+
pqb.addCode(code, ");");
|
|
2072
2174
|
return code;
|
|
2073
2175
|
};
|
|
2074
2176
|
const createTable = (ast) => {
|
|
@@ -2135,24 +2237,32 @@ const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2))
|
|
|
2135
2237
|
var _a;
|
|
2136
2238
|
const config = processRakeDbConfig(partialConfig);
|
|
2137
2239
|
const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2240
|
+
try {
|
|
2241
|
+
if (command === "create") {
|
|
2242
|
+
await createDb(options, config);
|
|
2243
|
+
} else if (command === "drop") {
|
|
2244
|
+
await dropDb(options);
|
|
2245
|
+
} else if (command === "reset") {
|
|
2246
|
+
await resetDb(options, config);
|
|
2247
|
+
} else if (command === "migrate") {
|
|
2248
|
+
await migrate(options, config, args.slice(1));
|
|
2249
|
+
} else if (command === "rollback") {
|
|
2250
|
+
await rollback(options, config, args.slice(1));
|
|
2251
|
+
} else if (command === "g" || command === "generate") {
|
|
2252
|
+
await generate(config, args.slice(1));
|
|
2253
|
+
} else if (command === "pull") {
|
|
2254
|
+
await pullDbStructure(pqb.toArray(options)[0], config);
|
|
2255
|
+
} else if (config.commands[command]) {
|
|
2256
|
+
await config.commands[command](pqb.toArray(options), config, args.slice(1));
|
|
2257
|
+
} else {
|
|
2258
|
+
printHelp();
|
|
2259
|
+
}
|
|
2260
|
+
} catch (err) {
|
|
2261
|
+
if (err instanceof RakeDbError) {
|
|
2262
|
+
console.error(err.message);
|
|
2263
|
+
process.exit(1);
|
|
2264
|
+
}
|
|
2265
|
+
throw err;
|
|
2156
2266
|
}
|
|
2157
2267
|
};
|
|
2158
2268
|
const printHelp = () => console.log(
|
|
@@ -2195,11 +2305,13 @@ Generate arguments:
|
|
|
2195
2305
|
|
|
2196
2306
|
exports.MigrationBase = MigrationBase;
|
|
2197
2307
|
exports.change = change;
|
|
2308
|
+
exports.changeCache = changeCache;
|
|
2198
2309
|
exports.createDb = createDb;
|
|
2199
2310
|
exports.createMigrationInterface = createMigrationInterface;
|
|
2200
2311
|
exports.dropDb = dropDb;
|
|
2201
2312
|
exports.generate = generate;
|
|
2202
2313
|
exports.migrate = migrate;
|
|
2314
|
+
exports.migrateOrRollback = migrateOrRollback;
|
|
2203
2315
|
exports.rakeDb = rakeDb;
|
|
2204
2316
|
exports.resetDb = resetDb;
|
|
2205
2317
|
exports.rollback = rollback;
|