Skip to content

Commit 0866cf9

Browse files
authored
Allow enabling and disabling PITR on Firestore databases (#6427)
Also show Point In Time Recovery setting, earliest version time, and version retention period on get
1 parent 83e61b6 commit 0866cf9

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Added support for enabling, disabling, and displaying Point In Time Recovery enablement state on Firestore databases (#6388)

src/commands/firestore-databases-create.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const command = new Command("firestore:databases:create <database>")
1818
"--delete-protection <deleteProtectionState>",
1919
"Whether or not to prevent deletion of database, for example 'ENABLED' or 'DISABLED'. Default is 'DISABLED'"
2020
)
21+
.option(
22+
"--point-in-time-recovery <enablement>",
23+
"Whether to enable the PITR feature on this database, for example 'ENABLED' or 'DISABLED'. Default is 'DISABLED'"
24+
)
2125
.before(requirePermissions, ["datastore.databases.create"])
2226
.before(warnEmulatorNotSupported, Emulators.FIRESTORE)
2327
.action(async (database: string, options: FirestoreOptions) => {
@@ -45,12 +49,28 @@ export const command = new Command("firestore:databases:create <database>")
4549
? types.DatabaseDeleteProtectionState.ENABLED
4650
: types.DatabaseDeleteProtectionState.DISABLED;
4751

52+
if (
53+
options.pointInTimeRecovery &&
54+
options.pointInTimeRecovery !== types.PointInTimeRecoveryEnablementOption.ENABLED &&
55+
options.pointInTimeRecovery !== types.PointInTimeRecoveryEnablementOption.DISABLED
56+
) {
57+
logger.error(
58+
"Invalid value for flag --point-in-time-recovery. See firebase firestore:databases:create --help for more info."
59+
);
60+
return;
61+
}
62+
const pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement =
63+
options.pointInTimeRecovery === types.PointInTimeRecoveryEnablementOption.ENABLED
64+
? types.PointInTimeRecoveryEnablement.ENABLED
65+
: types.PointInTimeRecoveryEnablement.DISABLED;
66+
4867
const databaseResp: types.DatabaseResp = await api.createDatabase(
4968
options.project,
5069
database,
5170
options.location,
5271
type,
53-
deleteProtectionState
72+
deleteProtectionState,
73+
pointInTimeRecoveryEnablement
5474
);
5575

5676
if (options.json) {

src/commands/firestore-databases-update.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ export const command = new Command("firestore:databases:update <database>")
1717
"--delete-protection <deleteProtectionState>",
1818
"Whether or not to prevent deletion of database, for example 'ENABLED' or 'DISABLED'. Default is 'DISABLED'"
1919
)
20+
.option(
21+
"--point-in-time-recovery <enablement>",
22+
"Whether to enable the PITR feature on this database, for example 'ENABLED' or 'DISABLED'. Default is 'DISABLED'"
23+
)
2024
.before(requirePermissions, ["datastore.databases.update"])
2125
.before(warnEmulatorNotSupported, Emulators.FIRESTORE)
2226
.action(async (database: string, options: FirestoreOptions) => {
2327
const api = new fsi.FirestoreApi();
2428

25-
if (!options.type && !options.deleteProtection) {
29+
if (!options.type && !options.deleteProtection && !options.pointInTimeRecovery) {
2630
logger.error(
2731
"Missing properties to update. See firebase firestore:databases:update --help for more info."
2832
);
@@ -44,11 +48,27 @@ export const command = new Command("firestore:databases:update <database>")
4448
? types.DatabaseDeleteProtectionState.ENABLED
4549
: types.DatabaseDeleteProtectionState.DISABLED;
4650

51+
if (
52+
options.pointInTimeRecovery &&
53+
options.pointInTimeRecovery !== types.PointInTimeRecoveryEnablementOption.ENABLED &&
54+
options.pointInTimeRecovery !== types.PointInTimeRecoveryEnablementOption.DISABLED
55+
) {
56+
logger.error(
57+
"Invalid value for flag --point-in-time-recovery. See firebase firestore:databases:update --help for more info."
58+
);
59+
return;
60+
}
61+
const pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement =
62+
options.pointInTimeRecovery === types.PointInTimeRecoveryEnablementOption.ENABLED
63+
? types.PointInTimeRecoveryEnablement.ENABLED
64+
: types.PointInTimeRecoveryEnablement.DISABLED;
65+
4766
const databaseResp: types.DatabaseResp = await api.updateDatabase(
4867
options.project,
4968
database,
5069
type,
51-
deleteProtectionState
70+
deleteProtectionState,
71+
pointInTimeRecoveryEnablement
5272
);
5373

5474
if (options.json) {

src/firestore/api-types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,21 @@ export enum DatabaseDeleteProtectionState {
105105
DISABLED = "DELETE_PROTECTION_DISABLED",
106106
}
107107

108+
export enum PointInTimeRecoveryEnablementOption {
109+
ENABLED = "ENABLED",
110+
DISABLED = "DISABLED",
111+
}
112+
113+
export enum PointInTimeRecoveryEnablement {
114+
ENABLED = "POINT_IN_TIME_RECOVERY_ENABLED",
115+
DISABLED = "POINT_IN_TIME_RECOVERY_DISABLED",
116+
}
117+
108118
export interface DatabaseReq {
109119
locationId?: string;
110120
type?: DatabaseType;
111121
deleteProtectionState?: DatabaseDeleteProtectionState;
122+
pointInTimeRecoveryEnablement?: PointInTimeRecoveryEnablement;
112123
}
113124

114125
export interface DatabaseResp {
@@ -122,5 +133,8 @@ export interface DatabaseResp {
122133
appEngineIntegrationMode: string;
123134
keyPrefix: string;
124135
deleteProtectionState: DatabaseDeleteProtectionState;
136+
pointInTimeRecoveryEnablement: PointInTimeRecoveryEnablement;
125137
etag: string;
138+
versionRetentionPeriod: string;
139+
earliestVersionTime: string;
126140
}

src/firestore/api.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ export class FirestoreApi {
321321
["Last Update Time", clc.yellow(database.updateTime)],
322322
["Type", clc.yellow(database.type)],
323323
["Location", clc.yellow(database.locationId)],
324-
["Delete Protection State", clc.yellow(database.deleteProtectionState)]
324+
["Delete Protection State", clc.yellow(database.deleteProtectionState)],
325+
["Point In Time Recovery", clc.yellow(database.pointInTimeRecoveryEnablement)],
326+
["Earliest Version Time", clc.yellow(database.earliestVersionTime)],
327+
["Version Retention Period", clc.yellow(database.versionRetentionPeriod)]
325328
);
326329
logger.info(table.toString());
327330
}
@@ -716,19 +719,22 @@ export class FirestoreApi {
716719
* @param locationId the id of the region the database will be created in
717720
* @param type FIRESTORE_NATIVE or DATASTORE_MODE
718721
* @param deleteProtectionState DELETE_PROTECTION_ENABLED or DELETE_PROTECTION_DISABLED
722+
* @param pointInTimeRecoveryEnablement POINT_IN_TIME_RECOVERY_ENABLED or POINT_IN_TIME_RECOVERY_DISABLED
719723
*/
720724
async createDatabase(
721725
project: string,
722726
databaseId: string,
723727
locationId: string,
724728
type: types.DatabaseType,
725-
deleteProtectionState: types.DatabaseDeleteProtectionState
729+
deleteProtectionState: types.DatabaseDeleteProtectionState,
730+
pointInTimeRecoveryEnablement: types.PointInTimeRecoveryEnablement
726731
): Promise<types.DatabaseResp> {
727732
const url = `/projects/${project}/databases`;
728733
const payload: types.DatabaseReq = {
729734
type,
730735
locationId,
731736
deleteProtectionState,
737+
pointInTimeRecoveryEnablement,
732738
};
733739
const options = { queryParams: { databaseId: databaseId } };
734740
const res = await this.apiClient.post<types.DatabaseReq, { response?: types.DatabaseResp }>(
@@ -750,17 +756,20 @@ export class FirestoreApi {
750756
* @param databaseId the name of the Firestore Database
751757
* @param type FIRESTORE_NATIVE or DATASTORE_MODE
752758
* @param deleteProtectionState DELETE_PROTECTION_ENABLED or DELETE_PROTECTION_DISABLED
759+
* @param pointInTimeRecoveryEnablement POINT_IN_TIME_RECOVERY_ENABLED or POINT_IN_TIME_RECOVERY_DISABLED
753760
*/
754761
async updateDatabase(
755762
project: string,
756763
databaseId: string,
757764
type?: types.DatabaseType,
758-
deleteProtectionState?: types.DatabaseDeleteProtectionState
765+
deleteProtectionState?: types.DatabaseDeleteProtectionState,
766+
pointInTimeRecoveryEnablement?: types.PointInTimeRecoveryEnablement
759767
): Promise<types.DatabaseResp> {
760768
const url = `/projects/${project}/databases/${databaseId}`;
761769
const payload: types.DatabaseReq = {
762770
type,
763771
deleteProtectionState,
772+
pointInTimeRecoveryEnablement,
764773
};
765774
const res = await this.apiClient.patch<types.DatabaseReq, { response?: types.DatabaseResp }>(
766775
url,

src/firestore/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export interface FirestoreOptions extends Options {
1616
location?: string;
1717
type?: types.DatabaseType;
1818
deleteProtection?: types.DatabaseDeleteProtectionStateOption;
19+
pointInTimeRecoveryEnablement?: types.PointInTimeRecoveryEnablementOption;
1920
}

0 commit comments

Comments
 (0)