AI Resource Pro / website / scripts / update-category-counts.sql
| 1 | -- SQL to update the tool_count field in the categories table to reflect |
| 2 | -- the total count of instances across tools, news, and blog posts |
| 3 | |
| 4 | -- First, check table structures to see what columns actually exist |
| 5 | DO $$ |
| 6 | DECLARE |
| 7 | column_exists BOOLEAN; |
| 8 | tools_has_category_id BOOLEAN; |
| 9 | news_has_category_id BOOLEAN; |
| 10 | blog_has_primary_category_id BOOLEAN; |
| 11 | news_has_category_column BOOLEAN; |
| 12 | tools_categories_exists BOOLEAN; |
| 13 | news_categories_exists BOOLEAN; |
| 14 | blog_categories_exists BOOLEAN; |
| 15 | BEGIN |
| 16 | -- Check if tables and key columns exist |
| 17 | SELECT EXISTS ( |
| 18 | SELECT 1 FROM information_schema.columns |
| 19 | WHERE table_schema = 'public' |
| 20 | AND table_name = 'tools' |
| 21 | AND column_name = 'category_id' |
| 22 | ) INTO tools_has_category_id; |
| 23 | |
| 24 | SELECT EXISTS ( |
| 25 | SELECT 1 FROM information_schema.columns |
| 26 | WHERE table_schema = 'public' |
| 27 | AND table_name = 'news' |
| 28 | AND column_name = 'category_id' |
| 29 | ) INTO news_has_category_id; |
| 30 | |
| 31 | SELECT EXISTS ( |
| 32 | SELECT 1 FROM information_schema.columns |
| 33 | WHERE table_schema = 'public' |
| 34 | AND table_name = 'news' |
| 35 | AND column_name = 'category' |
| 36 | ) INTO news_has_category_column; |
| 37 | |
| 38 | SELECT EXISTS ( |
| 39 | SELECT 1 FROM information_schema.columns |
| 40 | WHERE table_schema = 'public' |
| 41 | AND table_name = 'blog_posts' |
| 42 | AND column_name = 'primary_category_id' |
| 43 | ) INTO blog_has_primary_category_id; |
| 44 | |
| 45 | SELECT EXISTS ( |
| 46 | SELECT 1 FROM information_schema.tables |
| 47 | WHERE table_schema = 'public' |
| 48 | AND table_name = 'tools_categories' |
| 49 | ) INTO tools_categories_exists; |
| 50 | |
| 51 | SELECT EXISTS ( |
| 52 | SELECT 1 FROM information_schema.tables |
| 53 | WHERE table_schema = 'public' |
| 54 | AND table_name = 'news_categories' |
| 55 | ) INTO news_categories_exists; |
| 56 | |
| 57 | SELECT EXISTS ( |
| 58 | SELECT 1 FROM information_schema.tables |
| 59 | WHERE table_schema = 'public' |
| 60 | AND table_name = 'blog_posts_categories' |
| 61 | ) INTO blog_categories_exists; |
| 62 | |
| 63 | RAISE NOTICE 'Table structure check:'; |
| 64 | RAISE NOTICE 'tools.category_id exists: %', tools_has_category_id; |
| 65 | RAISE NOTICE 'news.category_id exists: %', news_has_category_id; |
| 66 | RAISE NOTICE 'news.category exists: %', news_has_category_column; |
| 67 | RAISE NOTICE 'blog_posts.primary_category_id exists: %', blog_has_primary_category_id; |
| 68 | RAISE NOTICE 'tools_categories table exists: %', tools_categories_exists; |
| 69 | RAISE NOTICE 'news_categories table exists: %', news_categories_exists; |
| 70 | RAISE NOTICE 'blog_posts_categories table exists: %', blog_categories_exists; |
| 71 | END $$; |
| 72 | |
| 73 | -- Update the tool_count in the categories table with conditional logic |
| 74 | -- to handle variations in table structure |
| 75 | DO $$ |
| 76 | DECLARE |
| 77 | cat_record RECORD; |
| 78 | total_count INTEGER; |
| 79 | tools_count INTEGER; |
| 80 | news_count INTEGER; |
| 81 | blog_count INTEGER; |
| 82 | tools_categories_count INTEGER; |
| 83 | news_categories_count INTEGER; |
| 84 | blog_categories_count INTEGER; |
| 85 | |
| 86 | tools_has_category_id BOOLEAN; |
| 87 | news_has_category_id BOOLEAN; |
| 88 | news_has_category_column BOOLEAN; |
| 89 | blog_has_primary_category_id BOOLEAN; |
| 90 | tools_categories_exists BOOLEAN; |
| 91 | news_categories_exists BOOLEAN; |
| 92 | blog_categories_exists BOOLEAN; |
| 93 | BEGIN |
| 94 | -- Check table structure first |
| 95 | SELECT EXISTS ( |
| 96 | SELECT 1 FROM information_schema.columns |
| 97 | WHERE table_schema = 'public' |
| 98 | AND table_name = 'tools' |
| 99 | AND column_name = 'category_id' |
| 100 | ) INTO tools_has_category_id; |
| 101 | |
| 102 | SELECT EXISTS ( |
| 103 | SELECT 1 FROM information_schema.columns |
| 104 | WHERE table_schema = 'public' |
| 105 | AND table_name = 'news' |
| 106 | AND column_name = 'category_id' |
| 107 | ) INTO news_has_category_id; |
| 108 | |
| 109 | SELECT EXISTS ( |
| 110 | SELECT 1 FROM information_schema.columns |
| 111 | WHERE table_schema = 'public' |
| 112 | AND table_name = 'news' |
| 113 | AND column_name = 'category' |
| 114 | ) INTO news_has_category_column; |
| 115 | |
| 116 | SELECT EXISTS ( |
| 117 | SELECT 1 FROM information_schema.columns |
| 118 | WHERE table_schema = 'public' |
| 119 | AND table_name = 'blog_posts' |
| 120 | AND column_name = 'primary_category_id' |
| 121 | ) INTO blog_has_primary_category_id; |
| 122 | |
| 123 | SELECT EXISTS ( |
| 124 | SELECT 1 FROM information_schema.tables |
| 125 | WHERE table_schema = 'public' |
| 126 | AND table_name = 'tools_categories' |
| 127 | ) INTO tools_categories_exists; |
| 128 | |
| 129 | SELECT EXISTS ( |
| 130 | SELECT 1 FROM information_schema.tables |
| 131 | WHERE table_schema = 'public' |
| 132 | AND table_name = 'news_categories' |
| 133 | ) INTO news_categories_exists; |
| 134 | |
| 135 | SELECT EXISTS ( |
| 136 | SELECT 1 FROM information_schema.tables |
| 137 | WHERE table_schema = 'public' |
| 138 | AND table_name = 'blog_posts_categories' |
| 139 | ) INTO blog_categories_exists; |
| 140 | |
| 141 | -- Process each category |
| 142 | FOR cat_record IN SELECT id, name FROM categories |
| 143 | LOOP |
| 144 | -- Initialize counts |
| 145 | total_count := 0; |
| 146 | tools_count := 0; |
| 147 | news_count := 0; |
| 148 | blog_count := 0; |
| 149 | tools_categories_count := 0; |
| 150 | news_categories_count := 0; |
| 151 | blog_categories_count := 0; |
| 152 | |
| 153 | -- Count tools directly related to category |
| 154 | IF tools_has_category_id THEN |
| 155 | SELECT COUNT(*) INTO tools_count FROM tools |
| 156 | WHERE category_id = cat_record.id; |
| 157 | total_count := total_count + tools_count; |
| 158 | END IF; |
| 159 | |
| 160 | -- Count news with category_id |
| 161 | IF news_has_category_id THEN |
| 162 | SELECT COUNT(*) INTO news_count FROM news |
| 163 | WHERE category_id = cat_record.id; |
| 164 | total_count := total_count + news_count; |
| 165 | END IF; |
| 166 | |
| 167 | -- Count news with category text field matching category name |
| 168 | IF news_has_category_column THEN |
| 169 | SELECT COUNT(*) INTO news_count FROM news |
| 170 | WHERE category = cat_record.name; |
| 171 | total_count := total_count + news_count; |
| 172 | END IF; |
| 173 | |
| 174 | -- Count blog posts |
| 175 | IF blog_has_primary_category_id THEN |
| 176 | SELECT COUNT(*) INTO blog_count FROM blog_posts |
| 177 | WHERE primary_category_id = cat_record.id; |
| 178 | total_count := total_count + blog_count; |
| 179 | END IF; |
| 180 | |
| 181 | -- Count tools _categories junction entries |
| 182 | IF tools_categories_exists THEN |
| 183 | SELECT COUNT(*) INTO tools_categories_count FROM tools_categories |
| 184 | WHERE category_id = cat_record.id; |
| 185 | total_count := total_count + tools_categories_count; |
| 186 | END IF; |
| 187 | |
| 188 | -- Count news_categories junction entries |
| 189 | IF news_categories_exists THEN |
| 190 | SELECT COUNT(*) INTO news_categories_count FROM news_categories |
| 191 | WHERE category_id = cat_record.id; |
| 192 | total_count := total_count + news_categories_count; |
| 193 | END IF; |
| 194 | |
| 195 | -- Count blog_posts_categories junction entries |
| 196 | IF blog_categories_exists THEN |
| 197 | SELECT COUNT(*) INTO blog_categories_count FROM blog_posts_categories |
| 198 | WHERE category_id = cat_record.id; |
| 199 | total_count := total_count + blog_categories_count; |
| 200 | END IF; |
| 201 | |
| 202 | -- Update category with total count |
| 203 | UPDATE categories |
| 204 | SET tool_count = total_count |
| 205 | WHERE id = cat_record.id; |
| 206 | |
| 207 | RAISE NOTICE 'Updated category % (%) with total count: %', |
| 208 | cat_record.name, cat_record.id, total_count; |
| 209 | END LOOP; |
| 210 | END $$; |
| 211 | |
| 212 | -- Output the results for verification |
| 213 | SELECT |
| 214 | c.id, |
| 215 | c.name, |
| 216 | c.tool_count, |
| 217 | (CASE WHEN EXISTS ( |
| 218 | SELECT 1 FROM information_schema.columns |
| 219 | WHERE table_schema = 'public' |
| 220 | AND table_name = 'tools' |
| 221 | AND column_name = 'category_id' |
| 222 | ) THEN |
| 223 | (SELECT COUNT(*) FROM tools WHERE category_id = c.id) |
| 224 | ELSE 0 END) AS tools_count, |
| 225 | |
| 226 | (CASE WHEN EXISTS ( |
| 227 | SELECT 1 FROM information_schema.tables |
| 228 | WHERE table_schema = 'public' |
| 229 | AND table_name = 'tools_categories' |
| 230 | ) THEN |
| 231 | (SELECT COUNT(*) FROM tools_categories WHERE category_id = c.id) |
| 232 | ELSE 0 END) AS tools_categories_count, |
| 233 | |
| 234 | (CASE WHEN EXISTS ( |
| 235 | SELECT 1 FROM information_schema.columns |
| 236 | WHERE table_schema = 'public' |
| 237 | AND table_name = 'news' |
| 238 | AND column_name = 'category_id' |
| 239 | ) THEN |
| 240 | (SELECT COUNT(*) FROM news WHERE category_id = c.id) |
| 241 | ELSE 0 END) + |
| 242 | (CASE WHEN EXISTS ( |
| 243 | SELECT 1 FROM information_schema.columns |
| 244 | WHERE table_schema = 'public' |
| 245 | AND table_name = 'news' |
| 246 | AND column_name = 'category' |
| 247 | ) THEN |
| 248 | (SELECT COUNT(*) FROM news WHERE category = c.name) |
| 249 | ELSE 0 END) AS news_count, |
| 250 | |
| 251 | (CASE WHEN EXISTS ( |
| 252 | SELECT 1 FROM information_schema.tables |
| 253 | WHERE table_schema = 'public' |
| 254 | AND table_name = 'news_categories' |
| 255 | ) THEN |
| 256 | (SELECT COUNT(*) FROM news_categories WHERE category_id = c.id) |
| 257 | ELSE 0 END) AS news_categories_count, |
| 258 | |
| 259 | (CASE WHEN EXISTS ( |
| 260 | SELECT 1 FROM information_schema.columns |
| 261 | WHERE table_schema = 'public' |
| 262 | AND table_name = 'blog_posts' |
| 263 | AND column_name = 'primary_category_id' |
| 264 | ) THEN |
| 265 | (SELECT COUNT(*) FROM blog_posts WHERE primary_category_id = c.id) |
| 266 | ELSE 0 END) AS blog_posts_count, |
| 267 | |
| 268 | (CASE WHEN EXISTS ( |
| 269 | SELECT 1 FROM information_schema.tables |
| 270 | WHERE table_schema = 'public' |
| 271 | AND table_name = 'blog_posts_categories' |
| 272 | ) THEN |
| 273 | (SELECT COUNT(*) FROM blog_posts_categories WHERE category_id = c.id) |
| 274 | ELSE 0 END) AS blog_categories_count |
| 275 | FROM |
| 276 | categories c |
| 277 | ORDER BY |
| 278 | c.tool_count DESC; |