config: structured database config

This commit is contained in:
2026-01-25 18:04:08 +09:00
parent 2ae651a1fa
commit 2331e53795
4 changed files with 54 additions and 8 deletions

View File

@@ -34,3 +34,32 @@ def load_config(config_path: Path | None = None):
else:
print("Could not read config file, it was neither provided nor readable in default location", file=sys.stderr)
sys.exit(1)
def config_db_string() -> str:
db_type = config["database"]["type"]
if db_type == "sqlite":
path = Path(config["database"]["sqlite"]["path"])
return f"sqlite:///{path.absolute()}"
elif db_type == "postgresql":
host = config["database"]["postgresql"]["host"]
port = config["database"]["postgresql"].get("port", 5432)
username = config["database"]["postgresql"].get("username", "dibbler")
dbname = config["database"]["postgresql"].get("dbname", "dibbler")
if "password_file" in config["database"]["postgresql"]:
with Path(config["database"]["postgresql"]["password_file"]).open("r") as f:
password = f.read().strip()
elif "password" in config["database"]["postgresql"]:
password = config["database"]["postgresql"]["password"]
else:
password = ''
if host.startswith("/"):
return f"postgresql+psycopg2://{username}:{password}@/{dbname}?host={host}"
else:
return f"postgresql+psycopg2://{username}:{password}@{host}:{port}/{dbname}"
else:
print(f"Error: unknown database type '{db_type}'")
exit(1)

View File

@@ -4,7 +4,7 @@ from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from dibbler.conf import load_config
from dibbler.conf import load_config, config_db_string
from dibbler.db import engine, session
parser = argparse.ArgumentParser()
@@ -34,10 +34,8 @@ def main():
load_config(args.config)
from dibbler.conf import config
global engine, session
engine = create_engine(config['database']['url'])
engine = create_engine(config_db_string())
session = sessionmaker(bind=engine)
if args.subcommand == "loop":

View File

@@ -5,8 +5,24 @@ show_tracebacks = true
input_encoding = 'utf8'
[database]
# url = "postgresql://robertem@127.0.0.1/pvvvv"
url = "sqlite:///test.db"
type = 'sqlite'
[database.sqlite]
path = 'sqlite:///test.db'
[database.postgresql]
host = 'localhost'
# host = '/run/postgresql'
port = 5432
username = 'dibbler'
dbname = 'dibbler'
# You can either specify a path to a file containing the password,
# or just specify the password directly
# password = 'superhemlig'
# password_file = '/var/lib/dibbler/db-password'
[limits]
low_credit_warning_limit = -100
@@ -15,5 +31,5 @@ user_recent_transaction_limit = 100
# See https://pypi.org/project/brother_ql/ for label types
# Set rotate to False for endless labels
[printer]
label_type = "62"
label_type = '62'
label_rotate = false

View File

@@ -77,7 +77,10 @@ in {
groups.dibbler = { };
};
services.dibbler.settings.database.url = lib.mkIf cfg.createLocalDatabase "postgresql://dibbler?host=/run/postgresql";
services.dibbler.settings.database = lib.mkIf cfg.createLocalDatabase {
type = "postgresql";
postgresql.host = "/run/postgresql";
};
services.postgresql = lib.mkIf cfg.createLocalDatabase {
ensureDatabases = [ "dibbler" ];