push changes

This commit is contained in:
Mateusz Gruszczyński
2026-03-20 09:14:00 +01:00
parent ab411ba67c
commit 2a59c4bf3e
7 changed files with 148 additions and 27 deletions

View File

@@ -7,12 +7,34 @@ load_dotenv(BASE_DIR / '.env')
def _normalize_sqlalchemy_db_url(raw: str | None) -> str:
if not raw:
return f"sqlite:///{(BASE_DIR / 'instance' / 'app.db').resolve()}"
if raw.startswith('sqlite:///') and not raw.startswith('sqlite:////'):
rel = raw.replace('sqlite:///', '', 1)
return f"sqlite:///{(BASE_DIR / rel).resolve()}"
return raw
if raw:
raw = raw.strip()
if raw.startswith('postgres://'):
return raw.replace('postgres://', 'postgresql+psycopg://', 1)
if raw.startswith('sqlite:///') and not raw.startswith('sqlite:////'):
rel = raw.replace('sqlite:///', '', 1)
return f"sqlite:///{(BASE_DIR / rel).resolve()}"
return raw
db_engine = os.getenv('DB_ENGINE', 'sqlite').strip().lower()
db_host = os.getenv('DB_HOST', 'localhost').strip()
db_port = os.getenv('DB_PORT', '').strip()
db_name = os.getenv('DB_NAME', 'ksef').strip()
db_user = os.getenv('DB_USER', '').strip()
db_password = os.getenv('DB_PASSWORD', '').strip()
if db_engine == 'sqlite':
return f"sqlite:///{(BASE_DIR / 'db' / 'sqlite' / 'app.db').resolve()}"
if db_engine in ('pgsql', 'postgres', 'postgresql'):
port = db_port or '5432'
return f"postgresql+psycopg://{db_user}:{db_password}@{db_host}:{port}/{db_name}"
if db_engine == 'mysql':
port = db_port or '3306'
return f"mysql+pymysql://{db_user}:{db_password}@{db_host}:{port}/{db_name}"
raise ValueError(f"Nieobsługiwany DB_ENGINE: {db_engine}")
def _path_from_env(name: str, default: Path) -> Path: