Fixed erroneous upsert
This commit is contained in:
@@ -175,17 +175,16 @@ class TimescaleDBClient:
|
|||||||
status: Campaign status
|
status: Campaign status
|
||||||
objective: Campaign objective
|
objective: Campaign objective
|
||||||
"""
|
"""
|
||||||
logger.info(
|
|
||||||
f"upsert_campaign called: campaign_id={campaign_id}, account_id={account_id}, "
|
|
||||||
f"campaign_name={campaign_name!r}, status={status}, objective={objective}"
|
|
||||||
)
|
|
||||||
|
|
||||||
query = """
|
query = """
|
||||||
INSERT INTO campaigns (campaign_id, account_id, campaign_name, status, objective, updated_at)
|
INSERT INTO campaigns (campaign_id, account_id, campaign_name, status, objective, updated_at)
|
||||||
VALUES ($1, $2, $3, $4, $5, NOW())
|
VALUES ($1, $2, $3, $4, $5, NOW())
|
||||||
ON CONFLICT (campaign_id)
|
ON CONFLICT (campaign_id)
|
||||||
DO UPDATE SET
|
DO UPDATE SET
|
||||||
campaign_name = EXCLUDED.campaign_name,
|
campaign_name = CASE
|
||||||
|
WHEN EXCLUDED.campaign_name = 'Unknown' THEN campaigns.campaign_name
|
||||||
|
ELSE EXCLUDED.campaign_name
|
||||||
|
END,
|
||||||
status = COALESCE(EXCLUDED.status, campaigns.status),
|
status = COALESCE(EXCLUDED.status, campaigns.status),
|
||||||
objective = COALESCE(EXCLUDED.objective, campaigns.objective),
|
objective = COALESCE(EXCLUDED.objective, campaigns.objective),
|
||||||
updated_at = NOW()
|
updated_at = NOW()
|
||||||
@@ -197,16 +196,6 @@ class TimescaleDBClient:
|
|||||||
result = await conn.execute(query, campaign_id, account_id, campaign_name, status, objective)
|
result = await conn.execute(query, campaign_id, account_id, campaign_name, status, objective)
|
||||||
logger.debug(f"Query result: {result}")
|
logger.debug(f"Query result: {result}")
|
||||||
|
|
||||||
# Query back what was actually stored
|
|
||||||
stored = await conn.fetchrow(
|
|
||||||
"SELECT campaign_name, status, objective FROM campaigns WHERE campaign_id = $1",
|
|
||||||
campaign_id
|
|
||||||
)
|
|
||||||
logger.info(
|
|
||||||
f"After upsert, database contains: campaign_id={campaign_id}, "
|
|
||||||
f"campaign_name={stored['campaign_name']!r}, status={stored['status']}, "
|
|
||||||
f"objective={stored['objective']}"
|
|
||||||
)
|
|
||||||
|
|
||||||
async def upsert_adset(
|
async def upsert_adset(
|
||||||
self,
|
self,
|
||||||
@@ -416,17 +405,10 @@ class TimescaleDBClient:
|
|||||||
"""
|
"""
|
||||||
# Cache metadata if requested and available in the insights data
|
# Cache metadata if requested and available in the insights data
|
||||||
if cache_metadata:
|
if cache_metadata:
|
||||||
# First ensure campaign exists (adset references campaign)
|
# Cache adset metadata if available
|
||||||
# We don't have campaign name in adset insights, so only create if needed
|
# Note: Campaign should already exist from cache_campaigns_metadata or grab_campaign_insights
|
||||||
await self.upsert_campaign(
|
# If it doesn't exist, the foreign key constraint will fail with a clear error
|
||||||
campaign_id=campaign_id,
|
# This is intentional - we should never silently create campaigns with 'Unknown' names
|
||||||
account_id=account_id,
|
|
||||||
campaign_name='Unknown', # Campaign name not in adset insights
|
|
||||||
status=None,
|
|
||||||
objective=None,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Then cache adset metadata if available
|
|
||||||
if data.get("adset_name"):
|
if data.get("adset_name"):
|
||||||
await self.upsert_adset(
|
await self.upsert_adset(
|
||||||
adset_id=adset_id,
|
adset_id=adset_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user