@firebase-function-kits/firestore-bigquery-export 0.0.2-rc.1 → 0.0.2-rc.11
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 +4 -0
- package/README.md +236 -84
- package/lib/config.d.ts +3 -2
- package/lib/config.d.ts.map +1 -1
- package/lib/config.js +58 -57
- package/lib/config.js.map +1 -1
- package/lib/events.d.ts.map +1 -1
- package/lib/events.js.map +1 -1
- package/lib/export-config.d.ts +19 -5
- package/lib/export-config.d.ts.map +1 -1
- package/lib/export-config.js +6 -1
- package/lib/export-config.js.map +1 -1
- package/lib/handlers.d.ts +38 -6
- package/lib/handlers.d.ts.map +1 -1
- package/lib/handlers.js +63 -18
- package/lib/handlers.js.map +1 -1
- package/lib/index.d.ts +13 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +51 -12
- package/lib/index.js.map +1 -1
- package/lib/init.d.ts +1 -1
- package/lib/init.js +1 -1
- package/lib/init.js.map +1 -1
- package/lib/lib.d.ts +1 -1
- package/lib/lib.d.ts.map +1 -1
- package/lib/lib.js +2 -1
- package/lib/lib.js.map +1 -1
- package/lib/logs.d.ts +2 -2
- package/lib/logs.d.ts.map +1 -1
- package/lib/logs.js +4 -3
- package/lib/logs.js.map +1 -1
- package/lib/region.d.ts +7 -0
- package/lib/region.d.ts.map +1 -0
- package/lib/region.js +37 -0
- package/lib/region.js.map +1 -0
- package/lib/tasks.d.ts +40 -0
- package/lib/tasks.d.ts.map +1 -0
- package/lib/tasks.js +107 -0
- package/lib/tasks.js.map +1 -0
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js.map +1 -1
- package/npm-shrinkwrap.json +160 -780
- package/package.json +10 -4
- package/src/config.ts +70 -59
- package/src/export-config.ts +26 -6
- package/src/handlers.ts +96 -27
- package/src/index.ts +71 -13
- package/src/init.ts +1 -1
- package/src/lib.ts +2 -0
- package/src/logs.ts +5 -3
- package/src/region.ts +37 -0
- package/src/tasks.ts +126 -0
- package/tests/config.test.ts +101 -14
- package/tests/export-config.test.ts +10 -4
- package/tests/handlers.test.ts +139 -10
- package/tests/index.test.ts +162 -0
- package/tests/region.test.ts +44 -0
- package/tests/required-apis.test.ts +55 -0
- package/tests/tasks.emulator.test.ts +131 -0
- package/tests/tasks.test.ts +229 -0
- package/tsconfig.tsbuildinfo +1 -1
package/src/index.ts
CHANGED
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
/**
|
|
18
18
|
* Main entry point. Exports the wired functions with deploy-time param
|
|
19
19
|
* expressions, then resolves concrete config lazily at runtime. Re-export
|
|
20
|
-
* `fsexportbigquery` and `initBigQuerySync` from your own
|
|
21
|
-
* entry; configuration comes from a `.env` (or
|
|
20
|
+
* `fsexportbigquery`, `syncBigQuery`, and `initBigQuerySync` from your own
|
|
21
|
+
* functions codebase entry; configuration comes from a `.env` (or
|
|
22
22
|
* `.env.<projectId>`), which the Firebase CLI loads at deploy.
|
|
23
23
|
*
|
|
24
24
|
* Because this module initializes runtime dependencies lazily, deploy discovery
|
|
@@ -40,10 +40,21 @@ import {
|
|
|
40
40
|
} from "firebase-functions/v2/lifecycle";
|
|
41
41
|
import { CONFIG_EXPRESSIONS, configFromEnv } from "./config";
|
|
42
42
|
import * as events from "./events";
|
|
43
|
-
import {
|
|
44
|
-
|
|
43
|
+
import {
|
|
44
|
+
DEFAULT_MAX_DISPATCHES_PER_SECOND,
|
|
45
|
+
resolveExportConfig,
|
|
46
|
+
toTrackerConfig,
|
|
47
|
+
} from "./export-config";
|
|
48
|
+
import {
|
|
49
|
+
type HandlerContext,
|
|
50
|
+
type SerializedDocumentChange,
|
|
51
|
+
handleDocumentWrite,
|
|
52
|
+
handleSyncBigQueryTask,
|
|
53
|
+
} from "./handlers";
|
|
45
54
|
import { createEnsureInitialized } from "./init";
|
|
46
55
|
import * as logs from "./logs";
|
|
56
|
+
import { firestoreLocationToFunctionRegion } from "./region";
|
|
57
|
+
import { enqueueSyncTask } from "./tasks";
|
|
47
58
|
|
|
48
59
|
// Re-export the side-effect-free library surface (handlers and config types).
|
|
49
60
|
export * from "./lib";
|
|
@@ -54,6 +65,11 @@ const LIFECYCLE_RETRY_CONFIG = {
|
|
|
54
65
|
maxAttempts: 15,
|
|
55
66
|
minBackoffSeconds: 60,
|
|
56
67
|
} as const;
|
|
68
|
+
const SYNC_RETRY_CONFIG = {
|
|
69
|
+
maxAttempts: 5,
|
|
70
|
+
minBackoffSeconds: 60,
|
|
71
|
+
} as const;
|
|
72
|
+
const SYNC_MAX_CONCURRENT_DISPATCHES = 500;
|
|
57
73
|
const REQUIRED_ROLES: ReadonlyArray<Role> = [
|
|
58
74
|
"roles/bigquery.dataEditor",
|
|
59
75
|
"roles/datastore.user",
|
|
@@ -61,8 +77,14 @@ const REQUIRED_ROLES: ReadonlyArray<Role> = [
|
|
|
61
77
|
// Gen2 Firestore triggers need Eventarc receive and run.invoker on the function SA.
|
|
62
78
|
"roles/eventarc.eventReceiver",
|
|
63
79
|
"roles/run.invoker",
|
|
80
|
+
// The trigger enqueues failed writes onto its own syncBigQuery task queue.
|
|
81
|
+
"roles/cloudtasks.enqueuer",
|
|
64
82
|
];
|
|
65
83
|
const REQUIRED_APIS = [
|
|
84
|
+
{
|
|
85
|
+
api: "firestore.googleapis.com",
|
|
86
|
+
reason: "Receives document change events from Cloud Firestore.",
|
|
87
|
+
},
|
|
66
88
|
{
|
|
67
89
|
api: "bigquery.googleapis.com",
|
|
68
90
|
reason: "Mirrors data from your Cloud Firestore collection in BigQuery.",
|
|
@@ -111,30 +133,66 @@ function getHandlerContext(): HandlerContext {
|
|
|
111
133
|
tracker,
|
|
112
134
|
config,
|
|
113
135
|
ensureInitialized,
|
|
136
|
+
enqueue: (change: SerializedDocumentChange) =>
|
|
137
|
+
enqueueSyncTask(change, config.maxEnqueueAttempts),
|
|
114
138
|
};
|
|
115
139
|
|
|
116
140
|
return ctx;
|
|
117
141
|
}
|
|
118
142
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
143
|
+
/*
|
|
144
|
+
* Read at module load: the CLI populates `.env` values into the discovery
|
|
145
|
+
* process env (firebase-tools >= 15.28.0), and the region option cannot be a
|
|
146
|
+
* param expression. When unset, no function declares a region and the CLI
|
|
147
|
+
* falls back to its default. The Eventarc trigger region needs no handling:
|
|
148
|
+
* the CLI pins it to the database's own region regardless of where the
|
|
149
|
+
* function runs.
|
|
150
|
+
*/
|
|
151
|
+
const functionRegion = firestoreLocationToFunctionRegion(
|
|
152
|
+
process.env.DATABASE_REGION
|
|
153
|
+
);
|
|
122
154
|
|
|
123
155
|
/**
|
|
124
156
|
* Firestore trigger: streams document writes on the watched collection into the
|
|
125
|
-
* BigQuery changelog table.
|
|
126
|
-
*
|
|
157
|
+
* BigQuery changelog table. A failed inline write buffers through the
|
|
158
|
+
* `syncBigQuery` queue and the execution still succeeds. No runtime retry
|
|
159
|
+
* policy, as in the extension: a failure before the write is attempted fails
|
|
160
|
+
* the execution once, and a failed enqueue is logged and dropped.
|
|
127
161
|
*/
|
|
128
162
|
export const fsexportbigquery = onDocumentWritten(
|
|
129
163
|
{
|
|
130
|
-
...
|
|
164
|
+
...(functionRegion ? { region: functionRegion } : {}),
|
|
131
165
|
document: expr`${CONFIG_EXPRESSIONS.collectionPath}/{documentId}`,
|
|
132
166
|
database: CONFIG_EXPRESSIONS.database,
|
|
133
|
-
retry: true,
|
|
134
167
|
},
|
|
135
168
|
(event) => handleDocumentWrite(event, getHandlerContext())
|
|
136
169
|
);
|
|
137
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Write-buffer task queue: re-attempts writes that failed inline, on Cloud
|
|
173
|
+
* Tasks' schedule (5 attempts, 60s minimum backoff, dispatch-throttled by
|
|
174
|
+
* `MAX_DISPATCHES_PER_SECOND`). After the last attempt the task is dropped;
|
|
175
|
+
* by then the tracker has written the row to `BACKUP_COLLECTION` on every
|
|
176
|
+
* terminal insert failure, when that collection is configured.
|
|
177
|
+
*/
|
|
178
|
+
export const syncBigQuery = onTaskDispatched<SerializedDocumentChange>(
|
|
179
|
+
{
|
|
180
|
+
...(functionRegion ? { region: functionRegion } : {}),
|
|
181
|
+
retryConfig: SYNC_RETRY_CONFIG,
|
|
182
|
+
rateLimits: {
|
|
183
|
+
maxConcurrentDispatches: SYNC_MAX_CONCURRENT_DISPATCHES, // A blank .env value reaches this deploy-time expression as 0, which
|
|
184
|
+
// Cloud Tasks would not accept; runtime falls back to the same default.
|
|
185
|
+
maxDispatchesPerSecond: CONFIG_EXPRESSIONS.maxDispatchesPerSecond
|
|
186
|
+
.lessThan(1)
|
|
187
|
+
.thenElse(
|
|
188
|
+
DEFAULT_MAX_DISPATCHES_PER_SECOND,
|
|
189
|
+
CONFIG_EXPRESSIONS.maxDispatchesPerSecond
|
|
190
|
+
),
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
(req) => handleSyncBigQueryTask(req, getHandlerContext())
|
|
194
|
+
);
|
|
195
|
+
|
|
138
196
|
async function handleBigQuerySyncInitialization(): Promise<void> {
|
|
139
197
|
try {
|
|
140
198
|
await getHandlerContext().ensureInitialized();
|
|
@@ -155,7 +213,7 @@ async function handleBigQuerySyncInitialization(): Promise<void> {
|
|
|
155
213
|
*/
|
|
156
214
|
export const initBigQuerySync = onTaskDispatched(
|
|
157
215
|
{
|
|
158
|
-
...
|
|
216
|
+
...(functionRegion ? { region: functionRegion } : {}),
|
|
159
217
|
retryConfig: LIFECYCLE_RETRY_CONFIG,
|
|
160
218
|
},
|
|
161
219
|
handleBigQuerySyncInitialization
|
|
@@ -167,7 +225,7 @@ export const initBigQuerySync = onTaskDispatched(
|
|
|
167
225
|
*/
|
|
168
226
|
export const setupBigQuerySync = onTaskDispatched(
|
|
169
227
|
{
|
|
170
|
-
...
|
|
228
|
+
...(functionRegion ? { region: functionRegion } : {}),
|
|
171
229
|
retryConfig: LIFECYCLE_RETRY_CONFIG,
|
|
172
230
|
},
|
|
173
231
|
handleBigQuerySyncInitialization
|
package/src/init.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type { FirestoreBigQueryEventHistoryTracker } from "@firebaseextensions/f
|
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Builds the provisioning guard used by the `initBigQuerySync` endpoint and the
|
|
21
|
-
*
|
|
21
|
+
* lifecycle tasks. The write paths never call it.
|
|
22
22
|
*
|
|
23
23
|
* The returned function runs `tracker.initialize()` at most once per instance:
|
|
24
24
|
* concurrent invocations on a cold instance share a single in-flight promise. A
|
package/src/lib.ts
CHANGED
package/src/logs.ts
CHANGED
|
@@ -180,8 +180,8 @@ export const init = (config?: unknown) => {
|
|
|
180
180
|
logger.info("Initializing with configuration", config);
|
|
181
181
|
};
|
|
182
182
|
|
|
183
|
-
export const start = (
|
|
184
|
-
logger.info("Started execution
|
|
183
|
+
export const start = () => {
|
|
184
|
+
logger.info("Started execution of extension");
|
|
185
185
|
};
|
|
186
186
|
|
|
187
187
|
export const timestampMissingValue = (fieldName: string) => {
|
|
@@ -208,7 +208,8 @@ export const logFailedEventAction = (
|
|
|
208
208
|
document_name: string,
|
|
209
209
|
event_id: string,
|
|
210
210
|
operation: ChangeType,
|
|
211
|
-
error: Error
|
|
211
|
+
error: Error,
|
|
212
|
+
retry_count?: number
|
|
212
213
|
) => {
|
|
213
214
|
const changeTypeMap = {
|
|
214
215
|
0: "CREATE",
|
|
@@ -222,6 +223,7 @@ export const logFailedEventAction = (
|
|
|
222
223
|
event_id,
|
|
223
224
|
operation: changeTypeMap[operation],
|
|
224
225
|
error,
|
|
226
|
+
...(retry_count === undefined ? {} : { retry_count }),
|
|
225
227
|
});
|
|
226
228
|
};
|
|
227
229
|
|
package/src/region.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2019 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Firestore multi-region locations are not Cloud Run regions; deploying a
|
|
18
|
+
// function to one hard-fails, so they map to a region inside the multi-region.
|
|
19
|
+
const MULTI_REGION_TO_FUNCTION_REGION: Record<string, string> = {
|
|
20
|
+
nam5: "us-central1",
|
|
21
|
+
nam7: "us-central1",
|
|
22
|
+
eur3: "europe-west1",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Maps a Firestore database location to the Cloud Run region the functions
|
|
27
|
+
* should deploy to. Regional locations pass through unchanged; an unset or
|
|
28
|
+
* empty location returns `undefined`, meaning the functions declare no region.
|
|
29
|
+
*/
|
|
30
|
+
export function firestoreLocationToFunctionRegion(
|
|
31
|
+
location: string | undefined
|
|
32
|
+
): string | undefined {
|
|
33
|
+
if (!location) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
return MULTI_REGION_TO_FUNCTION_REGION[location] ?? location;
|
|
37
|
+
}
|
package/src/tasks.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2019 Google LLC
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { getFunctions } from "firebase-admin/functions";
|
|
19
|
+
import type { SerializedDocumentChange } from "./handlers";
|
|
20
|
+
import { firestoreLocationToFunctionRegion } from "./region";
|
|
21
|
+
|
|
22
|
+
/** Export name of the write-buffer task function. */
|
|
23
|
+
export const SYNC_BIGQUERY_FUNCTION = "syncBigQuery";
|
|
24
|
+
|
|
25
|
+
const MAX_BACKOFF_MS = 5000;
|
|
26
|
+
const BACKOFF_BASE_MS = 100;
|
|
27
|
+
const JITTER_MS = 100;
|
|
28
|
+
|
|
29
|
+
// Hashed rather than sanitized: a lossy mapping could collapse two event ids
|
|
30
|
+
// into one task id, and Cloud Tasks wants ids uniformly distributed.
|
|
31
|
+
function taskIdFor(change: SerializedDocumentChange): string {
|
|
32
|
+
return createHash("sha256").update(change.eventId).digest("hex");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Resolves the queue resource path for a task function of this kit instance.
|
|
37
|
+
*
|
|
38
|
+
* The name is deliberately unprefixed: firebase-admin >= 14.2.0 resolves the
|
|
39
|
+
* deployed `kit-<instance id>-` prefix itself from the
|
|
40
|
+
* `FIREBASE_KIT_INSTANCE_ID` env var, which the CLI sets on every deployed kit
|
|
41
|
+
* function. All functions of a kit instance deploy to one region, so the
|
|
42
|
+
* enqueuing function's own region is the queue's region.
|
|
43
|
+
*
|
|
44
|
+
* The CLI-set `FUNCTION_REGION` wins because it is the region the function
|
|
45
|
+
* was actually deployed to; a `DATABASE_REGION`-derived region can disagree
|
|
46
|
+
* with it on a first deploy or when the variable is unset, and is only the
|
|
47
|
+
* fallback for local runs where the CLI has not populated the environment.
|
|
48
|
+
* With neither set, the bare function name is returned and the Admin SDK
|
|
49
|
+
* applies its default location, `us-central1`, which is also where the CLI
|
|
50
|
+
* places functions that declare no region.
|
|
51
|
+
*
|
|
52
|
+
* @param functionName - The export name of the task function.
|
|
53
|
+
* @returns The queue resource path, `locations/<region>/functions/<name>`,
|
|
54
|
+
* or the bare `<name>` when no region is known.
|
|
55
|
+
*/
|
|
56
|
+
export function syncQueuePath(
|
|
57
|
+
functionName: string = SYNC_BIGQUERY_FUNCTION
|
|
58
|
+
): string {
|
|
59
|
+
const region =
|
|
60
|
+
process.env.FUNCTION_REGION ||
|
|
61
|
+
firestoreLocationToFunctionRegion(process.env.DATABASE_REGION);
|
|
62
|
+
|
|
63
|
+
return region
|
|
64
|
+
? `locations/${region}/functions/${functionName}`
|
|
65
|
+
: functionName;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function backoffMs(attempt: number, jitter: number): number {
|
|
69
|
+
return (
|
|
70
|
+
Math.min(Math.pow(2, attempt) * BACKOFF_BASE_MS, MAX_BACKOFF_MS) + jitter
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Enqueues a payload onto the `syncBigQuery` queue, retrying transient enqueue
|
|
76
|
+
* failures in-process with exponential backoff and jitter.
|
|
77
|
+
*
|
|
78
|
+
* The task id is derived from the event id, so a retried enqueue of an event
|
|
79
|
+
* that already reached Cloud Tasks is rejected rather than buffered twice.
|
|
80
|
+
*
|
|
81
|
+
* @param payload - The serialized change to enqueue.
|
|
82
|
+
* @param maxAttempts - How many enqueue attempts to make before giving up.
|
|
83
|
+
* Anything but a positive integer means a single attempt: resolving without
|
|
84
|
+
* an enqueue would report success for an event that was never buffered.
|
|
85
|
+
* @throws The last enqueue error, once every attempt has failed.
|
|
86
|
+
*/
|
|
87
|
+
export async function enqueueSyncTask(
|
|
88
|
+
payload: SerializedDocumentChange,
|
|
89
|
+
maxAttempts: number
|
|
90
|
+
): Promise<void> {
|
|
91
|
+
const queue = getFunctions().taskQueue(syncQueuePath());
|
|
92
|
+
const id = taskIdFor(payload);
|
|
93
|
+
|
|
94
|
+
// Math.max(1, NaN) is NaN and would skip the loop entirely.
|
|
95
|
+
const attemptBudget =
|
|
96
|
+
Number.isInteger(maxAttempts) && maxAttempts >= 1 ? maxAttempts : 1;
|
|
97
|
+
const jitter = Math.random() * JITTER_MS;
|
|
98
|
+
let attempts = 0;
|
|
99
|
+
|
|
100
|
+
while (attempts < attemptBudget) {
|
|
101
|
+
if (attempts > 0) {
|
|
102
|
+
await new Promise((resolve) =>
|
|
103
|
+
setTimeout(resolve, backoffMs(attempts, jitter))
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
attempts++;
|
|
108
|
+
try {
|
|
109
|
+
await queue.enqueue(payload, { id });
|
|
110
|
+
return;
|
|
111
|
+
} catch (enqueueErr) {
|
|
112
|
+
// The event is already buffered; a second task would double-write the row.
|
|
113
|
+
// firebase-admin prefixes its codes: `functions/task-already-exists`.
|
|
114
|
+
if (
|
|
115
|
+
(enqueueErr as { code?: string })?.code ===
|
|
116
|
+
"functions/task-already-exists"
|
|
117
|
+
) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (attempts >= attemptBudget) {
|
|
122
|
+
throw enqueueErr;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
package/tests/config.test.ts
CHANGED
|
@@ -33,19 +33,17 @@ vi.mock("firebase-functions/params", () => ({
|
|
|
33
33
|
toJSON(): string {
|
|
34
34
|
return this.toString();
|
|
35
35
|
}
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
) => ({
|
|
41
|
-
value: () =>
|
|
42
|
-
typeof opts?.default === "string"
|
|
43
|
-
? opts.default
|
|
44
|
-
: opts?.default?.value() ?? "",
|
|
36
|
+
}, // Mirrors the real StringParam: the env var or "", never the declared
|
|
37
|
+
// default, which only the CLI prompt consults.
|
|
38
|
+
defineString: (_name: string) => ({
|
|
39
|
+
value: () => process.env[_name] || "",
|
|
45
40
|
toString: () => `params.${_name}`,
|
|
46
41
|
}),
|
|
42
|
+
// Mirrors the real IntParam: a missing or blank env var resolves to 0, and
|
|
43
|
+
// the declared default never reaches runtime.
|
|
47
44
|
defineInt: (_name: string, opts?: { default?: number }) => ({
|
|
48
|
-
|
|
45
|
+
name: _name,
|
|
46
|
+
value: () => Number.parseInt(process.env[_name] || "0", 10) || 0,
|
|
49
47
|
toString: () => `params.${_name}`,
|
|
50
48
|
}),
|
|
51
49
|
defineBoolean: (_name: string, opts?: { default?: boolean }) => ({
|
|
@@ -162,21 +160,110 @@ describe("buildPartitioningConfig", () => {
|
|
|
162
160
|
});
|
|
163
161
|
|
|
164
162
|
describe("configFromEnv", () => {
|
|
165
|
-
test("maps params
|
|
163
|
+
test("maps params", () => {
|
|
164
|
+
// The CLI resolves the param's default into the env at deploy.
|
|
165
|
+
vi.stubEnv("BIGQUERY_PROJECT_ID", "test-project");
|
|
166
166
|
const config = configFromEnv();
|
|
167
167
|
expect(config.projectId).toBe("test-project");
|
|
168
168
|
expect(config.bqProjectId).toBe("test-project");
|
|
169
169
|
expect(config.databaseId).toBe("(default)");
|
|
170
|
-
expect(resolveExportConfig(config).location).toBe("");
|
|
171
170
|
expect(config.viewType).toBe("view");
|
|
171
|
+
expect(resolveExportConfig(config)).not.toHaveProperty("location");
|
|
172
|
+
vi.unstubAllEnvs();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("reports unset queue params as undefined so the documented defaults apply", () => {
|
|
176
|
+
// IntParam.value() resolves an unset var to 0, which would defeat the
|
|
177
|
+
// `?? 100` / `?? 3` fallbacks in resolveExportConfig.
|
|
178
|
+
const config = configFromEnv();
|
|
179
|
+
expect(config.maxDispatchesPerSecond).toBeUndefined();
|
|
180
|
+
expect(config.maxEnqueueAttempts).toBeUndefined();
|
|
181
|
+
|
|
182
|
+
const resolved = resolveExportConfig(config);
|
|
183
|
+
expect(resolved.maxDispatchesPerSecond).toBe(100);
|
|
184
|
+
expect(resolved.maxEnqueueAttempts).toBe(3);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("reports a blank queue param as undefined", () => {
|
|
188
|
+
vi.stubEnv("MAX_ENQUEUE_ATTEMPTS", " ");
|
|
189
|
+
expect(configFromEnv().maxEnqueueAttempts).toBeUndefined();
|
|
190
|
+
vi.unstubAllEnvs();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("passes an explicit queue param through", () => {
|
|
194
|
+
vi.stubEnv("MAX_ENQUEUE_ATTEMPTS", "7");
|
|
195
|
+
vi.stubEnv("MAX_DISPATCHES_PER_SECOND", "250");
|
|
196
|
+
const config = configFromEnv();
|
|
197
|
+
expect(config.maxEnqueueAttempts).toBe(7);
|
|
198
|
+
expect(config.maxDispatchesPerSecond).toBe(250);
|
|
199
|
+
vi.unstubAllEnvs();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("yes/no selects default to off", () => {
|
|
203
|
+
const config = configFromEnv();
|
|
204
|
+
expect(config.useNewSnapshotQuerySyntax).toBe(false);
|
|
205
|
+
expect(config.excludeOldData).toBe(false);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test("yes/no selects enable on the extension's literal yes", () => {
|
|
209
|
+
vi.stubEnv("USE_NEW_SNAPSHOT_QUERY_SYNTAX", "yes");
|
|
210
|
+
vi.stubEnv("EXCLUDE_OLD_DATA", "yes");
|
|
211
|
+
const config = configFromEnv();
|
|
212
|
+
expect(config.useNewSnapshotQuerySyntax).toBe(true);
|
|
213
|
+
expect(config.excludeOldData).toBe(true);
|
|
214
|
+
vi.unstubAllEnvs();
|
|
215
|
+
});
|
|
216
|
+
test("yes/no selects forgive the label's case and surrounding whitespace", () => {
|
|
217
|
+
vi.stubEnv("USE_NEW_SNAPSHOT_QUERY_SYNTAX", "Yes");
|
|
218
|
+
vi.stubEnv("EXCLUDE_OLD_DATA", " yes ");
|
|
219
|
+
const config = configFromEnv();
|
|
220
|
+
expect(config.useNewSnapshotQuerySyntax).toBe(true);
|
|
221
|
+
expect(config.excludeOldData).toBe(true);
|
|
222
|
+
vi.unstubAllEnvs();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("yes/no selects treat no and anything else as off", () => {
|
|
226
|
+
vi.stubEnv("USE_NEW_SNAPSHOT_QUERY_SYNTAX", "no");
|
|
227
|
+
vi.stubEnv("EXCLUDE_OLD_DATA", "true");
|
|
228
|
+
expect(configFromEnv().useNewSnapshotQuerySyntax).toBe(false);
|
|
229
|
+
expect(configFromEnv().excludeOldData).toBe(false);
|
|
230
|
+
vi.unstubAllEnvs();
|
|
172
231
|
});
|
|
173
232
|
|
|
174
233
|
test("exposes deploy-time expressions for trigger metadata", () => {
|
|
175
234
|
expect(CONFIG_EXPRESSIONS.collectionPath.toString()).toBe(
|
|
176
235
|
"params.COLLECTION_PATH"
|
|
177
236
|
);
|
|
178
|
-
expect(CONFIG_EXPRESSIONS.
|
|
179
|
-
|
|
237
|
+
expect(CONFIG_EXPRESSIONS.database.toString()).toBe("params.DATABASE");
|
|
238
|
+
expect(CONFIG_EXPRESSIONS.maxDispatchesPerSecond.toString()).toBe(
|
|
239
|
+
"params.MAX_DISPATCHES_PER_SECOND"
|
|
180
240
|
);
|
|
241
|
+
expect(CONFIG_EXPRESSIONS).not.toHaveProperty("location");
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("resolveExportConfig queue defaults", () => {
|
|
246
|
+
test("applies the extension's queue defaults when unset", () => {
|
|
247
|
+
const resolved = resolveExportConfig({
|
|
248
|
+
collectionPath: "users",
|
|
249
|
+
datasetId: "ds",
|
|
250
|
+
tableId: "tbl",
|
|
251
|
+
projectId: "p",
|
|
252
|
+
});
|
|
253
|
+
expect(resolved.maxDispatchesPerSecond).toBe(100);
|
|
254
|
+
expect(resolved.maxEnqueueAttempts).toBe(3);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test("passes explicit queue values through", () => {
|
|
258
|
+
const resolved = resolveExportConfig({
|
|
259
|
+
collectionPath: "users",
|
|
260
|
+
datasetId: "ds",
|
|
261
|
+
tableId: "tbl",
|
|
262
|
+
projectId: "p",
|
|
263
|
+
maxDispatchesPerSecond: 250,
|
|
264
|
+
maxEnqueueAttempts: 5,
|
|
265
|
+
});
|
|
266
|
+
expect(resolved.maxDispatchesPerSecond).toBe(250);
|
|
267
|
+
expect(resolved.maxEnqueueAttempts).toBe(5);
|
|
181
268
|
});
|
|
182
269
|
});
|
|
@@ -22,7 +22,6 @@ describe("resolveExportConfig", () => {
|
|
|
22
22
|
collectionPath: "users",
|
|
23
23
|
datasetId: "analytics",
|
|
24
24
|
tableId: "users",
|
|
25
|
-
location: "us-central1",
|
|
26
25
|
projectId: "test-project",
|
|
27
26
|
};
|
|
28
27
|
|
|
@@ -42,12 +41,10 @@ describe("resolveExportConfig", () => {
|
|
|
42
41
|
test("keeps caller-supplied values", () => {
|
|
43
42
|
const resolved = resolveExportConfig({
|
|
44
43
|
...minimal,
|
|
45
|
-
location: "europe-west2",
|
|
46
44
|
databaseId: "secondary",
|
|
47
45
|
wildcardIds: true,
|
|
48
46
|
});
|
|
49
47
|
|
|
50
|
-
expect(resolved.location).toBe("europe-west2");
|
|
51
48
|
expect(resolved.databaseId).toBe("secondary");
|
|
52
49
|
expect(resolved.wildcardIds).toBe(true);
|
|
53
50
|
});
|
|
@@ -65,7 +62,6 @@ describe("toTrackerConfig", () => {
|
|
|
65
62
|
collectionPath: "users",
|
|
66
63
|
datasetId: "analytics",
|
|
67
64
|
tableId: "users",
|
|
68
|
-
location: "us-central1",
|
|
69
65
|
projectId: "test-project",
|
|
70
66
|
};
|
|
71
67
|
|
|
@@ -111,4 +107,14 @@ describe("toTrackerConfig", () => {
|
|
|
111
107
|
);
|
|
112
108
|
expect(tracker.bqProjectId).toBe("analytics-project");
|
|
113
109
|
});
|
|
110
|
+
|
|
111
|
+
test("wires the backup collection into the tracker (queue exhaustion durability)", () => {
|
|
112
|
+
const tracker = toTrackerConfig(
|
|
113
|
+
resolveExportConfig({ ...base, backupCollectionId: "bq_failures" })
|
|
114
|
+
);
|
|
115
|
+
expect(tracker.backupTableId).toBe("bq_failures");
|
|
116
|
+
|
|
117
|
+
const withoutBackup = toTrackerConfig(resolveExportConfig(base));
|
|
118
|
+
expect(withoutBackup.backupTableId).toBeUndefined();
|
|
119
|
+
});
|
|
114
120
|
});
|