rake-db 2.3.6 → 2.3.9
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 +21 -0
- package/dist/index.d.ts +219 -7
- package/dist/index.js +66 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +66 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/commands/migrateOrRollback.test.ts +24 -6
- package/src/commands/migrateOrRollback.ts +45 -8
- package/src/common.ts +8 -0
- package/src/migration/changeTable.ts +3 -3
- package/src/migration/createTable.ts +3 -3
- package/src/migration/migration.ts +57 -43
- package/src/migration/migrationUtils.ts +2 -3
- package/src/test-utils.ts +3 -3
- package/src/pull/getColumnByType.ts +0 -15
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { singleQuote, quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, resetTableData, ColumnType, emptyObject, TransactionAdapter, logParamToLogObject, Adapter, columnsByType, instantiateColumn, codeToString, addCode, quoteObjectKey, primaryKeyToCode, indexToCode, foreignKeyToCode, TimestampColumn } from 'pqb';
|
|
1
|
+
import { singleQuote, quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, resetTableData, ColumnType, emptyObject, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, columnsByType, instantiateColumn, codeToString, addCode, quoteObjectKey, primaryKeyToCode, indexToCode, foreignKeyToCode, TimestampColumn } from 'pqb';
|
|
2
2
|
import Enquirer from 'enquirer';
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { readdir, mkdir, writeFile } from 'fs/promises';
|
|
@@ -427,7 +427,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
|
427
427
|
validatePrimaryKey(ast);
|
|
428
428
|
const queries = astToQueries$1(ast);
|
|
429
429
|
for (const query of queries) {
|
|
430
|
-
await migration.query(query);
|
|
430
|
+
await migration.adapter.query(query);
|
|
431
431
|
}
|
|
432
432
|
await runCodeUpdater(migration, ast);
|
|
433
433
|
};
|
|
@@ -661,7 +661,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
661
661
|
const ast = makeAst(up, tableName, changeData, changeTableData, options);
|
|
662
662
|
const queries = astToQueries(ast);
|
|
663
663
|
for (const query of queries) {
|
|
664
|
-
await migration.query(query);
|
|
664
|
+
await migration.adapter.query(query);
|
|
665
665
|
}
|
|
666
666
|
await runCodeUpdater(migration, ast);
|
|
667
667
|
};
|
|
@@ -890,21 +890,31 @@ var __spreadValues$2 = (a, b) => {
|
|
|
890
890
|
return a;
|
|
891
891
|
};
|
|
892
892
|
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
}
|
|
893
|
+
const createMigrationInterface = (tx, up, options, adapterOptions, appCodeUpdaterCache) => {
|
|
894
|
+
const adapter = new TransactionAdapter(tx, tx.client, tx.types);
|
|
895
|
+
const { query, arrays } = adapter;
|
|
896
|
+
const log = logParamToLogObject(options.logger || console, options.log);
|
|
897
|
+
adapter.query = (q, types) => {
|
|
898
|
+
return wrapWithLog(log, q, () => query.call(adapter, q, types));
|
|
899
|
+
};
|
|
900
|
+
adapter.arrays = (q, types) => {
|
|
901
|
+
return wrapWithLog(log, q, () => arrays.call(adapter, q, types));
|
|
902
|
+
};
|
|
903
|
+
const db = createDb$1({ adapter, columnTypes });
|
|
904
|
+
const { prototype: proto } = MigrationBase;
|
|
905
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
906
|
+
db[key] = proto[key];
|
|
907
|
+
}
|
|
908
|
+
return Object.assign(db, {
|
|
909
|
+
adapter,
|
|
910
|
+
log,
|
|
911
|
+
up,
|
|
912
|
+
options,
|
|
913
|
+
adapterOptions,
|
|
914
|
+
appCodeUpdaterCache
|
|
915
|
+
});
|
|
916
|
+
};
|
|
917
|
+
class MigrationBase {
|
|
908
918
|
createTable(tableName, cbOrOptions, cb) {
|
|
909
919
|
const options = typeof cbOrOptions === "function" ? {} : cbOrOptions;
|
|
910
920
|
const fn = cb || cbOrOptions;
|
|
@@ -929,7 +939,7 @@ class Migration extends TransactionAdapter {
|
|
|
929
939
|
toSchema,
|
|
930
940
|
to: t
|
|
931
941
|
};
|
|
932
|
-
await this.query(
|
|
942
|
+
await this.adapter.query(
|
|
933
943
|
`ALTER TABLE ${quoteWithSchema({
|
|
934
944
|
schema: ast.fromSchema,
|
|
935
945
|
name: ast.from
|
|
@@ -1052,7 +1062,7 @@ const createSchema$1 = async (migration, up, name) => {
|
|
|
1052
1062
|
action: up ? "create" : "drop",
|
|
1053
1063
|
name
|
|
1054
1064
|
};
|
|
1055
|
-
await migration.query(
|
|
1065
|
+
await migration.adapter.query(
|
|
1056
1066
|
`${ast.action === "create" ? "CREATE" : "DROP"} SCHEMA "${name}"`
|
|
1057
1067
|
);
|
|
1058
1068
|
await runCodeUpdater(migration, ast);
|
|
@@ -1069,11 +1079,11 @@ const createExtension = async (migration, up, name, options) => {
|
|
|
1069
1079
|
} else {
|
|
1070
1080
|
query = `CREATE EXTENSION${ast.ifExists ? " IF NOT EXISTS" : ""} "${ast.name}"${ast.schema ? ` SCHEMA "${ast.schema}"` : ""}${ast.version ? ` VERSION '${ast.version}'` : ""}${ast.cascade ? " CASCADE" : ""}`;
|
|
1071
1081
|
}
|
|
1072
|
-
await migration.query(query);
|
|
1082
|
+
await migration.adapter.query(query);
|
|
1073
1083
|
await runCodeUpdater(migration, ast);
|
|
1074
1084
|
};
|
|
1075
1085
|
const queryExists = (db, sql) => {
|
|
1076
|
-
return db.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
1086
|
+
return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
|
|
1077
1087
|
};
|
|
1078
1088
|
const runCodeUpdater = (migration, ast) => {
|
|
1079
1089
|
var _a, _b;
|
|
@@ -1100,8 +1110,9 @@ var __spreadValues$1 = (a, b) => {
|
|
|
1100
1110
|
}
|
|
1101
1111
|
return a;
|
|
1102
1112
|
};
|
|
1113
|
+
const getDb = (adapter) => createDb$1({ adapter, columnTypes });
|
|
1103
1114
|
const migrateOrRollback = async (options, config, args, up) => {
|
|
1104
|
-
var _a;
|
|
1115
|
+
var _a, _b, _c, _d, _e;
|
|
1105
1116
|
config = __spreadValues$1({}, config);
|
|
1106
1117
|
const files = await getMigrationFiles(config, up);
|
|
1107
1118
|
let count = up ? Infinity : 1;
|
|
@@ -1119,8 +1130,14 @@ const migrateOrRollback = async (options, config, args, up) => {
|
|
|
1119
1130
|
delete config.appCodeUpdater;
|
|
1120
1131
|
const appCodeUpdaterCache = {};
|
|
1121
1132
|
for (const opts of toArray(options)) {
|
|
1122
|
-
const
|
|
1123
|
-
|
|
1133
|
+
const adapter = new Adapter(opts);
|
|
1134
|
+
let db;
|
|
1135
|
+
if (up) {
|
|
1136
|
+
await ((_a = config.beforeMigrate) == null ? void 0 : _a.call(config, db != null ? db : db = getDb(adapter)));
|
|
1137
|
+
} else {
|
|
1138
|
+
await ((_b = config.beforeRollback) == null ? void 0 : _b.call(config, db != null ? db : db = getDb(adapter)));
|
|
1139
|
+
}
|
|
1140
|
+
const migratedVersions = await getMigratedVersionsMap(adapter, config);
|
|
1124
1141
|
try {
|
|
1125
1142
|
for (const file of files) {
|
|
1126
1143
|
if (up && migratedVersions[file.version] || !up && !migratedVersions[file.version]) {
|
|
@@ -1128,11 +1145,23 @@ const migrateOrRollback = async (options, config, args, up) => {
|
|
|
1128
1145
|
}
|
|
1129
1146
|
if (count-- <= 0)
|
|
1130
1147
|
break;
|
|
1131
|
-
await processMigration(
|
|
1132
|
-
|
|
1148
|
+
await processMigration(
|
|
1149
|
+
adapter,
|
|
1150
|
+
up,
|
|
1151
|
+
file,
|
|
1152
|
+
config,
|
|
1153
|
+
opts,
|
|
1154
|
+
appCodeUpdaterCache
|
|
1155
|
+
);
|
|
1156
|
+
(_c = config.logger) == null ? void 0 : _c.log(`${file.path} ${up ? "migrated" : "rolled back"}`);
|
|
1157
|
+
}
|
|
1158
|
+
if (up) {
|
|
1159
|
+
await ((_d = config.afterMigrate) == null ? void 0 : _d.call(config, db != null ? db : db = getDb(adapter)));
|
|
1160
|
+
} else {
|
|
1161
|
+
await ((_e = config.afterRollback) == null ? void 0 : _e.call(config, db != null ? db : db = getDb(adapter)));
|
|
1133
1162
|
}
|
|
1134
1163
|
} finally {
|
|
1135
|
-
await
|
|
1164
|
+
await adapter.close();
|
|
1136
1165
|
}
|
|
1137
1166
|
delete config.appCodeUpdater;
|
|
1138
1167
|
}
|
|
@@ -1140,7 +1169,13 @@ const migrateOrRollback = async (options, config, args, up) => {
|
|
|
1140
1169
|
const changeCache = {};
|
|
1141
1170
|
const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
|
|
1142
1171
|
await db.transaction(async (tx) => {
|
|
1143
|
-
const db2 =
|
|
1172
|
+
const db2 = createMigrationInterface(
|
|
1173
|
+
tx,
|
|
1174
|
+
up,
|
|
1175
|
+
config,
|
|
1176
|
+
options,
|
|
1177
|
+
appCodeUpdaterCache
|
|
1178
|
+
);
|
|
1144
1179
|
setCurrentMigration(db2);
|
|
1145
1180
|
setCurrentMigrationUp(up);
|
|
1146
1181
|
const callback = changeCache[file.path];
|
|
@@ -1152,7 +1187,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
|
|
|
1152
1187
|
}
|
|
1153
1188
|
await getCurrentPromise();
|
|
1154
1189
|
await (up ? saveMigratedVersion : removeMigratedVersion)(
|
|
1155
|
-
db2,
|
|
1190
|
+
db2.adapter,
|
|
1156
1191
|
file.version,
|
|
1157
1192
|
config
|
|
1158
1193
|
);
|
|
@@ -1941,5 +1976,5 @@ Generate arguments:
|
|
|
1941
1976
|
`
|
|
1942
1977
|
);
|
|
1943
1978
|
|
|
1944
|
-
export {
|
|
1979
|
+
export { MigrationBase, change, createDb, createMigrationInterface, dropDb, generate, migrate, rakeDb, resetDb, rollback, runCodeUpdater, writeMigrationFile };
|
|
1945
1980
|
//# sourceMappingURL=index.mjs.map
|