mirror of
https://github.com/bleak-ai/gcontext.git
synced 2026-08-11 13:19:23 +02:00
API: replace the marketplace (submit/approve/reject/moderation) with a minimal counter table (id + downloads). Single upsert endpoint auto-creates rows on first ping, X-Source: site exclusion preserved. Inline migration copies existing counts and drops legacy tables. Removes slowapi, pyyaml, manifest validation, and all moderation routes. CLI: gcontext add now pings the API after a successful registry install so the download counter reflects real usage. Fire-and-forget with 3s timeout, gated to skip when GCONTEXT_REGISTRY is overridden (tests, private registries). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
from sqlalchemy import create_engine, inspect, text
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from . import settings
|
|
from .models import Base
|
|
|
|
_engine = None
|
|
_session_factory = None
|
|
|
|
|
|
def engine():
|
|
global _engine
|
|
if _engine is None:
|
|
_engine = create_engine(settings.database_url(), pool_pre_ping=True)
|
|
return _engine
|
|
|
|
|
|
def init_db() -> None:
|
|
Base.metadata.create_all(engine())
|
|
_migrate_legacy(engine())
|
|
|
|
|
|
def _migrate_legacy(eng) -> None:
|
|
"""One-time migration: copy download counts from the legacy tables and drop them."""
|
|
if not inspect(eng).has_table("templates"):
|
|
return
|
|
with eng.begin() as conn:
|
|
conn.execute(text(
|
|
"INSERT INTO workflows (id, downloads) "
|
|
"SELECT id, SUM(downloads) FROM templates GROUP BY id "
|
|
"ON CONFLICT (id) DO NOTHING"
|
|
))
|
|
conn.execute(text("DROP TABLE IF EXISTS template_files"))
|
|
conn.execute(text("DROP TABLE IF EXISTS templates"))
|
|
|
|
|
|
def get_session():
|
|
global _session_factory
|
|
if _session_factory is None:
|
|
_session_factory = sessionmaker(bind=engine(), expire_on_commit=False)
|
|
session: Session = _session_factory()
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|