Created
July 6, 2023 09:19
-
-
Save Hosuke/7856251516530ce16dfe153ee3a4a64a to your computer and use it in GitHub Desktop.
legacy_spells_mover
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
import os | |
import re | |
import shutil | |
# Get current dir | |
dir_path = os.getcwd() | |
for root,dirs,files in os.walk(dir_path): | |
for file in files: | |
# Check for .sql file | |
if file.endswith('.sql'): | |
# Get full file path | |
full_path = os.path.join(root,file) | |
with open(full_path) as f: | |
content = f.read() | |
pattern = r'alias\s*=\s*[\'"](\w+)[\'"]' | |
result = re.sub(pattern, r"alias = alias('\1', legacy_model=True)", content) | |
# Get the filename without extension | |
filename_noext = os.path.splitext(file)[0] | |
# Rename file | |
os.rename(full_path, os.path.join(root, filename_noext+'_legacy.sql')) | |
with open(os.path.join(root, filename_noext + '_legacy.sql'), 'w') as f: | |
f.write(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment