create-db: print where db was created, exit 0

This commit is contained in:
2026-01-12 03:21:38 +09:00
parent 30f8ae6093
commit f48dc97b09
2 changed files with 24 additions and 0 deletions

View File

@@ -72,6 +72,8 @@ def main():
database_url = Config.db_string()
engine = create_engine(database_url)
Base.metadata.create_all(engine)
print(f"Database schema created at '{Config.db_string_no_password()}'")
exit(0)
if args.command == "devscripts":
sql_session = _connect_to_database(echo=Config["logging.debug_sql"])

View File

@@ -89,6 +89,28 @@ class Config:
print(f"Error: unknown database type '{db_config.get('type')}'")
exit(1)
@classmethod
def db_string_no_password(cls) -> str:
db_type = cls._config.get("database").get("type")
if db_type == "sqlite":
path = Path(cls._config.get("database").get("sqlite").get("path"))
return f"sqlite:///{path.absolute()}"
elif db_type == "postgresql":
db_config = cls._config.get("database").get("postgresql")
host = db_config.get("host")
port = db_config.get("port")
username = db_config.get("username")
database = db_config.get("database")
if host.startswith("/"):
return f"postgresql+psycopg2://{username}:<password>@/{database}?host={host}"
else:
return f"postgresql+psycopg2://{username}:<password>@{host}:{port}/{database}"
else:
print(f"Error: unknown database type '{db_config.get('type')}'")
exit(1)
@classmethod
def debug(cls) -> str:
return pformat(cls._config)