Skip to content

Instantly share code, notes, and snippets.

@Hosuke
Created July 11, 2023 06:19
Show Gist options
  • Save Hosuke/a3997e9220786f5aa5a4760a254ac50f to your computer and use it in GitHub Desktop.
Save Hosuke/a3997e9220786f5aa5a4760a254ac50f to your computer and use it in GitHub Desktop.
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('legacy.sql'):
# Get full file path
full_path = os.path.join(root, file)
with open(full_path) as f:
model_contents = f.read()
pattern = r'ref\(\'(\w+)\'\)'
model_contents = re.sub(pattern, lambda m: "ref('{}_legacy')".format(m.group(1)), model_contents)
# Write only to original .sql file
with open(full_path, 'w') as f:
f.write(model_contents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment