|
1 | 1 | # local-storage-utils |
2 | | -Set of scripts and tools for managing GCP Datastore data in local |
| 2 | + |
| 3 | +Utilities for analyzing and managing local Datastore/Firestore (Datastore mode) data. Works with both the Datastore Emulator and GCP using Application Default Credentials. |
| 4 | + |
| 5 | +## Install (PyPI) |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install local-storage-utils |
| 9 | +``` |
| 10 | + |
| 11 | +This installs the `lsu` CLI. |
| 12 | + |
| 13 | +## Install (from source) |
| 14 | + |
| 15 | +git clone <this-repo-url> |
| 16 | +cd local-storage-utils |
| 17 | +python3 -m venv .venv |
| 18 | +source .venv/bin/activate |
| 19 | +python -m pip install -U pip |
| 20 | +pip install -e . |
| 21 | + |
| 22 | +### Troubleshooting local installs |
| 23 | +- If you see "Command 'python' not found", use `python3 -m venv .venv` (above). Inside the venv, `python` will point to Python 3. |
| 24 | +- If you see "externally-managed-environment", you are attempting a system-wide install. Always install into a virtual environment: |
| 25 | + - Create a venv: `python3 -m venv .venv && source .venv/bin/activate` |
| 26 | + - Then use the venv pip: `python -m pip install -U pip && pip install -e .` |
| 27 | + ```bash |
| 28 | + sudo apt-get update && sudo apt-get install -y python3-venv |
| 29 | + ``` |
| 30 | + |
| 31 | +## Configuration |
| 32 | + |
| 33 | +- Create a local `config.yaml` in your working directory. It is gitignored and not included in the repo. |
| 34 | +- Any CLI flag overrides values from `config.yaml`. |
| 35 | +- If neither config nor flags provide a value, the tool falls back to environment variables (for emulator detection) or sensible defaults. |
| 36 | + |
| 37 | +Example `config.yaml`: |
| 38 | + |
| 39 | +```yaml |
| 40 | +project_id: "my-project" # If omitted, ADC/env will be used |
| 41 | +emulator_host: "localhost:8010" # If set, uses Datastore Emulator |
| 42 | + |
| 43 | +# Explicit filters (empty means all) |
| 44 | +namespaces: [""] # Empty -> iterate all namespaces (including default "") |
| 45 | +kinds: [] # Empty -> iterate all kinds per namespace |
| 46 | + |
| 47 | +# Optional defaults |
| 48 | +kind: "SourceCollectionStateEntity" # Default for analyze-fields |
| 49 | + |
| 50 | +# Cleanup |
| 51 | +ttl_field: "expireAt" |
| 52 | +delete_missing_ttl: true |
| 53 | +batch_size: 500 |
| 54 | + |
| 55 | +# Analysis |
| 56 | +group_by_field: null |
| 57 | + |
| 58 | +# Logging |
| 59 | +log_level: "INFO" |
| 60 | +``` |
| 61 | +
|
| 62 | +## CLI usage |
| 63 | +
|
| 64 | +```bash |
| 65 | +# Kind-level counts and size estimates |
| 66 | +lsu analyze-kinds --project my-project |
| 67 | + |
| 68 | +# Use all namespaces/kinds by default, or restrict explicitly |
| 69 | +lsu analyze-kinds --namespace "" --namespace tenant-a --kind SourceCollectionStateEntity |
| 70 | + |
| 71 | +# Field contribution analysis (falls back to config.kind/config.namespace if not provided) |
| 72 | +lsu analyze-fields --kind SourceCollectionStateEntity --namespace "" --group-by batchId |
| 73 | + |
| 74 | +# TTL cleanup across namespaces/kinds (dry-run) |
| 75 | +lsu cleanup --ttl-field expireAt --dry-run |
| 76 | + |
| 77 | +# TTL cleanup restricted to specific namespaces/kinds |
| 78 | +lsu cleanup --namespace "" --namespace tenant-a --kind pipeline-job |
| 79 | +``` |
| 80 | + |
| 81 | +Use `--help` on any command for full options. Config can be provided via `config.yaml` or flags. |
| 82 | + |
| 83 | +## Development |
| 84 | + |
| 85 | +- Create a virtual environment and install in editable mode as shown above |
| 86 | +- Run tests: |
| 87 | + |
| 88 | +```bash |
| 89 | +python -m pip install pytest |
| 90 | +pytest -q |
| 91 | +``` |
| 92 | + |
| 93 | +- Lint/format (optional if you use pre-commit/CI): |
| 94 | +```bash |
| 95 | +python -m pip install ruff black |
| 96 | +ruff check . |
| 97 | +black . |
| 98 | +``` |
| 99 | + |
| 100 | +## Publishing |
| 101 | + |
| 102 | +- Automated: pushing to `main` triggers versioning, tagging, GitHub release, and PyPI publish via semantic-release. |
| 103 | +- Prerequisites: |
| 104 | + - Add a PyPI token to repo secrets as `PYPI_API_TOKEN`. |
| 105 | + - Use conventional commits for proper versioning. |
| 106 | + |
| 107 | +Main branch should be protected (require PRs, disallow direct pushes) in repository settings. |
0 commit comments