SQL schema-prefix rename
Rename a schema (qualified-name prefix) such as ra02. or rtmp. to a new prefix inside a folder of Python-wrapped Spark SQL scripts. These files look like Python but embed SQL in Sql = [ ... ] triple-quoted strings; do not be confused by the Python scaffolding. The SQL statements use -- comments, while the file header uses # Python comments.
Workflow
-
Locate and inspect. List the target folder (including subfolders) for
.txt/.py/.sqlscripts. Read one file to confirm the structure: a# -*- coding: utf-8 -*-header block (# 目标表:,# 依赖表:, …), thenSql = [ ... ]holding triple-quoted SQL with--comments. -
Enumerate occurrences. Grep for the source prefixes case-insensitively (to also catch uppercase
RA02./RTMP.). Classify each hit by context:
-INSERT INTO <prefix>.<table>andFROM / JOIN <prefix>.<table>inside an INSERT…SELECT (active or---commented).
-CREATE TABLE / ALTER TABLE / REFRESH TABLE / DROP TABLE <prefix>.<table>(active or---commented).
-# 目标表: <prefix>.<table>Python header comments. -
Confirm scope before editing. The scope is genuinely ambiguous and the edit is hard to reverse by hand, so ask the user (offer a recommended default):
- INSERT statements only — target table plus FROM/JOIN sources inside INSERT…SELECT, including--INSERT INTOblocks; leave CREATE/ALTER/REFRESH/DROP and#comments unchanged.
- All SQL statements — everything inside the SQL strings (INSERT/CREATE/ALTER/REFRESH/DROP, active and---commented), keep#header comments.
- Everything — also the#header comments.
Also confirm whether uppercase variants (RA02./RTMP.) count as the same schema (they normally do in Hive/Spark, where schema names are case-insensitive). -
Back up first. Copy the originals to a sibling
_backup_before_rename/before any in-place edit. -
Run the rename. Use
scripts/rename_schema.py(resolve relative to this skill directory):
text
python scripts/rename_schema.py --dir <dir> --map "ra02.=rcdm_temp.,rtmp.=rcdm_temp." \
--scope insert|all --case-insensitive [--keep-hash-comments] [--ext .txt,.py,.sql]
--mapis a comma-separated list offrom.=to.pairs; the trailing.is significant (it matches the schema-qualified-name prefix).--scope insertuses a line state machine: an activeINSERT INTOspans to its terminating;; a--INSERT INTOspans its contiguous--block until a blank line, a dashes-only line, or a non-comment line.--scope allreplaces on every line, except#-comment lines when--keep-hash-commentsis set.- Use the bundled Python (
load_workspace_dependencies) if a system Python is unavailable.
- Verify. Grep for the source prefixes again (case-insensitive). The only remaining hits must be the deliberately-preserved
#header comments (or none, for full scope). Reconcile counts:original_total = replaced + remaining. Confirm UTF-8 and line endings are preserved.
Notes
- Preserve uppercase table names; only the schema prefix changes.
- The script preserves UTF-8 encoding and CRLF/LF line endings exactly.
- Always report the backup path so the user can roll back.