-- SQL to update the tool_count field in the categories table to reflect -- the total count of instances across tools, news, and blog posts -- First, check table structures to see what columns actually exist DO $$ DECLARE column_exists BOOLEAN; tools_has_category_id BOOLEAN; news_has_category_id BOOLEAN; blog_has_primary_category_id BOOLEAN; news_has_category_column BOOLEAN; tools_categories_exists BOOLEAN; news_categories_exists BOOLEAN; blog_categories_exists BOOLEAN; BEGIN -- Check if tables and key columns exist SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'tools' AND column_name = 'category_id' ) INTO tools_has_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category_id' ) INTO news_has_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category' ) INTO news_has_category_column; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'blog_posts' AND column_name = 'primary_category_id' ) INTO blog_has_primary_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'tools_categories' ) INTO tools_categories_exists; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'news_categories' ) INTO news_categories_exists; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'blog_posts_categories' ) INTO blog_categories_exists; RAISE NOTICE 'Table structure check:'; RAISE NOTICE 'tools.category_id exists: %', tools_has_category_id; RAISE NOTICE 'news.category_id exists: %', news_has_category_id; RAISE NOTICE 'news.category exists: %', news_has_category_column; RAISE NOTICE 'blog_posts.primary_category_id exists: %', blog_has_primary_category_id; RAISE NOTICE 'tools_categories table exists: %', tools_categories_exists; RAISE NOTICE 'news_categories table exists: %', news_categories_exists; RAISE NOTICE 'blog_posts_categories table exists: %', blog_categories_exists; END $$; -- Update the tool_count in the categories table with conditional logic -- to handle variations in table structure DO $$ DECLARE cat_record RECORD; total_count INTEGER; tools_count INTEGER; news_count INTEGER; blog_count INTEGER; tools_categories_count INTEGER; news_categories_count INTEGER; blog_categories_count INTEGER; tools_has_category_id BOOLEAN; news_has_category_id BOOLEAN; news_has_category_column BOOLEAN; blog_has_primary_category_id BOOLEAN; tools_categories_exists BOOLEAN; news_categories_exists BOOLEAN; blog_categories_exists BOOLEAN; BEGIN -- Check table structure first SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'tools' AND column_name = 'category_id' ) INTO tools_has_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category_id' ) INTO news_has_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category' ) INTO news_has_category_column; SELECT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'blog_posts' AND column_name = 'primary_category_id' ) INTO blog_has_primary_category_id; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'tools_categories' ) INTO tools_categories_exists; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'news_categories' ) INTO news_categories_exists; SELECT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'blog_posts_categories' ) INTO blog_categories_exists; -- Process each category FOR cat_record IN SELECT id, name FROM categories LOOP -- Initialize counts total_count := 0; tools_count := 0; news_count := 0; blog_count := 0; tools_categories_count := 0; news_categories_count := 0; blog_categories_count := 0; -- Count tools directly related to category IF tools_has_category_id THEN SELECT COUNT(*) INTO tools_count FROM tools WHERE category_id = cat_record.id; total_count := total_count + tools_count; END IF; -- Count news with category_id IF news_has_category_id THEN SELECT COUNT(*) INTO news_count FROM news WHERE category_id = cat_record.id; total_count := total_count + news_count; END IF; -- Count news with category text field matching category name IF news_has_category_column THEN SELECT COUNT(*) INTO news_count FROM news WHERE category = cat_record.name; total_count := total_count + news_count; END IF; -- Count blog posts IF blog_has_primary_category_id THEN SELECT COUNT(*) INTO blog_count FROM blog_posts WHERE primary_category_id = cat_record.id; total_count := total_count + blog_count; END IF; -- Count tools _categories junction entries IF tools_categories_exists THEN SELECT COUNT(*) INTO tools_categories_count FROM tools_categories WHERE category_id = cat_record.id; total_count := total_count + tools_categories_count; END IF; -- Count news_categories junction entries IF news_categories_exists THEN SELECT COUNT(*) INTO news_categories_count FROM news_categories WHERE category_id = cat_record.id; total_count := total_count + news_categories_count; END IF; -- Count blog_posts_categories junction entries IF blog_categories_exists THEN SELECT COUNT(*) INTO blog_categories_count FROM blog_posts_categories WHERE category_id = cat_record.id; total_count := total_count + blog_categories_count; END IF; -- Update category with total count UPDATE categories SET tool_count = total_count WHERE id = cat_record.id; RAISE NOTICE 'Updated category % (%) with total count: %', cat_record.name, cat_record.id, total_count; END LOOP; END $$; -- Output the results for verification SELECT c.id, c.name, c.tool_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'tools' AND column_name = 'category_id' ) THEN (SELECT COUNT(*) FROM tools WHERE category_id = c.id) ELSE 0 END) AS tools_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'tools_categories' ) THEN (SELECT COUNT(*) FROM tools_categories WHERE category_id = c.id) ELSE 0 END) AS tools_categories_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category_id' ) THEN (SELECT COUNT(*) FROM news WHERE category_id = c.id) ELSE 0 END) + (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'news' AND column_name = 'category' ) THEN (SELECT COUNT(*) FROM news WHERE category = c.name) ELSE 0 END) AS news_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'news_categories' ) THEN (SELECT COUNT(*) FROM news_categories WHERE category_id = c.id) ELSE 0 END) AS news_categories_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'blog_posts' AND column_name = 'primary_category_id' ) THEN (SELECT COUNT(*) FROM blog_posts WHERE primary_category_id = c.id) ELSE 0 END) AS blog_posts_count, (CASE WHEN EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'blog_posts_categories' ) THEN (SELECT COUNT(*) FROM blog_posts_categories WHERE category_id = c.id) ELSE 0 END) AS blog_categories_count FROM categories c ORDER BY c.tool_count DESC;