add example to cleanup a schema based on table age and name - #61
Draft
owenhw wants to merge 3 commits into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new Python example script for automatically dropping Redshift tables in a schema when they haven’t been “touched” (per Civis table catalog metadata) in more than N days, with optional table-name regex filtering and a dry-run mode.
Changes:
- New
automatic-schema-cleanup.pyscript that scans schema/table metadata via Civis Platform API, computes “last touched” age, and drops qualifying tables. - Optional regex filtering to target subsets of tables by name.
- Dry-run mode and summary logging for review before executing drops.
Suppressed comments (2)
python/automatic-schema-cleanup/automatic-schema-cleanup.py:122
- DRY_RUN and SKIP_SCANS are documented as optional/defaulted, but using os.environ[...] makes them required and the current parsing is case-sensitive (only exactly "true" works). This can cause unexpected KeyError or silently treat "True" as false.
dry_run = os.environ["DRY_RUN"] == "true"
skip_scans = os.environ["SKIP_SCANS"] == "true"
python/automatic-schema-cleanup/automatic-schema-cleanup.py:187
- If credential_id is optional, passing credential_id=None into query_civis may not behave as intended. Only include credential_id when explicitly provided so the default credential path is used.
civis.io.query_civis(
f"DROP TABLE {full_name}",
database=database_id,
credential_id=credential_id,
client=client,
hidden=True,
).result()
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+119
to
+120
| database_id = int(os.environ["DB_ID"]) | ||
| credential_id = int(os.environ["DB_CREDENTIAL_ID"]) |
Comment on lines
+85
to
+91
| page = client.tables.list( | ||
| database_id=database_id, | ||
| schema=schema, | ||
| credential_id=credential_id, | ||
| limit=1000, | ||
| page_num=page_num, | ||
| ) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds an example to drop tables in a Redshift schema older than X days. It also implements regular expressions to match table names if provided.
The code was tested with this script: https://platform.civisanalytics.com/spa/#/scripts/python3/365711394