Skip to content

Commit

Permalink
Fix two issues with Firestore database updating:
Browse files Browse the repository at this point in the history
* Performing an update on a Datastore mode database could silently change it to Firestore Native
* Enabling PITR (--point-in-time-recovery=ENABLED) while not specifying --disaster-recovery=ENABLED
  would silently disable disaster recovery, and vice-versa

 The former issue was due to always sending FIRESTORE_NATIVE as the database type, when we
 actually didn't need to send it at all. The latter issue was due to command-line option
 parsing that assumed not specifying an option was equivalent to sending an explicit DISABLE.
  • Loading branch information
rwhogg committed Oct 26, 2023
1 parent e53c50d commit febce9a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
24 changes: 13 additions & 11 deletions src/commands/firestore-databases-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ export const command = new Command("firestore:databases:update <database>")
.action(async (database: string, options: FirestoreOptions) => {
const api = new fsi.FirestoreApi();

if (!options.type && !options.deleteProtection && !options.pointInTimeRecovery) {
if (!options.deleteProtection && !options.pointInTimeRecovery) {
logger.error(
"Missing properties to update. See firebase firestore:databases:update --help for more info."
);
return;
}
const type: types.DatabaseType = types.DatabaseType.FIRESTORE_NATIVE;
if (
options.deleteProtection &&
options.deleteProtection !== types.DatabaseDeleteProtectionStateOption.ENABLED &&
Expand All @@ -43,10 +42,12 @@ export const command = new Command("firestore:databases:update <database>")
);
return;
}
const deleteProtectionState: types.DatabaseDeleteProtectionState =
options.deleteProtection === types.DatabaseDeleteProtectionStateOption.ENABLED
? types.DatabaseDeleteProtectionState.ENABLED
: types.DatabaseDeleteProtectionState.DISABLED;
let deleteProtectionState: types.DatabaseDeleteProtectionState | undefined;
if (options.deleteProtection === types.DatabaseDeleteProtectionStateOption.ENABLED) {
deleteProtectionState = types.DatabaseDeleteProtectionState.ENABLED;
} else if (options.deleteProtection === types.DatabaseDeleteProtectionStateOption.DISABLED) {
deleteProtectionState = types.DatabaseDeleteProtectionState.DISABLED;
}

if (
options.pointInTimeRecovery &&
Expand All @@ -58,15 +59,16 @@ export const command = new Command("firestore:databases:update <database>")
);
return;
}
const pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement =
options.pointInTimeRecovery === types.PointInTimeRecoveryEnablementOption.ENABLED
? types.PointInTimeRecoveryEnablement.ENABLED
: types.PointInTimeRecoveryEnablement.DISABLED;
let pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement | undefined;
if (options.pointInTimeRecovery === types.PointInTimeRecoveryEnablementOption.ENABLED) {
pointInTimeRecoveryEnablement = types.PointInTimeRecoveryEnablement.ENABLED;
} else if (options.pointInTimeRecovery === types.PointInTimeRecoveryEnablementOption.DISABLED) {
pointInTimeRecoveryEnablement = types.PointInTimeRecoveryEnablement.DISABLED;
}

const databaseResp: types.DatabaseResp = await api.updateDatabase(
options.project,
database,
type,
deleteProtectionState,
pointInTimeRecoveryEnablement
);
Expand Down
3 changes: 0 additions & 3 deletions src/firestore/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,20 +754,17 @@ export class FirestoreApi {
* Update a named Firestore Database
* @param project the Firebase project id.
* @param databaseId the name of the Firestore Database
* @param type FIRESTORE_NATIVE or DATASTORE_MODE
* @param deleteProtectionState DELETE_PROTECTION_ENABLED or DELETE_PROTECTION_DISABLED
* @param pointInTimeRecoveryEnablement POINT_IN_TIME_RECOVERY_ENABLED or POINT_IN_TIME_RECOVERY_DISABLED
*/
async updateDatabase(
project: string,
databaseId: string,
type?: types.DatabaseType,
deleteProtectionState?: types.DatabaseDeleteProtectionState,
pointInTimeRecoveryEnablement?: types.PointInTimeRecoveryEnablement
): Promise<types.DatabaseResp> {
const url = `/projects/${project}/databases/${databaseId}`;
const payload: types.DatabaseReq = {
type,
deleteProtectionState,
pointInTimeRecoveryEnablement,
};
Expand Down

0 comments on commit febce9a

Please sign in to comment.