Skip to content

add example to cleanup a schema based on table age and name - #61

Draft
owenhw wants to merge 3 commits into
mainfrom
automatic-schema-cleanup
Draft

add example to cleanup a schema based on table age and name#61
owenhw wants to merge 3 commits into
mainfrom
automatic-schema-cleanup

Conversation

@owenhw

@owenhw owenhw commented Aug 18, 2026

Copy link
Copy Markdown

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py script 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,
)
Comment thread python/automatic-schema-cleanup/automatic-schema-cleanup.py Outdated
Comment thread python/automatic-schema-cleanup/automatic-schema-cleanup.py
owenhw and others added 2 commits August 18, 2026 10:32
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>
@owenhw
owenhw requested a review from rdj215 August 18, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants