humbleParser/check_db.py

95 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import json
from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, Text
from sqlalchemy.orm import declarative_base, relationship, sessionmaker
# Basis-Klasse (SQLAlchemy 2.0-konform)
Base = declarative_base()
class Bundle(Base):
__tablename__ = 'bundles'
id = Column(Integer, primary_key=True)
machine_name = Column(String, unique=True)
human_name = Column(String)
current_version_id = Column(Integer, ForeignKey('bundle_versions.id'))
# Beziehung zur aktuellen Version (explizit mit foreign_keys für current_version_id)
current_version = relationship("BundleVersion", uselist=False, foreign_keys=[current_version_id])
# Alle Versionen hier verwenden wir einen lambda-Ausdruck, um die Spalte BundleVersion.bundle_id zu referenzieren
versions = relationship("BundleVersion", back_populates="bundle", foreign_keys=lambda: [BundleVersion.bundle_id])
# Verkaufshistorie
sales_history = relationship("BundleSalesHistory", back_populates="bundle")
class BundleVersion(Base):
__tablename__ = 'bundle_versions'
id = Column(Integer, primary_key=True)
bundle_id = Column(Integer, ForeignKey('bundles.id'))
version_hash = Column(String)
version_data = Column(Text) # Als JSON-String
timestamp = Column(DateTime, default=datetime.utcnow)
# Hier geben wir explizit an, dass für diese Beziehung die Spalte bundle_id verwendet wird
bundle = relationship("Bundle", back_populates="versions", foreign_keys=[bundle_id])
class BundleSalesHistory(Base):
__tablename__ = 'bundle_sales_history'
id = Column(Integer, primary_key=True)
bundle_id = Column(Integer, ForeignKey('bundles.id'))
bundles_sold = Column(Float)
timestamp = Column(DateTime, default=datetime.utcnow)
bundle = relationship("Bundle", back_populates="sales_history")
def main():
# Verbindung zur bereits vorhandenen SQLite-Datenbank
engine = create_engine('sqlite:///bundles.db')
Session = sessionmaker(bind=engine)
session = Session()
# Alle Bundles aus der Tabelle 'bundles' abfragen
bundles = session.query(Bundle).all()
if not bundles:
print("Die Datenbank enthält noch keine Bundles.")
return
for bundle in bundles:
print("---------------------------------------------------")
print(f"Bundle-ID: {bundle.id}")
print(f"Machine Name: {bundle.machine_name}")
print(f"Human Name: {bundle.human_name}")
if bundle.current_version:
print(f"Aktuelle Version (ID): {bundle.current_version.id}")
print(f"Version Hash: {bundle.current_version.version_hash}")
try:
version_info = json.loads(bundle.current_version.version_data)
# Hier geben wir nur einen gekürzten Ausschnitt der Version-Daten aus
version_str = json.dumps(version_info, indent=2)[:300]
print("Version-Daten (gekürzt):")
print(version_str + " ...")
except Exception:
print("Version-Daten (roher Text):")
print(bundle.current_version.version_data)
print(f"Letzte Aktualisierung: {bundle.current_version.timestamp}")
else:
print("Keine aktuelle Version hinterlegt.")
# Alle historischen Versionen ausgeben
print("\nHistorische Versionen:")
for version in bundle.versions:
print(f" Version-ID: {version.id}, Timestamp: {version.timestamp}, Hash: {version.version_hash}")
# Verkaufszahlen ausgeben
print("\nVerkaufshistorie:")
if bundle.sales_history:
for sale in bundle.sales_history:
print(f" {sale.timestamp}: {sale.bundles_sold} verkauft")
else:
print(" Keine Verkaufsdaten vorhanden.")
print("---------------------------------------------------")
if __name__ == "__main__":
main()