fix trigger creation bug
This commit is contained in:
parent
df8209e86d
commit
3e9ad2dcb1
@ -28,11 +28,36 @@ def init_database():
|
||||
print(f"📍 Database URL: {database_url}")
|
||||
|
||||
try:
|
||||
# Create all tables
|
||||
# Create all tables first
|
||||
print("📊 Creating tables...")
|
||||
Base.metadata.create_all(bind=engine)
|
||||
print("✅ Tables created successfully")
|
||||
|
||||
# Verify critical tables exist before creating triggers
|
||||
print("🔍 Verifying tables exist...")
|
||||
with engine.connect() as connection:
|
||||
# Check if products and products_history tables exist
|
||||
products_exists = connection.execute(text("""
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_name = 'products'
|
||||
);
|
||||
""")).scalar()
|
||||
|
||||
history_exists = connection.execute(text("""
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_name = 'products_history'
|
||||
);
|
||||
""")).scalar()
|
||||
|
||||
if not products_exists:
|
||||
raise Exception("Products table was not created")
|
||||
if not history_exists:
|
||||
raise Exception("Products history table was not created")
|
||||
|
||||
print("✅ Required tables verified")
|
||||
|
||||
# Create triggers (if not already created by event listener)
|
||||
print("⚙️ Ensuring triggers are created...")
|
||||
with engine.connect() as connection:
|
||||
|
||||
@ -199,10 +199,10 @@ class ProductHistory(Base):
|
||||
operation = Column(String(1), nullable=False) # 'U' for Update, 'D' for Delete
|
||||
archived_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
|
||||
# Create trigger after ProductHistory table is created
|
||||
@event.listens_for(ProductHistory.__table__, 'after_create')
|
||||
def create_products_versioning_trigger(target, connection, **kw):
|
||||
"""Create the products versioning trigger after the history table is created"""
|
||||
# Create trigger after ALL tables are created
|
||||
@event.listens_for(Base.metadata, 'after_create')
|
||||
def create_products_versioning_trigger_after_all_tables(target, connection, **kw):
|
||||
"""Create the products versioning trigger after all tables are created"""
|
||||
try:
|
||||
connection.execute(text(PRODUCTS_VERSIONING_TRIGGER_SQL))
|
||||
print("✅ Products versioning trigger created successfully")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user