Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(brands): keep brand setup working while 00379 isn't applied yet (#1778)

Cihan5d913a64ae8e
diff --git a/server/src/lib/site-type-detect.js b/server/src/lib/site-type-detect.js
index 8f7248125..bf387f7f6 100644
--- a/server/src/lib/site-type-detect.js
+++ b/server/src/lib/site-type-detect.js
@@ -365,6 +365,9 @@ export async function detectSiteType(
*
* @param {string} brandId
*/
+/** Postgres' code for a column that doesn't exist (here: 00379 not applied). */
+const UNDEFINED_COLUMN = '42703';
+
export async function applySiteTypeDetection(
brandId,
{ db = supabaseAdmin, detect = detectSiteType, now = new Date(), write = true } = {},
@@ -377,6 +380,9 @@ export async function applySiteTypeDetection(
.single(),
db.from('brand_domains').select('domain, is_primary').eq('brand_id', brandId),
]);
+ // 00379 not applied yet (a deploy ahead of its migration): no detection,
+ // rather than an error where a brand is created or set up.
+ if (brandRes.error?.code === UNDEFINED_COLUMN) return { skipped: 'unavailable' };
if (brandRes.error) throw new Error(brandRes.error.message);
if (domainsRes.error) throw new Error(domainsRes.error.message);
const brand = brandRes.data;
@@ -406,6 +412,7 @@ export async function applySiteTypeDetection(
.eq('id', brandId)
.is('site_type', null)
.select('id');
+ if (error?.code === UNDEFINED_COLUMN) return { skipped: 'unavailable' };
if (error) throw new Error(error.message);
if (!typed?.length) return { skipped: 'manual' };
diff --git a/server/src/lib/site-type-detect.test.js b/server/src/lib/site-type-detect.test.js
index 6a03490a9..4cb62e207 100644
--- a/server/src/lib/site-type-detect.test.js
+++ b/server/src/lib/site-type-detect.test.js
@@ -428,4 +428,38 @@ describe('applySiteTypeDetection', () => {
});
expect(db.writes).toEqual([]);
});
+ it('skips, without an error, while 00379 is not applied yet', async () => {
+ const missing = { code: '42703', message: 'column brands.site_type_source does not exist' };
+ const detect = vi.fn(async () => shop);
+ const unread = {
+ from: (table) =>
+ table === 'brand_domains'
+ ? { select: () => ({ eq: async () => ({ data: [], error: null }) }) }
+ : {
+ select: () => ({
+ eq: () => ({ single: async () => ({ data: null, error: missing }) }),
+ }),
+ },
+ };
+ expect(await applySiteTypeDetection('b1', { db: unread, detect, now: NOW })).toEqual({
+ skipped: 'unavailable',
+ });
+ expect(detect).not.toHaveBeenCalled();
+
+ // Read before the migration, written after a deploy that dropped it: the same.
+ const db = fakeDb(base);
+ db.from = ((from) => (table) => {
+ const t = from(table);
+ if (table !== 'brands') return t;
+ return {
+ ...t,
+ update: () => ({
+ eq: () => ({ is: () => ({ select: async () => ({ data: null, error: missing }) }) }),
+ }),
+ };
+ })(db.from.bind(db));
+ expect(await applySiteTypeDetection('b1', { db, detect, now: NOW })).toEqual({
+ skipped: 'unavailable',
+ });
+ });
});
diff --git a/web/src/lib/actions/brand-ecommerce.test.ts b/web/src/lib/actions/brand-ecommerce.test.ts
index 09593a4c0..56f315d44 100644
--- a/web/src/lib/actions/brand-ecommerce.test.ts
+++ b/web/src/lib/actions/brand-ecommerce.test.ts
@@ -18,6 +18,9 @@ let prompts: { id: string; platforms: string[] }[];
let writes: { table: string; op: string; value: unknown }[];
// RLS lets a write through, or matches no row without an error (an analyst).
let denied: boolean;
+// 00379 not applied yet: its columns don't exist (Postgres 42703).
+let missingColumn: boolean;
+const MISSING = { code: '42703', message: 'column brands.site_type_source does not exist' };
vi.mock('next/cache', () => ({ revalidatePath: vi.fn() }));
vi.mock('@/lib/guards/plan-guard', () => ({ getOrgPlan: vi.fn() }));
@@ -27,16 +30,29 @@ vi.mock('@/lib/supabase/server', () => ({
from: (table: string) => {
let op = 'select';
let value: unknown = null;
+ let columns = '';
const settle = async () => {
if (op !== 'select') {
+ if (
+ op === 'update' &&
+ missingColumn &&
+ Object.hasOwn(value as object, 'site_type_detected_at')
+ ) {
+ return { data: null, error: MISSING };
+ }
if (op === 'update' && denied) return { data: [], error: null };
writes.push({ table, op, value });
return { data: [{ id: 'row' }], error: null };
}
+ if (missingColumn && columns.includes('site_type_source'))
+ return { data: null, error: MISSING };
return { data: table === 'prompts' ? prompts : brand, error: null };
};
const chain: Record<string, unknown> = {
- select: () => chain,
+ select: (cols?: string) => {
+ if (op === 'select' && cols) columns = cols;
+ return chain;
+ },
eq: () => chain,
single: settle,
update: (patch: unknown) => {
@@ -72,9 +88,25 @@ beforeEach(() => {
];
writes = [];
denied = false;
+ missingColumn = false;
});
describe('markBrandAsEcommerce', () => {
+ it('still turns Shopping mode on when 00379 is not applied yet, leaving the type', async () => {
+ missingColumn = true;
+ expect(await markBrandAsEcommerce('b1')).toEqual({
+ industry: 'E-commerce & Retail',
+ promptsUpdated: 1,
+ siteType: null,
+ });
+ expect(writes.some((w) => Object.hasOwn(w.value as object, 'site_type'))).toBe(false);
+ expect(writes).toContainEqual({
+ table: 'brands',
+ op: 'update',
+ value: { shopping_mode_enabled: true },
+ });
+ });
+
it('sets the site type and industry and turns Shopping mode on', async () => {
expect(await markBrandAsEcommerce('b1')).toEqual({
industry: 'E-commerce & Retail',
diff --git a/web/src/lib/actions/brand.ts b/web/src/lib/actions/brand.ts
index 76e8911d4..5f47c026f 100644
--- a/web/src/lib/actions/brand.ts
+++ b/web/src/lib/actions/brand.ts
@@ -357,6 +357,9 @@ async function applyShoppingMode(
return { promptsUpdated };
}
+/** Postgres' code for a column that doesn't exist (here: 00379 not applied). */
+const UNDEFINED_COLUMN = '42703';
+
/**
* Setup found the brand sells products online (competitor research's
* isEcommerce): its site type becomes e-commerce, as a detection, unless a
@@ -370,26 +373,37 @@ export async function markBrandAsEcommerce(
): Promise<{ industry: string | null; promptsUpdated: number; siteType: SiteType | null }> {
await assertWritable({ onboarding: true });
const supabase = await createClient();
- const { data: brand, error } = await supabase
+ const read = await supabase
.from('brands')
.select('industry, shopping_mode_enabled, site_type, site_type_source')
.eq('id', brandId)
.single();
+ // 00379 not applied yet (a deploy ahead of its migration): Shopping mode
+ // still goes on; the site type is left for detection once it is.
+ const typeKnown = read.error?.code !== UNDEFINED_COLUMN;
+ const { data: brand, error } = typeKnown
+ ? read
+ : await supabase
+ .from('brands')
+ .select('industry, shopping_mode_enabled, site_type')
+ .eq('id', brandId)
+ .single();
if (error || !brand) throw new Error(error?.message ?? 'Brand not found');
let siteType: SiteType | null = null;
- if (brand.site_type_source !== 'manual' && brand.site_type !== 'ecommerce') {
+ const source = 'site_type_source' in brand ? brand.site_type_source : null;
+ if (typeKnown && source !== 'manual' && brand.site_type !== 'ecommerce') {
// Stamped as a detection, so the trigger keeps it auto.
const { data: typed, error: typeErr } = await supabase
.from('brands')
.update({ site_type: 'ecommerce', site_type_detected_at: new Date().toISOString() })
.eq('id', brandId)
.select('id');
- if (typeErr) throw new Error(typeErr.message);
- if (!typed || typed.length === 0) {
+ if (typeErr && typeErr.code !== UNDEFINED_COLUMN) throw new Error(typeErr.message);
+ if (!typeErr && (!typed || typed.length === 0)) {
throw new Error('Brand not found or you do not have permission to update it.');
}
- siteType = 'ecommerce';
+ if (!typeErr) siteType = 'ecommerce';
}
if (brand.shopping_mode_enabled) {