One-time migration to set is_archived = (phase_name == 'Archived') for all projects already in Firestore.
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
One-off script to backfill is_archived field on all projects in Firestore.
|
|
|
|
This sets is_archived = True for projects where phase_name == "Archived",
|
|
and is_archived = False for all other projects.
|
|
|
|
Usage:
|
|
python backfill_is_archived.py
|
|
"""
|
|
|
|
import os
|
|
from firebase_admin import credentials, initialize_app, firestore
|
|
|
|
# Path to your staging service account JSON
|
|
CREDENTIALS_PATH = "./rothbard-staging2-12345-firebase-adminsdk-fbsvc-7f95268383.json"
|
|
|
|
def main():
|
|
# Initialize Firebase Admin with staging credentials
|
|
cred = credentials.Certificate(CREDENTIALS_PATH)
|
|
app = initialize_app(cred, name='backfill-is-archived')
|
|
db = firestore.client(app=app)
|
|
|
|
projects_ref = db.collection("projects")
|
|
docs = list(projects_ref.stream())
|
|
|
|
total = len(docs)
|
|
archived_count = 0
|
|
updated_count = 0
|
|
batch_size = 500
|
|
|
|
print(f"Found {total} projects. Processing in batches of {batch_size}...")
|
|
|
|
for i in range(0, total, batch_size):
|
|
batch = db.batch()
|
|
batch_docs = docs[i:i + batch_size]
|
|
|
|
for doc in batch_docs:
|
|
data = doc.to_dict()
|
|
phase_name = data.get("phase_name", "")
|
|
is_archived = (phase_name == "Archived")
|
|
|
|
if is_archived:
|
|
archived_count += 1
|
|
|
|
# Only update if the field is missing or different
|
|
if data.get("is_archived") != is_archived:
|
|
ref = projects_ref.document(doc.id)
|
|
batch.update(ref, {"is_archived": is_archived})
|
|
updated_count += 1
|
|
|
|
batch.commit()
|
|
print(f" Committed batch {i//batch_size + 1}/{(total + batch_size - 1)//batch_size}")
|
|
|
|
print(f"\nDone!")
|
|
print(f" Total projects: {total}")
|
|
print(f" Projects with phase_name == 'Archived': {archived_count}")
|
|
print(f" Documents updated: {updated_count}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|