@cumulus/db 21.3.1-alpha.0 → 21.3.1

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
@@ -30,7 +30,7 @@ export { translateApiReconReportToPostgresReconReport, translatePostgresReconRep
30
30
  export { getCollectionsByGranuleIds, getUniqueCollectionsByGranuleFilter, } from './lib/collection';
31
31
  export { batchDeleteExecutionFromDatabaseByCumulusCollectionId, executionArnsFromGranuleIdsAndWorkflowNames, getApiExecutionCumulusIds, getApiGranuleExecutionCumulusIdsByExecution, getExecutionInfoByGranuleCumulusId, getWorkflowNameIntersectFromGranuleIds, newestExecutionArnFromGranuleIdWorkflowName, } from './lib/execution';
32
32
  export { getFilesAndGranuleInfoQuery, getGranuleIdAndCollectionIdFromFile, } from './lib/file';
33
- export { getApiGranuleCumulusIds, getApiGranuleExecutionCumulusIds, getGranuleCollectionId, upsertGranuleWithExecutionJoinRecord, getGranulesByApiPropertiesQuery, getGranulesByGranuleId, getGranuleAndCollection, updateBatchGranulesCollection, } from './lib/granule';
33
+ export { getApiGranuleCumulusIds, getApiGranuleExecutionCumulusIds, getGranuleCollectionId, getUniqueGranuleByGranuleId, getGranuleByUniqueColumns, upsertGranuleWithExecutionJoinRecord, getGranulesByApiPropertiesQuery, getGranulesByGranuleId, getGranuleAndCollection, updateBatchGranulesCollection, } from './lib/granule';
34
34
  export { QuerySearchClient, } from './lib/QuerySearchClient';
35
35
  export { AsyncOperationSearch, } from './search/AsyncOperationSearch';
36
36
  export { CollectionSearch, } from './search/CollectionSearch';
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.getApiExecutionCumulusIds = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.batchDeleteExecutionFromDatabaseByCumulusCollectionId = exports.getUniqueCollectionsByGranuleFilter = exports.getCollectionsByGranuleIds = exports.translatePostgresReconReportToApiReconReport = exports.translateApiReconReportToPostgresReconReport = 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.fakeReconciliationReportRecordFactory = 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.ReconciliationReportPgModel = exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.ReconciliationReportSearch = exports.StatsSearch = exports.RuleSearch = exports.ProviderSearch = exports.PdrSearch = exports.GranuleSearch = exports.ExecutionSearch = exports.CollectionSearch = exports.AsyncOperationSearch = exports.QuerySearchClient = exports.updateBatchGranulesCollection = exports.getGranuleAndCollection = exports.getGranulesByGranuleId = exports.getGranulesByApiPropertiesQuery = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleCollectionId = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = exports.getGranuleIdAndCollectionIdFromFile = exports.getFilesAndGranuleInfoQuery = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.getWorkflowNameIntersectFromGranuleIds = exports.getExecutionInfoByGranuleCumulusId = exports.getApiGranuleExecutionCumulusIdsByExecution = void 0;
27
+ exports.RulePgModel = exports.ReconciliationReportPgModel = exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.ReconciliationReportSearch = exports.StatsSearch = exports.RuleSearch = exports.ProviderSearch = exports.PdrSearch = exports.GranuleSearch = exports.ExecutionSearch = exports.CollectionSearch = exports.AsyncOperationSearch = exports.QuerySearchClient = exports.updateBatchGranulesCollection = exports.getGranuleAndCollection = exports.getGranulesByGranuleId = exports.getGranulesByApiPropertiesQuery = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleByUniqueColumns = exports.getUniqueGranuleByGranuleId = exports.getGranuleCollectionId = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = exports.getGranuleIdAndCollectionIdFromFile = exports.getFilesAndGranuleInfoQuery = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.getWorkflowNameIntersectFromGranuleIds = exports.getExecutionInfoByGranuleCumulusId = exports.getApiGranuleExecutionCumulusIdsByExecution = 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; } });
@@ -106,6 +106,8 @@ var granule_1 = require("./lib/granule");
106
106
  Object.defineProperty(exports, "getApiGranuleCumulusIds", { enumerable: true, get: function () { return granule_1.getApiGranuleCumulusIds; } });
107
107
  Object.defineProperty(exports, "getApiGranuleExecutionCumulusIds", { enumerable: true, get: function () { return granule_1.getApiGranuleExecutionCumulusIds; } });
108
108
  Object.defineProperty(exports, "getGranuleCollectionId", { enumerable: true, get: function () { return granule_1.getGranuleCollectionId; } });
109
+ Object.defineProperty(exports, "getUniqueGranuleByGranuleId", { enumerable: true, get: function () { return granule_1.getUniqueGranuleByGranuleId; } });
110
+ Object.defineProperty(exports, "getGranuleByUniqueColumns", { enumerable: true, get: function () { return granule_1.getGranuleByUniqueColumns; } });
109
111
  Object.defineProperty(exports, "upsertGranuleWithExecutionJoinRecord", { enumerable: true, get: function () { return granule_1.upsertGranuleWithExecutionJoinRecord; } });
110
112
  Object.defineProperty(exports, "getGranulesByApiPropertiesQuery", { enumerable: true, get: function () { return granule_1.getGranulesByApiPropertiesQuery; } });
111
113
  Object.defineProperty(exports, "getGranulesByGranuleId", { enumerable: true, get: function () { return granule_1.getGranulesByGranuleId; } });
@@ -31,23 +31,52 @@ export declare const upsertGranuleWithExecutionJoinRecord: ({ knexTransaction, g
31
31
  *
32
32
  * @param {Knex | Knex.Transaction} knexOrTransaction -
33
33
  * DB client or transaction
34
- * @param {Array<string>} granules - array of granuleIds
34
+ * @param {Array<Object>} granules - array of granules with collectionId and granuleId
35
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
35
36
  * @param {Object} [granulePgModel] - Granule PG model class instance
36
37
  * @returns {Promise<number[]>}
37
38
  */
38
- export declare const getApiGranuleCumulusIds: (knexOrTransaction: Knex | Knex.Transaction, granules: Array<string>, granulePgModel?: GranulePgModel) => Promise<number[]>;
39
+ export declare const getApiGranuleCumulusIds: (knexOrTransaction: Knex | Knex.Transaction, granules: Array<{
40
+ collectionId: string;
41
+ granuleId: string;
42
+ }>, collectionPgModel?: CollectionPgModel, granulePgModel?: GranulePgModel) => Promise<number[]>;
43
+ /**
44
+ * Get one Granule for a granule_id. If more than one or none are found, throw an error
45
+ *
46
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
47
+ * DB client or transaction
48
+ * @param {string} granuleId - a Granule ID
49
+ * @param {GranulePgModel} granulePgModel - Granule PG model class instance
50
+ * @returns {Promise<PostgresGranuleRecord>}
51
+ */
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>;
39
64
  /**
40
65
  * Get cumulus IDs for all executions associated to a set of granules
41
66
  *
42
67
  * @param {Knex | Knex.Transaction} knexOrTransaction -
43
68
  * DB client or transaction
44
- * @param {Array<string>} granules - array of granuleIds
69
+ * @param {Array<Object>} granules - array of granules with collectionId and granuleId
70
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
45
71
  * @param {Object} [granulePgModel] - Granule PG model class instance
46
72
  * @param {Object} [granulesExecutionsPgModel]
47
73
  * Granules/executions PG model class instance
48
74
  * @returns {Promise<number[]>}
49
75
  */
50
- export declare const getApiGranuleExecutionCumulusIds: (knexOrTransaction: Knex | Knex.Transaction, granules: Array<string>, granulePgModel?: GranulePgModel, granulesExecutionsPgModel?: GranulesExecutionsPgModel) => Promise<Array<number>>;
76
+ export declare const getApiGranuleExecutionCumulusIds: (knexOrTransaction: Knex | Knex.Transaction, granules: Array<{
77
+ collectionId: string;
78
+ granuleId: string;
79
+ }>, collectionPgModel?: CollectionPgModel, granulePgModel?: GranulePgModel, granulesExecutionsPgModel?: GranulesExecutionsPgModel) => Promise<Array<number>>;
51
80
  /**
52
81
  * Helper to build a query to search granules by various API granule record properties.
53
82
  */
@@ -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.updateBatchGranulesCollection = exports.getGranulesByGranuleId = exports.getGranuleAndCollection = exports.getGranulesByApiPropertiesQuery = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = exports.upsertGranuleWithExecutionJoinRecord = exports.getGranuleCollectionId = void 0;
6
+ exports.updateBatchGranulesCollection = 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 log = new logger_1.default({ sender: '@cumulus/db/lib/granules' });
15
16
  const getGranuleCollectionId = async (knexOrTransaction, granule) => {
@@ -57,30 +58,80 @@ exports.upsertGranuleWithExecutionJoinRecord = upsertGranuleWithExecutionJoinRec
57
58
  *
58
59
  * @param {Knex | Knex.Transaction} knexOrTransaction -
59
60
  * DB client or transaction
60
- * @param {Array<string>} granules - array of granuleIds
61
+ * @param {Array<Object>} granules - array of granules with collectionId and granuleId
62
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
61
63
  * @param {Object} [granulePgModel] - Granule PG model class instance
62
64
  * @returns {Promise<number[]>}
63
65
  */
64
- const getApiGranuleCumulusIds = async (knexOrTransaction, granules, granulePgModel = new granule_1.GranulePgModel()) => {
65
- const granuleCumulusIds = await Promise.all(granules.map(async (granuleId) => granulePgModel.getRecordCumulusId(knexOrTransaction, {
66
- granule_id: granuleId,
67
- })));
66
+ const getApiGranuleCumulusIds = async (knexOrTransaction, granules, collectionPgModel = new collection_1.CollectionPgModel(), granulePgModel = new granule_1.GranulePgModel()) => {
67
+ const collectionMap = {};
68
+ const granuleCumulusIds = await Promise.all(granules.map(async (granule) => {
69
+ const { collectionId } = granule;
70
+ let collectionCumulusId = collectionMap[collectionId];
71
+ if (!collectionCumulusId) {
72
+ const { name, version } = (0, Collections_1.deconstructCollectionId)(collectionId);
73
+ collectionCumulusId = await collectionPgModel.getRecordCumulusId(knexOrTransaction, { name, version });
74
+ collectionMap[collectionId] = collectionCumulusId;
75
+ }
76
+ return await granulePgModel.getRecordCumulusId(knexOrTransaction, {
77
+ granule_id: granule.granuleId,
78
+ collection_cumulus_id: collectionCumulusId,
79
+ });
80
+ }));
68
81
  return [...new Set(granuleCumulusIds)];
69
82
  };
70
83
  exports.getApiGranuleCumulusIds = getApiGranuleCumulusIds;
84
+ /**
85
+ * Get one Granule for a granule_id. If more than one or none are found, throw an error
86
+ *
87
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
88
+ * DB client or transaction
89
+ * @param {string} granuleId - a Granule ID
90
+ * @param {GranulePgModel} granulePgModel - Granule PG model class instance
91
+ * @returns {Promise<PostgresGranuleRecord>}
92
+ */
93
+ const getUniqueGranuleByGranuleId = async (knexOrTransaction, granuleId, granulePgModel = new granule_1.GranulePgModel()) => {
94
+ deprecate('@cumulus/db/getUniqueGranuleByGranuleId', 'RDS-Phase-3', '@cumulus/db/getGranuleByUniqueColumns');
95
+ const logger = new logger_1.default({ sender: '@cumulus/api/granules' });
96
+ const PgGranuleRecords = await granulePgModel.search(knexOrTransaction, {
97
+ granule_id: granuleId,
98
+ });
99
+ if (PgGranuleRecords.length > 1) {
100
+ logger.warn(`Granule ID ${granuleId} is not unique across collections, cannot make an update action based on granule Id alone`);
101
+ throw new Error(`Failed to write ${granuleId} due to granuleId duplication on postgres granule record`);
102
+ }
103
+ if (PgGranuleRecords.length === 0) {
104
+ throw new errors_1.RecordDoesNotExist(`Granule ${granuleId} does not exist or was already deleted`);
105
+ }
106
+ return PgGranuleRecords[0];
107
+ };
108
+ exports.getUniqueGranuleByGranuleId = getUniqueGranuleByGranuleId;
109
+ /**
110
+ * Get one Granule for a granule_id and collection_cumulus_id
111
+ *
112
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
113
+ * DB client or transaction
114
+ * @param {string} granuleId - a granule.granule_id
115
+ * @param {number} collectionCumulusId - a granule.collection_cumulus_id
116
+ * @param {GranulePgModel} granulePgModel - Granule PG model class instance
117
+ * @returns {Promise<PostgresGranuleRecord>}
118
+ */
119
+ const getGranuleByUniqueColumns = (knexOrTransaction, granuleId, collectionCumulusId, granulePgModel = new granule_1.GranulePgModel()) => granulePgModel.get(knexOrTransaction, { granule_id: granuleId, collection_cumulus_id: collectionCumulusId });
120
+ exports.getGranuleByUniqueColumns = getGranuleByUniqueColumns;
71
121
  /**
72
122
  * Get cumulus IDs for all executions associated to a set of granules
73
123
  *
74
124
  * @param {Knex | Knex.Transaction} knexOrTransaction -
75
125
  * DB client or transaction
76
- * @param {Array<string>} granules - array of granuleIds
126
+ * @param {Array<Object>} granules - array of granules with collectionId and granuleId
127
+ * @param {Object} [collectionPgModel] - Collection PG model class instance
77
128
  * @param {Object} [granulePgModel] - Granule PG model class instance
78
129
  * @param {Object} [granulesExecutionsPgModel]
79
130
  * Granules/executions PG model class instance
80
131
  * @returns {Promise<number[]>}
81
132
  */
82
- const getApiGranuleExecutionCumulusIds = async (knexOrTransaction, granules, granulePgModel = new granule_1.GranulePgModel(), granulesExecutionsPgModel = new granules_executions_1.GranulesExecutionsPgModel()) => {
83
- const granuleCumulusIds = await (0, exports.getApiGranuleCumulusIds)(knexOrTransaction, granules, granulePgModel);
133
+ const getApiGranuleExecutionCumulusIds = async (knexOrTransaction, granules, collectionPgModel = new collection_1.CollectionPgModel(), granulePgModel = new granule_1.GranulePgModel(), granulesExecutionsPgModel = new granules_executions_1.GranulesExecutionsPgModel()) => {
134
+ const granuleCumulusIds = await (0, exports.getApiGranuleCumulusIds)(knexOrTransaction, granules, collectionPgModel, granulePgModel);
84
135
  const executionCumulusIds = await granulesExecutionsPgModel
85
136
  .searchByGranuleCumulusIds(knexOrTransaction, granuleCumulusIds);
86
137
  return executionCumulusIds;
@@ -162,7 +213,7 @@ const getGranuleAndCollection = async (knexOrTransaction, collectionPgModel = ne
162
213
  let notFoundError;
163
214
  try {
164
215
  pgCollection = await collectionPgModel.get(knexOrTransaction, (0, Collections_1.deconstructCollectionId)(collectionId));
165
- pgGranule = await granulePgModel.get(knexOrTransaction, { granule_id: granuleId });
216
+ pgGranule = await granulePgModel.get(knexOrTransaction, { granule_id: granuleId, collection_cumulus_id: pgCollection.cumulus_id });
166
217
  }
167
218
  catch (error) {
168
219
  if (error instanceof errors_1.RecordDoesNotExist) {
@@ -62,8 +62,8 @@ class GranulePgModel extends base_1.BasePgModel {
62
62
  */
63
63
  get(knexOrTransaction, params) {
64
64
  if (!isRecordSelect(params)) {
65
- if (!params.granule_id) {
66
- throw new errors_1.InvalidArgument(`Cannot find granule, must provide either granule_id or cumulus_id: params(${JSON.stringify(params)})`);
65
+ if (!(params.granule_id && params.collection_cumulus_id)) {
66
+ throw new errors_1.InvalidArgument(`Cannot find granule, must provide either granule_id and collection_cumulus_id or cumulus_id: params(${JSON.stringify(params)})`);
67
67
  }
68
68
  }
69
69
  return super.get(knexOrTransaction, params);
@@ -1,10 +1,10 @@
1
1
  export declare type GranuleStatus = 'completed' | 'failed' | 'running' | 'queued';
2
2
  export interface PostgresGranuleUniqueColumns {
3
3
  granule_id: string;
4
+ collection_cumulus_id: number;
4
5
  }
5
6
  export interface PostgresGranule extends PostgresGranuleUniqueColumns {
6
7
  archived: boolean;
7
- collection_cumulus_id: number;
8
8
  producer_granule_id: string;
9
9
  status?: GranuleStatus;
10
10
  cmr_link?: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/db",
3
- "version": "21.3.1-alpha.0",
3
+ "version": "21.3.1",
4
4
  "description": "Utilities for working with the Cumulus DB",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -33,12 +33,12 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@aws-sdk/client-secrets-manager": "^3.621.0",
36
- "@cumulus/aws-client": "21.3.1-alpha.0",
37
- "@cumulus/common": "21.3.1-alpha.0",
38
- "@cumulus/errors": "21.3.1-alpha.0",
39
- "@cumulus/logger": "21.3.1-alpha.0",
40
- "@cumulus/message": "21.3.1-alpha.0",
41
- "@cumulus/types": "21.3.1-alpha.0",
36
+ "@cumulus/aws-client": "21.3.1",
37
+ "@cumulus/common": "21.3.1",
38
+ "@cumulus/errors": "21.3.1",
39
+ "@cumulus/logger": "21.3.1",
40
+ "@cumulus/message": "21.3.1",
41
+ "@cumulus/types": "21.3.1",
42
42
  "crypto-random-string": "^3.2.0",
43
43
  "is-valid-hostname": "1.0.2",
44
44
  "knex": "2.4.1",
@@ -50,5 +50,5 @@
50
50
  "devDependencies": {
51
51
  "@types/uuid": "^8.0.0"
52
52
  },
53
- "gitHead": "5be3f53f3646008d95e1e8d0a0740301c9c19fcd"
53
+ "gitHead": "ba5c9b04fa1181fecbb97149fb4c01a92000ebb4"
54
54
  }