Skip to content

Commit

Permalink
Urlencode option params and move params to one constant
Browse files Browse the repository at this point in the history
  • Loading branch information
epicserve committed Nov 2, 2024
1 parent bfcc317 commit a9e41ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 44 deletions.
62 changes: 32 additions & 30 deletions src/dj_beat_drop/new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import shutil
import urllib.parse
from pathlib import Path
from textwrap import dedent

Expand All @@ -10,6 +11,17 @@
from dj_beat_drop import utils
from dj_beat_drop.utils import color

EXTRA_SQLITE_PARAMS = {
"transaction_mode": "IMMEDIATE",
"init_command": (
"PRAGMA journal_mode = WAL;"
"PRAGMA synchronous = NORMAL;"
"PRAGMA mmap_size = 134217728;"
"PRAGMA journal_size_limit = 27103364;"
"PRAGMA cache_size = 2000"
),
}


def rename_template_files(project_dir):
# Rename .py-tpl files to .py
Expand All @@ -24,25 +36,26 @@ def replace_sqlite_config(content: str, django_version: str) -> str:
if Version(django_version) < Version("5.1"):
return content

init_command_str = "".join(
[f' "{param};"\n' for param in EXTRA_SQLITE_PARAMS["init_command"].split(";")]
)
rtn_val = content
rtn_val = re.sub(
r"^DATABASES\s*=\s*\{.+?\}\n\}",
dedent(r'''DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'OPTIONS': {
'transaction_mode': 'IMMEDIATE',
'init_command': """
PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA mmap_size = 134217728;
PRAGMA journal_size_limit = 27103364;
PRAGMA cache_size=2000;
""",
},
}
}'''),
dedent(
"DATABASES = {\n"
" 'default': {\n"
" 'ENGINE': 'django.db.backends.sqlite3',\n"
" 'NAME': BASE_DIR / 'db.sqlite3',\n"
" 'OPTIONS': {\n"
f" 'transaction_mode': '{EXTRA_SQLITE_PARAMS['transaction_mode']}',\n"
' \'init_command\': (\n'
f'{init_command_str}'
' )\n'
" }\n"
" }\n"
"}\n"
),
rtn_val,
flags=re.MULTILINE | re.DOTALL,
)
Expand Down Expand Up @@ -78,21 +91,10 @@ def replace_settings_with_environs(content: str) -> str:

def create_dot_envfile(project_dir, context: dict[str, str]):
env_file_path = project_dir / ".env"
env_content = (
"DEBUG=True\n"
f"SECRET_KEY=\"{context['secret_key']}\"\n"
f'ALLOWED_HOSTS=\n'
f"DATABASE_URL=sqlite:///{project_dir / 'db.sqlite3'}"
)
sqlite_url = f"sqlite:///{project_dir / 'db.sqlite3'}"
if Version(context["django_version"]) >= Version("5.1"):
env_content += (
"?transaction_mode=immediate"
"&init_command=PRAGMA journal_mode=WAL"
";PRAGMA synchronous=NORMAL"
";PRAGMA mmap_size=134217728"
";PRAGMA journal_size_limit=27103364"
";PRAGMA cache_size=2000"
)
sqlite_url += "?" + urllib.parse.urlencode(EXTRA_SQLITE_PARAMS)
env_content = f"DEBUG=True\nSECRET_KEY=\"{context['secret_key']}\"\nALLOWED_HOSTS=\nDATABASE_URL={sqlite_url}\n"

with open(env_file_path, "w") as f:
f.write(env_content)
Expand Down
29 changes: 15 additions & 14 deletions tests/test_new_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@

ENV_SECRET_KEY_PATTERN = 'SECRET_KEY = env.str("SECRET_KEY")' # noqa: S105
SQLITE_OPTIONS_ENV = (
"?transaction_mode=immediate"
"&init_command=PRAGMA journal_mode=WAL"
";PRAGMA synchronous=NORMAL"
";PRAGMA mmap_size=134217728"
";PRAGMA journal_size_limit=27103364"
";PRAGMA cache_size=2000"
"?transaction_mode=IMMEDIATE"
"&init_command=PRAGMA+journal_mode+%3D+WAL"
"%3BPRAGMA+synchronous+%3D+NORMAL"
"%3BPRAGMA+mmap_size+%3D+134217728"
"%3BPRAGMA+journal_size_limit+%3D+27103364"
"%3BPRAGMA+cache_size+%3D+2000"
)
SQLITE_OPTIONS = dedent('''
SQLITE_OPTIONS = dedent("""
'transaction_mode': 'IMMEDIATE',
'init_command': """
PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA mmap_size = 134217728;
PRAGMA journal_size_limit = 27103364;
PRAGMA cache_size=2000;
''')
'init_command': (
"PRAGMA journal_mode = WAL;"
"PRAGMA synchronous = NORMAL;"
"PRAGMA mmap_size = 134217728;"
"PRAGMA journal_size_limit = 27103364;"
"PRAGMA cache_size = 2000;"
)
""")
FILE_ASSERTIONS = {
"manage.py": [
"os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')",
Expand Down

0 comments on commit a9e41ce

Please sign in to comment.