-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_sqlite.py
More file actions
50 lines (43 loc) · 1.64 KB
/
replace_sqlite.py
File metadata and controls
50 lines (43 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import os
import re
def replace_sqlite_with_postgres(settings_file_path):
# Define the new PostgreSQL DATABASES configuration
new_databases_config = """
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('POSTGRES_DB', 'mydatabase'),
'USER': os.getenv('POSTGRES_USER', 'myuser'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD', 'mypassword'),
'HOST': os.getenv('POSTGRES_HOST', 'localhost'),
'PORT': os.getenv('POSTGRES_PORT', '5432'),
}
}
"""
# Read in the original base.py file
with open(settings_file_path, "r") as file:
content = file.read()
# Use regex to find and replace the DATABASES block
content = re.sub(
r"DATABASES\s*=\s*\{.*?\n\}", # Match DATABASES block (non-greedy to stop at the first closing brace)
new_databases_config.strip(), # Replacement with the new PostgreSQL configuration
content,
flags=re.DOTALL
)
# Write the modified content back to base.py
with open(settings_file_path, "w") as file:
file.write(content)
if __name__ == "__main__":
# Ensure the script is called with the settings file path
if len(sys.argv) != 2:
print("Usage: python replace_sqlite.py <path_to_base.py>")
sys.exit(1)
settings_file_path = sys.argv[1]
# Call the function to replace the DATABASES block
if os.path.exists(settings_file_path):
replace_sqlite_with_postgres(settings_file_path)
print(f"Replaced DATABASES block in {settings_file_path}")
else:
print(f"File {settings_file_path} not found!")
sys.exit(1)