@cumulus/db 15.0.3 → 16.0.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 CHANGED
@@ -21,13 +21,13 @@ export { translateApiFiletoPostgresFile, translatePostgresFileToApiFile, } from
21
21
  export { translateApiCollectionToPostgresCollection, translatePostgresCollectionToApiCollection, } from './translate/collections';
22
22
  export { translateApiProviderToPostgresProvider, translatePostgresProviderToApiProvider, } from './translate/providers';
23
23
  export { translatePostgresRuleToApiRule, translateApiRuleToPostgresRule, translateApiRuleToPostgresRuleRaw, } from './translate/rules';
24
- export { translateApiExecutionToPostgresExecution, translatePostgresExecutionToApiExecution, } from './translate/executions';
24
+ export { translateApiExecutionToPostgresExecution, translateApiExecutionToPostgresExecutionWithoutNilsRemoved, translatePostgresExecutionToApiExecution, } from './translate/executions';
25
25
  export { translateApiGranuleToPostgresGranule, translateApiGranuleToPostgresGranuleWithoutNilsRemoved, translatePostgresGranuleToApiGranule, translatePostgresGranuleResultToApiGranule, } from './translate/granules';
26
26
  export { translateApiPdrToPostgresPdr, translatePostgresPdrToApiPdr, } from './translate/pdr';
27
27
  export { getCollectionsByGranuleIds, } from './lib/collection';
28
28
  export { executionArnsFromGranuleIdsAndWorkflowNames, newestExecutionArnFromGranuleIdWorkflowName, getWorkflowNameIntersectFromGranuleIds, getApiExecutionCumulusIds, getApiGranuleExecutionCumulusIdsByExecution, getExecutionInfoByGranuleCumulusId, } from './lib/execution';
29
29
  export { getFilesAndGranuleInfoQuery, } from './lib/file';
30
- export { getApiGranuleCumulusIds, getApiGranuleExecutionCumulusIds, getGranuleCollectionId, getUniqueGranuleByGranuleId, upsertGranuleWithExecutionJoinRecord, getGranulesByApiPropertiesQuery, getGranulesByGranuleId, } from './lib/granule';
30
+ export { getApiGranuleCumulusIds, getApiGranuleExecutionCumulusIds, getGranuleCollectionId, getUniqueGranuleByGranuleId, getGranuleByUniqueColumns, upsertGranuleWithExecutionJoinRecord, getGranulesByApiPropertiesQuery, getGranulesByGranuleId, getGranuleAndCollection, } from './lib/granule';
31
31
  export { QuerySearchClient, } from './lib/QuerySearchClient';
32
32
  export { AsyncOperationPgModel } from './models/async_operation';
33
33
  export { BasePgModel } from './models/base';
package/dist/index.js CHANGED
@@ -23,8 +23,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getApiGranuleCumulusIds = exports.getFilesAndGranuleInfoQuery = exports.getExecutionInfoByGranuleCumulusId = exports.getApiGranuleExecutionCumulusIdsByExecution = exports.getApiExecutionCumulusIds = exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.getCollectionsByGranuleIds = exports.translatePostgresPdrToApiPdr = exports.translateApiPdrToPostgresPdr = exports.translatePostgresGranuleResultToApiGranule = exports.translatePostgresGranuleToApiGranule = exports.translateApiGranuleToPostgresGranuleWithoutNilsRemoved = exports.translateApiGranuleToPostgresGranule = exports.translatePostgresExecutionToApiExecution = exports.translateApiExecutionToPostgresExecution = exports.translateApiRuleToPostgresRuleRaw = exports.translateApiRuleToPostgresRule = exports.translatePostgresRuleToApiRule = exports.translatePostgresProviderToApiProvider = exports.translateApiProviderToPostgresProvider = exports.translatePostgresCollectionToApiCollection = exports.translateApiCollectionToPostgresCollection = exports.translatePostgresFileToApiFile = exports.translateApiFiletoPostgresFile = exports.translatePostgresAsyncOperationToApiAsyncOperation = exports.translateApiAsyncOperationToPostgresAsyncOperation = exports.nullifyUndefinedProviderValues = exports.validateProviderHost = exports.migrationDir = exports.TableNames = exports.createRejectableTransaction = exports.localStackConnectionEnv = exports.getKnexConfig = exports.getKnexClient = exports.isCollisionError = exports.generateLocalTestDb = exports.fakeRuleRecordFactory = exports.fakeProviderRecordFactory = exports.fakePdrRecordFactory = exports.fakeGranuleRecordFactory = exports.fakeFileRecordFactory = exports.fakeExecutionRecordFactory = exports.fakeCollectionRecordFactory = exports.fakeAsyncOperationRecordFactory = exports.destroyLocalTestDb = exports.deleteTestDatabase = exports.createTestDatabase = exports.Knex = void 0;
27
- exports.RulePgModel = exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.QuerySearchClient = exports.getGranulesByGranuleId = exports.getGranulesByApiPropertiesQuery = exports.upsertGranuleWithExecutionJoinRecord = exports.getUniqueGranuleByGranuleId = exports.getGranuleCollectionId = exports.getApiGranuleExecutionCumulusIds = void 0;
26
+ exports.getFilesAndGranuleInfoQuery = exports.getExecutionInfoByGranuleCumulusId = exports.getApiGranuleExecutionCumulusIdsByExecution = exports.getApiExecutionCumulusIds = exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.getCollectionsByGranuleIds = exports.translatePostgresPdrToApiPdr = exports.translateApiPdrToPostgresPdr = exports.translatePostgresGranuleResultToApiGranule = exports.translatePostgresGranuleToApiGranule = exports.translateApiGranuleToPostgresGranuleWithoutNilsRemoved = exports.translateApiGranuleToPostgresGranule = exports.translatePostgresExecutionToApiExecution = exports.translateApiExecutionToPostgresExecutionWithoutNilsRemoved = exports.translateApiExecutionToPostgresExecution = exports.translateApiRuleToPostgresRuleRaw = exports.translateApiRuleToPostgresRule = exports.translatePostgresRuleToApiRule = exports.translatePostgresProviderToApiProvider = exports.translateApiProviderToPostgresProvider = exports.translatePostgresCollectionToApiCollection = exports.translateApiCollectionToPostgresCollection = exports.translatePostgresFileToApiFile = exports.translateApiFiletoPostgresFile = exports.translatePostgresAsyncOperationToApiAsyncOperation = exports.translateApiAsyncOperationToPostgresAsyncOperation = exports.nullifyUndefinedProviderValues = exports.validateProviderHost = exports.migrationDir = exports.TableNames = exports.createRejectableTransaction = exports.localStackConnectionEnv = exports.getKnexConfig = exports.getKnexClient = exports.isCollisionError = exports.generateLocalTestDb = exports.fakeRuleRecordFactory = exports.fakeProviderRecordFactory = exports.fakePdrRecordFactory = exports.fakeGranuleRecordFactory = exports.fakeFileRecordFactory = exports.fakeExecutionRecordFactory = exports.fakeCollectionRecordFactory = exports.fakeAsyncOperationRecordFactory = exports.destroyLocalTestDb = exports.deleteTestDatabase = exports.createTestDatabase = exports.Knex = void 0;
27
+ exports.RulePgModel = exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.QuerySearchClient = exports.getGranuleAndCollection = exports.getGranulesByGranuleId = exports.getGranulesByApiPropertiesQuery = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleByUniqueColumns = exports.getUniqueGranuleByGranuleId = exports.getGranuleCollectionId = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = void 0;
28
28
  const path = __importStar(require("path"));
29
29
  var knex_1 = require("knex");
30
30
  Object.defineProperty(exports, "Knex", { enumerable: true, get: function () { return knex_1.Knex; } });
@@ -74,6 +74,7 @@ Object.defineProperty(exports, "translateApiRuleToPostgresRule", { enumerable: t
74
74
  Object.defineProperty(exports, "translateApiRuleToPostgresRuleRaw", { enumerable: true, get: function () { return rules_1.translateApiRuleToPostgresRuleRaw; } });
75
75
  var executions_1 = require("./translate/executions");
76
76
  Object.defineProperty(exports, "translateApiExecutionToPostgresExecution", { enumerable: true, get: function () { return executions_1.translateApiExecutionToPostgresExecution; } });
77
+ Object.defineProperty(exports, "translateApiExecutionToPostgresExecutionWithoutNilsRemoved", { enumerable: true, get: function () { return executions_1.translateApiExecutionToPostgresExecutionWithoutNilsRemoved; } });
77
78
  Object.defineProperty(exports, "translatePostgresExecutionToApiExecution", { enumerable: true, get: function () { return executions_1.translatePostgresExecutionToApiExecution; } });
78
79
  var granules_1 = require("./translate/granules");
79
80
  Object.defineProperty(exports, "translateApiGranuleToPostgresGranule", { enumerable: true, get: function () { return granules_1.translateApiGranuleToPostgresGranule; } });
@@ -99,9 +100,11 @@ Object.defineProperty(exports, "getApiGranuleCumulusIds", { enumerable: true, ge
99
100
  Object.defineProperty(exports, "getApiGranuleExecutionCumulusIds", { enumerable: true, get: function () { return granule_1.getApiGranuleExecutionCumulusIds; } });
100
101
  Object.defineProperty(exports, "getGranuleCollectionId", { enumerable: true, get: function () { return granule_1.getGranuleCollectionId; } });
101
102
  Object.defineProperty(exports, "getUniqueGranuleByGranuleId", { enumerable: true, get: function () { return granule_1.getUniqueGranuleByGranuleId; } });
103
+ Object.defineProperty(exports, "getGranuleByUniqueColumns", { enumerable: true, get: function () { return granule_1.getGranuleByUniqueColumns; } });
102
104
  Object.defineProperty(exports, "upsertGranuleWithExecutionJoinRecord", { enumerable: true, get: function () { return granule_1.upsertGranuleWithExecutionJoinRecord; } });
103
105
  Object.defineProperty(exports, "getGranulesByApiPropertiesQuery", { enumerable: true, get: function () { return granule_1.getGranulesByApiPropertiesQuery; } });
104
106
  Object.defineProperty(exports, "getGranulesByGranuleId", { enumerable: true, get: function () { return granule_1.getGranulesByGranuleId; } });
107
+ Object.defineProperty(exports, "getGranuleAndCollection", { enumerable: true, get: function () { return granule_1.getGranuleAndCollection; } });
105
108
  var QuerySearchClient_1 = require("./lib/QuerySearchClient");
106
109
  Object.defineProperty(exports, "QuerySearchClient", { enumerable: true, get: function () { return QuerySearchClient_1.QuerySearchClient; } });
107
110
  var async_operation_1 = require("./models/async_operation");
@@ -50,6 +50,17 @@ export declare const getApiGranuleCumulusIds: (knexOrTransaction: Knex | Knex.Tr
50
50
  * @returns {Promise<PostgresGranuleRecord>}
51
51
  */
52
52
  export declare const getUniqueGranuleByGranuleId: (knexOrTransaction: Knex | Knex.Transaction, granuleId: string, granulePgModel?: GranulePgModel) => Promise<PostgresGranuleRecord>;
53
+ /**
54
+ * Get one Granule for a granule_id and collection_cumulus_id
55
+ *
56
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
57
+ * DB client or transaction
58
+ * @param {string} granuleId - a granule.granule_id
59
+ * @param {number} collectionCumulusId - a granule.collection_cumulus_id
60
+ * @param {GranulePgModel} granulePgModel - Granule PG model class instance
61
+ * @returns {Promise<PostgresGranuleRecord>}
62
+ */
63
+ export declare const getGranuleByUniqueColumns: (knexOrTransaction: Knex | Knex.Transaction, granuleId: string, collectionCumulusId: number, granulePgModel?: GranulePgModel) => Promise<PostgresGranuleRecord>;
53
64
  /**
54
65
  * Get cumulus IDs for all executions associated to a set of granules
55
66
  *
@@ -86,6 +97,19 @@ export declare const getGranulesByApiPropertiesQuery: (knex: Knex, { collectionI
86
97
  updatedAtRange?: UpdatedAtRange | undefined;
87
98
  status?: string | undefined;
88
99
  }, sortByFields?: string | string[]) => Knex.QueryBuilder;
100
+ /**
101
+ * Get Postgres Granule and Collection objects for a granuleId + collectionId
102
+ *
103
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
104
+ * DB client or transaction
105
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
106
+ * @param {Object} [granulePgModel] - Granule PG model class instance
107
+ * @param {String} granuleId - primary ID for granule record
108
+ * @param {String} collectionId - collection ID in 'name___version' format
109
+ * @returns {Promise<object>} an object containing the Postgres Granule,
110
+ * Postgres Collection, and an optional "Not Found" error message
111
+ */
112
+ export declare const getGranuleAndCollection: (knexOrTransaction: Knex | Knex.Transaction, collectionPgModel: CollectionPgModel | undefined, granulePgModel: GranulePgModel | undefined, granuleId: string, collectionId: string) => Promise<object>;
89
113
  /**
90
114
  * Get granules from table where granule_id matches provided granuleId
91
115
  *
@@ -3,13 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getGranulesByGranuleId = exports.getGranulesByApiPropertiesQuery = exports.getApiGranuleExecutionCumulusIds = exports.getUniqueGranuleByGranuleId = exports.getApiGranuleCumulusIds = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleCollectionId = void 0;
6
+ exports.getGranulesByGranuleId = exports.getGranuleAndCollection = exports.getGranulesByApiPropertiesQuery = exports.getApiGranuleExecutionCumulusIds = exports.getGranuleByUniqueColumns = exports.getUniqueGranuleByGranuleId = exports.getApiGranuleCumulusIds = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleCollectionId = void 0;
7
7
  const Collections_1 = require("@cumulus/message/Collections");
8
8
  const errors_1 = require("@cumulus/errors");
9
9
  const logger_1 = __importDefault(require("@cumulus/logger"));
10
10
  const collection_1 = require("../models/collection");
11
11
  const granule_1 = require("../models/granule");
12
12
  const granules_executions_1 = require("../models/granules-executions");
13
+ const { deprecate } = require('@cumulus/common/util');
13
14
  const { TableNames } = require('../tables');
14
15
  const getGranuleCollectionId = async (knexOrTransaction, granule) => {
15
16
  const collectionPgModel = new collection_1.CollectionPgModel();
@@ -89,6 +90,7 @@ exports.getApiGranuleCumulusIds = getApiGranuleCumulusIds;
89
90
  * @returns {Promise<PostgresGranuleRecord>}
90
91
  */
91
92
  const getUniqueGranuleByGranuleId = async (knexOrTransaction, granuleId, granulePgModel = new granule_1.GranulePgModel()) => {
93
+ deprecate('@cumulus/db/getUniqueGranuleByGranuleId', 'RDS-Phase-3', '@cumulus/db/getGranuleByUniqueColumns');
92
94
  const logger = new logger_1.default({ sender: '@cumulus/api/granules' });
93
95
  const PgGranuleRecords = await granulePgModel.search(knexOrTransaction, {
94
96
  granule_id: granuleId,
@@ -103,6 +105,18 @@ const getUniqueGranuleByGranuleId = async (knexOrTransaction, granuleId, granule
103
105
  return PgGranuleRecords[0];
104
106
  };
105
107
  exports.getUniqueGranuleByGranuleId = getUniqueGranuleByGranuleId;
108
+ /**
109
+ * Get one Granule for a granule_id and collection_cumulus_id
110
+ *
111
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
112
+ * DB client or transaction
113
+ * @param {string} granuleId - a granule.granule_id
114
+ * @param {number} collectionCumulusId - a granule.collection_cumulus_id
115
+ * @param {GranulePgModel} granulePgModel - Granule PG model class instance
116
+ * @returns {Promise<PostgresGranuleRecord>}
117
+ */
118
+ const getGranuleByUniqueColumns = (knexOrTransaction, granuleId, collectionCumulusId, granulePgModel = new granule_1.GranulePgModel()) => granulePgModel.get(knexOrTransaction, { granule_id: granuleId, collection_cumulus_id: collectionCumulusId });
119
+ exports.getGranuleByUniqueColumns = getGranuleByUniqueColumns;
106
120
  /**
107
121
  * Get cumulus IDs for all executions associated to a set of granules
108
122
  *
@@ -182,6 +196,46 @@ const getGranulesByApiPropertiesQuery = (knex, { collectionIds, granuleIds, prov
182
196
  .groupBy(`${providersTable}.cumulus_id`);
183
197
  };
184
198
  exports.getGranulesByApiPropertiesQuery = getGranulesByApiPropertiesQuery;
199
+ /**
200
+ * Get Postgres Granule and Collection objects for a granuleId + collectionId
201
+ *
202
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
203
+ * DB client or transaction
204
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
205
+ * @param {Object} [granulePgModel] - Granule PG model class instance
206
+ * @param {String} granuleId - primary ID for granule record
207
+ * @param {String} collectionId - collection ID in 'name___version' format
208
+ * @returns {Promise<object>} an object containing the Postgres Granule,
209
+ * Postgres Collection, and an optional "Not Found" error message
210
+ */
211
+ const getGranuleAndCollection = async (knexOrTransaction, collectionPgModel = new collection_1.CollectionPgModel(), granulePgModel = new granule_1.GranulePgModel(), granuleId, collectionId) => {
212
+ let pgGranule;
213
+ let pgCollection;
214
+ let notFoundError;
215
+ try {
216
+ pgCollection = await collectionPgModel.get(knexOrTransaction, (0, Collections_1.deconstructCollectionId)(collectionId));
217
+ pgGranule = await granulePgModel.get(knexOrTransaction, { granule_id: granuleId, collection_cumulus_id: pgCollection.cumulus_id });
218
+ }
219
+ catch (error) {
220
+ if (error instanceof errors_1.RecordDoesNotExist) {
221
+ if (collectionId && pgCollection === undefined) {
222
+ notFoundError = `No collection found for granuleId ${granuleId} with collectionId ${collectionId}`;
223
+ }
224
+ else {
225
+ notFoundError = 'Granule not found';
226
+ }
227
+ }
228
+ else {
229
+ throw error;
230
+ }
231
+ }
232
+ return {
233
+ pgGranule,
234
+ pgCollection,
235
+ notFoundError,
236
+ };
237
+ };
238
+ exports.getGranuleAndCollection = getGranuleAndCollection;
185
239
  /**
186
240
  * Get granules from table where granule_id matches provided granuleId
187
241
  *
@@ -18,7 +18,6 @@ const up = async (knex) => {
18
18
  'Dead-Letter Processing',
19
19
  'Kinesis Replay',
20
20
  'Reconciliation Report',
21
- 'Migration Count Report',
22
21
  'Data Migration',
23
22
  'SQS Replay',
24
23
  ]));
@@ -33,7 +32,6 @@ const down = async (knex) => {
33
32
  'Dead-Letter Processing',
34
33
  'Kinesis Replay',
35
34
  'Reconciliation Report',
36
- 'Migration Count Report',
37
35
  'Data Migration',
38
36
  ]));
39
37
  };
@@ -15,7 +15,7 @@ class AsyncOperationPgModel extends base_1.BasePgModel {
15
15
  .insert(asyncOperation)
16
16
  .onConflict('id')
17
17
  .merge()
18
- .returning('cumulus_id');
18
+ .returning('*');
19
19
  }
20
20
  }
21
21
  exports.AsyncOperationPgModel = AsyncOperationPgModel;
@@ -5,7 +5,7 @@ declare class ExecutionPgModel extends BasePgModel<PostgresExecution, PostgresEx
5
5
  constructor();
6
6
  static nonActiveStatuses: string[];
7
7
  create(knexOrTransaction: Knex | Knex.Transaction, item: PostgresExecution): Promise<unknown[] | Object[]>;
8
- upsert(knexOrTrx: Knex | Knex.Transaction, execution: PostgresExecution): Promise<any[]>;
8
+ upsert(knexOrTrx: Knex | Knex.Transaction, execution: PostgresExecution, writeConstraints?: boolean): Promise<any[]>;
9
9
  /**
10
10
  * Get executions from the execution cumulus_id
11
11
  *
@@ -13,8 +13,8 @@ class ExecutionPgModel extends base_1.BasePgModel {
13
13
  create(knexOrTransaction, item) {
14
14
  return super.create(knexOrTransaction, item, '*');
15
15
  }
16
- async upsert(knexOrTrx, execution) {
17
- if (execution.status === 'running') {
16
+ async upsert(knexOrTrx, execution, writeConstraints = true) {
17
+ if (writeConstraints && execution.status === 'running') {
18
18
  return await knexOrTrx(this.tableName)
19
19
  .insert(execution)
20
20
  .onConflict('arn')
@@ -5,8 +5,8 @@ declare class ProviderPgModel extends BasePgModel<PostgresProvider, PostgresProv
5
5
  constructor();
6
6
  upsert(knexOrTransaction: Knex | Knex.Transaction, provider: PostgresProvider): Knex.QueryBuilder<any, {
7
7
  _base: any;
8
- _hasSelection: true;
9
- _keys: "cumulus_id";
8
+ _hasSelection: false;
9
+ _keys: never;
10
10
  _aliases: {};
11
11
  _single: false;
12
12
  _intersectProps: {};
@@ -14,7 +14,7 @@ class ProviderPgModel extends base_1.BasePgModel {
14
14
  .insert(provider)
15
15
  .onConflict('name')
16
16
  .merge()
17
- .returning('cumulus_id');
17
+ .returning('*');
18
18
  }
19
19
  }
20
20
  exports.ProviderPgModel = ProviderPgModel;
@@ -3,10 +3,11 @@ import { BasePgModel } from './base';
3
3
  import { PostgresRule, PostgresRuleRecord } from '../types/rule';
4
4
  declare class RulePgModel extends BasePgModel<PostgresRule, PostgresRuleRecord> {
5
5
  constructor();
6
+ create(knexOrTransaction: Knex | Knex.Transaction, item: PostgresRule): Promise<unknown[] | Object[]>;
6
7
  upsert(knexOrTransaction: Knex | Knex.Transaction, rule: PostgresRule): Knex.QueryBuilder<any, {
7
8
  _base: any;
8
- _hasSelection: true;
9
- _keys: "cumulus_id";
9
+ _hasSelection: false;
10
+ _keys: never;
10
11
  _aliases: {};
11
12
  _single: false;
12
13
  _intersectProps: {};
@@ -9,12 +9,15 @@ class RulePgModel extends base_1.BasePgModel {
9
9
  tableName: tables_1.TableNames.rules,
10
10
  });
11
11
  }
12
+ create(knexOrTransaction, item) {
13
+ return super.create(knexOrTransaction, item, '*');
14
+ }
12
15
  upsert(knexOrTransaction, rule) {
13
16
  return knexOrTransaction(this.tableName)
14
17
  .insert(rule)
15
18
  .onConflict('name')
16
19
  .merge()
17
- .returning('cumulus_id');
20
+ .returning('*');
18
21
  }
19
22
  }
20
23
  exports.RulePgModel = RulePgModel;
@@ -8,6 +8,7 @@ const crypto_random_string_1 = __importDefault(require("crypto-random-string"));
8
8
  const uuid_1 = require("uuid");
9
9
  const p_retry_1 = __importDefault(require("p-retry"));
10
10
  const Executions_1 = require("@cumulus/message/Executions");
11
+ const test_utils_1 = require("@cumulus/common/test-utils");
11
12
  const connection_1 = require("./connection");
12
13
  const config_1 = require("./config");
13
14
  const createTestDatabase = async (knex, dbName, dbUser) => {
@@ -40,7 +41,7 @@ const destroyLocalTestDb = async ({ knex, knexAdmin, testDbName, }) => {
40
41
  exports.destroyLocalTestDb = destroyLocalTestDb;
41
42
  const fakeRuleRecordFactory = (params) => ({
42
43
  name: (0, crypto_random_string_1.default)({ length: 8 }),
43
- workflow: 'Random Workflow',
44
+ workflow: (0, test_utils_1.randomId)('workflow'),
44
45
  type: 'onetime',
45
46
  enabled: false,
46
47
  created_at: new Date(),
@@ -82,6 +83,8 @@ const fakeProviderRecordFactory = (params) => ({
82
83
  name: `provider${(0, crypto_random_string_1.default)({ length: 5 })}`,
83
84
  host: 'test-bucket',
84
85
  protocol: 's3',
86
+ created_at: new Date(),
87
+ updated_at: new Date(),
85
88
  ...params,
86
89
  });
87
90
  exports.fakeProviderRecordFactory = fakeProviderRecordFactory;
@@ -1,16 +1,16 @@
1
1
  import { Knex } from 'knex';
2
- import { ExecutionRecord } from '@cumulus/types/api/executions';
2
+ import { ApiExecution, ApiExecutionRecord } from '@cumulus/types/api/executions';
3
3
  import { PostgresExecution, PostgresExecutionRecord } from '../types/execution';
4
4
  import { ExecutionPgModel } from '../models/execution';
5
5
  import { CollectionPgModel } from '../models/collection';
6
6
  import { AsyncOperationPgModel } from '../models/async_operation';
7
- export declare const translatePostgresExecutionToApiExecution: (executionRecord: PostgresExecutionRecord, knex: Knex, collectionPgModel?: CollectionPgModel, asyncOperationPgModel?: AsyncOperationPgModel, executionPgModel?: ExecutionPgModel) => Promise<ExecutionRecord>;
7
+ export declare const translatePostgresExecutionToApiExecution: (executionRecord: PostgresExecutionRecord, knex: Knex, collectionPgModel?: CollectionPgModel, asyncOperationPgModel?: AsyncOperationPgModel, executionPgModel?: ExecutionPgModel) => Promise<ApiExecutionRecord>;
8
8
  /**
9
- * Translate execution record from Dynamo to RDS.
9
+ * Translate execution record from API to RDS.
10
10
  *
11
- * @param {AWS.DynamoDB.DocumentClient.AttributeMap} dynamoRecord
12
- * Source record from DynamoDB
13
- * @param {AWS.DynamoDB.DocumentClient.AttributeMap} knex
11
+ * @param {ApiExecution} apiRecord
12
+ * Source record from API
13
+ * @param {Knex} knex
14
14
  * Knex client
15
15
  * @param {Object} collectionPgModel
16
16
  * Instance of the collection database model
@@ -20,5 +20,6 @@ export declare const translatePostgresExecutionToApiExecution: (executionRecord:
20
20
  * Instance of the execution database model
21
21
  * @returns {PostgresExecutionRecord} - converted Execution
22
22
  */
23
- export declare const translateApiExecutionToPostgresExecution: (dynamoRecord: ExecutionRecord, knex: Knex, collectionPgModel?: CollectionPgModel, asyncOperationPgModel?: AsyncOperationPgModel, executionPgModel?: ExecutionPgModel) => Promise<PostgresExecution>;
23
+ export declare const translateApiExecutionToPostgresExecutionWithoutNilsRemoved: (apiRecord: ApiExecution, knex: Knex, collectionPgModel?: CollectionPgModel, asyncOperationPgModel?: AsyncOperationPgModel, executionPgModel?: ExecutionPgModel) => Promise<PostgresExecution>;
24
+ export declare const translateApiExecutionToPostgresExecution: (apiRecord: ApiExecution, knex: Knex, collectionPgModel?: CollectionPgModel, asyncOperationPgModel?: AsyncOperationPgModel, executionPgModel?: ExecutionPgModel) => Promise<PostgresExecution>;
24
25
  //# sourceMappingURL=executions.d.ts.map
@@ -3,10 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.translateApiExecutionToPostgresExecution = exports.translatePostgresExecutionToApiExecution = void 0;
6
+ exports.translateApiExecutionToPostgresExecution = exports.translateApiExecutionToPostgresExecutionWithoutNilsRemoved = exports.translatePostgresExecutionToApiExecution = void 0;
7
+ const isNil_1 = __importDefault(require("lodash/isNil"));
8
+ const isNull_1 = __importDefault(require("lodash/isNull"));
7
9
  const errors_1 = require("@cumulus/errors");
8
10
  const logger_1 = __importDefault(require("@cumulus/logger"));
9
11
  const util_1 = require("@cumulus/common/util");
12
+ const errors_2 = require("@cumulus/errors");
10
13
  const Collections_1 = require("@cumulus/message/Collections");
11
14
  const execution_1 = require("../models/execution");
12
15
  const collection_1 = require("../models/collection");
@@ -60,11 +63,30 @@ const translatePostgresExecutionToApiExecution = async (executionRecord, knex, c
60
63
  };
61
64
  exports.translatePostgresExecutionToApiExecution = translatePostgresExecutionToApiExecution;
62
65
  /**
63
- * Translate execution record from Dynamo to RDS.
66
+ * Validate translation api record doesn't contain invalid null/undefined values based
67
+ * on PostgresExecution typings. Throw if invalid nulls detected
64
68
  *
65
- * @param {AWS.DynamoDB.DocumentClient.AttributeMap} dynamoRecord
66
- * Source record from DynamoDB
67
- * @param {AWS.DynamoDB.DocumentClient.AttributeMap} knex
69
+ * @param {ApiExecution} apiExecution
70
+ * Record from api
71
+ * @returns {undefined}
72
+ */
73
+ const validateApiToPostgresExecutionObject = (apiExecution) => {
74
+ if ((0, isNil_1.default)(apiExecution.arn)) {
75
+ throw new errors_2.ValidationError('arn cannot be undefined on a execution, executions must have a arn and a name');
76
+ }
77
+ if ((0, isNil_1.default)(apiExecution.name)) {
78
+ throw new errors_2.ValidationError('name cannot be undefined on a execution, executions must have a arn and a name');
79
+ }
80
+ if ((0, isNull_1.default)(apiExecution.status)) {
81
+ throw new errors_2.ValidationError('status cannot be null on a execution, executions must have a arn and a name');
82
+ }
83
+ };
84
+ /**
85
+ * Translate execution record from API to RDS.
86
+ *
87
+ * @param {ApiExecution} apiRecord
88
+ * Source record from API
89
+ * @param {Knex} knex
68
90
  * Knex client
69
91
  * @param {Object} collectionPgModel
70
92
  * Instance of the collection database model
@@ -74,34 +96,38 @@ exports.translatePostgresExecutionToApiExecution = translatePostgresExecutionToA
74
96
  * Instance of the execution database model
75
97
  * @returns {PostgresExecutionRecord} - converted Execution
76
98
  */
77
- const translateApiExecutionToPostgresExecution = async (dynamoRecord, knex, collectionPgModel = new collection_1.CollectionPgModel(), asyncOperationPgModel = new async_operation_1.AsyncOperationPgModel(), executionPgModel = new execution_1.ExecutionPgModel()) => {
99
+ const translateApiExecutionToPostgresExecutionWithoutNilsRemoved = async (apiRecord, knex, collectionPgModel = new collection_1.CollectionPgModel(), asyncOperationPgModel = new async_operation_1.AsyncOperationPgModel(), executionPgModel = new execution_1.ExecutionPgModel()) => {
78
100
  const logger = new logger_1.default({ sender: '@cumulus/db/translate/executions' });
101
+ validateApiToPostgresExecutionObject(apiRecord);
79
102
  // Map old record to new schema.
80
103
  const translatedRecord = {
81
- async_operation_cumulus_id: (dynamoRecord.asyncOperationId ? await asyncOperationPgModel.getRecordCumulusId(knex, { id: dynamoRecord.asyncOperationId }) : undefined),
82
- status: dynamoRecord.status,
83
- arn: dynamoRecord.arn,
84
- duration: dynamoRecord.duration,
85
- error: dynamoRecord.error,
86
- tasks: dynamoRecord.tasks,
87
- original_payload: dynamoRecord.originalPayload,
88
- final_payload: dynamoRecord.finalPayload,
89
- workflow_name: dynamoRecord.type,
90
- url: dynamoRecord.execution,
91
- cumulus_version: dynamoRecord.cumulusVersion,
92
- timestamp: dynamoRecord.timestamp ? new Date(dynamoRecord.timestamp) : undefined,
93
- created_at: dynamoRecord.createdAt ? new Date(dynamoRecord.createdAt) : undefined,
94
- updated_at: dynamoRecord.updatedAt ? new Date(dynamoRecord.updatedAt) : undefined,
104
+ async_operation_cumulus_id: (apiRecord.asyncOperationId ? await asyncOperationPgModel.getRecordCumulusId(knex, { id: apiRecord.asyncOperationId }) : ((0, isNull_1.default)(apiRecord.asyncOperationId) ? null : undefined)),
105
+ status: apiRecord.status,
106
+ arn: apiRecord.arn,
107
+ duration: apiRecord.duration,
108
+ error: apiRecord.error,
109
+ tasks: apiRecord.tasks,
110
+ original_payload: apiRecord.originalPayload,
111
+ final_payload: apiRecord.finalPayload,
112
+ workflow_name: apiRecord.type,
113
+ url: apiRecord.execution,
114
+ cumulus_version: apiRecord.cumulusVersion,
115
+ timestamp: (0, util_1.returnNullOrUndefinedOrDate)(apiRecord.timestamp),
116
+ created_at: (0, util_1.returnNullOrUndefinedOrDate)(apiRecord.createdAt),
117
+ updated_at: (0, util_1.returnNullOrUndefinedOrDate)(apiRecord.updatedAt),
95
118
  };
96
- if (dynamoRecord.collectionId !== undefined) {
97
- const { name, version } = (0, Collections_1.deconstructCollectionId)(dynamoRecord.collectionId);
119
+ if (!(0, isNil_1.default)(apiRecord.collectionId)) {
120
+ const { name, version } = (0, Collections_1.deconstructCollectionId)(apiRecord.collectionId);
98
121
  translatedRecord.collection_cumulus_id = await collectionPgModel.getRecordCumulusId(knex, { name, version });
99
122
  }
123
+ else if ((0, isNull_1.default)(apiRecord.collectionId)) {
124
+ translatedRecord.collection_cumulus_id = null;
125
+ }
100
126
  // If we have a parentArn, try a lookup in Postgres. If there's a match, set the parent_cumulus_id
101
- if (dynamoRecord.parentArn !== undefined) {
127
+ if (!(0, isNil_1.default)(apiRecord.parentArn)) {
102
128
  let parentId;
103
129
  try {
104
- parentId = await executionPgModel.getRecordCumulusId(knex, { arn: dynamoRecord.parentArn });
130
+ parentId = await executionPgModel.getRecordCumulusId(knex, { arn: apiRecord.parentArn });
105
131
  if (parentId !== undefined) {
106
132
  translatedRecord.parent_cumulus_id = parentId;
107
133
  }
@@ -115,7 +141,12 @@ const translateApiExecutionToPostgresExecution = async (dynamoRecord, knex, coll
115
141
  }
116
142
  }
117
143
  }
118
- return (0, util_1.removeNilProperties)(translatedRecord);
144
+ else if ((0, isNull_1.default)(apiRecord.parentArn)) {
145
+ translatedRecord.parent_cumulus_id = null;
146
+ }
147
+ return translatedRecord;
119
148
  };
149
+ exports.translateApiExecutionToPostgresExecutionWithoutNilsRemoved = translateApiExecutionToPostgresExecutionWithoutNilsRemoved;
150
+ const translateApiExecutionToPostgresExecution = async (apiRecord, knex, collectionPgModel = new collection_1.CollectionPgModel(), asyncOperationPgModel = new async_operation_1.AsyncOperationPgModel(), executionPgModel = new execution_1.ExecutionPgModel()) => (0, util_1.removeNilProperties)(await (0, exports.translateApiExecutionToPostgresExecutionWithoutNilsRemoved)(apiRecord, knex, collectionPgModel, asyncOperationPgModel, executionPgModel));
120
151
  exports.translateApiExecutionToPostgresExecution = translateApiExecutionToPostgresExecution;
121
152
  //# sourceMappingURL=executions.js.map
@@ -81,7 +81,6 @@ const translatePostgresGranuleToApiGranule = async ({ granulePgRecord, collectio
81
81
  return apiGranule;
82
82
  };
83
83
  exports.translatePostgresGranuleToApiGranule = translatePostgresGranuleToApiGranule;
84
- const returnNullOrUndefinedOrDate = (dateVal) => ((0, isNil_1.default)(dateVal) ? dateVal : new Date(dateVal));
85
84
  /**
86
85
  * Validate translation request doesn't contain invalid null files based
87
86
  * on onPostgresGranule typings. Throw if invalid nulls detected
@@ -154,15 +153,15 @@ const translateApiGranuleToPostgresGranuleWithoutNilsRemoved = async ({ dynamoRe
154
153
  pdr_cumulus_id,
155
154
  provider_cumulus_id,
156
155
  query_fields: dynamoRecord.queryFields,
157
- beginning_date_time: returnNullOrUndefinedOrDate(dynamoRecord.beginningDateTime),
158
- ending_date_time: returnNullOrUndefinedOrDate(dynamoRecord.endingDateTime),
159
- last_update_date_time: returnNullOrUndefinedOrDate(dynamoRecord.lastUpdateDateTime),
160
- processing_end_date_time: returnNullOrUndefinedOrDate(dynamoRecord.processingEndDateTime),
161
- processing_start_date_time: returnNullOrUndefinedOrDate(dynamoRecord.processingStartDateTime),
162
- production_date_time: returnNullOrUndefinedOrDate(dynamoRecord.productionDateTime),
163
- timestamp: returnNullOrUndefinedOrDate(dynamoRecord.timestamp),
164
- created_at: returnNullOrUndefinedOrDate(dynamoRecord.createdAt),
165
- updated_at: returnNullOrUndefinedOrDate(dynamoRecord.updatedAt),
156
+ beginning_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.beginningDateTime),
157
+ ending_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.endingDateTime),
158
+ last_update_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.lastUpdateDateTime),
159
+ processing_end_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.processingEndDateTime),
160
+ processing_start_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.processingStartDateTime),
161
+ production_date_time: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.productionDateTime),
162
+ timestamp: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.timestamp),
163
+ created_at: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.createdAt),
164
+ updated_at: (0, util_1.returnNullOrUndefinedOrDate)(dynamoRecord.updatedAt),
166
165
  };
167
166
  return granuleRecord;
168
167
  };
@@ -1,21 +1,21 @@
1
1
  import { ExecutionRecordStatus } from '@cumulus/types/api/executions';
2
2
  export interface PostgresExecution {
3
3
  arn: string;
4
- async_operation_cumulus_id?: number;
5
- collection_cumulus_id?: number;
6
- created_at?: Date;
7
- cumulus_version?: string;
8
- duration?: number;
9
- error?: object;
10
- final_payload?: object;
11
- original_payload?: object;
12
- parent_cumulus_id?: number;
13
- status: ExecutionRecordStatus;
14
- tasks?: object;
15
- timestamp?: Date;
16
- updated_at?: Date;
17
- url?: string;
18
- workflow_name?: string;
4
+ async_operation_cumulus_id?: number | null;
5
+ collection_cumulus_id?: number | null;
6
+ created_at?: Date | null;
7
+ cumulus_version?: string | null;
8
+ duration?: number | null;
9
+ error?: object | null;
10
+ final_payload?: object | null;
11
+ original_payload?: object | null;
12
+ parent_cumulus_id?: number | null;
13
+ status?: ExecutionRecordStatus;
14
+ tasks?: object | null;
15
+ timestamp?: Date | null;
16
+ updated_at?: Date | null;
17
+ url?: string | null;
18
+ workflow_name?: string | null;
19
19
  }
20
20
  export interface PostgresExecutionRecord extends PostgresExecution {
21
21
  created_at: Date;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/db",
3
- "version": "15.0.3",
3
+ "version": "16.0.0",
4
4
  "description": "Utilities for working with the Cumulus DB",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -29,22 +29,22 @@
29
29
  "node": ">=16.19.0"
30
30
  },
31
31
  "dependencies": {
32
- "@cumulus/aws-client": "15.0.3",
33
- "@cumulus/common": "15.0.3",
34
- "@cumulus/errors": "15.0.3",
35
- "@cumulus/logger": "15.0.3",
36
- "@cumulus/message": "15.0.3",
37
- "@cumulus/types": "15.0.3",
32
+ "@cumulus/aws-client": "16.0.0",
33
+ "@cumulus/common": "16.0.0",
34
+ "@cumulus/errors": "16.0.0",
35
+ "@cumulus/logger": "16.0.0",
36
+ "@cumulus/message": "16.0.0",
37
+ "@cumulus/types": "16.0.0",
38
38
  "crypto-random-string": "^3.2.0",
39
39
  "is-valid-hostname": "1.0.2",
40
40
  "knex": "2.4.1",
41
41
  "lodash": "^4.17.21",
42
- "pg": "^8.3.0",
42
+ "pg": "~8.10",
43
43
  "snake-camel": "^1.0.6",
44
44
  "uuid": "8.3.2"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/uuid": "^8.0.0"
48
48
  },
49
- "gitHead": "5fd5febe740ef96f365d1defc7b9f76ae64c9cc0"
49
+ "gitHead": "f73d94f869ff8c234db26cb0ec939b50b3de1d19"
50
50
  }