rake-db 2.4.5 → 2.4.7

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { ColumnType, EnumColumn, ColumnTypes, ColumnsShape, DbResult, DefaultColumnTypes, TransactionAdapter, QueryLogObject, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, AdapterOptions, QueryLogOptions } from 'pqb';
1
+ import { ColumnType, EnumColumn, ColumnTypes, ColumnsShape, DbResult, DefaultColumnTypes, TransactionAdapter, QueryLogObject, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, AdapterOptions, QueryLogOptions, Adapter } from 'pqb';
2
2
  import { EmptyObject, RawExpression, ColumnTypesBase, raw, MaybeArray } from 'orchid-core';
3
3
 
4
4
  declare function add(this: ColumnTypesBase, item: ColumnType, options?: {
@@ -216,8 +216,9 @@ declare const createDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig)
216
216
  declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
217
217
  declare const resetDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) => Promise<void>;
218
218
 
219
- declare const writeMigrationFile: (config: RakeDbConfig, name: string, content: string) => Promise<void>;
219
+ declare const writeMigrationFile: (config: RakeDbConfig, version: string, name: string, content: string) => Promise<void>;
220
220
  declare const generate: (config: RakeDbConfig, args: string[]) => Promise<void>;
221
+ declare const makeFileTimeStamp: () => string;
221
222
 
222
223
  declare type ChangeCallback = (db: Migration, up: boolean) => Promise<void>;
223
224
  declare const change: (fn: ChangeCallback) => void;
@@ -229,4 +230,8 @@ declare const rollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConf
229
230
 
230
231
  declare const rakeDb: (options: MaybeArray<AdapterOptions>, partialConfig?: Partial<RakeDbConfig>, args?: string[]) => Promise<void>;
231
232
 
232
- export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, changeCache, createDb, createMigrationInterface, dropDb, generate, migrate, migrateOrRollback, rakeDb, resetDb, rollback, writeMigrationFile };
233
+ declare const saveMigratedVersion: (db: Adapter, version: string, config: RakeDbConfig) => Promise<void>;
234
+ declare const removeMigratedVersion: (db: Adapter, version: string, config: RakeDbConfig) => Promise<void>;
235
+ declare const getMigratedVersionsMap: (db: Adapter, config: RakeDbConfig) => Promise<Record<string, boolean>>;
236
+
237
+ export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, TableOptions, change, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
package/dist/index.js CHANGED
@@ -1250,6 +1250,36 @@ const queryExists = (db, sql) => {
1250
1250
  return db.adapter.query(sql).then(({ rowCount }) => rowCount > 0);
1251
1251
  };
1252
1252
 
1253
+ const saveMigratedVersion = async (db, version, config) => {
1254
+ await db.query(
1255
+ `INSERT INTO ${quoteWithSchema({
1256
+ name: config.migrationsTable
1257
+ })} VALUES ('${version}')`
1258
+ );
1259
+ };
1260
+ const removeMigratedVersion = async (db, version, config) => {
1261
+ await db.query(
1262
+ `DELETE FROM ${quoteWithSchema({
1263
+ name: config.migrationsTable
1264
+ })} WHERE version = '${version}'`
1265
+ );
1266
+ };
1267
+ const getMigratedVersionsMap = async (db, config) => {
1268
+ try {
1269
+ const result = await db.arrays(
1270
+ `SELECT *
1271
+ FROM ${quoteWithSchema({ name: config.migrationsTable })}`
1272
+ );
1273
+ return Object.fromEntries(result.rows.map((row) => [row[0], true]));
1274
+ } catch (err) {
1275
+ if (err.code === "42P01") {
1276
+ await createSchemaMigrations(db, config);
1277
+ return {};
1278
+ }
1279
+ throw err;
1280
+ }
1281
+ };
1282
+
1253
1283
  var __defProp$1 = Object.defineProperty;
1254
1284
  var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
1255
1285
  var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
@@ -1362,34 +1392,6 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
1362
1392
  }));
1363
1393
  }
1364
1394
  };
1365
- const saveMigratedVersion = async (db, version, config) => {
1366
- await db.query(
1367
- `INSERT INTO ${quoteWithSchema({
1368
- name: config.migrationsTable
1369
- })} VALUES ('${version}')`
1370
- );
1371
- };
1372
- const removeMigratedVersion = async (db, version, config) => {
1373
- await db.query(
1374
- `DELETE FROM ${quoteWithSchema({
1375
- name: config.migrationsTable
1376
- })} WHERE version = '${version}'`
1377
- );
1378
- };
1379
- const getMigratedVersionsMap = async (db, config) => {
1380
- try {
1381
- const result = await db.arrays(
1382
- `SELECT * FROM ${quoteWithSchema({ name: config.migrationsTable })}`
1383
- );
1384
- return Object.fromEntries(result.rows.map((row) => [row[0], true]));
1385
- } catch (err) {
1386
- if (err.code === "42P01") {
1387
- await createSchemaMigrations(db, config);
1388
- return {};
1389
- }
1390
- throw err;
1391
- }
1392
- };
1393
1395
  const migrate = (options, config, args = []) => migrateOrRollback(options, config, args, true);
1394
1396
  const rollback = (options, config, args = []) => migrateOrRollback(options, config, args, false);
1395
1397
 
@@ -1493,13 +1495,10 @@ const resetDb = async (arg, config) => {
1493
1495
  await migrate(arg, config);
1494
1496
  };
1495
1497
 
1496
- const writeMigrationFile = async (config, name, content) => {
1498
+ const writeMigrationFile = async (config, version, name, content) => {
1497
1499
  var _a;
1498
1500
  await promises.mkdir(config.migrationsPath, { recursive: true });
1499
- const filePath = path__default["default"].resolve(
1500
- config.migrationsPath,
1501
- `${makeFileTimeStamp()}_${name}.ts`
1502
- );
1501
+ const filePath = path__default["default"].resolve(config.migrationsPath, `${version}_${name}.ts`);
1503
1502
  await promises.writeFile(filePath, content);
1504
1503
  (_a = config.logger) == null ? void 0 : _a.log(`Created ${orchidCore.pathToLog(filePath)}`);
1505
1504
  };
@@ -1507,7 +1506,13 @@ const generate = async (config, args) => {
1507
1506
  const name = args[0];
1508
1507
  if (!name)
1509
1508
  throw new Error("Migration name is missing");
1510
- await writeMigrationFile(config, name, makeContent(name, args.slice(1)));
1509
+ const version = makeFileTimeStamp();
1510
+ await writeMigrationFile(
1511
+ config,
1512
+ version,
1513
+ name,
1514
+ makeContent(name, args.slice(1))
1515
+ );
1511
1516
  };
1512
1517
  const makeFileTimeStamp = () => {
1513
1518
  const now = new Date();
@@ -2285,6 +2290,7 @@ const createForeignKey = (item) => {
2285
2290
  };
2286
2291
 
2287
2292
  const pullDbStructure = async (options, config) => {
2293
+ var _a;
2288
2294
  const adapter = new pqb.Adapter(options);
2289
2295
  const db = new DbStructure(adapter);
2290
2296
  const ast = await structureToAst(db);
@@ -2292,7 +2298,19 @@ const pullDbStructure = async (options, config) => {
2292
2298
  const result = astToMigration(config, ast);
2293
2299
  if (!result)
2294
2300
  return;
2295
- await writeMigrationFile(config, "pull", result);
2301
+ const version = makeFileTimeStamp();
2302
+ await writeMigrationFile(config, version, "pull", result);
2303
+ await saveMigratedVersion(adapter, version, config);
2304
+ const cache = {};
2305
+ for (const item of ast) {
2306
+ await ((_a = config == null ? void 0 : config.appCodeUpdater) == null ? void 0 : _a.call(config, {
2307
+ ast: item,
2308
+ options,
2309
+ basePath: config.basePath,
2310
+ cache,
2311
+ logger: config.logger
2312
+ }));
2313
+ }
2296
2314
  };
2297
2315
 
2298
2316
  const rakeDb = async (options, partialConfig = {}, args = process.argv.slice(2)) => {
@@ -2370,10 +2388,14 @@ exports.createDb = createDb;
2370
2388
  exports.createMigrationInterface = createMigrationInterface;
2371
2389
  exports.dropDb = dropDb;
2372
2390
  exports.generate = generate;
2391
+ exports.getMigratedVersionsMap = getMigratedVersionsMap;
2392
+ exports.makeFileTimeStamp = makeFileTimeStamp;
2373
2393
  exports.migrate = migrate;
2374
2394
  exports.migrateOrRollback = migrateOrRollback;
2375
2395
  exports.rakeDb = rakeDb;
2396
+ exports.removeMigratedVersion = removeMigratedVersion;
2376
2397
  exports.resetDb = resetDb;
2377
2398
  exports.rollback = rollback;
2399
+ exports.saveMigratedVersion = saveMigratedVersion;
2378
2400
  exports.writeMigrationFile = writeMigrationFile;
2379
2401
  //# sourceMappingURL=index.js.map