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}")
|
print(f"📍 Database URL: {database_url}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Create all tables
|
# Create all tables first
|
||||||
print("📊 Creating tables...")
|
print("📊 Creating tables...")
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
print("✅ Tables created successfully")
|
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)
|
# Create triggers (if not already created by event listener)
|
||||||
print("⚙️ Ensuring triggers are created...")
|
print("⚙️ Ensuring triggers are created...")
|
||||||
with engine.connect() as connection:
|
with engine.connect() as connection:
|
||||||
|
|||||||
@ -199,10 +199,10 @@ class ProductHistory(Base):
|
|||||||
operation = Column(String(1), nullable=False) # 'U' for Update, 'D' for Delete
|
operation = Column(String(1), nullable=False) # 'U' for Update, 'D' for Delete
|
||||||
archived_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
archived_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||||
|
|
||||||
# Create trigger after ProductHistory table is created
|
# Create trigger after ALL tables are created
|
||||||
@event.listens_for(ProductHistory.__table__, 'after_create')
|
@event.listens_for(Base.metadata, 'after_create')
|
||||||
def create_products_versioning_trigger(target, connection, **kw):
|
def create_products_versioning_trigger_after_all_tables(target, connection, **kw):
|
||||||
"""Create the products versioning trigger after the history table is created"""
|
"""Create the products versioning trigger after all tables are created"""
|
||||||
try:
|
try:
|
||||||
connection.execute(text(PRODUCTS_VERSIONING_TRIGGER_SQL))
|
connection.execute(text(PRODUCTS_VERSIONING_TRIGGER_SQL))
|
||||||
print("✅ Products versioning trigger created successfully")
|
print("✅ Products versioning trigger created successfully")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user