neue Version

This commit is contained in:
Czechman 2025-02-16 13:50:53 +01:00
parent 38c7094a9d
commit 60964f7f6e
1 changed files with 20 additions and 13 deletions

View File

@ -5,7 +5,7 @@ from datetime import datetime
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, Text from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, ForeignKey, Text
from sqlalchemy.orm import declarative_base, relationship, sessionmaker from sqlalchemy.orm import declarative_base, relationship, sessionmaker
# Definiere dieselben Modelle, wie im Bundle-Erfassungsskript: # Basis-Klasse (SQLAlchemy 2.0-konform)
Base = declarative_base() Base = declarative_base()
class Bundle(Base): class Bundle(Base):
@ -15,8 +15,11 @@ class Bundle(Base):
human_name = Column(String) human_name = Column(String)
current_version_id = Column(Integer, ForeignKey('bundle_versions.id')) 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]) current_version = relationship("BundleVersion", uselist=False, foreign_keys=[current_version_id])
versions = relationship("BundleVersion", back_populates="bundle", foreign_keys='BundleVersion.bundle_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") sales_history = relationship("BundleSalesHistory", back_populates="bundle")
class BundleVersion(Base): class BundleVersion(Base):
@ -24,10 +27,11 @@ class BundleVersion(Base):
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
bundle_id = Column(Integer, ForeignKey('bundles.id')) bundle_id = Column(Integer, ForeignKey('bundles.id'))
version_hash = Column(String) version_hash = Column(String)
version_data = Column(Text) # als JSON-String version_data = Column(Text) # Als JSON-String
timestamp = Column(DateTime, default=datetime.utcnow) timestamp = Column(DateTime, default=datetime.utcnow)
bundle = relationship("Bundle", back_populates="versions") # 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): class BundleSalesHistory(Base):
__tablename__ = 'bundle_sales_history' __tablename__ = 'bundle_sales_history'
@ -39,12 +43,12 @@ class BundleSalesHistory(Base):
bundle = relationship("Bundle", back_populates="sales_history") bundle = relationship("Bundle", back_populates="sales_history")
def main(): def main():
# Verbindung zur vorhandenen SQLite-Datenbank # Verbindung zur bereits vorhandenen SQLite-Datenbank
engine = create_engine('sqlite:///bundles.db') engine = create_engine('sqlite:///bundles.db')
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
session = Session() session = Session()
# Alle Bundles abfragen # Alle Bundles aus der Tabelle 'bundles' abfragen
bundles = session.query(Bundle).all() bundles = session.query(Bundle).all()
if not bundles: if not bundles:
print("Die Datenbank enthält noch keine Bundles.") print("Die Datenbank enthält noch keine Bundles.")
@ -55,26 +59,29 @@ def main():
print(f"Bundle-ID: {bundle.id}") print(f"Bundle-ID: {bundle.id}")
print(f"Machine Name: {bundle.machine_name}") print(f"Machine Name: {bundle.machine_name}")
print(f"Human Name: {bundle.human_name}") print(f"Human Name: {bundle.human_name}")
if bundle.current_version: if bundle.current_version:
print(f"Aktuelle Version (ID): {bundle.current_version.id}") print(f"Aktuelle Version (ID): {bundle.current_version.id}")
print(f"Version Hash: {bundle.current_version.version_hash}") print(f"Version Hash: {bundle.current_version.version_hash}")
try: try:
version_info = json.loads(bundle.current_version.version_data) version_info = json.loads(bundle.current_version.version_data)
# Optional: gebe z.B. den 'human_name' oder weitere Felder aus # Hier geben wir nur einen gekürzten Ausschnitt der Version-Daten aus
print(f"Version-Daten (gekürzt): {json.dumps(version_info, indent=2)[:300]} ...") version_str = json.dumps(version_info, indent=2)[:300]
print("Version-Daten (gekürzt):")
print(version_str + " ...")
except Exception: except Exception:
print("Version-Daten (roher Text):") print("Version-Daten (roher Text):")
print(bundle.current_version.version_data) print(bundle.current_version.version_data)
print(f"Letzte Aktualisierung: {bundle.current_version.timestamp}") print(f"Letzte Aktualisierung: {bundle.current_version.timestamp}")
else: else:
print("Keine Version hinterlegt.") print("Keine aktuelle Version hinterlegt.")
# Alle Versionen anzeigen # Alle historischen Versionen ausgeben
print("\nHistorische Versionen:") print("\nHistorische Versionen:")
for version in bundle.versions: for version in bundle.versions:
print(f" Version-ID: {version.id}, Timestamp: {version.timestamp}, Hash: {version.version_hash}") print(f" Version-ID: {version.id}, Timestamp: {version.timestamp}, Hash: {version.version_hash}")
# Verkaufszahlen anzeigen # Verkaufszahlen ausgeben
print("\nVerkaufshistorie:") print("\nVerkaufshistorie:")
if bundle.sales_history: if bundle.sales_history:
for sale in bundle.sales_history: for sale in bundle.sales_history: