Skip to content

Commit 9c5b57c

Browse files
committed
fix: add client cleanup, put error handling, and cumulative validation to migration script
1 parent 0cb69c5 commit 9c5b57c

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

ide/shell_cmds/buildUserHistory.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def build_history(client):
3939
skipped += 1
4040

4141
print(f"Done: {count} users, {skipped} skipped (no joinDate)")
42+
expected = count - skipped
4243

4344
sorted_months = sorted(monthly_new.keys())
4445
cumulative = 0
@@ -47,6 +48,9 @@ def build_history(client):
4748
cumulative += monthly_new[month]
4849
points.append({'month': month, 'count': cumulative})
4950

51+
if points and points[-1]['count'] != expected:
52+
print(f"WARNING: cumulative total {points[-1]['count']} != expected {expected}")
53+
5054
history = {
5155
'updated': datetime.now(timezone.utc).strftime('%Y-%m-%d'),
5256
'points': points,
@@ -56,16 +60,25 @@ def build_history(client):
5660
if not existing:
5761
existing = Setting(id='user_count_history')
5862
existing.value = json.dumps(history)
59-
existing.put()
63+
try:
64+
existing.put()
65+
except Exception as e:
66+
import sys
67+
print(f"ERROR: Failed to store user history: {e}", file=sys.stderr)
68+
sys.exit(1)
6069

6170
print(f"Stored {len(points)} monthly data points")
6271
if points:
6372
print(f"Latest: {points[-1]}")
6473

6574

6675
if __name__ == '__main__':
76+
import sys
6777
project = os.environ.get('GOOGLE_CLOUD_PROJECT', 'glowscript')
6878
emulator = os.environ.get('DATASTORE_EMULATOR_HOST')
69-
client = ndb.Client(project='glowscript-dev' if emulator else project)
79+
client = ndb.Client(project=project)
7080
print(f"Connecting to {'emulator at ' + emulator if emulator else 'production Datastore'}...")
71-
build_history(client)
81+
try:
82+
build_history(client)
83+
finally:
84+
client.close()

0 commit comments

Comments
 (0)